static void server_connect (server_t *self, const char *endpoint) { zsock_t *remote = zsock_new (ZMQ_DEALER); assert (remote); // No recovery if exhausted // Never block on sending; we use an infinite HWM and buffer as many // messages as needed in outgoing pipes. Note that the maximum number // is the overall tuple set size. zsock_set_unbounded (remote); if (zsock_connect (remote, "%s", endpoint)) { zsys_warning ("bad zgossip endpoint '%s'", endpoint); zsock_destroy (&remote); return; } // Send HELLO and then PUBLISH for each tuple we have zgossip_msg_t *gossip = zgossip_msg_new (); zgossip_msg_set_id (gossip, ZGOSSIP_MSG_HELLO); zgossip_msg_send (gossip, remote); tuple_t *tuple = (tuple_t *) zhashx_first (self->tuples); while (tuple) { zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (gossip, tuple->key); zgossip_msg_set_value (gossip, tuple->value); zgossip_msg_send (gossip, remote); tuple = (tuple_t *) zhashx_next (self->tuples); } // Now monitor this remote for incoming messages zgossip_msg_destroy (&gossip); engine_handle_socket (self, remote, remote_handler); zlistx_add_end (self->remotes, remote); }
static void get_tuple_to_forward (client_t *self) { // Hold this in server->cur_tuple so it's available to all // clients; the whole broadcast operation happens in one thread // so there's no risk of confusion here. tuple_t *tuple = self->server->cur_tuple; zgossip_msg_set_key (self->message, tuple->key); zgossip_msg_set_value (self->message, tuple->value); }
zmsg_t * zgossip_msg_encode_publish ( const char *key, const char *value) { zgossip_msg_t *self = zgossip_msg_new (ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (self, key); zgossip_msg_set_value (self, value); return zgossip_msg_encode (&self); }
int zgossip_msg_send_publish ( void *output, const char *key, const char *value) { zgossip_msg_t *self = zgossip_msg_new (ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (self, key); zgossip_msg_set_value (self, value); return zgossip_msg_send (&self, output); }
static void get_next_tuple (client_t *self) { tuple_t *tuple = (tuple_t *) zhashx_next (self->server->tuples); if (tuple) { zgossip_msg_set_key (self->message, tuple->key); zgossip_msg_set_value (self->message, tuple->value); engine_set_next_event (self, ok_event); } else engine_set_next_event (self, finished_event); }
server_connect (server_t *self, const char *endpoint) #endif { zsock_t *remote = zsock_new (ZMQ_DEALER); assert (remote); // No recovery if exhausted #ifdef CZMQ_BUILD_DRAFT_API // DRAFT-API: Security if (public_key){ zcert_t *cert = zcert_new_from_txt (self->public_key, self->secret_key); zcert_apply(cert, remote); zsock_set_curve_serverkey (remote, public_key); #ifndef ZMQ_CURVE // legacy ZMQ support // inline incase the underlying assert is removed bool ZMQ_CURVE = false; #endif assert (zsock_mechanism (remote) == ZMQ_CURVE); zcert_destroy(&cert); } #endif // Never block on sending; we use an infinite HWM and buffer as many // messages as needed in outgoing pipes. Note that the maximum number // is the overall tuple set size. zsock_set_unbounded (remote); if (zsock_connect (remote, "%s", endpoint)) { zsys_warning ("bad zgossip endpoint '%s'", endpoint); zsock_destroy (&remote); return; } // Send HELLO and then PUBLISH for each tuple we have zgossip_msg_t *gossip = zgossip_msg_new (); zgossip_msg_set_id (gossip, ZGOSSIP_MSG_HELLO); zgossip_msg_send (gossip, remote); tuple_t *tuple = (tuple_t *) zhashx_first (self->tuples); while (tuple) { zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (gossip, tuple->key); zgossip_msg_set_value (gossip, tuple->value); zgossip_msg_send (gossip, remote); tuple = (tuple_t *) zhashx_next (self->tuples); } // Now monitor this remote for incoming messages zgossip_msg_destroy (&gossip); engine_handle_socket (self, remote, remote_handler); zlistx_add_end (self->remotes, remote); }
static void server_accept (server_t *self, const char *key, const char *value) { tuple_t *tuple = (tuple_t *) zhashx_lookup (self->tuples, key); if (tuple && streq (tuple->value, value)) return; // Duplicate tuple, do nothing // Create new tuple tuple = (tuple_t *) zmalloc (sizeof (tuple_t)); assert (tuple); tuple->container = self->tuples; tuple->key = strdup (key); tuple->value = strdup (value); // Store new tuple zhashx_update (tuple->container, key, tuple); zhashx_freefn (tuple->container, key, tuple_free); // Deliver to calling application zstr_sendx (self->pipe, "DELIVER", key, value, NULL); // Hold in server context so we can broadcast to all clients self->cur_tuple = tuple; engine_broadcast_event (self, NULL, forward_event); // Copy new tuple announcement to all remotes zgossip_msg_t *gossip = zgossip_msg_new (); zgossip_msg_set_id (gossip, ZGOSSIP_MSG_PUBLISH); zsock_t *remote = (zsock_t *) zlistx_first (self->remotes); while (remote) { zgossip_msg_set_key (gossip, tuple->key); zgossip_msg_set_value (gossip, tuple->value); zgossip_msg_send (gossip, remote); remote = (zsock_t *) zlistx_next (self->remotes); } zgossip_msg_destroy (&gossip); }
int zgossip_msg_test (bool verbose) { printf (" * zgossip_msg: "); // @selftest // Simple create/destroy test zgossip_msg_t *self = zgossip_msg_new (0); assert (self); zgossip_msg_destroy (&self); // Create pair of sockets we can send through zsock_t *input = zsock_new (ZMQ_ROUTER); assert (input); zsock_connect (input, "inproc://selftest-zgossip_msg"); zsock_t *output = zsock_new (ZMQ_DEALER); assert (output); zsock_bind (output, "inproc://selftest-zgossip_msg"); // Encode/send/decode and verify each message type int instance; zgossip_msg_t *copy; self = zgossip_msg_new (ZGOSSIP_MSG_HELLO); // Check that _dup works on empty message copy = zgossip_msg_dup (self); assert (copy); zgossip_msg_destroy (©); // Send twice from same object zgossip_msg_send_again (self, output); zgossip_msg_send (&self, output); for (instance = 0; instance < 2; instance++) { self = zgossip_msg_recv (input); assert (self); assert (zgossip_msg_routing_id (self)); zgossip_msg_destroy (&self); } self = zgossip_msg_new (ZGOSSIP_MSG_PUBLISH); // Check that _dup works on empty message copy = zgossip_msg_dup (self); assert (copy); zgossip_msg_destroy (©); zgossip_msg_set_key (self, "Life is short but Now lasts for ever"); zgossip_msg_set_value (self, "Life is short but Now lasts for ever"); // Send twice from same object zgossip_msg_send_again (self, output); zgossip_msg_send (&self, output); for (instance = 0; instance < 2; instance++) { self = zgossip_msg_recv (input); assert (self); assert (zgossip_msg_routing_id (self)); assert (streq (zgossip_msg_key (self), "Life is short but Now lasts for ever")); assert (streq (zgossip_msg_value (self), "Life is short but Now lasts for ever")); zgossip_msg_destroy (&self); } self = zgossip_msg_new (ZGOSSIP_MSG_PING); // Check that _dup works on empty message copy = zgossip_msg_dup (self); assert (copy); zgossip_msg_destroy (©); // Send twice from same object zgossip_msg_send_again (self, output); zgossip_msg_send (&self, output); for (instance = 0; instance < 2; instance++) { self = zgossip_msg_recv (input); assert (self); assert (zgossip_msg_routing_id (self)); zgossip_msg_destroy (&self); } self = zgossip_msg_new (ZGOSSIP_MSG_PONG); // Check that _dup works on empty message copy = zgossip_msg_dup (self); assert (copy); zgossip_msg_destroy (©); // Send twice from same object zgossip_msg_send_again (self, output); zgossip_msg_send (&self, output); for (instance = 0; instance < 2; instance++) { self = zgossip_msg_recv (input); assert (self); assert (zgossip_msg_routing_id (self)); zgossip_msg_destroy (&self); } self = zgossip_msg_new (ZGOSSIP_MSG_INVALID); // Check that _dup works on empty message copy = zgossip_msg_dup (self); assert (copy); zgossip_msg_destroy (©); // Send twice from same object zgossip_msg_send_again (self, output); zgossip_msg_send (&self, output); for (instance = 0; instance < 2; instance++) { self = zgossip_msg_recv (input); assert (self); assert (zgossip_msg_routing_id (self)); zgossip_msg_destroy (&self); } zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); return 0; }
void zgossip_msg_test (bool verbose) { printf (" * zgossip_msg:"); if (verbose) printf ("\n"); // @selftest // Simple create/destroy test zgossip_msg_t *self = zgossip_msg_new (); assert (self); zgossip_msg_destroy (&self); // Create pair of sockets we can send through // We must bind before connect if we wish to remain compatible with ZeroMQ < v4 zsock_t *output = zsock_new (ZMQ_DEALER); assert (output); int rc = zsock_bind (output, "inproc://selftest-zgossip_msg"); assert (rc == 0); zsock_t *input = zsock_new (ZMQ_ROUTER); assert (input); rc = zsock_connect (input, "inproc://selftest-zgossip_msg"); assert (rc == 0); // Encode/send/decode and verify each message type int instance; self = zgossip_msg_new (); zgossip_msg_set_id (self, ZGOSSIP_MSG_HELLO); // Send twice zgossip_msg_send (self, output); zgossip_msg_send (self, output); for (instance = 0; instance < 2; instance++) { zgossip_msg_recv (self, input); assert (zgossip_msg_routing_id (self)); } zgossip_msg_set_id (self, ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (self, "Life is short but Now lasts for ever"); zgossip_msg_set_value (self, "Life is short but Now lasts for ever"); zgossip_msg_set_ttl (self, 123); // Send twice zgossip_msg_send (self, output); zgossip_msg_send (self, output); for (instance = 0; instance < 2; instance++) { zgossip_msg_recv (self, input); assert (zgossip_msg_routing_id (self)); assert (streq (zgossip_msg_key (self), "Life is short but Now lasts for ever")); assert (streq (zgossip_msg_value (self), "Life is short but Now lasts for ever")); assert (zgossip_msg_ttl (self) == 123); } zgossip_msg_set_id (self, ZGOSSIP_MSG_PING); // Send twice zgossip_msg_send (self, output); zgossip_msg_send (self, output); for (instance = 0; instance < 2; instance++) { zgossip_msg_recv (self, input); assert (zgossip_msg_routing_id (self)); } zgossip_msg_set_id (self, ZGOSSIP_MSG_PONG); // Send twice zgossip_msg_send (self, output); zgossip_msg_send (self, output); for (instance = 0; instance < 2; instance++) { zgossip_msg_recv (self, input); assert (zgossip_msg_routing_id (self)); } zgossip_msg_set_id (self, ZGOSSIP_MSG_INVALID); // Send twice zgossip_msg_send (self, output); zgossip_msg_send (self, output); for (instance = 0; instance < 2; instance++) { zgossip_msg_recv (self, input); assert (zgossip_msg_routing_id (self)); } zgossip_msg_destroy (&self); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); }