/** * Reply to a ping request from FontForge on localhost */ static int s_ping (zloop_t *loop, zmq_pollitem_t *poller, void *args) { clonesrv_t *self = (clonesrv_t *) args; char* p = zstr_recv_nowait( self->ping ); if( p && !strcmp(p,"quit")) { if( service_beacon ) zbeacon_destroy( &service_beacon ); exit(0); } zstr_send( self->ping, "pong" ); return 0; }
int main (int argc, char *argv []) { zctx_t *ctx = zctx_new (); // Use the CZMQ zbeacon class to make sure we listen on the // same network interface as our peers void *collector = zsocket_new (ctx, ZMQ_SUB); zbeacon_t *beacon = zbeacon_new (ZRE_DISCOVERY_PORT); char *host = zbeacon_hostname (beacon); // Bind to an ephemeral port int port = zsocket_bind (collector, "tcp://%s:*", host); // Announce this to all peers we connect to zre_node_t *node = zre_node_new (); zre_node_header_set (node, "X-ZRELOG", "tcp://%s:%d", host, port); // Get all log messages (don't filter) zsocket_set_subscribe (collector, ""); zmq_pollitem_t pollitems [] = { { collector, 0, ZMQ_POLLIN, 0 }, { zre_node_handle (node), 0, ZMQ_POLLIN, 0 } }; while (!zctx_interrupted) { if (zmq_poll (pollitems, 2, 1000 * ZMQ_POLL_MSEC) == -1) break; // Interrupted // Handle input on collector if (pollitems [0].revents & ZMQ_POLLIN) s_print_log_msg (collector); // Handle event from node (ignore it) if (pollitems [1].revents & ZMQ_POLLIN) { zmsg_t *msg = zre_node_recv (node); if (!msg) break; // Interrupted zmsg_destroy (&msg); } } zre_node_destroy (&node); zbeacon_destroy (&beacon); zctx_destroy (&ctx); return 0; }
static void zyre_node_destroy (zyre_node_t **self_p) { assert (self_p); if (*self_p) { zyre_node_t *self = *self_p; zuuid_destroy (&self->uuid); zhash_destroy (&self->peers); zhash_destroy (&self->peer_groups); zhash_destroy (&self->own_groups); zhash_destroy (&self->headers); zbeacon_destroy (&self->beacon); zyre_log_destroy (&self->log); free (self); *self_p = NULL; } }
static int zyre_node_stop (zyre_node_t *self) { if (self->beacon) { // Stop broadcast/listen beacon beacon_t beacon; beacon.protocol [0] = 'Z'; beacon.protocol [1] = 'R'; beacon.protocol [2] = 'E'; beacon.version = BEACON_VERSION; beacon.port = 0; // Zero means we're stopping zuuid_export (self->uuid, beacon.uuid); zbeacon_publish (self->beacon, (byte *) &beacon, sizeof (beacon_t)); zclock_sleep (1); // Allow 1 msec for beacon to go out zbeacon_destroy (&self->beacon); } // Stop polling on inbox zpoller_destroy (&self->poller); self->poller = zpoller_new (self->pipe, NULL); return 0; }
static void zyre_node_destroy (zyre_node_t **self_p) { assert (self_p); if (*self_p) { zyre_node_t *self = *self_p; zpoller_destroy (&self->poller); zuuid_destroy (&self->uuid); zhash_destroy (&self->peers); zhash_destroy (&self->peer_groups); zhash_destroy (&self->own_groups); zhash_destroy (&self->headers); zsock_destroy (&self->inbox); zsock_destroy (&self->outbox); zbeacon_destroy (&self->beacon); zstr_free (&self->endpoint); free (self->name); free (self); *self_p = NULL; } }
int main (int argc, char *argv []) { zctx_t *ctx = zctx_new (); // Use the CZMQ zbeacon class to make sure we listen on the // same network interface as our peers zbeacon_t *beacon = zbeacon_new (ctx, ZRE_DISCOVERY_PORT); char *host = zbeacon_hostname (beacon); // Bind to an ephemeral port void *collector = zsocket_new (ctx, ZMQ_SUB); int port = zsocket_bind (collector, "tcp://%s:*", host); zsocket_set_subscribe (collector, ""); // Announce this to all peers we connect to zyre_t *node = zyre_new (ctx); zyre_set_header (node, "X-ZRELOG", "tcp://%s:%d", host, port); zyre_start (node); zpoller_t *poller = zpoller_new (collector, zyre_socket (node), NULL); while (!zctx_interrupted) { void *which = zpoller_wait (poller, -1); if (which == collector) s_print_log_msg (collector); else if (which == zyre_socket (node)) { zmsg_t *msg = zyre_recv (node); if (!msg) break; // Interrupted zmsg_destroy (&msg); } else break; // Interrupted } zyre_destroy (&node); zbeacon_destroy (&beacon); zctx_destroy (&ctx); return 0; }