forked from sthoduka/zyre-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzyre_listener.c
60 lines (55 loc) · 1.47 KB
/
zyre_listener.c
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
#include <zyre.h>
// this program starts a zyre node, joins a group,
// receives messages and prints the message frame by frame
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: ./zyre_listener <group name>\n");
return 1;
}
char * group_name = argv[1];
// create a new node
zyre_t *node = zyre_new("listener");
if (!node)
{
return 1; // Could not create new node
}
// this sends an ENTER message
zyre_start(node);
// this sends a JOIN message
zyre_join(node, group_name);
// wait for a while
zclock_sleep(250);
// print UUID of node
printf("UUID: %s\n", zyre_uuid(node));
while (true)
{
zmsg_t * msg = zyre_recv(node);
// loop through the frames in the message
while(true)
{
char * msg_str = zmsg_popstr(msg);
if (!msg_str)
{
break;
}
printf("frame: %s\n", msg_str);
// with zmsg_popstr, you own the frame string
// so you need to free it
free(msg_str);
}
printf("------\n");
// you own the message with zyre_recv
// so you need to destroy it
zmsg_destroy(&msg);
}
// this sends a LEAVE message
zyre_leave(node, group_name);
// this sends an EXIT message
zyre_stop(node);
// wait for node to stop
zclock_sleep(100);
zyre_destroy(&node);
return 0;
}