Skip to content

Instantly share code, notes, and snippets.

@sanandak

sanandak/zyretest.c

Created Jan 24, 2015
Embed
What would you like to do?
zyre test
/*
* test zyre lib
*/
#include <zyre.h>
int main(int argc, char *argv[])
{
char progname[128];
int nread, ret;
openlog(progname, LOG_PID, LOG_USER);
syslog(LOG_INFO, "Starting testZyre...\n");
/* open and join the zyre network. Announce your name */
int major, minor, patch;
zyre_version(&major, &minor, &patch);
fprintf(stderr, "zyre: %d %d %d\n", major, minor, patch);
zsys_version(&major, &minor, &patch);
fprintf(stderr, "czmq: %d %d %d\n", major, minor, patch);
zyre_t *node = zyre_new(NULL);
assert(node);
zyre_set_verbose(node);
ret = zyre_start(node);
assert(ret==0);
zyre_join(node, "GLOBAL");
while(1) {
static char *sender, *ipaddr, *base = NULL, *hdr, *name;
char *msgstring;
zyre_event_t *zev = zyre_event_new(node);
zmsg_t *msg;// = zyre_recv(node);
printf("after zyre event new\n");
switch(zyre_event_type(zev)) {
case ZYRE_EVENT_ENTER:
sender = zyre_event_sender(zev);
ipaddr = zyre_event_address(zev);
hdr = zyre_event_header(zev, "X-HELLO");
name = zyre_event_name(zev);
if(strncmp(name, "base", 4) == 0) {
base = sender;
syslog(LOG_INFO, "base %s name %s entered with header: %s\n", base, name, hdr);
} else {
sender = zyre_event_sender(zev);
syslog(LOG_INFO, "node %s name %s entered with header: %s\n", sender, name, hdr);
}
break;
case ZYRE_EVENT_JOIN:
sender = zyre_event_sender(zev);
syslog(LOG_INFO, "JOIN - node %s joined\n", sender);
break;
case ZYRE_EVENT_SHOUT:
case ZYRE_EVENT_WHISPER:
name = zyre_event_name(zev);
syslog(LOG_DEBUG,"SHOUT/WHISPER from %s", name);
//msg = zmsg_new();
msg = zyre_event_msg(zev);
syslog(LOG_DEBUG, "SHOUT/WHISPER msg ");
zmsg_print(msg);
// handle msg
zmsg_destroy(&msg);
break;
case ZYRE_EVENT_EXIT:
sender = zyre_event_sender(zev);
syslog(LOG_INFO, "EXIT - node %s\n", sender);
break;
}
}
zyre_stop(node);
zyre_destroy(&node);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.