1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <nanomsg/nn.h>
#include <nanomsg/pair.h>
#include <stdio.h>
#define NODE0 "node0"
#define NODE1 "node1"
int send_name(int sock, const char *name)
{
printf ("%s: SENDING \"%s\"\n", name, name);
int sz_n = strlen (name) + 1; // '\0' too
return nn_send (sock, name, sz_n, 0);
}
int recv_name(int sock, const char *name)
{
char *buf = NULL;
int result = nn_recv (sock, &buf, NN_MSG, 0);
if (result > 0)
{
printf ("%s: RECEIVED \"%s\"\n", name, buf);
nn_freemsg (buf);
}
return result;
}
int send_recv(int sock, const char *name)
{
int to = 100;
assert (nn_setsockopt (sock, NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof (to)) >= 0);
while(1)
{
recv_name(sock, name);
sleep(1);
send_name(sock, name);
}
}
int node0 (const char *url)
{
int sock = nn_socket (AF_SP, NN_PAIR);
assert (sock >= 0);
assert (nn_bind (sock, url) >= 0);
send_recv(sock, NODE0);
return nn_shutdown (sock, 0);
}
int node1 (const char *url)
{
int sock = nn_socket (AF_SP, NN_PAIR);
assert (sock >= 0);
assert (nn_connect (sock, url) >= 0);
send_recv(sock, NODE1);
return nn_shutdown (sock, 0);
}
int main (const int argc, const char **argv)
{
if (strncmp (NODE0, argv[1], strlen (NODE0)) == 0 && argc > 1)
return node0 (argv[2]);
else if (strncmp (NODE1, argv[1], strlen (NODE1)) == 0 && argc > 1)
return node1 (argv[2]);
else
{
fprintf (stderr, "Usage: pair %s|%s <URL> <ARG> ...\n",
NODE0, NODE1);
return 1;
}
}
|