consume.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #include "process.h"
  43. #define MAX_LISTEN_KEYS 1024
  44. #define LISTEN_KEYS_DELIMITER ","
  45. /* Convert a amqp_bytes_t to an escaped string form for printing. We
  46. use the same escaping conventions as rabbitmqctl. */
  47. static char *stringify_bytes(amqp_bytes_t bytes) {
  48. /* We will need up to 4 chars per byte, plus the terminating 0 */
  49. char *res = malloc(bytes.len * 4 + 1);
  50. uint8_t *data = bytes.bytes;
  51. char *p = res;
  52. size_t i;
  53. for (i = 0; i < bytes.len; i++) {
  54. if (data[i] >= 32 && data[i] != 127) {
  55. *p++ = data[i];
  56. } else {
  57. *p++ = '\\';
  58. *p++ = '0' + (data[i] >> 6);
  59. *p++ = '0' + (data[i] >> 3 & 0x7);
  60. *p++ = '0' + (data[i] & 0x7);
  61. }
  62. }
  63. *p = 0;
  64. return res;
  65. }
  66. static amqp_bytes_t setup_queue(amqp_connection_state_t conn, char *queue,
  67. char *exchange, char *routing_key, int declare,
  68. int exclusive) {
  69. amqp_bytes_t queue_bytes = cstring_bytes(queue);
  70. char *routing_key_rest;
  71. char *routing_key_token;
  72. char *routing_tmp;
  73. int routing_key_count = 0;
  74. /* if an exchange name wasn't provided, check that we don't have options that
  75. * require it. */
  76. if (!exchange && routing_key) {
  77. fprintf(stderr,
  78. "--routing-key option requires an exchange name to be provided "
  79. "with --exchange\n");
  80. exit(1);
  81. }
  82. if (!queue || exchange || declare || exclusive) {
  83. /* Declare the queue as auto-delete. */
  84. amqp_queue_declare_ok_t *res = amqp_queue_declare(
  85. conn, 1, queue_bytes, 0, 0, exclusive, 1, amqp_empty_table);
  86. if (!res) {
  87. die_rpc(amqp_get_rpc_reply(conn), "queue.declare");
  88. }
  89. if (!queue) {
  90. /* the server should have provided a queue name */
  91. char *sq;
  92. queue_bytes = amqp_bytes_malloc_dup(res->queue);
  93. sq = stringify_bytes(queue_bytes);
  94. fprintf(stderr, "Server provided queue name: %s\n", sq);
  95. free(sq);
  96. }
  97. /* Bind to an exchange if requested */
  98. if (exchange) {
  99. amqp_bytes_t eb = amqp_cstring_bytes(exchange);
  100. routing_tmp = strdup(routing_key);
  101. if (NULL == routing_tmp) {
  102. fprintf(stderr, "could not allocate memory to parse routing key\n");
  103. exit(1);
  104. }
  105. for (routing_key_token =
  106. strtok_r(routing_tmp, LISTEN_KEYS_DELIMITER, &routing_key_rest);
  107. NULL != routing_key_token && routing_key_count < MAX_LISTEN_KEYS - 1;
  108. routing_key_token =
  109. strtok_r(NULL, LISTEN_KEYS_DELIMITER, &routing_key_rest)) {
  110. if (!amqp_queue_bind(conn, 1, queue_bytes, eb,
  111. cstring_bytes(routing_key_token),
  112. amqp_empty_table)) {
  113. die_rpc(amqp_get_rpc_reply(conn), "queue.bind");
  114. }
  115. }
  116. free(routing_tmp);
  117. }
  118. }
  119. return queue_bytes;
  120. }
  121. #define AMQP_CONSUME_MAX_PREFETCH_COUNT 65535
  122. static void do_consume(amqp_connection_state_t conn, amqp_bytes_t queue,
  123. int no_ack, int count, int prefetch_count,
  124. const char *const *argv) {
  125. int i;
  126. /* If there is a limit, set the qos to match */
  127. if (count > 0 && count <= AMQP_CONSUME_MAX_PREFETCH_COUNT &&
  128. !amqp_basic_qos(conn, 1, 0, count, 0)) {
  129. die_rpc(amqp_get_rpc_reply(conn), "basic.qos");
  130. }
  131. /* if there is a maximum number of messages to be received at a time, set the
  132. * qos to match */
  133. if (prefetch_count > 0 && prefetch_count <= AMQP_CONSUME_MAX_PREFETCH_COUNT) {
  134. /* the maximum number of messages to be received at a time must be less
  135. * than the global maximum number of messages. */
  136. if (!(count > 0 && count <= AMQP_CONSUME_MAX_PREFETCH_COUNT &&
  137. prefetch_count >= count)) {
  138. if (!amqp_basic_qos(conn, 1, 0, prefetch_count, 0)) {
  139. die_rpc(amqp_get_rpc_reply(conn), "basic.qos");
  140. }
  141. }
  142. }
  143. if (!amqp_basic_consume(conn, 1, queue, amqp_empty_bytes, 0, no_ack, 0,
  144. amqp_empty_table)) {
  145. die_rpc(amqp_get_rpc_reply(conn), "basic.consume");
  146. }
  147. for (i = 0; count < 0 || i < count; i++) {
  148. amqp_frame_t frame;
  149. struct pipeline pl;
  150. uint64_t delivery_tag;
  151. amqp_basic_deliver_t *deliver;
  152. int res = amqp_simple_wait_frame(conn, &frame);
  153. die_amqp_error(res, "waiting for header frame");
  154. if (frame.frame_type != AMQP_FRAME_METHOD ||
  155. frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) {
  156. continue;
  157. }
  158. deliver = (amqp_basic_deliver_t *)frame.payload.method.decoded;
  159. delivery_tag = deliver->delivery_tag;
  160. pipeline(argv, &pl);
  161. copy_body(conn, pl.infd);
  162. if (finish_pipeline(&pl) && !no_ack)
  163. die_amqp_error(amqp_basic_ack(conn, 1, delivery_tag, 0), "basic.ack");
  164. amqp_maybe_release_buffers(conn);
  165. }
  166. }
  167. int main(int argc, const char **argv) {
  168. poptContext opts;
  169. amqp_connection_state_t conn;
  170. const char *const *cmd_argv;
  171. static char *queue = NULL;
  172. static char *exchange = NULL;
  173. static char *routing_key = NULL;
  174. static int declare = 0;
  175. static int exclusive = 0;
  176. static int no_ack = 0;
  177. static int count = -1;
  178. static int prefetch_count = -1;
  179. amqp_bytes_t queue_bytes;
  180. struct poptOption options[] = {
  181. INCLUDE_OPTIONS(connect_options),
  182. {"queue", 'q', POPT_ARG_STRING, &queue, 0, "the queue to consume from",
  183. "queue"},
  184. {"exchange", 'e', POPT_ARG_STRING, &exchange, 0,
  185. "bind the queue to this exchange", "exchange"},
  186. {"routing-key", 'r', POPT_ARG_STRING, &routing_key, 0,
  187. "the routing key to bind with", "routing key"},
  188. {"declare", 'd', POPT_ARG_NONE, &declare, 0,
  189. "declare an exclusive queue (deprecated, use --exclusive instead)",
  190. NULL},
  191. {"exclusive", 'x', POPT_ARG_NONE, &exclusive, 0,
  192. "declare the queue as exclusive", NULL},
  193. {"no-ack", 'A', POPT_ARG_NONE, &no_ack, 0, "consume in no-ack mode",
  194. NULL},
  195. {"count", 'c', POPT_ARG_INT, &count, 0,
  196. "stop consuming after this many messages are consumed", "limit"},
  197. {"prefetch-count", 'p', POPT_ARG_INT, &prefetch_count, 0,
  198. "receive only this many message at a time from the server", "limit"},
  199. POPT_AUTOHELP{NULL, '\0', 0, NULL, 0, NULL, NULL}};
  200. opts = process_options(argc, argv, options, "[OPTIONS]... <command> <args>");
  201. cmd_argv = poptGetArgs(opts);
  202. if (!cmd_argv || !cmd_argv[0]) {
  203. fprintf(stderr, "consuming command not specified\n");
  204. poptPrintUsage(opts, stderr, 0);
  205. goto error;
  206. }
  207. conn = make_connection();
  208. queue_bytes =
  209. setup_queue(conn, queue, exchange, routing_key, declare, exclusive);
  210. do_consume(conn, queue_bytes, no_ack, count, prefetch_count, cmd_argv);
  211. close_connection(conn);
  212. return 0;
  213. error:
  214. poptFreeContext(opts);
  215. return 1;
  216. }