amqp_exchange_declare.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "utils.h"
  42. int main(int argc, char const *const *argv) {
  43. char const *hostname;
  44. int port, status;
  45. char const *exchange;
  46. char const *exchangetype;
  47. amqp_socket_t *socket = NULL;
  48. amqp_connection_state_t conn;
  49. if (argc < 5) {
  50. fprintf(stderr,
  51. "Usage: amqp_exchange_declare host port exchange exchangetype\n");
  52. return 1;
  53. }
  54. hostname = argv[1];
  55. port = atoi(argv[2]);
  56. exchange = argv[3];
  57. exchangetype = argv[4];
  58. conn = amqp_new_connection();
  59. socket = amqp_tcp_socket_new(conn);
  60. if (!socket) {
  61. die("creating TCP socket");
  62. }
  63. status = amqp_socket_open(socket, hostname, port);
  64. if (status) {
  65. die("opening TCP socket");
  66. }
  67. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  68. "guest", "guest"),
  69. "Logging in");
  70. amqp_channel_open(conn, 1);
  71. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  72. amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange),
  73. amqp_cstring_bytes(exchangetype), 0, 0, 0, 0,
  74. amqp_empty_table);
  75. die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange");
  76. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  77. "Closing channel");
  78. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  79. "Closing connection");
  80. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  81. return 0;
  82. }