amqp_connect_timeout.c 3.0 KB

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