Debian Codegraph @

examples/amqp_listenq.c

raw symbols SCIP
1 // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
2 // SPDX-License-Identifier: mit
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 *queuename;
20 amqp_socket_t *socket = NULL;
21 amqp_connection_state_t conn;
22
23 if (argc < 4) {
24 fprintf(stderr, "Usage: amqp_listenq host port queuename\n");
25 return 1;
26 }
27
28 hostname = argv[1];
29 port = atoi(argv[2]);
30 queuename = argv[3];
31
32 conn = amqp_new_connection();
33
34 socket = amqp_tcp_socket_new(conn);
35 if (!socket) {
36 die("creating TCP socket");
37 }
38
39 status = amqp_socket_open(socket, hostname, port);
40 if (status) {
41 die("opening TCP socket");
42 }
43
44 die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
45 "guest", "guest"),
46 "Logging in");
47 amqp_channel_open(conn, 1);
48 die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
49
50 amqp_basic_consume(conn, 1, amqp_cstring_bytes(queuename), amqp_empty_bytes,
51 0, 0, 0, amqp_empty_table);
52 die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming");
53
54 for (;;) {
55 amqp_rpc_reply_t res;
56 amqp_envelope_t envelope;
57
58 amqp_maybe_release_buffers(conn);
59
60 res = amqp_consume_message(conn, &envelope, NULL, 0);
61
62 if (AMQP_RESPONSE_NORMAL != res.reply_type) {
63 break;
64 }
65
66 printf("Delivery %u, exchange %.*s routingkey %.*s\n",
67 (unsigned)envelope.delivery_tag, (int)envelope.exchange.len,
68 (char *)envelope.exchange.bytes, (int)envelope.routing_key.len,
69 (char *)envelope.routing_key.bytes);
70
71 if (envelope.message.properties._flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
72 printf("Content-type: %.*s\n",
73 (int)envelope.message.properties.content_type.len,
74 (char *)envelope.message.properties.content_type.bytes);
75 }
76 printf("----\n");
77
78 amqp_dump(envelope.message.body.bytes, envelope.message.body.len);
79
80 amqp_destroy_envelope(&envelope);
81 }
82
83 die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
84 "Closing channel");
85 die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
86 "Closing connection");
87 die_on_error(amqp_destroy_connection(conn), "Ending connection");
88
89 return 0;
90 }