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