static int zyre_node_start (zyre_node_t *self) { // If application didn't bind explicitly, we grab an ephemeral port // on all available network interfaces. This is orthogonal to // beaconing, since we can connect to other peers and they will // gossip our endpoint to others. if (!self->bound) { self->port = zsock_bind (self->inbox, "tcp://*:*"); if (self->port < 0) return 1; // Could not get new port to bind to? self->bound = true; } // Start UDP beaconing, if the application didn't disable it if (self->beacon_port) { assert (!self->beacon); self->beacon = zbeacon_new (NULL, self->beacon_port); if (!self->beacon) return 1; // Not possible to start beacon if (self->interval) zbeacon_set_interval (self->beacon, self->interval); zpoller_add (self->poller, zbeacon_socket (self->beacon)); // Set broadcast/listen beacon beacon_t beacon; beacon.protocol [0] = 'Z'; beacon.protocol [1] = 'R'; beacon.protocol [2] = 'E'; beacon.version = BEACON_VERSION; beacon.port = htons (self->port); zuuid_export (self->uuid, beacon.uuid); zbeacon_noecho (self->beacon); zbeacon_publish (self->beacon, (byte *) &beacon, sizeof (beacon_t)); zbeacon_subscribe (self->beacon, (byte *) "ZRE", 3); // Our own host endpoint is provided by the beacon assert (!self->endpoint); self->endpoint = zsys_sprintf ("tcp://%s:%d", zbeacon_hostname (self->beacon), self->port); } else if (!self->endpoint) { char *hostname = zsys_hostname (); self->endpoint = zsys_sprintf ("tcp://%s:%d", hostname, self->port); zstr_free (&hostname); } // Start polling on inbox zpoller_add (self->poller, self->inbox); 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; }
void collabclient_ensureClientBeacon(void) { if( client_beacon ) return; peers = g_hash_table_new_full( g_str_hash, g_str_equal, 0, free ); client_beacon_timerID = 0; client_beacon = zbeacon_new (5670); zbeacon_subscribe (client_beacon, NULL, 0); int fd = 0; size_t fdsz = sizeof(fd); int rc = zmq_getsockopt( zbeacon_socket(client_beacon), ZMQ_FD, &fd, &fdsz ); printf("beacon rc:%d fd:%d\n", rc, fd ); // GDrawAddReadFD( 0, fd, cc, zeromq_beacon_fd_callback ); client_beacon_timerID = BackgroundTimer_new( 1000, zeromq_beacon_timer_callback, 0 ); }
void collabclient_ensureClientBeacon(void) { if( client_beacon ) return; peers = g_hash_table_new_full( g_str_hash, g_str_equal, 0, g_free ); client_beacon_timerID = 0; client_beacon = zbeacon_new( obtainMainZMQContext(), 5670 ); DEBUG("client beacon address: %s\n", zbeacon_hostname(client_beacon)); zbeacon_subscribe (client_beacon, NULL, 0); zsocket_set_rcvtimeo (zbeacon_socket (client_beacon), 100); int fd = 0; size_t fdsz = sizeof(fd); int rc = zmq_getsockopt( zbeacon_socket(client_beacon), ZMQ_FD, &fd, &fdsz ); // printf("beacon rc:%d fd:%d\n", rc, fd ); // GDrawAddReadFD( 0, fd, cc, zeromq_beacon_fd_callback ); client_beacon_timerID = BackgroundTimer_new( 1000, zeromq_beacon_timer_callback, 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 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; }
/** * COllect Updates * * We store each update with a new sequence number, and if necessary, a * time-to-live. We publish updates immediately on our publisher socket: */ static int s_collector (zloop_t *loop, zmq_pollitem_t *poller, void *args) { clonesrv_t *self = (clonesrv_t *) args; DEBUG("I: s_collector"); kvmsg_t *kvmsg = kvmsg_recv (poller->socket); if (kvmsg) { kvmsg_set_sequence (kvmsg, ++self->sequence); kvmsg_fmt_key(kvmsg, "%s%d", SUBTREE, self->sequence-1 ); if( !strcmp(MSG_TYPE_SFD, kvmsg_get_prop (kvmsg, "type"))) { // setup the beacon // Broadcast on the zyre port beacon_announce_t ba; memset( &ba, 0, sizeof(ba)); strcpy( ba.protocol, "fontforge-collab" ); ba.version = 2; char* uuid = kvmsg_get_prop (kvmsg, "collab_uuid" ); if( uuid ) { strcpy( ba.uuid, uuid ); } else { ff_uuid_generate( ba.uuid ); } strncpy( ba.username, GetAuthor(), beacon_announce_username_sz ); ff_gethostname( ba.machinename, beacon_announce_machinename_sz ); ba.port = htons( self->port ); strcpy( ba.fontname, "" ); DEBUG("I: adding beacon, payloadsz:%zu user:%s machine:%s", sizeof(beacon_announce_t), ba.username, ba.machinename ); char* fontname = kvmsg_get_prop (kvmsg, "fontname" ); if( fontname ) { strcpy( ba.fontname, fontname ); } service_beacon = zbeacon_new( self->ctx, 5670 ); zbeacon_set_interval (service_beacon, 300 ); zbeacon_publish (service_beacon, (byte*)&ba, sizeof(ba)); } kvmsg_send (kvmsg, self->publisher); // int ttl = atoi (kvmsg_get_prop (kvmsg, "ttl")); // if (ttl) // kvmsg_set_prop (kvmsg, "ttl", // "%" PRId64, zclock_time () + ttl * 1000); DEBUG ("I: publishing update=%d type:%s", (int) self->sequence,kvmsg_get_prop (kvmsg, "type")); DEBUG("I:x hash size:%ld", zhash_size(self->kvmap)); kvmsg_store( &kvmsg, self->kvmap ); } return 0; }