00001
00006
00007
00008
00009
00010 #include <cstdio>
00011 #include <cstdlib>
00012 #include <ctime>
00013 #include <cstring>
00014 #include <unistd.h>
00015 #include <nanomsg/nn.h>
00016 #include <nanomsg/reqrep.h>
00017 #include <iostream>
00018
00019 #define SOCKET_ADDRESS "inproc://test"
00020
00021 void test_recv(int sock, const char *data) {
00022 size_t data_length;
00023 void* buf = NULL;
00024 data_length = strlen(data);
00025 int bytes;
00026 bytes = nn_recv(sock, &buf, NN_MSG, 0);
00027 printf ("Socket %d EXPECTED %s RECEIVED %s [%d bytes]\n", sock, data, (char*)buf, bytes);
00028 nn_freemsg(buf);
00029 }
00030
00031 int main ()
00032 {
00033 int rc;
00034 int rep1;
00035 int rep2;
00036 int req1;
00037 int req2;
00038 int resend_ivl;
00039 char buf [7];
00040 int timeo;
00041
00042
00043 rep1 = nn_socket (AF_SP, NN_REP);
00044 nn_bind (rep1, SOCKET_ADDRESS);
00045 req1 = nn_socket (AF_SP, NN_REQ);
00046 nn_connect (req1, SOCKET_ADDRESS);
00047 req2 = nn_socket (AF_SP, NN_REQ);
00048 nn_connect (req2, SOCKET_ADDRESS);
00049
00050
00051 rc = nn_send (rep1, "ABC", 3, 0);
00052
00053 rc = nn_recv (req1, buf, sizeof (buf), 0);
00054
00055
00056
00057 nn_send (req2, "ABC", 3, 0);
00058 test_recv (rep1, "ABC");
00059 nn_send (rep1, "ABC", 3, 0);
00060 test_recv (req2, "ABC");
00061
00062 nn_send (req1, "ABC", 3, 0);
00063 test_recv (rep1, "ABC");
00064 nn_send (rep1, "ABC", 3, 0);
00065 test_recv (req1, "ABC");
00066
00067 nn_close (rep1);
00068 nn_close (req1);
00069 nn_close (req2);
00070
00071
00072 req1 = nn_socket (AF_SP, NN_REQ);
00073 nn_bind (req1, SOCKET_ADDRESS);
00074 rep1 = nn_socket (AF_SP, NN_REP);
00075 nn_connect (rep1, SOCKET_ADDRESS);
00076 rep2 = nn_socket (AF_SP, NN_REP);
00077 nn_connect (rep2, SOCKET_ADDRESS);
00078
00079 nn_send (req1, "ABC", 3, 0);
00080 test_recv (rep1, "ABC");
00081 nn_send (rep1, "ABC", 3, 0);
00082 test_recv (req1, "ABC");
00083
00084 nn_send (req1, "ABC", 3, 0);
00085 test_recv (rep2, "ABC");
00086 nn_send (rep2, "ABC", 3, 0);
00087 test_recv (req1, "ABC");
00088
00089 nn_close (rep2);
00090 nn_close (rep1);
00091 nn_close (req1);
00092
00093
00094 rep1 = nn_socket (AF_SP, NN_REP);
00095 nn_bind (rep1, SOCKET_ADDRESS);
00096 req1 = nn_socket (AF_SP, NN_REQ);
00097 nn_connect (req1, SOCKET_ADDRESS);
00098 resend_ivl = 100;
00099 rc = nn_setsockopt (req1, NN_REQ, NN_REQ_RESEND_IVL,
00100 &resend_ivl, sizeof (resend_ivl));
00101
00102 nn_send (req1, "ABC", 3, 0);
00103 test_recv (rep1, "ABC");
00104
00105 test_recv (rep1, "ABC");
00106
00107 nn_close (req1);
00108 nn_close (rep1);
00109
00110
00111
00112
00113 req1 = nn_socket (AF_SP, NN_REQ);
00114 nn_connect (req1, SOCKET_ADDRESS);
00115 nn_send (req1, "ABC", 3, 0);
00116
00117 rep1 = nn_socket (AF_SP, NN_REP);
00118 nn_bind (rep1, SOCKET_ADDRESS);
00119 timeo = 200;
00120 rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
00121 &timeo, sizeof (timeo));
00122
00123 nn_close (req1);
00124 nn_close (rep1);
00125
00126
00127
00128
00129 req1 = nn_socket (AF_SP, NN_REQ);
00130 nn_bind (req1, SOCKET_ADDRESS);
00131 rep1 = nn_socket (AF_SP, NN_REP);
00132 nn_connect (rep1, SOCKET_ADDRESS);
00133 rep2 = nn_socket (AF_SP, NN_REP);
00134 nn_connect (rep2, SOCKET_ADDRESS);
00135
00136 timeo = 200;
00137 rc = nn_setsockopt (rep1, NN_SOL_SOCKET, NN_RCVTIMEO,
00138 &timeo, sizeof (timeo));
00139 rc = nn_setsockopt (rep2, NN_SOL_SOCKET, NN_RCVTIMEO,
00140 &timeo, sizeof (timeo));
00141
00142 nn_send (req1, "ABC", 3, 0);
00143
00144 test_recv (rep1, "ABC");
00145
00146 nn_close (rep1);
00147
00148 test_recv (rep2, "ABC");
00149
00150 nn_send (rep2, "REPLY", 5, 0);
00151 test_recv (req1, "REPLY");
00152
00153
00154 nn_close (req1);
00155 nn_close (rep2);
00156
00157
00158
00159 req1 = nn_socket (AF_SP, NN_REQ);
00160 nn_connect (req1, SOCKET_ADDRESS);
00161 nn_send (req1, "ABC", 3, 0);
00162 nn_send (req1, "DEF", 3, 0);
00163
00164 rep1 = nn_socket (AF_SP, NN_REP);
00165 nn_bind (rep1, SOCKET_ADDRESS);
00166 timeo = 100;
00167
00168
00169
00170 test_recv (rep1, "DEF");
00171
00172 nn_close (req1);
00173 nn_close (rep1);
00174
00175 return 0;
00176 }
00177