| 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 *argv[]) { |
| 17 |
char const *hostname; |
| 18 |
int port, status; |
| 19 |
char const *exchange; |
| 20 |
char const *routingkey; |
| 21 |
char const *messagebody; |
| 22 |
amqp_socket_t *socket = NULL; |
| 23 |
amqp_connection_state_t conn; |
| 24 |
amqp_bytes_t reply_to_queue; |
| 25 |
|
| 26 |
if (argc < 6) { |
| 27 |
fprintf(stderr, |
| 28 |
"usage:\namqp_rpc_sendstring_client host port exchange routingkey " |
| 29 |
"messagebody\n"); |
| 30 |
return 1; |
| 31 |
} |
| 32 |
|
| 33 |
hostname = argv[1]; |
| 34 |
port = atoi(argv[2]); |
| 35 |
exchange = argv[3]; |
| 36 |
routingkey = argv[4]; |
| 37 |
messagebody = argv[5]; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
conn = amqp_new_connection(); |
| 44 |
|
| 45 |
socket = amqp_tcp_socket_new(conn); |
| 46 |
if (!socket) { |
| 47 |
die("creating TCP socket"); |
| 48 |
} |
| 49 |
|
| 50 |
status = amqp_socket_open(socket, hostname, port); |
| 51 |
if (status) { |
| 52 |
die("opening TCP socket"); |
| 53 |
} |
| 54 |
|
| 55 |
die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN, |
| 56 |
"guest", "guest"), |
| 57 |
"Logging in"); |
| 58 |
amqp_channel_open(conn, 1); |
| 59 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel"); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
{ |
| 66 |
amqp_queue_declare_ok_t *r = amqp_queue_declare( |
| 67 |
conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table); |
| 68 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue"); |
| 69 |
reply_to_queue = amqp_bytes_malloc_dup(r->queue); |
| 70 |
if (reply_to_queue.bytes == NULL) { |
| 71 |
fprintf(stderr, "Out of memory while copying queue name"); |
| 72 |
return 1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
{ |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
amqp_basic_properties_t props; |
| 85 |
props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | |
| 86 |
AMQP_BASIC_DELIVERY_MODE_FLAG | AMQP_BASIC_REPLY_TO_FLAG | |
| 87 |
AMQP_BASIC_CORRELATION_ID_FLAG; |
| 88 |
props.content_type = amqp_literal_bytes("text/plain"); |
| 89 |
props.delivery_mode = 2; |
| 90 |
props.reply_to = amqp_bytes_malloc_dup(reply_to_queue); |
| 91 |
if (props.reply_to.bytes == NULL) { |
| 92 |
fprintf(stderr, "Out of memory while copying queue name"); |
| 93 |
return 1; |
| 94 |
} |
| 95 |
props.correlation_id = amqp_literal_bytes("1"); |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange), |
| 101 |
amqp_cstring_bytes(routingkey), 0, 0, |
| 102 |
&props, amqp_cstring_bytes(messagebody)), |
| 103 |
"Publishing"); |
| 104 |
|
| 105 |
amqp_bytes_free(props.reply_to); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
{ |
| 113 |
amqp_basic_consume(conn, 1, reply_to_queue, amqp_empty_bytes, 0, 1, 0, |
| 114 |
amqp_empty_table); |
| 115 |
die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming"); |
| 116 |
amqp_bytes_free(reply_to_queue); |
| 117 |
|
| 118 |
{ |
| 119 |
amqp_frame_t frame; |
| 120 |
int result; |
| 121 |
|
| 122 |
amqp_basic_deliver_t *d; |
| 123 |
amqp_basic_properties_t *p; |
| 124 |
size_t body_target; |
| 125 |
size_t body_received; |
| 126 |
|
| 127 |
for (;;) { |
| 128 |
amqp_maybe_release_buffers(conn); |
| 129 |
result = amqp_simple_wait_frame(conn, &frame); |
| 130 |
printf("Result: %d\n", result); |
| 131 |
if (result < 0) { |
| 132 |
break; |
| 133 |
} |
| 134 |
|
| 135 |
printf("Frame type: %u channel: %u\n", frame.frame_type, frame.channel); |
| 136 |
if (frame.frame_type != AMQP_FRAME_METHOD) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
printf("Method: %s\n", amqp_method_name(frame.payload.method.id)); |
| 141 |
if (frame.payload.method.id != AMQP_BASIC_DELIVER_METHOD) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
d = (amqp_basic_deliver_t *)frame.payload.method.decoded; |
| 146 |
printf("Delivery: %u exchange: %.*s routingkey: %.*s\n", |
| 147 |
(unsigned)d->delivery_tag, (int)d->exchange.len, |
| 148 |
(char *)d->exchange.bytes, (int)d->routing_key.len, |
| 149 |
(char *)d->routing_key.bytes); |
| 150 |
|
| 151 |
result = amqp_simple_wait_frame(conn, &frame); |
| 152 |
if (result < 0) { |
| 153 |
break; |
| 154 |
} |
| 155 |
|
| 156 |
if (frame.frame_type != AMQP_FRAME_HEADER) { |
| 157 |
fprintf(stderr, "Expected header!"); |
| 158 |
abort(); |
| 159 |
} |
| 160 |
p = (amqp_basic_properties_t *)frame.payload.properties.decoded; |
| 161 |
if (p->_flags & AMQP_BASIC_CONTENT_TYPE_FLAG) { |
| 162 |
printf("Content-type: %.*s\n", (int)p->content_type.len, |
| 163 |
(char *)p->content_type.bytes); |
| 164 |
} |
| 165 |
printf("----\n"); |
| 166 |
|
| 167 |
body_target = (size_t)frame.payload.properties.body_size; |
| 168 |
body_received = 0; |
| 169 |
|
| 170 |
while (body_received < body_target) { |
| 171 |
result = amqp_simple_wait_frame(conn, &frame); |
| 172 |
if (result < 0) { |
| 173 |
break; |
| 174 |
} |
| 175 |
|
| 176 |
if (frame.frame_type != AMQP_FRAME_BODY) { |
| 177 |
fprintf(stderr, "Expected body!"); |
| 178 |
abort(); |
| 179 |
} |
| 180 |
|
| 181 |
body_received += frame.payload.body_fragment.len; |
| 182 |
assert(body_received <= body_target); |
| 183 |
|
| 184 |
amqp_dump(frame.payload.body_fragment.bytes, |
| 185 |
frame.payload.body_fragment.len); |
| 186 |
} |
| 187 |
|
| 188 |
if (body_received != body_target) { |
| 189 |
|
| 190 |
|
| 191 |
break; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS), |
| 205 |
"Closing channel"); |
| 206 |
die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), |
| 207 |
"Closing connection"); |
| 208 |
die_on_error(amqp_destroy_connection(conn), "Ending connection"); |
| 209 |
|
| 210 |
return 0; |
| 211 |
} |