zmsg_t * zgossip_msg_encode_publish ( const char *key, const char *value, uint32_t ttl) { zgossip_msg_t *self = zgossip_msg_new (ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (self, key); zgossip_msg_set_value (self, value); zgossip_msg_set_ttl (self, ttl); return zgossip_msg_encode (&self); }
int zgossip_msg_send_publish ( void *output, const char *key, const char *value, uint32_t ttl) { zgossip_msg_t *self = zgossip_msg_new (ZGOSSIP_MSG_PUBLISH); zgossip_msg_set_key (self, key); zgossip_msg_set_value (self, value); zgossip_msg_set_ttl (self, ttl); return zgossip_msg_send (&self, output); }
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"); zgossip_msg_set_ttl (self, 123); // 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")); assert (zgossip_msg_ttl (self) == 123); 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"); }