amqp_rpc_sendstring_client.c 7.1 KB

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