nanomsg_services.cpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7 ** Includes
8 *****************************************************************************/
9 
10 #include <cstdio>
11 #include <cstdlib>
12 #include <ctime>
13 #include <cstring>
14 #include <unistd.h>
15 #include <nanomsg/nn.h>
16 #include <nanomsg/reqrep.h>
17 #include <iostream>
18 
19 #define SOCKET_ADDRESS "inproc://test"
20 
21 void test_recv(int sock, const char *data) {
22  size_t data_length;
23  void* buf = NULL;
24  data_length = strlen(data);
25  int bytes;
26  bytes = nn_recv(sock, &buf, NN_MSG, 0);
27  printf ("Socket %d EXPECTED %s RECEIVED %s [%d bytes]\n", sock, data, (char*)buf, bytes);
28  nn_freemsg(buf);
29 }
30 
31 int main ()
32 {
33  int rc;
34  int rep1;
35  int rep2;
36  int req1;
37  int req2;
38  int resend_ivl;
39  char buf [7];
40  int timeo;
41 
42  /* Test req/rep with full socket types. */
43  rep1 = nn_socket (AF_SP, NN_REP);
44  nn_bind (rep1, SOCKET_ADDRESS);
45  req1 = nn_socket (AF_SP, NN_REQ);
46  nn_connect (req1, SOCKET_ADDRESS);
47  req2 = nn_socket (AF_SP, NN_REQ);
48  nn_connect (req2, SOCKET_ADDRESS);
49 
50  /* Check invalid sequence of sends and recvs. */
51  rc = nn_send (rep1, "ABC", 3, 0);
52  //nn_assert (rc == -1 && nn_errno () == EFSM);
53  rc = nn_recv (req1, buf, sizeof (buf), 0);
54  //nn_assert (rc == -1 && nn_errno () == EFSM);
55 
56  /* Check fair queueing the requests. */
57  nn_send (req2, "ABC", 3, 0);
58  test_recv (rep1, "ABC");
59  nn_send (rep1, "ABC", 3, 0);
60  test_recv (req2, "ABC");
61 
62  nn_send (req1, "ABC", 3, 0);
63  test_recv (rep1, "ABC");
64  nn_send (rep1, "ABC", 3, 0);
65  test_recv (req1, "ABC");
66 
67  nn_close (rep1);
68  nn_close (req1);
69  nn_close (req2);
70 
71  /* Check load-balancing of requests. */
72  req1 = nn_socket (AF_SP, NN_REQ);
73  nn_bind (req1, SOCKET_ADDRESS);
74  rep1 = nn_socket (AF_SP, NN_REP);
75  nn_connect (rep1, SOCKET_ADDRESS);
76  rep2 = nn_socket (AF_SP, NN_REP);
77  nn_connect (rep2, SOCKET_ADDRESS);
78 
79  nn_send (req1, "ABC", 3, 0);
80  test_recv (rep1, "ABC");
81  nn_send (rep1, "ABC", 3, 0);
82  test_recv (req1, "ABC");
83 
84  nn_send (req1, "ABC", 3, 0);
85  test_recv (rep2, "ABC");
86  nn_send (rep2, "ABC", 3, 0);
87  test_recv (req1, "ABC");
88 
89  nn_close (rep2);
90  nn_close (rep1);
91  nn_close (req1);
92 
93  /* Test re-sending of the request. */
94  rep1 = nn_socket (AF_SP, NN_REP);
95  nn_bind (rep1, SOCKET_ADDRESS);
96  req1 = nn_socket (AF_SP, NN_REQ);
97  nn_connect (req1, SOCKET_ADDRESS);
98  resend_ivl = 100;
99  rc = nn_setsockopt (req1, NN_REQ, NN_REQ_RESEND_IVL,
100  &resend_ivl, sizeof (resend_ivl));
101 
102  nn_send (req1, "ABC", 3, 0);
103  test_recv (rep1, "ABC");
104  /* The following waits for request to be resent */
105  test_recv (rep1, "ABC");
106 
107  nn_close (req1);
108  nn_close (rep1);
109 
110  /* Check sending a request when the peer is not available. (It should
111  be sent immediatelly when the peer comes online rather than relying
112  on the resend algorithm. */
113  req1 = nn_socket (AF_SP, NN_REQ);
114  nn_connect (req1, SOCKET_ADDRESS);
115  nn_send (req1, "ABC", 3, 0);
116 
117  rep1 = nn_socket (AF_SP, NN_REP);
118  nn_bind (rep1, SOCKET_ADDRESS);
119  timeo = 200;
120  rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
121  &timeo, sizeof (timeo));
122 
123  nn_close (req1);
124  nn_close (rep1);
125 
126  /* Check removing socket request sent to (It should
127  be sent immediatelly to other peer rather than relying
128  on the resend algorithm). */
129  req1 = nn_socket (AF_SP, NN_REQ);
130  nn_bind (req1, SOCKET_ADDRESS);
131  rep1 = nn_socket (AF_SP, NN_REP);
132  nn_connect (rep1, SOCKET_ADDRESS);
133  rep2 = nn_socket (AF_SP, NN_REP);
134  nn_connect (rep2, SOCKET_ADDRESS);
135 
136  timeo = 200;
137  rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
138  &timeo, sizeof (timeo));
139  rc = nn_setsockopt (rep2, NN_SOL_SOCKET, NN_RCVTIMEO,
140  &timeo, sizeof (timeo));
141 
142  nn_send (req1, "ABC", 3, 0);
143  /* We got request through rep1 */
144  test_recv (rep1, "ABC");
145  /* But instead replying we simulate crash */
146  nn_close (rep1);
147  /* The rep2 should get request immediately */
148  test_recv (rep2, "ABC");
149  /* Let's check it's delivered well */
150  nn_send (rep2, "REPLY", 5, 0);
151  test_recv (req1, "REPLY");
152 
153 
154  nn_close (req1);
155  nn_close (rep2);
156 
157  /* Test cancelling delayed request */
158 
159  req1 = nn_socket (AF_SP, NN_REQ);
160  nn_connect (req1, SOCKET_ADDRESS);
161  nn_send (req1, "ABC", 3, 0);
162  nn_send (req1, "DEF", 3, 0);
163 
164  rep1 = nn_socket (AF_SP, NN_REP);
165  nn_bind (rep1, SOCKET_ADDRESS);
166  timeo = 100;
167 // rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
168 // &timeo, sizeof (timeo));
169 // errno_assert (rc == 0);
170  test_recv (rep1, "DEF");
171 
172  nn_close (req1);
173  nn_close (rep1);
174 
175  return 0;
176 }
177 
void test_recv(int sock, const char *data)
int main()
#define SOCKET_ADDRESS


mm_mux_demux
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:52:14