device.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright (c) 2012 Martin Sustrik All rights reserved.
  3. Copyright (c) 2013 GoPivotal, Inc. All rights reserved.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom
  9. the Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  18. IN THE SOFTWARE.
  19. */
  20. #include "../src/nn.h"
  21. #include "../src/bus.h"
  22. #include "../src/pair.h"
  23. #include "../src/pipeline.h"
  24. #include "../src/inproc.h"
  25. #include "testutil.h"
  26. #include "../src/utils/attr.h"
  27. #include "../src/utils/thread.c"
  28. #define SOCKET_ADDRESS_A "inproc://a"
  29. #define SOCKET_ADDRESS_B "inproc://b"
  30. #define SOCKET_ADDRESS_C "inproc://c"
  31. #define SOCKET_ADDRESS_D "inproc://d"
  32. #define SOCKET_ADDRESS_E "inproc://e"
  33. void device1 (NN_UNUSED void *arg)
  34. {
  35. int rc;
  36. int deva;
  37. int devb;
  38. /* Intialise the device sockets. */
  39. deva = test_socket (AF_SP_RAW, NN_PAIR);
  40. test_bind (deva, SOCKET_ADDRESS_A);
  41. devb = test_socket (AF_SP_RAW, NN_PAIR);
  42. test_bind (devb, SOCKET_ADDRESS_B);
  43. /* Run the device. */
  44. rc = nn_device (deva, devb);
  45. nn_assert (rc < 0 && (nn_errno () == EBADF));
  46. /* Clean up. */
  47. test_close (devb);
  48. test_close (deva);
  49. }
  50. void device2 (NN_UNUSED void *arg)
  51. {
  52. int rc;
  53. int devc;
  54. int devd;
  55. /* Intialise the device sockets. */
  56. devc = test_socket (AF_SP_RAW, NN_PULL);
  57. test_bind (devc, SOCKET_ADDRESS_C);
  58. devd = test_socket (AF_SP_RAW, NN_PUSH);
  59. test_bind (devd, SOCKET_ADDRESS_D);
  60. /* Run the device. */
  61. rc = nn_device (devc, devd);
  62. nn_assert (rc < 0 && nn_errno () == EBADF);
  63. /* Clean up. */
  64. test_close (devd);
  65. test_close (devc);
  66. }
  67. void device3 (NN_UNUSED void *arg)
  68. {
  69. int rc;
  70. int deve;
  71. /* Intialise the device socket. */
  72. deve = test_socket (AF_SP_RAW, NN_BUS);
  73. test_bind (deve, SOCKET_ADDRESS_E);
  74. /* Run the device. */
  75. rc = nn_device (deve, -1);
  76. nn_assert (rc < 0 && nn_errno () == EBADF);
  77. /* Clean up. */
  78. test_close (deve);
  79. }
  80. int main ()
  81. {
  82. int enda;
  83. int endb;
  84. int endc;
  85. int endd;
  86. int ende1;
  87. int ende2;
  88. struct nn_thread thread1;
  89. struct nn_thread thread2;
  90. struct nn_thread thread3;
  91. int timeo;
  92. /* Test the bi-directional device. */
  93. /* Start the device. */
  94. nn_thread_init (&thread1, device1, NULL);
  95. /* Create two sockets to connect to the device. */
  96. enda = test_socket (AF_SP, NN_PAIR);
  97. test_connect (enda, SOCKET_ADDRESS_A);
  98. endb = test_socket (AF_SP, NN_PAIR);
  99. test_connect (endb, SOCKET_ADDRESS_B);
  100. /* Pass a pair of messages between endpoints. */
  101. test_send (enda, "ABC");
  102. test_recv (endb, "ABC");
  103. test_send (endb, "ABC");
  104. test_recv (enda, "ABC");
  105. /* Clean up. */
  106. test_close (endb);
  107. test_close (enda);
  108. /* Test the uni-directional device. */
  109. /* Start the device. */
  110. nn_thread_init (&thread2, device2, NULL);
  111. /* Create two sockets to connect to the device. */
  112. endc = test_socket (AF_SP, NN_PUSH);
  113. test_connect (endc, SOCKET_ADDRESS_C);
  114. endd = test_socket (AF_SP, NN_PULL);
  115. test_connect (endd, SOCKET_ADDRESS_D);
  116. /* Pass a message between endpoints. */
  117. test_send (endc, "XYZ");
  118. test_recv (endd, "XYZ");
  119. /* Clean up. */
  120. test_close (endd);
  121. test_close (endc);
  122. /* Test the loopback device. */
  123. /* Start the device. */
  124. nn_thread_init (&thread3, device3, NULL);
  125. /* Create two sockets to connect to the device. */
  126. ende1 = test_socket (AF_SP, NN_BUS);
  127. test_connect (ende1, SOCKET_ADDRESS_E);
  128. ende2 = test_socket (AF_SP, NN_BUS);
  129. test_connect (ende2, SOCKET_ADDRESS_E);
  130. /* BUS is unreliable so wait a bit for connections to be established. */
  131. nn_sleep (100);
  132. /* Pass a message to the bus. */
  133. test_send (ende1, "KLM");
  134. test_recv (ende2, "KLM");
  135. /* Make sure that the message doesn't arrive at the socket it was
  136. originally sent to. */
  137. timeo = 100;
  138. test_setsockopt (ende1, NN_SOL_SOCKET, NN_RCVTIMEO,
  139. &timeo, sizeof (timeo));
  140. test_drop (ende1, ETIMEDOUT);
  141. /* Clean up. */
  142. test_close (ende2);
  143. test_close (ende1);
  144. /* Shut down the devices. */
  145. nn_term ();
  146. nn_thread_term (&thread1);
  147. nn_thread_term (&thread2);
  148. nn_thread_term (&thread3);
  149. return 0;
  150. }