Debian Codegraph @

examples/amqp_sendstring.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 "utils.h"
13
14 int main(int argc, char const *const *argv) {
15 char const *hostname;
16 int port, status;
17 char const *exchange;
18 char const *routingkey;
19 char const *messagebody;
20 amqp_socket_t *socket = NULL;
21 amqp_connection_state_t conn;
22
23 if (argc < 6) {
24 fprintf(
25 stderr,
26 "Usage: amqp_sendstring host port exchange routingkey messagebody\n");
27 return 1;
28 }
29
30 hostname = argv[1];
31 port = atoi(argv[2]);
32 exchange = argv[3];
33 routingkey = argv[4];
34 messagebody = argv[5];
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_basic_properties_t props;
56 props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
57 props.content_type = amqp_literal_bytes("text/plain");
58 props.delivery_mode = 2; /* persistent delivery mode */
59 die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange),
60 amqp_cstring_bytes(routingkey), 0, 0,
61 &props, amqp_cstring_bytes(messagebody)),
62 "Publishing");
63 }
64
65 die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
66 "Closing channel");
67 die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
68 "Closing connection");
69 die_on_error(amqp_destroy_connection(conn), "Ending connection");
70 return 0;
71 }