zuuid_t * zuuid_dup (zuuid_t *self) { if (self) return zuuid_new_from (zuuid_data (self)); else return NULL; }
static zsync_node_t * zsync_node_new () { int rc; zsync_node_t *self = (zsync_node_t *) zmalloc (sizeof (zsync_node_t)); self->ctx = zctx_new (); assert (self->ctx); self->zyre = zyre_new (self->ctx); assert (self->zyre); // Obtain permanent UUID self->own_uuid = zuuid_new (); if (zsys_file_exists (UUID_FILE)) { // Read uuid from file zfile_t *uuid_file = zfile_new (".", UUID_FILE); int rc = zfile_input (uuid_file); // open file for reading assert (rc == 0); zchunk_t *uuid_chunk = zfile_read (uuid_file, 16, 0); assert (zchunk_size (uuid_chunk) == 16); // make sure read succeeded zuuid_set (self->own_uuid, zchunk_data (uuid_chunk)); zfile_destroy (&uuid_file); } else { // Write uuid to file zfile_t *uuid_file = zfile_new (".", UUID_FILE); rc = zfile_output (uuid_file); // open file for writing assert (rc == 0); zchunk_t *uuid_bin = zchunk_new ( zuuid_data (self->own_uuid), 16); rc = zfile_write (uuid_file, uuid_bin, 0); assert (rc == 0); zfile_destroy (&uuid_file); } // Obtain peers and states self->peers = zlist_new (); if (zsys_file_exists (PEER_STATES_FILE)) { zhash_t *peer_states = zhash_new (); int rc = zhash_load (peer_states, PEER_STATES_FILE); assert (rc == 0); zlist_t *uuids = zhash_keys (peer_states); char *uuid = zlist_first (uuids); while (uuid) { char * state_str = zhash_lookup (peer_states, uuid); uint64_t state; sscanf (state_str, "%"SCNd64, &state); zlist_append (self->peers, zsync_peer_new (uuid, state)); uuid = zlist_next (uuids); } } self->zyre_peers = zhash_new (); self->terminated = false; return self; }
zuuid_t * zuuid_dup (zuuid_t *self) { if (self) { zuuid_t *copy = zuuid_new (); if (copy) zuuid_set (copy, zuuid_data (self)); return copy; } else return NULL; }
void zyre_peer_connect (zyre_peer_t *self, zuuid_t *from, const char *endpoint) { assert (self); assert (!self->connected); // Create new outgoing socket (drop any messages in transit) self->mailbox = zsock_new (ZMQ_DEALER); if (!self->mailbox) return; // Null when we're shutting down // Set our own identity on the socket so that receiving node // knows who each message came from. Note that we cannot use // the UUID directly as the identity since it may contain a // zero byte at the start, which libzmq does not like for // historical and arguably bogus reasons that it nonetheless // enforces. byte routing_id [ZUUID_LEN + 1] = { 1 }; memcpy (routing_id + 1, zuuid_data (from), ZUUID_LEN); int rc = zmq_setsockopt (zsock_resolve (self->mailbox), ZMQ_IDENTITY, routing_id, ZUUID_LEN + 1); assert (rc == 0); // Set a high-water mark that allows for reasonable activity zsock_set_sndhwm (self->mailbox, PEER_EXPIRED * 100); // Send messages immediately or return EAGAIN zsock_set_sndtimeo (self->mailbox, 0); // Connect through to peer node rc = zsock_connect (self->mailbox, "%s", endpoint); if (rc != 0) { zsys_error ("(%s) cannot connect to endpoint=%s", self->origin, endpoint); // Don't really have any error handling yet; if connect // fails, there's something wrong with connect endpoint? assert (false); } assert (rc == 0); if (self->verbose) zsys_info ("(%s) connect to peer: endpoint=%s", self->origin, endpoint); self->endpoint = strdup (endpoint); self->connected = true; self->ready = false; }
int zyre_peer_connect (zyre_peer_t *self, zuuid_t *from, const char *endpoint, uint64_t expired_timeout) { assert (self); assert (!self->connected); // Create new outgoing socket (drop any messages in transit) self->mailbox = zsock_new (ZMQ_DEALER); if (!self->mailbox) return -1; // Null when we're shutting down // Set our own identity on the socket so that receiving node // knows who each message came from. Note that we cannot use // the UUID directly as the identity since it may contain a // zero byte at the start, which libzmq does not like for // historical and arguably bogus reasons that it nonetheless // enforces. byte routing_id [ZUUID_LEN + 1] = { 1 }; memcpy (routing_id + 1, zuuid_data (from), ZUUID_LEN); int rc = zmq_setsockopt (zsock_resolve (self->mailbox), ZMQ_IDENTITY, routing_id, ZUUID_LEN + 1); assert (rc == 0); // Set a high-water mark that allows for reasonable activity zsock_set_sndhwm (self->mailbox, expired_timeout * 100); // Send messages immediately or return EAGAIN zsock_set_sndtimeo (self->mailbox, 0); // If the peer is a link-local IPv6 address but the interface is not set, // use ZSYS_INTERFACE_ADDRESS if provided zrex_t *rex = zrex_new (NULL); char endpoint_iface [NI_MAXHOST] = {0}; if (zsys_ipv6 () && zsys_interface () && strlen(zsys_interface ()) && !streq (zsys_interface (), "*") && zrex_eq (rex, endpoint, "^tcp://(fe80[^%]+)(:\\d+)$")) { const char *hostname, *port; zrex_fetch (rex, &hostname, &port, NULL); strcat (endpoint_iface, "tcp://"); strcat (endpoint_iface, hostname); strcat (endpoint_iface, "%"); strcat (endpoint_iface, zsys_interface ()); strcat (endpoint_iface, port); } else strcat (endpoint_iface, endpoint); zrex_destroy (&rex); // Connect through to peer node rc = zsock_connect (self->mailbox, "%s", endpoint_iface); if (rc != 0) { zsys_debug ("(%s) cannot connect to endpoint=%s", self->origin, endpoint_iface); zsock_destroy (&self->mailbox); return -1; } if (self->verbose) zsys_info ("(%s) connect to peer: endpoint=%s", self->origin, endpoint_iface); self->endpoint = strdup (endpoint_iface); self->connected = true; self->ready = false; return 0; }
/// // Return UUID binary data. const byte * QZuuid::data () { const byte * rv = zuuid_data (self); return rv; }
/// // Return UUID binary data. const byte *QmlZuuid::data () { return zuuid_data (self); };
void zproto_example_test (bool verbose) { printf (" * zproto_example:"); if (verbose) printf ("\n"); // @selftest // Simple create/destroy test zproto_example_t *self = zproto_example_new (); assert (self); zproto_example_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-zproto_example"); assert (rc == 0); zsock_t *input = zsock_new (ZMQ_ROUTER); assert (input); rc = zsock_connect (input, "inproc://selftest-zproto_example"); assert (rc == 0); // Encode/send/decode and verify each message type int instance; self = zproto_example_new (); zproto_example_set_id (self, ZPROTO_EXAMPLE_LOG); zproto_example_set_sequence (self, 123); zproto_example_set_level (self, 2); zproto_example_set_event (self, 3); zproto_example_set_node (self, 45536); zproto_example_set_peer (self, 65535); zproto_example_set_time (self, 1427261426); zproto_example_set_host (self, "localhost"); zproto_example_set_data (self, "This is the message to log"); // Send twice zproto_example_send (self, output); zproto_example_send (self, output); for (instance = 0; instance < 2; instance++) { zproto_example_recv (self, input); assert (zproto_example_routing_id (self)); assert (zproto_example_sequence (self) == 123); assert (zproto_example_level (self) == 2); assert (zproto_example_event (self) == 3); assert (zproto_example_node (self) == 45536); assert (zproto_example_peer (self) == 65535); assert (zproto_example_time (self) == 1427261426); assert (streq (zproto_example_host (self), "localhost")); assert (streq (zproto_example_data (self), "This is the message to log")); } zproto_example_set_id (self, ZPROTO_EXAMPLE_STRUCTURES); zproto_example_set_sequence (self, 123); zlist_t *structures_aliases = zlist_new (); zlist_append (structures_aliases, "First alias"); zlist_append (structures_aliases, "Second alias"); zlist_append (structures_aliases, "Third alias"); zproto_example_set_aliases (self, &structures_aliases); zhash_t *structures_headers = zhash_new (); zhash_insert (structures_headers, "endpoint", "tcp://*****:*****@example.com"); zproto_example_set_supplier_forename (self, "Leslie"); zproto_example_set_supplier_surname (self, "Lamport"); zproto_example_set_supplier_mobile (self, "01987654321"); zproto_example_set_supplier_email (self, "*****@*****.**"); // Send twice zproto_example_send (self, output); zproto_example_send (self, output); for (instance = 0; instance < 2; instance++) { zproto_example_recv (self, input); assert (zproto_example_routing_id (self)); assert (zproto_example_sequence (self) == 123); assert (streq (zproto_example_client_forename (self), "Lucius Junius")); assert (streq (zproto_example_client_surname (self), "Brutus")); assert (streq (zproto_example_client_mobile (self), "01234567890")); assert (streq (zproto_example_client_email (self), "*****@*****.**")); assert (streq (zproto_example_supplier_forename (self), "Leslie")); assert (streq (zproto_example_supplier_surname (self), "Lamport")); assert (streq (zproto_example_supplier_mobile (self), "01987654321")); assert (streq (zproto_example_supplier_email (self), "*****@*****.**")); } zproto_example_destroy (&self); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); }
static void zsync_node_recv_from_zyre (zsync_node_t *self) { zsync_peer_t *sender; char *zyre_sender; zuuid_t *sender_uuid; zmsg_t *zyre_in, *zyre_out, *fm_msg; zlist_t *fpaths, *fmetadata; zyre_event_t *event = zyre_event_recv (self->zyre); zyre_sender = zyre_event_sender (event); // get tmp uuid switch (zyre_event_type (event)) { case ZYRE_EVENT_ENTER: printf("[ND] ZS_ENTER: %s\n", zyre_sender); zhash_insert (self->zyre_peers, zyre_sender, NULL); break; case ZYRE_EVENT_JOIN: printf ("[ND] ZS_JOIN: %s\n", zyre_sender); // Obtain own current state zsync_msg_send_req_state (self->zsync_pipe); zsync_msg_t *msg_state = zsync_msg_recv (self->zsync_pipe); assert (zsync_msg_id (msg_state) == ZSYNC_MSG_RES_STATE); uint64_t state = zsync_msg_state (msg_state); // Send GREET message zyre_out = zmsg_new (); zs_msg_pack_greet (zyre_out, zuuid_data (self->own_uuid), state); zyre_whisper (self->zyre, zyre_sender, &zyre_out); break; case ZYRE_EVENT_LEAVE: break; case ZYRE_EVENT_EXIT: /* printf("[ND] ZS_EXIT %s left the house!\n", zyre_sender); sender = zhash_lookup (self->zyre_peers, zyre_sender); if (sender) { // Reset Managers zmsg_t *reset_msg = zmsg_new (); zmsg_addstr (reset_msg, zsync_peer_uuid (sender)); zmsg_addstr (reset_msg, "ABORT"); zmsg_send (&reset_msg, self->file_pipe); reset_msg = zmsg_new (); zmsg_addstr (reset_msg, zsync_peer_uuid (sender)); zmsg_addstr (reset_msg, "ABORT"); zmsg_send (&reset_msg, self->credit_pipe); // Remove Peer from active list zhash_delete (self->zyre_peers, zyre_sender); }*/ break; case ZYRE_EVENT_WHISPER: case ZYRE_EVENT_SHOUT: printf ("[ND] ZS_WHISPER: %s\n", zyre_sender); sender = zhash_lookup (self->zyre_peers, zyre_sender); zyre_in = zyre_event_msg (event); zs_msg_t *msg = zs_msg_unpack (zyre_in); switch (zs_msg_get_cmd (msg)) { case ZS_CMD_GREET: // Get perm uuid sender_uuid = zuuid_new (); zuuid_set (sender_uuid, zs_msg_uuid (msg)); sender = zsync_node_peers_lookup (self, zuuid_str (sender_uuid)); if (!sender) { sender = zsync_peer_new (zuuid_str (sender_uuid), 0x0); zlist_append (self->peers, sender); } assert (sender); zhash_update (self->zyre_peers, zyre_sender, sender); zsync_peer_set_zyre_state (sender, ZYRE_EVENT_JOIN); // Get current state for sender uint64_t remote_current_state = zs_msg_get_state (msg); printf ("[ND] current state: %"PRId64"\n", remote_current_state); // Lookup last known state uint64_t last_state_local = zsync_peer_state (sender); printf ("[ND] last known state: %"PRId64"\n", zsync_peer_state (sender)); // Send LAST_STATE if differs if (remote_current_state >= last_state_local) { zmsg_t *lmsg = zmsg_new (); zs_msg_pack_last_state (lmsg, last_state_local); zyre_whisper (self->zyre, zyre_sender, &lmsg); } break; case ZS_CMD_LAST_STATE: assert (sender); zyre_out = zmsg_new (); // Gets updates from client uint64_t last_state_remote = zs_msg_get_state (msg); zsync_msg_send_req_update (self->zsync_pipe, last_state_remote); zsync_msg_t *msg_upd = zsync_msg_recv (self->zsync_pipe); assert (zsync_msg_id (msg_upd) == ZSYNC_MSG_UPDATE); // Send UPDATE zyre_out = zsync_msg_update_msg (msg_upd); zyre_whisper (self->zyre, zyre_sender, &zyre_out); break; case ZS_CMD_UPDATE: printf ("[ND] UPDATE\n"); assert (sender); uint64_t state = zs_msg_get_state (msg); zsync_peer_set_state (sender, state); zsync_node_save_peers (self); fmetadata = zs_msg_get_fmetadata (msg); zmsg_t *zsync_msg = zmsg_new (); zs_msg_pack_update (zsync_msg, zs_msg_get_state (msg), fmetadata); zsync_msg_send_update (self->zsync_pipe, zsync_peer_uuid (sender), zsync_msg); break; case ZS_CMD_REQUEST_FILES: printf ("[ND] REQUEST FILES\n"); fpaths = zs_msg_fpaths (msg); zmsg_t *fm_msg = zmsg_new (); zmsg_addstr (fm_msg, zsync_peer_uuid (sender)); zmsg_addstr (fm_msg, "REQUEST"); char *fpath = zs_msg_fpaths_first (msg); while (fpath) { zmsg_addstr (fm_msg, fpath); printf("[ND] %s\n", fpath); fpath = zs_msg_fpaths_next (msg); } zmsg_send (&fm_msg, self->file_pipe); break; case ZS_CMD_GIVE_CREDIT: printf("[ND] GIVE CREDIT\n"); fm_msg = zmsg_new (); zmsg_addstr (fm_msg, zsync_peer_uuid (sender)); zmsg_addstr (fm_msg, "CREDIT"); zmsg_addstrf (fm_msg, "%"PRId64, zs_msg_get_credit (msg)); zmsg_send (&fm_msg, self->file_pipe); break; case ZS_CMD_SEND_CHUNK: printf("[ND] SEND_CHUNK (RCV)\n"); // Send receival to credit manager zframe_t *zframe = zs_msg_get_chunk (msg); uint64_t chunk_size = zframe_size (zframe); zsync_credit_msg_send_update (self->credit_pipe, zsync_peer_uuid (sender), chunk_size); // Pass chunk to client byte *data = zframe_data (zframe); zchunk_t *chunk = zchunk_new (data, chunk_size); char *path = zs_msg_get_file_path (msg); uint64_t seq = zs_msg_get_sequence (msg); uint64_t off = zs_msg_get_offset (msg); zsync_msg_send_chunk (self->zsync_pipe, chunk, path, seq, off); break; case ZS_CMD_ABORT: // TODO abort protocol managed file transfer printf("[ND] ABORT\n"); break; default: assert (false); break; } zs_msg_destroy (&msg); break; default: printf("[ND] Error command not found\n"); break; } zyre_event_destroy (&event); }
void xrap_traffic_test (bool verbose) { printf (" * xrap_traffic:"); if (verbose) printf ("\n"); // @selftest // Simple create/destroy test xrap_traffic_t *self = xrap_traffic_new (); assert (self); xrap_traffic_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-xrap_traffic"); assert (rc == 0); zsock_t *input = zsock_new (ZMQ_ROUTER); assert (input); rc = zsock_connect (input, "inproc://selftest-xrap_traffic"); assert (rc == 0); // Encode/send/decode and verify each message type int instance; self = xrap_traffic_new (); xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_OPEN); xrap_traffic_set_address (self, "Life is short but Now lasts for ever"); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); assert (streq (xrap_traffic_address (self), "Life is short but Now lasts for ever")); } xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_PING); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); } xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_PONG); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); } xrap_traffic_set_id (self, XRAP_TRAFFIC_CONNECTION_CLOSE); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); } xrap_traffic_set_id (self, XRAP_TRAFFIC_XRAP_SEND); xrap_traffic_set_timeout (self, 123); zmsg_t *xrap_send_content = zmsg_new (); xrap_traffic_set_content (self, &xrap_send_content); zmsg_addstr (xrap_traffic_content (self), "Captcha Diem"); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); assert (xrap_traffic_timeout (self) == 123); assert (zmsg_size (xrap_traffic_content (self)) == 1); char *content = zmsg_popstr (xrap_traffic_content (self)); assert (streq (content, "Captcha Diem")); zstr_free (&content); if (instance == 1) zmsg_destroy (&xrap_send_content); } xrap_traffic_set_id (self, XRAP_TRAFFIC_XRAP_OFFER); xrap_traffic_set_route (self, "Life is short but Now lasts for ever"); xrap_traffic_set_method (self, "Life is short but Now lasts for ever"); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); assert (streq (xrap_traffic_route (self), "Life is short but Now lasts for ever")); assert (streq (xrap_traffic_method (self), "Life is short but Now lasts for ever")); } xrap_traffic_set_id (self, XRAP_TRAFFIC_XRAP_DELIVER); zuuid_t *xrap_deliver_sender = zuuid_new (); xrap_traffic_set_sender (self, xrap_deliver_sender); zmsg_t *xrap_deliver_content = zmsg_new (); xrap_traffic_set_content (self, &xrap_deliver_content); zmsg_addstr (xrap_traffic_content (self), "Captcha Diem"); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); assert (zuuid_eq (xrap_deliver_sender, zuuid_data (xrap_traffic_sender (self)))); if (instance == 1) zuuid_destroy (&xrap_deliver_sender); assert (zmsg_size (xrap_traffic_content (self)) == 1); char *content = zmsg_popstr (xrap_traffic_content (self)); assert (streq (content, "Captcha Diem")); zstr_free (&content); if (instance == 1) zmsg_destroy (&xrap_deliver_content); } xrap_traffic_set_id (self, XRAP_TRAFFIC_OK); xrap_traffic_set_status_code (self, 123); xrap_traffic_set_status_reason (self, "Life is short but Now lasts for ever"); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); assert (xrap_traffic_status_code (self) == 123); assert (streq (xrap_traffic_status_reason (self), "Life is short but Now lasts for ever")); } xrap_traffic_set_id (self, XRAP_TRAFFIC_FAIL); xrap_traffic_set_status_code (self, 123); xrap_traffic_set_status_reason (self, "Life is short but Now lasts for ever"); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); assert (xrap_traffic_status_code (self) == 123); assert (streq (xrap_traffic_status_reason (self), "Life is short but Now lasts for ever")); } xrap_traffic_set_id (self, XRAP_TRAFFIC_ERROR); xrap_traffic_set_status_code (self, 123); xrap_traffic_set_status_reason (self, "Life is short but Now lasts for ever"); // Send twice xrap_traffic_send (self, output); xrap_traffic_send (self, output); for (instance = 0; instance < 2; instance++) { xrap_traffic_recv (self, input); assert (xrap_traffic_routing_id (self)); assert (xrap_traffic_status_code (self) == 123); assert (streq (xrap_traffic_status_reason (self), "Life is short but Now lasts for ever")); } xrap_traffic_destroy (&self); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); }