device6.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/survey.h"
  24. #include "../src/tcp.h"
  25. #include "testutil.h"
  26. #include "../src/utils/attr.h"
  27. #include "../src/utils/thread.c"
  28. static char socket_address_h[128], socket_address_i[128], socket_address_j[128];
  29. void device5 (NN_UNUSED void *arg)
  30. {
  31. int rc;
  32. int dev0;
  33. int dev1;
  34. /* Intialise the device sockets. */
  35. dev0 = test_socket (AF_SP_RAW, NN_RESPONDENT);
  36. test_bind (dev0, socket_address_h);
  37. dev1 = test_socket (AF_SP_RAW, NN_SURVEYOR);
  38. test_bind (dev1, socket_address_i);
  39. /* Run the device. */
  40. rc = nn_device (dev0, dev1);
  41. nn_assert (rc < 0 && nn_errno () == EBADF);
  42. /* Clean up. */
  43. test_close (dev0);
  44. test_close (dev1);
  45. }
  46. void device6 (NN_UNUSED void *arg)
  47. {
  48. int rc;
  49. int dev2;
  50. int dev3;
  51. dev2 = test_socket (AF_SP_RAW, NN_RESPONDENT);
  52. test_connect (dev2, socket_address_i);
  53. dev3 = test_socket (AF_SP_RAW, NN_SURVEYOR);
  54. test_bind (dev3, socket_address_j);
  55. /* Run the device. */
  56. rc = nn_device (dev2, dev3);
  57. nn_assert (rc < 0 && nn_errno () == EBADF);
  58. /* Clean up. */
  59. test_close (dev2);
  60. test_close (dev3);
  61. }
  62. int main (int argc, const char *argv[])
  63. {
  64. int end0;
  65. int end1;
  66. struct nn_thread thread5;
  67. struct nn_thread thread6;
  68. int port = get_test_port(argc, argv);
  69. test_addr_from(socket_address_h, "tcp", "127.0.0.1", port);
  70. test_addr_from(socket_address_i, "tcp", "127.0.0.1", port + 1);
  71. test_addr_from(socket_address_j, "tcp", "127.0.0.1", port + 2);
  72. /* Test the bi-directional device with SURVEYOR(headers). */
  73. /* Start the devices. */
  74. nn_thread_init (&thread5, device5, NULL);
  75. nn_thread_init (&thread6, device6, NULL);
  76. /* Create two sockets to connect to the device. */
  77. end0 = test_socket (AF_SP, NN_SURVEYOR);
  78. test_connect (end0, socket_address_h);
  79. end1 = test_socket (AF_SP, NN_RESPONDENT);
  80. test_connect (end1, socket_address_j);
  81. /* Wait up to a second for TCP to establish. */
  82. nn_sleep (1000);
  83. /* Pass a message between endpoints. */
  84. test_send (end0, "XYZ");
  85. test_recv (end1, "XYZ");
  86. /* Now send a reply. */
  87. test_send (end1, "REPLYXYZ");
  88. test_recv (end0, "REPLYXYZ");
  89. /* Clean up. */
  90. test_close (end0);
  91. test_close (end1);
  92. /* Shut down the devices. */
  93. nn_term ();
  94. nn_thread_term (&thread5);
  95. nn_thread_term (&thread6);
  96. return 0;
  97. }