consume.c 6.8 KB

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