device7.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Copyright (c) 2013 GoPivotal, Inc. All rights reserved.
  4. Copyright 2015 Garrett D'Amore <garrett@damore.org>
  5. Copyright 2016 Franklin "Snaipe" Mathieu <franklinmathieu@gmail.com>
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"),
  8. to deal in the Software without restriction, including without limitation
  9. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. and/or sell copies of the Software, and to permit persons to whom
  11. the Software is furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included
  13. in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. IN THE SOFTWARE.
  21. */
  22. #include "../src/nn.h"
  23. #include "../src/reqrep.h"
  24. #include "../src/tcp.h"
  25. #include "../src/inproc.h"
  26. #include "testutil.h"
  27. #include "../src/utils/attr.h"
  28. #include "../src/utils/thread.c"
  29. #define SOCKET_ADDRESS_I "inproc://nobody"
  30. static char socket_address_h[128], socket_address_j[128];
  31. void device5 (NN_UNUSED void *arg)
  32. {
  33. int rc;
  34. int dev0;
  35. int dev1;
  36. /* Intialise the device sockets. */
  37. dev0 = test_socket (AF_SP_RAW, NN_REP);
  38. test_bind (dev0, socket_address_h);
  39. dev1 = test_socket (AF_SP_RAW, NN_REQ);
  40. test_bind (dev1, SOCKET_ADDRESS_I);
  41. /* Run the device. */
  42. rc = nn_device (dev0, dev1);
  43. nn_assert (rc < 0 && nn_errno () == EBADF);
  44. /* Clean up. */
  45. test_close (dev0);
  46. test_close (dev1);
  47. }
  48. void device6 (NN_UNUSED void *arg)
  49. {
  50. int rc;
  51. int dev2;
  52. int dev3;
  53. dev2 = test_socket (AF_SP_RAW, NN_REP);
  54. test_connect (dev2, SOCKET_ADDRESS_I);
  55. dev3 = test_socket (AF_SP_RAW, NN_REQ);
  56. test_bind (dev3, socket_address_j);
  57. /* Run the device. */
  58. rc = nn_device (dev2, dev3);
  59. nn_assert (rc < 0 && nn_errno () == EBADF);
  60. /* Clean up. */
  61. test_close (dev2);
  62. test_close (dev3);
  63. }
  64. int main (int argc, const char *argv[])
  65. {
  66. int end0;
  67. int end1;
  68. struct nn_thread thread5;
  69. struct nn_thread thread6;
  70. int port = get_test_port(argc, argv);
  71. test_addr_from(socket_address_h, "tcp", "127.0.0.1", port);
  72. test_addr_from(socket_address_j, "tcp", "127.0.0.1", port + 1);
  73. /* Test the bi-directional device with REQ/REP (headers). */
  74. /* Start the devices. */
  75. nn_thread_init (&thread5, device5, NULL);
  76. nn_thread_init (&thread6, device6, NULL);
  77. /* Create two sockets to connect to the device. */
  78. end0 = test_socket (AF_SP, NN_REQ);
  79. test_connect (end0, socket_address_h);
  80. end1 = test_socket (AF_SP, NN_REP);
  81. test_connect (end1, socket_address_j);
  82. /* Wait for TCP to establish. */
  83. nn_sleep (1000);
  84. /* Pass a message between endpoints. */
  85. test_send (end0, "XYZ");
  86. test_recv (end1, "XYZ");
  87. /* Now send a reply. */
  88. test_send (end1, "REPLYXYZ");
  89. test_recv (end0, "REPLYXYZ");
  90. /* Clean up. */
  91. test_close (end0);
  92. test_close (end1);
  93. /* Shut down the devices. */
  94. nn_term ();
  95. nn_thread_term (&thread5);
  96. nn_thread_term (&thread6);
  97. return 0;
  98. }