publish.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MIT
  4. *
  5. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  6. * Alan Antonuk. All Rights Reserved.
  7. *
  8. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  9. * All Rights Reserved.
  10. *
  11. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  12. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person
  15. * obtaining a copy of this software and associated documentation
  16. * files (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy,
  18. * modify, merge, publish, distribute, sublicense, and/or sell copies
  19. * of the Software, and to permit persons to whom the Software is
  20. * furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. * ***** END LICENSE BLOCK *****
  34. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "common.h"
  42. #define MAX_LINE_LENGTH 1024 * 32
  43. static void do_publish(amqp_connection_state_t conn, char *exchange,
  44. char *routing_key, amqp_basic_properties_t *props,
  45. amqp_bytes_t body) {
  46. int res = amqp_basic_publish(conn, 1, cstring_bytes(exchange),
  47. cstring_bytes(routing_key), 0, 0, props, body);
  48. die_amqp_error(res, "basic.publish");
  49. }
  50. int main(int argc, const char **argv) {
  51. amqp_connection_state_t conn;
  52. static char *exchange = NULL;
  53. static char *routing_key = NULL;
  54. static char *content_type = NULL;
  55. static char *content_encoding = NULL;
  56. static char **headers = NULL;
  57. static char *reply_to = NULL;
  58. static char *body = NULL;
  59. amqp_basic_properties_t props;
  60. amqp_bytes_t body_bytes;
  61. static int delivery = 1; /* non-persistent by default */
  62. static int line_buffered = 0;
  63. static char **pos;
  64. struct poptOption options[] = {
  65. INCLUDE_OPTIONS(connect_options),
  66. {"exchange", 'e', POPT_ARG_STRING, &exchange, 0,
  67. "the exchange to publish to", "exchange"},
  68. {"routing-key", 'r', POPT_ARG_STRING, &routing_key, 0,
  69. "the routing key to publish with", "routing key"},
  70. {"persistent", 'p', POPT_ARG_VAL, &delivery, 2,
  71. "use the persistent delivery mode", NULL},
  72. {"content-type", 'C', POPT_ARG_STRING, &content_type, 0,
  73. "the content-type for the message", "content type"},
  74. {"reply-to", 't', POPT_ARG_STRING, &reply_to, 0,
  75. "the replyTo to use for the message", "reply to"},
  76. {"line-buffered", 'l', POPT_ARG_VAL, &line_buffered, 2,
  77. "treat each line from standard in as a separate message", NULL},
  78. {"content-encoding", 'E', POPT_ARG_STRING, &content_encoding, 0,
  79. "the content-encoding for the message", "content encoding"},
  80. {"header", 'H', POPT_ARG_ARGV, &headers, 0,
  81. "set a message header (may be specified multiple times)",
  82. "\"key: value\""},
  83. {"body", 'b', POPT_ARG_STRING, &body, 0, "specify the message body",
  84. "body"},
  85. POPT_AUTOHELP{NULL, '\0', 0, NULL, 0, NULL, NULL}};
  86. process_all_options(argc, argv, options);
  87. if (!exchange && !routing_key) {
  88. fprintf(stderr, "neither exchange nor routing key specified\n");
  89. return 1;
  90. }
  91. memset(&props, 0, sizeof props);
  92. props._flags = AMQP_BASIC_DELIVERY_MODE_FLAG;
  93. props.delivery_mode = delivery;
  94. if (content_type) {
  95. props._flags |= AMQP_BASIC_CONTENT_TYPE_FLAG;
  96. props.content_type = amqp_cstring_bytes(content_type);
  97. }
  98. if (content_encoding) {
  99. props._flags |= AMQP_BASIC_CONTENT_ENCODING_FLAG;
  100. props.content_encoding = amqp_cstring_bytes(content_encoding);
  101. }
  102. if (reply_to) {
  103. props._flags |= AMQP_BASIC_REPLY_TO_FLAG;
  104. props.reply_to = amqp_cstring_bytes(reply_to);
  105. }
  106. if (headers) {
  107. int num = 0;
  108. for (pos = headers; *pos; pos++) {
  109. num++;
  110. }
  111. if (num > 0) {
  112. amqp_table_t *table = &props.headers;
  113. table->num_entries = num;
  114. table->entries = calloc(num, sizeof(amqp_table_entry_t));
  115. int i = 0;
  116. for (pos = headers; *pos; pos++) {
  117. char *colon = strchr(*pos, ':');
  118. if (colon) {
  119. *colon++ = '\0';
  120. while (*colon == ' ') colon++;
  121. table->entries[i].key = amqp_cstring_bytes(*pos);
  122. table->entries[i].value.kind = AMQP_FIELD_KIND_UTF8;
  123. table->entries[i].value.value.bytes = amqp_cstring_bytes(colon);
  124. i++;
  125. } else {
  126. fprintf(stderr,
  127. "Ignored header definition missing ':' delimiter in \"%s\"\n",
  128. *pos);
  129. }
  130. }
  131. props._flags |= AMQP_BASIC_HEADERS_FLAG;
  132. }
  133. }
  134. conn = make_connection();
  135. if (body) {
  136. body_bytes = amqp_cstring_bytes(body);
  137. } else {
  138. if (line_buffered) {
  139. body_bytes.bytes = (char *)malloc(MAX_LINE_LENGTH);
  140. while (fgets(body_bytes.bytes, MAX_LINE_LENGTH, stdin)) {
  141. body_bytes.len = strlen(body_bytes.bytes);
  142. do_publish(conn, exchange, routing_key, &props, body_bytes);
  143. }
  144. } else {
  145. body_bytes = read_all(0);
  146. }
  147. }
  148. if (!line_buffered) {
  149. do_publish(conn, exchange, routing_key, &props, body_bytes);
  150. }
  151. if (props.headers.num_entries > 0) {
  152. free(props.headers.entries);
  153. }
  154. if (!body) {
  155. free(body_bytes.bytes);
  156. }
  157. close_connection(conn);
  158. return 0;
  159. }