| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <stdlib.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <rabbitmq-c/amqp.h> |
| 10 |
#include <rabbitmq-c/tcp_socket.h> |
| 11 |
|
| 12 |
#include <assert.h> |
| 13 |
|
| 14 |
#include "utils.h" |
| 15 |
|
| 16 |
int main(int argc, char const *const *argv) { |
| 17 |
char const *hostname; |
| 18 |
int port, status; |
| 19 |
char const *exchange; |
| 20 |
char const *bindingkey; |
| 21 |
amqp_socket_t *socket = NULL; |
| 22 |
amqp_connection_state_t conn; |
| 23 |
|
| 24 |
amqp_bytes_t queuename; |
| 25 |
|
| 26 |
if (argc < 5) { |
| 27 |
fprintf(stderr, "Usage: amqp_listen host port exchange bindingkey\n"); |
| 28 |
return 1; |
| 29 |
} |
| 30 |
|
| 31 |
hostname = argv[1]; |
| 32 |
port = atoi(argv[2]); |
| 33 |
exchange = argv[3]; |
| 34 |
bindingkey = argv[4]; |
| 35 |
|
| 36 |
conn = amqp_new_connection(); |
| 37 |
|
| 38 |
socket = amqp_tcp_socket_new(conn); |
| 39 |
if (!socket) { |
| 40 |
die("creating TCP socket"); |
| 41 |
} |
| 42 |
|
| 43 |
status = amqp_socket_open(socket, hostname, port); |
| 44 |
if (status) { |
| 45 |
die("opening TCP socket"); |
| 46 |
} |
| 47 |
|
| 48 |
die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, |
| 49 |
"guest", "guest"), |
| 50 |
"Logging in"); |
| 51 |
amqp_channel_open(conn, 1); |
| 52 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); |
| 53 |
|
| 54 |
{ |
| 55 |
amqp_queue_declare_ok_t *r = amqp_queue_declare( |
| 56 |
conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table); |
| 57 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue"); |
| 58 |
queuename = amqp_bytes_malloc_dup(r->queue); |
| 59 |
if (queuename.bytes == NULL) { |
| 60 |
fprintf(stderr, "Out of memory while copying queue name"); |
| 61 |
return 1; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange), |
| 66 |
amqp_cstring_bytes(bindingkey), amqp_empty_table); |
| 67 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue"); |
| 68 |
|
| 69 |
amqp_basic_consume(conn, 1, queuename, amqp_empty_bytes, 0, 1, 0, |
| 70 |
amqp_empty_table); |
| 71 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); |
| 72 |
|
| 73 |
{ |
| 74 |
for (;;) { |
| 75 |
amqp_rpc_reply_t res; |
| 76 |
amqp_envelope_t envelope; |
| 77 |
|
| 78 |
amqp_maybe_release_buffers(conn); |
| 79 |
|
| 80 |
res = amqp_consume_message(conn, &envelope, NULL, 0); |
| 81 |
|
| 82 |
if (AMQP_RESPONSE_NORMAL != res.reply_type) { |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
printf("Delivery %u, exchange %.*s routingkey %.*s\n", |
| 87 |
(unsigned)envelope.delivery_tag, (int)envelope.exchange.len, |
| 88 |
(char *)envelope.exchange.bytes, (int)envelope.routing_key.len, |
| 89 |
(char *)envelope.routing_key.bytes); |
| 90 |
|
| 91 |
if (envelope.message.properties._flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { |
| 92 |
printf("Content-type: %.*s\n", |
| 93 |
(int)envelope.message.properties.content_type.len, |
| 94 |
(char *)envelope.message.properties.content_type.bytes); |
| 95 |
} |
| 96 |
printf("----\n"); |
| 97 |
|
| 98 |
amqp_dump(envelope.message.body.bytes, envelope.message.body.len); |
| 99 |
|
| 100 |
amqp_destroy_envelope(&envelope); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
amqp_bytes_free(queuename); |
| 105 |
|
| 106 |
die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), |
| 107 |
"Closing channel"); |
| 108 |
die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), |
| 109 |
"Closing connection"); |
| 110 |
die_on_error(amqp_destroy_connection(conn), "Ending connection"); |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |