| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include "common.h" |
| 9 |
#include "process.h" |
| 10 |
|
| 11 |
#define MAX_LISTEN_KEYS 1024 |
| 12 |
#define LISTEN_KEYS_DELIMITER "," |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static char *stringify_bytes(amqp_bytes_t bytes) { |
| 17 |
|
| 18 |
char *res = malloc(bytes.len * 4 + 1); |
| 19 |
if (res == NULL) { |
| 20 |
return NULL; |
| 21 |
} |
| 22 |
uint8_t *data = bytes.bytes; |
| 23 |
char *p = res; |
| 24 |
size_t i; |
| 25 |
|
| 26 |
for (i = 0; i < bytes.len; i++) { |
| 27 |
if (data[i] >= 32 && data[i] != 127) { |
| 28 |
*p++ = data[i]; |
| 29 |
} else { |
| 30 |
*p++ = '\\'; |
| 31 |
*p++ = '0' + (data[i] >> 6); |
| 32 |
*p++ = '0' + (data[i] >> 3 & 0x7); |
| 33 |
*p++ = '0' + (data[i] & 0x7); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
*p = 0; |
| 38 |
return res; |
| 39 |
} |
| 40 |
|
| 41 |
static amqp_bytes_t setup_queue(amqp_connection_state_t conn, char *queue, |
| 42 |
char *exchange, char *routing_key, int declare, |
| 43 |
int exclusive) { |
| 44 |
amqp_bytes_t queue_bytes = cstring_bytes(queue); |
| 45 |
|
| 46 |
char *routing_key_rest; |
| 47 |
char *routing_key_token; |
| 48 |
char *routing_tmp; |
| 49 |
int routing_key_count = 0; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if (!exchange && routing_key) { |
| 54 |
fprintf(stderr, |
| 55 |
"--routing-key option requires an exchange name to be provided " |
| 56 |
"with --exchange\n"); |
| 57 |
exit(1); |
| 58 |
} |
| 59 |
|
| 60 |
if (!queue || exchange || declare || exclusive) { |
| 61 |
|
| 62 |
amqp_queue_declare_ok_t *res = amqp_queue_declare( |
| 63 |
conn, 1, queue_bytes, 0, 0, exclusive, 1, amqp_empty_table); |
| 64 |
if (!res) { |
| 65 |
die_rpc(amqp_get_rpc_reply(conn), "queue.declare"); |
| 66 |
} |
| 67 |
|
| 68 |
if (!queue) { |
| 69 |
|
| 70 |
char *sq; |
| 71 |
queue_bytes = amqp_bytes_malloc_dup(res->queue); |
| 72 |
sq = stringify_bytes(queue_bytes); |
| 73 |
if (sq == NULL) { |
| 74 |
fprintf(stderr, "Memory allocation failed.\n"); |
| 75 |
exit(1); |
| 76 |
} |
| 77 |
fprintf(stderr, "Server provided queue name: %s\n", sq); |
| 78 |
free(sq); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
if (exchange) { |
| 83 |
amqp_bytes_t eb = amqp_cstring_bytes(exchange); |
| 84 |
|
| 85 |
routing_tmp = strdup(routing_key); |
| 86 |
if (NULL == routing_tmp) { |
| 87 |
fprintf(stderr, "could not allocate memory to parse routing key\n"); |
| 88 |
exit(1); |
| 89 |
} |
| 90 |
|
| 91 |
for (routing_key_token = |
| 92 |
strtok_r(routing_tmp, LISTEN_KEYS_DELIMITER, &routing_key_rest); |
| 93 |
NULL != routing_key_token && routing_key_count < MAX_LISTEN_KEYS - 1; |
| 94 |
routing_key_token = |
| 95 |
strtok_r(NULL, LISTEN_KEYS_DELIMITER, &routing_key_rest)) { |
| 96 |
|
| 97 |
if (!amqp_queue_bind(conn, 1, queue_bytes, eb, |
| 98 |
cstring_bytes(routing_key_token), |
| 99 |
amqp_empty_table)) { |
| 100 |
die_rpc(amqp_get_rpc_reply(conn), "queue.bind"); |
| 101 |
} |
| 102 |
} |
| 103 |
free(routing_tmp); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return queue_bytes; |
| 108 |
} |
| 109 |
|
| 110 |
#define AMQP_CONSUME_MAX_PREFETCH_COUNT 65535 |
| 111 |
|
| 112 |
static void do_consume(amqp_connection_state_t conn, amqp_bytes_t queue, |
| 113 |
int no_ack, int count, int prefetch_count, |
| 114 |
const char *const *argv) { |
| 115 |
int i; |
| 116 |
|
| 117 |
|
| 118 |
if (count > 0 && count <= AMQP_CONSUME_MAX_PREFETCH_COUNT && |
| 119 |
!amqp_basic_qos(conn, 1, 0, count, 0)) { |
| 120 |
die_rpc(amqp_get_rpc_reply(conn), "basic.qos"); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
if (prefetch_count > 0 && prefetch_count <= AMQP_CONSUME_MAX_PREFETCH_COUNT) { |
| 126 |
|
| 127 |
|
| 128 |
if (!(count > 0 && count <= AMQP_CONSUME_MAX_PREFETCH_COUNT && |
| 129 |
prefetch_count >= count)) { |
| 130 |
if (!amqp_basic_qos(conn, 1, 0, prefetch_count, 0)) { |
| 131 |
die_rpc(amqp_get_rpc_reply(conn), "basic.qos"); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if (!amqp_basic_consume(conn, 1, queue, amqp_empty_bytes, 0, no_ack, 0, |
| 137 |
amqp_empty_table)) { |
| 138 |
die_rpc(amqp_get_rpc_reply(conn), "basic.consume"); |
| 139 |
} |
| 140 |
|
| 141 |
for (i = 0; count < 0 || i < count; i++) { |
| 142 |
amqp_frame_t frame; |
| 143 |
struct pipeline pl; |
| 144 |
uint64_t delivery_tag; |
| 145 |
amqp_basic_deliver_t *deliver; |
| 146 |
int res = amqp_simple_wait_frame(conn, &frame); |
| 147 |
die_amqp_error(res, "waiting for header frame"); |
| 148 |
|
| 149 |
if (frame.frame_type != AMQP_FRAME_METHOD || |
| 150 |
frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
deliver = (amqp_basic_deliver_t *)frame.payload.method.decoded; |
| 155 |
delivery_tag = deliver->delivery_tag; |
| 156 |
|
| 157 |
pipeline(argv, &pl); |
| 158 |
copy_body(conn, pl.infd); |
| 159 |
|
| 160 |
if (finish_pipeline(&pl) && !no_ack) |
| 161 |
die_amqp_error(amqp_basic_ack(conn, 1, delivery_tag, 0), "basic.ack"); |
| 162 |
|
| 163 |
amqp_maybe_release_buffers(conn); |
| 164 |
} |
| 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 |
|
| 181 |
struct poptOption options[] = { |
| 182 |
INCLUDE_OPTIONS(connect_options), |
| 183 |
{"queue", 'q', POPT_ARG_STRING, &queue, 0, "the queue to consume from", |
| 184 |
"queue"}, |
| 185 |
{"exchange", 'e', POPT_ARG_STRING, &exchange, 0, |
| 186 |
"bind the queue to this exchange", "exchange"}, |
| 187 |
{"routing-key", 'r', POPT_ARG_STRING, &routing_key, 0, |
| 188 |
"the routing key to bind with", "routing key"}, |
| 189 |
{"declare", 'd', POPT_ARG_NONE, &declare, 0, |
| 190 |
"declare an exclusive queue (deprecated, use --exclusive instead)", |
| 191 |
NULL}, |
| 192 |
{"exclusive", 'x', POPT_ARG_NONE, &exclusive, 0, |
| 193 |
"declare the queue as exclusive", NULL}, |
| 194 |
{"no-ack", 'A', POPT_ARG_NONE, &no_ack, 0, "consume in no-ack mode", |
| 195 |
NULL}, |
| 196 |
{"count", 'c', POPT_ARG_INT, &count, 0, |
| 197 |
"stop consuming after this many messages are consumed", "limit"}, |
| 198 |
{"prefetch-count", 'p', POPT_ARG_INT, &prefetch_count, 0, |
| 199 |
"receive only this many message at a time from the server", "limit"}, |
| 200 |
POPT_AUTOHELP{NULL, '\0', 0, NULL, 0, NULL, NULL}}; |
| 201 |
|
| 202 |
opts = process_options(argc, argv, options, "[OPTIONS]... <command> <args>"); |
| 203 |
|
| 204 |
cmd_argv = poptGetArgs(opts); |
| 205 |
if (!cmd_argv || !cmd_argv[0]) { |
| 206 |
fprintf(stderr, "consuming command not specified\n"); |
| 207 |
poptPrintUsage(opts, stderr, 0); |
| 208 |
goto error; |
| 209 |
} |
| 210 |
|
| 211 |
conn = make_connection(); |
| 212 |
queue_bytes = |
| 213 |
setup_queue(conn, queue, exchange, routing_key, declare, exclusive); |
| 214 |
do_consume(conn, queue_bytes, no_ack, count, prefetch_count, cmd_argv); |
| 215 |
close_connection(conn); |
| 216 |
return 0; |
| 217 |
|
| 218 |
error: |
| 219 |
poptFreeContext(opts); |
| 220 |
return 1; |
| 221 |
} |