static void zeromq_beacon_fd_callback(int zeromq_fd, void* datas ) { // cloneclient_t* cc = (cloneclient_t*)datas; // printf("zeromq_beacon_fd_callback(top)\n"); int opt = 0; size_t optsz = sizeof(int); zmq_getsockopt( zbeacon_socket (client_beacon), ZMQ_EVENTS, &opt, &optsz ); // printf("zeromq_beacon_fd_callback(2) opt:%d\n", opt ); if( opt & ZMQ_POLLIN ) { // printf("zeromq_beacon_fd_callback() have message!\n"); while( 1 ) { char *ipaddress = zstr_recv_nowait (zbeacon_socket (client_beacon)); // printf("zeromq_beacon_fd_callback() have data? p:%p\n", ipaddress ); if( ipaddress ) { // printf("zeromq_beacon_fd_callback() have message! ip:%s\n", ipaddress ); zframe_t *content = zframe_recv_nowait (zbeacon_socket (client_beacon)); if( content ) { beacon_announce_t* ba = (beacon_announce_t*)zframe_data(content); // printf("uuid:%s\n", ba->uuid ); // printf("user:%s\n", ba->username ); // printf("mach:%s\n", ba->machinename ); // if( ba->version >= 2 && ff_uuid_isValid(ba->uuid) ) { // printf("have a beacon back for xuid:%s\n", ba->uuid ); // } beacon_announce_t* copy = g_malloc( sizeof(beacon_announce_t)); memcpy( copy, ba, sizeof(beacon_announce_t)); copy->last_msg_from_peer_time = time(0); copy->port = ntohs( copy->port ); strncpy( copy->ip, ipaddress, beacon_announce_ip_sz ); g_hash_table_replace( peers, copy->uuid, copy ); zframe_destroy (&content); } free (ipaddress); } else break; } } zeromq_beacon_show_peers(0); }
void zyre_node_engine (void *args, zctx_t *ctx, void *pipe) { // Create node instance to pass around zyre_node_t *self = zyre_node_new (ctx, pipe); if (!self) // Interrupted return; zsocket_signal (self->pipe); uint64_t reap_at = zclock_time () + REAP_INTERVAL; zpoller_t *poller = zpoller_new ( self->pipe, self->inbox, zbeacon_socket (self->beacon), NULL); while (!zpoller_terminated (poller)) { if (self->terminated) break; int timeout = (int) (reap_at - zclock_time ()); assert (timeout <= REAP_INTERVAL); if (timeout < 0) timeout = 0; void *which = zpoller_wait (poller, timeout); if (which == self->pipe) zyre_node_recv_api (self); else if (which == self->inbox) zyre_node_recv_peer (self); else if (which == zbeacon_socket (self->beacon)) zyre_node_recv_beacon (self); else if (zpoller_expired(poller)) { if (zclock_time () >= reap_at) { reap_at = zclock_time () + REAP_INTERVAL; // Ping all peers and reap any expired ones zhash_foreach (self->peers, zyre_node_ping_peer, self); } } else if (zpoller_terminated(poller)) break; else // This should never happen assert(false); } zpoller_destroy (&poller); zyre_node_destroy (&self); }
static int zyre_node_recv_beacon (zyre_node_t *self) { // Get IP address and beacon of peer char *ipaddress = zstr_recv (zbeacon_socket (self->beacon)); zframe_t *frame = zframe_recv (zbeacon_socket (self->beacon)); // Ignore anything that isn't a valid beacon bool is_valid = true; beacon_t beacon; if (zframe_size (frame) == sizeof (beacon_t)) { memcpy (&beacon, zframe_data (frame), zframe_size (frame)); if (beacon.version != BEACON_VERSION) is_valid = false; } else is_valid = false; // Check that the peer, identified by its UUID, exists if (is_valid) { zuuid_t *uuid = zuuid_new (); zuuid_set (uuid, beacon.uuid); if (beacon.port) { char endpoint [30]; sprintf (endpoint, "tcp://%s:%d", ipaddress, ntohs (beacon.port)); zyre_peer_t *peer = zyre_node_require_peer (self, uuid, endpoint); zyre_peer_refresh (peer); } else { // Zero port means peer is going away; remove it if // we had any knowledge of it already zyre_peer_t *peer = (zyre_peer_t *) zhash_lookup ( self->peers, zuuid_str (uuid)); if (peer) zyre_node_remove_peer (self, peer); } zuuid_destroy (&uuid); } zstr_free (&ipaddress); zframe_destroy (&frame); return 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 ); }
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; }
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 zyre_node_actor (zsock_t *pipe, void *args) { // Create node instance to pass around zyre_node_t *self = zyre_node_new (pipe, args); if (!self) // Interrupted return; // Signal actor successfully initialized zsock_signal (self->pipe, 0); // Loop until the agent is terminated one way or another uint64_t reap_at = zclock_time () + REAP_INTERVAL; while (!self->terminated) { int timeout = (int) (reap_at - zclock_time ()); assert (timeout <= REAP_INTERVAL); if (timeout < 0) timeout = 0; void *which = zpoller_wait (self->poller, timeout); if (which == self->pipe) zyre_node_recv_api (self); else if (which == self->inbox) zyre_node_recv_peer (self); else if (self->beacon && which == zbeacon_socket (self->beacon)) zyre_node_recv_beacon (self); else if (zpoller_expired (self->poller)) { if (zclock_time () >= reap_at) { reap_at = zclock_time () + REAP_INTERVAL; // Ping all peers and reap any expired ones zhash_foreach (self->peers, zyre_node_ping_peer, self); } } else if (zpoller_terminated (self->poller)) break; // Interrupted } zyre_node_destroy (&self); }