1 /*
2  * ***** BEGIN LICENSE BLOCK *****
3  * Version: MIT
4  *
5  * Portions created by Alan Antonuk are Copyright (c) 2012-2013
6  * Alan Antonuk. All Rights Reserved.
7  *
8  * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
9  * All Rights Reserved.
10  *
11  * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
12  * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
13  *
14  * Permission is hereby granted, free of charge, to any person
15  * obtaining a copy of this software and associated documentation
16  * files (the "Software"), to deal in the Software without
17  * restriction, including without limitation the rights to use, copy,
18  * modify, merge, publish, distribute, sublicense, and/or sell copies
19  * of the Software, and to permit persons to whom the Software is
20  * furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be
23  * included in all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  * ***** END LICENSE BLOCK *****
34  */
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include <amqp.h>
42 #include <amqp_tcp_socket.h>
43 
44 #include "utils.h"
45 
main(int argc,char const * const * argv)46 int main(int argc, char const *const *argv) {
47   char const *hostname;
48   int port, status;
49   char const *exchange;
50   char const *routingkey;
51   char const *messagebody;
52   amqp_socket_t *socket = NULL;
53   amqp_connection_state_t conn;
54 
55   if (argc < 6) {
56     fprintf(
57         stderr,
58         "Usage: amqp_sendstring host port exchange routingkey messagebody\n");
59     return 1;
60   }
61 
62   hostname = argv[1];
63   port = atoi(argv[2]);
64   exchange = argv[3];
65   routingkey = argv[4];
66   messagebody = argv[5];
67 
68   conn = amqp_new_connection();
69 
70   socket = amqp_tcp_socket_new(conn);
71   if (!socket) {
72     die("creating TCP socket");
73   }
74 
75   status = amqp_socket_open(socket, hostname, port);
76   if (status) {
77     die("opening TCP socket");
78   }
79 
80   die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
81                                "guest", "guest"),
82                     "Logging in");
83   amqp_channel_open(conn, 1);
84   die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
85 
86   {
87     amqp_basic_properties_t props;
88     props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
89     props.content_type = amqp_cstring_bytes("text/plain");
90     props.delivery_mode = 2; /* persistent delivery mode */
91     die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange),
92                                     amqp_cstring_bytes(routingkey), 0, 0,
93                                     &props, amqp_cstring_bytes(messagebody)),
94                  "Publishing");
95   }
96 
97   die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
98                     "Closing channel");
99   die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
100                     "Closing connection");
101   die_on_error(amqp_destroy_connection(conn), "Ending connection");
102   return 0;
103 }
104