void zyre_peer_test (bool verbose) { printf (" * zyre_peer: "); zsock_t *mailbox = zsock_new_dealer ("@tcp://127.0.0.1:5551"); zhash_t *peers = zhash_new (); zuuid_t *you = zuuid_new (); zuuid_t *me = zuuid_new (); zyre_peer_t *peer = zyre_peer_new (peers, you); assert (!zyre_peer_connected (peer)); zyre_peer_connect (peer, me, "tcp://127.0.0.1:5551"); assert (zyre_peer_connected (peer)); zyre_peer_set_name (peer, "peer"); assert (streq (zyre_peer_name (peer), "peer")); zre_msg_t *msg = zre_msg_new (ZRE_MSG_HELLO); zre_msg_set_endpoint (msg, "tcp://127.0.0.1:5552"); int rc = zyre_peer_send (peer, &msg); assert (rc == 0); msg = zre_msg_recv (mailbox); assert (msg); if (verbose) zre_msg_print (msg); zre_msg_destroy (&msg); // Destroying container destroys all peers it contains zhash_destroy (&peers); zuuid_destroy (&me); zuuid_destroy (&you); zsock_destroy (&mailbox); printf ("OK\n"); }
void zuuid_test (bool verbose) { printf (" * zuuid: "); // @selftest // Simple create/destroy test zuuid_t *uuid = zuuid_new (); assert (uuid); assert (zuuid_size (uuid) == 16); assert (strlen (zuuid_str (uuid)) == 32); zuuid_t *copy = zuuid_dup (uuid); assert (streq (zuuid_str (uuid), zuuid_str (copy))); // Check set/set_str/export methods const char *myuuid = "8CB3E9A9649B4BEF8DE225E9C2CEBB38"; zuuid_set_str (uuid, myuuid); assert (streq (zuuid_str (uuid), myuuid)); byte copy_uuid [16]; zuuid_export (uuid, copy_uuid); zuuid_set (uuid, copy_uuid); assert (streq (zuuid_str (uuid), myuuid)); zuuid_destroy (&uuid); zuuid_destroy (©); // @end printf ("OK\n"); }
void zuuid_test (bool verbose) { printf (" * zuuid: "); // @selftest // Simple create/destroy test assert (ZUUID_LEN == 16); assert (ZUUID_STR_LEN == 32); zuuid_t *uuid = zuuid_new (); assert (uuid); assert (zuuid_size (uuid) == ZUUID_LEN); assert (strlen (zuuid_str (uuid)) == ZUUID_STR_LEN); zuuid_t *copy = zuuid_dup (uuid); assert (streq (zuuid_str (uuid), zuuid_str (copy))); // Check set/set_str/export methods const char *myuuid = "8CB3E9A9649B4BEF8DE225E9C2CEBB38"; const char *myuuid2 = "8CB3E9A9-649B-4BEF-8DE2-25E9C2CEBB38"; const char *myuuid3 = "{8CB3E9A9-649B-4BEF-8DE2-25E9C2CEBB38}"; const char *myuuid4 = "8CB3E9A9649B4BEF8DE225E9C2CEBB3838"; int rc = zuuid_set_str (uuid, myuuid); assert (rc == 0); assert (streq (zuuid_str (uuid), myuuid)); rc = zuuid_set_str (uuid, myuuid2); assert (rc == 0); assert (streq (zuuid_str (uuid), myuuid)); rc = zuuid_set_str (uuid, myuuid3); assert (rc == 0); assert (streq (zuuid_str (uuid), myuuid)); rc = zuuid_set_str (uuid, myuuid4); assert (rc == -1); byte copy_uuid [ZUUID_LEN]; zuuid_export (uuid, copy_uuid); zuuid_set (uuid, copy_uuid); assert (streq (zuuid_str (uuid), myuuid)); // Check the canonical string format assert (streq (zuuid_str_canonical (uuid), "8cb3e9a9-649b-4bef-8de2-25e9c2cebb38")); zuuid_destroy (&uuid); zuuid_destroy (©); // @end printf ("OK\n"); }
void zproto_example_set_identifier (zproto_example_t *self, zuuid_t *uuid) { assert (self); zuuid_destroy (&self->identifier); self->identifier = zuuid_dup (uuid); }
void xrap_traffic_set_sender (xrap_traffic_t *self, zuuid_t *uuid) { assert (self); zuuid_destroy (&self->sender); self->sender = zuuid_dup (uuid); }
zuuid_t * zuuid_new (void) { zuuid_t *self = (zuuid_t *) zmalloc (sizeof (zuuid_t)); assert (self); #if defined (__WINDOWS__) // Windows always has UUID support UUID uuid; assert (sizeof (uuid) == ZUUID_LEN); UuidCreate (&uuid); zuuid_set (self, (byte *) &uuid); #elif defined (__UTYPE_ANDROID) || !defined (HAVE_UUID) // No UUID system calls, so generate a random string byte uuid [ZUUID_LEN]; int fd = open ("/dev/urandom", O_RDONLY); if (fd != -1) { ssize_t bytes_read = read (fd, uuid, ZUUID_LEN); assert (bytes_read == ZUUID_LEN); close (fd); zuuid_set (self, uuid); } else { // We couldn't read /dev/urandom and we have no alternative // strategy zsys_error (strerror (errno)); assert (false); } #elif defined (__UTYPE_OPENBSD) || defined (__UTYPE_FREEBSD) || defined (__UTYPE_NETBSD) uuid_t uuid; uint32_t status = 0; uuid_create (&uuid, &status); if (status != uuid_s_ok) { zuuid_destroy (&self); return NULL; } byte buffer [ZUUID_LEN]; uuid_enc_be (&buffer, &uuid); zuuid_set (self, buffer); #elif defined (__UTYPE_LINUX) || defined (__UTYPE_OSX) || defined (__UTYPE_SUNOS) || defined (__UTYPE_SUNSOLARIS) || defined (__UTYPE_GNU) uuid_t uuid; assert (sizeof (uuid) == ZUUID_LEN); uuid_generate (uuid); zuuid_set (self, (byte *) uuid); #else # error "Unknown UNIX TYPE" #endif return self; }
void zyre_group_test (bool verbose) { printf (" * zyre_group: "); zsock_t *mailbox = zsock_new (ZMQ_DEALER); zsock_bind (mailbox, "tcp://127.0.0.1:5552"); zhash_t *groups = zhash_new (); zyre_group_t *group = zyre_group_new ("tests", groups); zhash_t *peers = zhash_new (); zuuid_t *you = zuuid_new (); zuuid_t *me = zuuid_new (); zyre_peer_t *peer = zyre_peer_new (peers, you); assert (!zyre_peer_connected (peer)); zyre_peer_connect (peer, me, "tcp://127.0.0.1:5552"); assert (zyre_peer_connected (peer)); zyre_group_join (group, peer); zre_msg_t *msg = zre_msg_new (ZRE_MSG_HELLO); zre_msg_set_endpoint (msg, "tcp://127.0.0.1:5552"); zyre_group_send (group, &msg); msg = zre_msg_recv (mailbox); assert (msg); if (verbose) zre_msg_print (msg); zre_msg_destroy (&msg); zuuid_destroy (&me); zuuid_destroy (&you); zhash_destroy (&peers); zhash_destroy (&groups); zsock_destroy (&mailbox); printf ("OK\n"); }
void zyre_peer_destroy (zyre_peer_t **self_p) { assert (self_p); if (*self_p) { zyre_peer_t *self = *self_p; zyre_peer_disconnect (self); zhash_destroy (&self->headers); zuuid_destroy (&self->uuid); free (self->name); free (self->origin); free (self); *self_p = NULL; } }
book_t* shelf_add_book (shelf_t *self, const char *author, const char *title) { zuuid_t *uuid = zuuid_new (); const char *str_uuid = zuuid_str (uuid); const char* sql = "INSERT INTO book(id,author,title) VALUES($1,$2,$3);"; zpgutil_session_sql (self->session, sql); zpgutil_session_set (self->session, str_uuid); zpgutil_session_set (self->session, author); zpgutil_session_set (self->session, title); zpgutil_session_execute (self->session); zpgutil_session_commit (self->session); book_t *book = book_new (str_uuid,author,title); zuuid_destroy (&uuid); return book; }
void xrap_traffic_destroy (xrap_traffic_t **self_p) { assert (self_p); if (*self_p) { xrap_traffic_t *self = *self_p; // Free class properties zframe_destroy (&self->routing_id); zmsg_destroy (&self->content); zuuid_destroy (&self->sender); // Free object itself free (self); *self_p = NULL; } }
static void zyre_node_destroy (zyre_node_t **self_p) { assert (self_p); if (*self_p) { zyre_node_t *self = *self_p; zuuid_destroy (&self->uuid); zhash_destroy (&self->peers); zhash_destroy (&self->peer_groups); zhash_destroy (&self->own_groups); zhash_destroy (&self->headers); zbeacon_destroy (&self->beacon); zyre_log_destroy (&self->log); free (self); *self_p = NULL; } }
static void zsync_node_destroy (zsync_node_t **self_p) { assert (self_p); if (*self_p) { zsync_node_t *self = *self_p; zuuid_destroy (&self->own_uuid); // TODO destroy all zsync_peers zlist_destroy (&self->peers); zhash_destroy (&self->zyre_peers); zyre_destroy (&self->zyre); free (self); self_p = NULL; } }
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; }
static void zyre_node_recv_beacon (zyre_node_t *self) { // Get IP address and beacon of peer char *ipaddress = zstr_recv (self->beacon); zframe_t *frame = zframe_recv (self->beacon); if (ipaddress == NULL) return; // Interrupted // Ignore anything that isn't a valid beacon beacon_t beacon; memset (&beacon, 0, sizeof (beacon_t)); if (zframe_size (frame) == sizeof (beacon_t)) memcpy (&beacon, zframe_data (frame), zframe_size (frame)); zframe_destroy (&frame); if (beacon.version != BEACON_VERSION) return; // Garbage beacon, ignore it zuuid_t *uuid = zuuid_new (); zuuid_set (uuid, beacon.uuid); if (beacon.port) { char endpoint [100]; const char *iface = zsys_interface (); if (zsys_ipv6 () && iface && !streq (iface, "") && !streq (iface, "*")) sprintf (endpoint, "tcp://%s%%%s:%d", ipaddress, iface, ntohs (beacon.port)); else sprintf (endpoint, "tcp://%s:%d", ipaddress, ntohs (beacon.port)); zyre_peer_t *peer = zyre_node_require_peer (self, uuid, endpoint); zyre_peer_refresh (peer, self->evasive_timeout, self->expired_timeout); } 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); }
static void client_destroy (client_t *c) { subscription_t *sub; ctx_t *ctx = c->ctx; if (c->disconnect_notify) { struct disconnect_notify *d; d = zhash_first (c->disconnect_notify); while (d) { disconnect_destroy (c, d); d = zhash_next (c->disconnect_notify); } zhash_destroy (&c->disconnect_notify); } if (c->subscriptions) { while ((sub = zlist_pop (c->subscriptions))) subscription_destroy (ctx->h, sub); zlist_destroy (&c->subscriptions); } if (c->uuid) zuuid_destroy (&c->uuid); if (c->outqueue) { flux_msg_t *msg; while ((msg = zlist_pop (c->outqueue))) flux_msg_destroy (msg); zlist_destroy (&c->outqueue); } flux_fd_watcher_stop (ctx->h, c->outw); flux_fd_watcher_destroy (c->outw); flux_msg_iobuf_clean (&c->outbuf); flux_fd_watcher_stop (ctx->h, c->inw); flux_fd_watcher_destroy (c->inw); flux_msg_iobuf_clean (&c->inbuf); if (c->fd != -1) close (c->fd); free (c); }
static void module_destroy (module_t *p) { assert (p->magic == MODULE_MAGIC); int errnum; if (p->t) { errnum = pthread_join (p->t, NULL); if (errnum) errn_exit (errnum, "pthread_join"); } assert (p->h == NULL); flux_watcher_stop (p->broker_w); flux_watcher_destroy (p->broker_w); zsocket_destroy (p->zctx, p->sock); dlclose (p->dso); zuuid_destroy (&p->uuid); free (p->digest); if (p->argz) free (p->argz); if (p->name) free (p->name); if (p->rmmod_cb) p->rmmod_cb (p, p->rmmod_arg); if (p->rmmod) { flux_msg_t *msg; while ((msg = zlist_pop (p->rmmod))) flux_msg_destroy (msg); } if (p->subs) { char *s; while ((s = zlist_pop (p->subs))) free (s); zlist_destroy (&p->subs); } zlist_destroy (&p->rmmod); p->magic = ~MODULE_MAGIC; free (p); }
static void zyre_node_destroy (zyre_node_t **self_p) { assert (self_p); if (*self_p) { zyre_node_t *self = *self_p; zpoller_destroy (&self->poller); zuuid_destroy (&self->uuid); zhash_destroy (&self->peers); zhash_destroy (&self->peer_groups); zhash_destroy (&self->own_groups); zhash_destroy (&self->headers); zsock_destroy (&self->inbox); zsock_destroy (&self->outbox); zbeacon_destroy (&self->beacon); zstr_free (&self->endpoint); free (self->name); free (self); *self_p = NULL; } }
void zproto_example_destroy (zproto_example_t **self_p) { assert (self_p); if (*self_p) { zproto_example_t *self = *self_p; // Free class properties zframe_destroy (&self->routing_id); free (self->data); if (self->aliases) zlist_destroy (&self->aliases); zhash_destroy (&self->headers); zchunk_destroy (&self->public_key); zuuid_destroy (&self->identifier); zframe_destroy (&self->address); zmsg_destroy (&self->content); // Free object itself free (self); *self_p = NULL; } }
static void zyre_node_recv_gossip (zyre_node_t *self) { // Get IP address and beacon of peer char *command = NULL, *uuidstr, *endpoint; zstr_recvx (self->gossip, &command, &uuidstr, &endpoint, NULL); if (command == NULL) return; // Interrupted // Any replies except DELIVER would signify an internal error; these // messages come from zgossip, not an external source assert (streq (command, "DELIVER")); // Require peer, if it's not us if (strneq (endpoint, self->endpoint)) { zuuid_t *uuid = zuuid_new (); zuuid_set_str (uuid, uuidstr); zyre_node_require_peer (self, uuid, endpoint); zuuid_destroy (&uuid); } zstr_free (&command); zstr_free (&uuidstr); zstr_free (&endpoint); }
/// // Destructor QZuuid::~QZuuid () { zuuid_destroy (&self); }
static void client_terminate (client_t *self) { zuuid_destroy (&self->id); }
int xrap_traffic_recv (xrap_traffic_t *self, zsock_t *input) { assert (input); if (zsock_type (input) == ZMQ_ROUTER) { zframe_destroy (&self->routing_id); self->routing_id = zframe_recv (input); if (!self->routing_id || !zsock_rcvmore (input)) { zsys_warning ("xrap_traffic: no routing ID"); return -1; // Interrupted or malformed } } zmq_msg_t frame; zmq_msg_init (&frame); int size = zmq_msg_recv (&frame, zsock_resolve (input), 0); if (size == -1) { zsys_warning ("xrap_traffic: interrupted"); goto malformed; // Interrupted } // Get and check protocol signature self->needle = (byte *) zmq_msg_data (&frame); self->ceiling = self->needle + zmq_msg_size (&frame); uint16_t signature; GET_NUMBER2 (signature); if (signature != (0xAAA0 | 9)) { zsys_warning ("xrap_traffic: invalid signature"); // TODO: discard invalid messages and loop, and return // -1 only on interrupt goto malformed; // Interrupted } // Get message id and parse per message type GET_NUMBER1 (self->id); switch (self->id) { case XRAP_TRAFFIC_CONNECTION_OPEN: { char protocol [256]; GET_STRING (protocol); if (strneq (protocol, "MALAMUTE")) { zsys_warning ("xrap_traffic: protocol is invalid"); goto malformed; } } { uint16_t version; GET_NUMBER2 (version); if (version != 1) { zsys_warning ("xrap_traffic: version is invalid"); goto malformed; } } GET_STRING (self->address); break; case XRAP_TRAFFIC_CONNECTION_PING: break; case XRAP_TRAFFIC_CONNECTION_PONG: break; case XRAP_TRAFFIC_CONNECTION_CLOSE: break; case XRAP_TRAFFIC_XRAP_SEND: GET_NUMBER4 (self->timeout); // Get zero or more remaining frames zmsg_destroy (&self->content); if (zsock_rcvmore (input)) self->content = zmsg_recv (input); else self->content = zmsg_new (); break; case XRAP_TRAFFIC_XRAP_OFFER: GET_STRING (self->route); GET_STRING (self->method); break; case XRAP_TRAFFIC_XRAP_DELIVER: if (self->needle + ZUUID_LEN > (self->ceiling)) { zsys_warning ("xrap_traffic: sender is invalid"); goto malformed; } zuuid_destroy (&self->sender); self->sender = zuuid_new_from (self->needle); self->needle += ZUUID_LEN; // Get zero or more remaining frames zmsg_destroy (&self->content); if (zsock_rcvmore (input)) self->content = zmsg_recv (input); else self->content = zmsg_new (); break; case XRAP_TRAFFIC_OK: GET_NUMBER2 (self->status_code); GET_STRING (self->status_reason); break; case XRAP_TRAFFIC_FAIL: GET_NUMBER2 (self->status_code); GET_STRING (self->status_reason); break; case XRAP_TRAFFIC_ERROR: GET_NUMBER2 (self->status_code); GET_STRING (self->status_reason); break; default: zsys_warning ("xrap_traffic: bad message ID"); goto malformed; } // Successful return zmq_msg_close (&frame); return 0; // Error returns malformed: zsys_warning ("xrap_traffic: xrap_traffic malformed message, fail"); zmq_msg_close (&frame); return -1; // Invalid message }
static int zyre_node_recv_peer (zyre_node_t *self) { // Router socket tells us the identity of this peer zre_msg_t *msg = zre_msg_recv (self->inbox); if (msg == NULL) return 0; // Interrupted // First frame is sender identity, holding binary UUID zuuid_t *uuid = zuuid_new (); zuuid_set (uuid, zframe_data (zre_msg_address (msg))); // On HELLO we may create the peer if it's unknown // On other commands the peer must already exist zyre_peer_t *peer = (zyre_peer_t *) zhash_lookup (self->peers, zuuid_str (uuid)); if (zre_msg_id (msg) == ZRE_MSG_HELLO) { peer = zyre_node_require_peer (self, uuid, zre_msg_ipaddress (msg), zre_msg_mailbox (msg)); assert (peer); zyre_peer_set_ready (peer, true); } // Ignore command if peer isn't ready if (peer == NULL || !zyre_peer_ready (peer)) { zre_msg_destroy (&msg); zuuid_destroy (&uuid); return 0; } if (!zyre_peer_check_message (peer, msg)) { zclock_log ("W: [%s] lost messages from %s", zuuid_str (self->uuid), zuuid_str (uuid)); assert (false); } // Now process each command if (zre_msg_id (msg) == ZRE_MSG_HELLO) { // Tell the caller about the peer zstr_sendm (self->pipe, "ENTER"); zstr_sendm (self->pipe, zuuid_str (uuid)); zframe_t *headers = zhash_pack (zre_msg_headers (msg)); zframe_send (&headers, self->pipe, 0); // Join peer to listed groups char *name = zre_msg_groups_first (msg); while (name) { zyre_node_join_peer_group (self, peer, name); name = zre_msg_groups_next (msg); } // Hello command holds latest status of peer zyre_peer_set_status (peer, zre_msg_status (msg)); // Store peer headers for future reference zyre_peer_set_headers (peer, zre_msg_headers (msg)); // If peer is a ZRE/LOG collector, connect to it char *collector = zre_msg_headers_string (msg, "X-ZRELOG", NULL); if (collector) zyre_log_connect (self->log, collector); } else if (zre_msg_id (msg) == ZRE_MSG_WHISPER) { // Pass up to caller API as WHISPER event zstr_sendm (self->pipe, "WHISPER"); zstr_sendm (self->pipe, zuuid_str (uuid)); zmsg_t *content = zmsg_dup (zre_msg_content (msg)); zmsg_send (&content, self->pipe); } else if (zre_msg_id (msg) == ZRE_MSG_SHOUT) { // Pass up to caller as SHOUT event zstr_sendm (self->pipe, "SHOUT"); zstr_sendm (self->pipe, zuuid_str (uuid)); zstr_sendm (self->pipe, zre_msg_group (msg)); zmsg_t *content = zmsg_dup (zre_msg_content (msg)); zmsg_send (&content, self->pipe); } else if (zre_msg_id (msg) == ZRE_MSG_PING) { zre_msg_t *msg = zre_msg_new (ZRE_MSG_PING_OK); zyre_peer_send (peer, &msg); } else if (zre_msg_id (msg) == ZRE_MSG_JOIN) { zyre_node_join_peer_group (self, peer, zre_msg_group (msg)); assert (zre_msg_status (msg) == zyre_peer_status (peer)); } else if (zre_msg_id (msg) == ZRE_MSG_LEAVE) { zyre_node_leave_peer_group (self, peer, zre_msg_group (msg)); assert (zre_msg_status (msg) == zyre_peer_status (peer)); } zuuid_destroy (&uuid); zre_msg_destroy (&msg); // Activity from peer resets peer timers zyre_peer_refresh (peer); return 0; }
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"); }
static void zyre_node_recv_peer (zyre_node_t *self) { // Router socket tells us the identity of this peer zre_msg_t *msg = zre_msg_recv (self->inbox); if (!msg) return; // Interrupted // First frame is sender identity byte *peerid_data = zframe_data (zre_msg_routing_id (msg)); size_t peerid_size = zframe_size (zre_msg_routing_id (msg)); // Identity must be [1] followed by 16-byte UUID if (peerid_size != ZUUID_LEN + 1) { zre_msg_destroy (&msg); return; } zuuid_t *uuid = zuuid_new (); zuuid_set (uuid, peerid_data + 1); // On HELLO we may create the peer if it's unknown // On other commands the peer must already exist zyre_peer_t *peer = (zyre_peer_t *) zhash_lookup (self->peers, zuuid_str (uuid)); if (zre_msg_id (msg) == ZRE_MSG_HELLO) { if (peer) { // Remove fake peers if (zyre_peer_ready (peer)) { zyre_node_remove_peer (self, peer); assert (!(zyre_peer_t *) zhash_lookup (self->peers, zuuid_str (uuid))); } else if (streq (zyre_peer_endpoint (peer), self->endpoint)) { // We ignore HELLO, if peer has same endpoint as current node zre_msg_destroy (&msg); zuuid_destroy (&uuid); return; } } peer = zyre_node_require_peer (self, uuid, zre_msg_endpoint (msg)); assert (peer); zyre_peer_set_ready (peer, true); } // Ignore command if peer isn't ready if (peer == NULL || !zyre_peer_ready (peer)) { zre_msg_destroy (&msg); zuuid_destroy (&uuid); return; } if (zyre_peer_messages_lost (peer, msg)) { zsys_warning ("(%s) messages lost from %s", self->name, zyre_peer_name (peer)); zyre_node_remove_peer (self, peer); zre_msg_destroy (&msg); zuuid_destroy (&uuid); return; } // Now process each command if (zre_msg_id (msg) == ZRE_MSG_HELLO) { // Store properties from HELLO command into peer zyre_peer_set_name (peer, zre_msg_name (msg)); zyre_peer_set_headers (peer, zre_msg_headers (msg)); // Tell the caller about the peer zstr_sendm (self->outbox, "ENTER"); zstr_sendm (self->outbox, zyre_peer_identity (peer)); zstr_sendm (self->outbox, zyre_peer_name (peer)); zframe_t *headers = zhash_pack (zyre_peer_headers (peer)); zframe_send (&headers, self->outbox, ZFRAME_MORE); zstr_send (self->outbox, zre_msg_endpoint (msg)); if (self->verbose) zsys_info ("(%s) ENTER name=%s endpoint=%s", self->name, zyre_peer_name (peer), zyre_peer_endpoint (peer)); // Join peer to listed groups const char *name = zre_msg_groups_first (msg); while (name) { zyre_node_join_peer_group (self, peer, name); name = zre_msg_groups_next (msg); } // Now take peer's status from HELLO, after joining groups zyre_peer_set_status (peer, zre_msg_status (msg)); } else if (zre_msg_id (msg) == ZRE_MSG_WHISPER) { // Pass up to caller API as WHISPER event zstr_sendm (self->outbox, "WHISPER"); zstr_sendm (self->outbox, zuuid_str (uuid)); zstr_sendm (self->outbox, zyre_peer_name (peer)); zmsg_t *content = zmsg_dup (zre_msg_content (msg)); zmsg_send (&content, self->outbox); } else if (zre_msg_id (msg) == ZRE_MSG_SHOUT) { // Pass up to caller as SHOUT event zstr_sendm (self->outbox, "SHOUT"); zstr_sendm (self->outbox, zuuid_str (uuid)); zstr_sendm (self->outbox, zyre_peer_name (peer)); zstr_sendm (self->outbox, zre_msg_group (msg)); zmsg_t *content = zmsg_dup (zre_msg_content (msg)); zmsg_send (&content, self->outbox); } else if (zre_msg_id (msg) == ZRE_MSG_PING) { zre_msg_t *msg = zre_msg_new (ZRE_MSG_PING_OK); zyre_peer_send (peer, &msg); } else if (zre_msg_id (msg) == ZRE_MSG_JOIN) { zyre_node_join_peer_group (self, peer, zre_msg_group (msg)); assert (zre_msg_status (msg) == zyre_peer_status (peer)); } else if (zre_msg_id (msg) == ZRE_MSG_LEAVE) { zyre_node_leave_peer_group (self, peer, zre_msg_group (msg)); assert (zre_msg_status (msg) == zyre_peer_status (peer)); } zuuid_destroy (&uuid); zre_msg_destroy (&msg); // Activity from peer resets peer timers zyre_peer_refresh (peer, self->evasive_timeout, self->expired_timeout); }
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"); }
int zproto_example_recv (zproto_example_t *self, zsock_t *input) { assert (input); if (zsock_type (input) == ZMQ_ROUTER) { zframe_destroy (&self->routing_id); self->routing_id = zframe_recv (input); if (!self->routing_id || !zsock_rcvmore (input)) { zsys_warning ("zproto_example: no routing ID"); return -1; // Interrupted or malformed } } zmq_msg_t frame; zmq_msg_init (&frame); int size = zmq_msg_recv (&frame, zsock_resolve (input), 0); if (size == -1) { zsys_warning ("zproto_example: interrupted"); goto malformed; // Interrupted } // Get and check protocol signature self->needle = (byte *) zmq_msg_data (&frame); self->ceiling = self->needle + zmq_msg_size (&frame); uint16_t signature; GET_NUMBER2 (signature); if (signature != (0xAAA0 | 0)) { zsys_warning ("zproto_example: invalid signature"); // TODO: discard invalid messages and loop, and return // -1 only on interrupt goto malformed; // Interrupted } // Get message id and parse per message type GET_NUMBER1 (self->id); switch (self->id) { case ZPROTO_EXAMPLE_LOG: GET_NUMBER2 (self->sequence); { uint16_t version; GET_NUMBER2 (version); if (version != 3) { zsys_warning ("zproto_example: version is invalid"); goto malformed; } } GET_NUMBER1 (self->level); GET_NUMBER1 (self->event); GET_NUMBER2 (self->node); GET_NUMBER2 (self->peer); GET_NUMBER8 (self->time); GET_STRING (self->host); GET_LONGSTR (self->data); break; case ZPROTO_EXAMPLE_STRUCTURES: GET_NUMBER2 (self->sequence); { size_t list_size; GET_NUMBER4 (list_size); self->aliases = zlist_new (); zlist_autofree (self->aliases); while (list_size--) { char *string = NULL; GET_LONGSTR (string); zlist_append (self->aliases, string); free (string); } } { size_t hash_size; GET_NUMBER4 (hash_size); self->headers = zhash_new (); zhash_autofree (self->headers); while (hash_size--) { char key [256]; char *value = NULL; GET_STRING (key); GET_LONGSTR (value); zhash_insert (self->headers, key, value); free (value); } } break; case ZPROTO_EXAMPLE_BINARY: GET_NUMBER2 (self->sequence); GET_OCTETS (self->flags, 4); { size_t chunk_size; GET_NUMBER4 (chunk_size); if (self->needle + chunk_size > (self->ceiling)) { zsys_warning ("zproto_example: public_key is missing data"); goto malformed; } zchunk_destroy (&self->public_key); self->public_key = zchunk_new (self->needle, chunk_size); self->needle += chunk_size; } if (self->needle + ZUUID_LEN > (self->ceiling)) { zsys_warning ("zproto_example: identifier is invalid"); goto malformed; } zuuid_destroy (&self->identifier); self->identifier = zuuid_new_from (self->needle); self->needle += ZUUID_LEN; // Get next frame off socket if (!zsock_rcvmore (input)) { zsys_warning ("zproto_example: address is missing"); goto malformed; } zframe_destroy (&self->address); self->address = zframe_recv (input); // Get zero or more remaining frames zmsg_destroy (&self->content); if (zsock_rcvmore (input)) self->content = zmsg_recv (input); else self->content = zmsg_new (); break; case ZPROTO_EXAMPLE_TYPES: GET_NUMBER2 (self->sequence); GET_STRING (self->client_forename); GET_STRING (self->client_surname); GET_STRING (self->client_mobile); GET_STRING (self->client_email); GET_STRING (self->supplier_forename); GET_STRING (self->supplier_surname); GET_STRING (self->supplier_mobile); GET_STRING (self->supplier_email); break; default: zsys_warning ("zproto_example: bad message ID"); goto malformed; } // Successful return zmq_msg_close (&frame); return 0; // Error returns malformed: zsys_warning ("zproto_example: zproto_example malformed message, fail"); zmq_msg_close (&frame); return -1; // Invalid message }
int main (int argc, char *argv []) { puts (PRODUCT); puts (COPYRIGHT); puts (NOWARRANTY); int argn = 1; bool verbose = false; if (argn < argc && streq (argv [argn], "-h")) { puts ("syntax: hydrad [ directory ]"); puts (" -- defaults to .hydra in current directory"); exit (0); } if (argn < argc && streq (argv [argn], "-v")) { verbose = true; argn++; } // By default, current node runs in .hydra directory; create this if // it's missing (don't create directory passed as argument); char *workdir = ".hydra"; if (argn < argc) workdir = argv [argn++]; else zsys_dir_create (workdir); // ---------------------------------------------------------------------- // This code eventually goes into a reusable hydra actor class // Switch to working directory zsys_info ("hydrad: data store in %s directory", workdir); if (zsys_dir_change (workdir)) { zsys_error ("hydrad: cannot access %s: %s", workdir, strerror (errno)); return 1; } // Check we are the only process currently running here if (zsys_run_as ("hydrad.lock", NULL, NULL)) { zsys_error ("hydrad: cannot start process safely, exiting"); return 1; } // Get node identity from config file, or generate new identity zconfig_t *config = zconfig_load ("hydra.cfg"); if (!config) { // Set defaults for Hydra service config = zconfig_new ("root", NULL); zconfig_put (config, "/server/timeout", "5000"); zconfig_put (config, "/server/background", "0"); zconfig_put (config, "/server/verbose", "0"); } char *identity = zconfig_resolve (config, "/hydra/identity", NULL); if (!identity) { zuuid_t *uuid = zuuid_new (); zconfig_put (config, "/hydra/identity", zuuid_str (uuid)); zconfig_put (config, "/hydra/nickname", "Anonymous"); zconfig_save (config, "hydra.cfg"); zuuid_destroy (&uuid); } // Create store structure, if necessary zsys_dir_create ("content"); zsys_dir_create ("posts"); // Start server and bind to ephemeral TCP port. We can run many // servers on the same box, for testing. zactor_t *server = zactor_new (hydra_server, NULL); if (verbose) zstr_send (server, "VERBOSE"); // Bind Hydra service to ephemeral port and get that port number char *command; int port_nbr; zsock_send (server, "ss", "CONFIGURE", "hydra.cfg"); zsock_send (server, "ss", "BIND", "tcp://*:*"); zsock_send (server, "s", "PORT"); zsock_recv (server, "si", &command, &port_nbr); zsys_info ("hydrad: TCP server started on port=%d", port_nbr); assert (streq (command, "PORT")); free (command); // We're going to use Zyre for discovery and presence, and our own // Hydra protocol for content exchange zyre_t *zyre = zyre_new (NULL); if (verbose) zyre_set_verbose (zyre); char *hostname = zsys_hostname (); char *endpoint = zsys_sprintf ("tcp://%s:%d", hostname, port_nbr); zyre_set_header (zyre, "X-HYDRA", "%s", endpoint); zstr_free (&endpoint); zstr_free (&hostname); if (zyre_start (zyre)) { zsys_info ("hydrad: can't start Zyre discovery service"); zactor_destroy (&server); zyre_destroy (&zyre); return 1; } // When we get a new peer, handle it zpoller_t *poller = zpoller_new (zyre_socket (zyre), NULL); while (!zpoller_terminated (poller)) { void *which = zpoller_wait (poller, -1); if (which == zyre_socket (zyre)) { zyre_event_t *event = zyre_event_new (zyre); if (zyre_event_type (event) == ZYRE_EVENT_ENTER) { zsys_debug ("hydrad: new peer name=%s endpoint=%s", zyre_event_name (event), zyre_event_header (event, "X-HYDRA")); s_handle_peer (zyre_event_header (event, "X-HYDRA"), verbose); } zyre_event_destroy (&event); } else break; } zsys_info ("hydrad: shutting down..."); zpoller_destroy (&poller); // Shutdown all services zactor_destroy (&server); zyre_destroy (&zyre); zconfig_destroy (&config); return 0; }
static void send_email (client_t *self) { assert (self); assert (self->message); zsys_info ("sending email !"); CURL *curl; CURLcode res = CURLE_OK; struct curl_slist *recipients = NULL; zuuid_t *uuid = zuuid_new (); const char *uuid_s = zuuid_str (uuid); char filename[256]; sprintf (filename, "./data/mail-%s.txt", uuid_s); zuuid_destroy (&uuid); char fn[200]; sprintf (fn, "mail-%s.txt", uuid_s); zfile_t *zf = zfile_new ("./data", fn); zfile_output (zf); FILE *pFile = zfile_handle (zf); //FILE *pFile = fopen (filename,"w"); // date -------- time_t timer; char buffer[256]; struct tm* tm_info; time (&timer); tm_info = localtime (&timer); strftime (buffer, 60, "%a, %d %b %Y %T %z\r\n", tm_info); //--------------- fprintf (pFile, "Date: %s", buffer); zsys_info ("To: %s \r\n", zmailer_msg_to(self->message)); fprintf (pFile,"To: %s\r\n", zmailer_msg_to(self->message)); fprintf (pFile, "From: %s\r\n", zmailer_msg_from (self->message)); fprintf (pFile, "Subject: %s \r\n", zmailer_msg_subject (self->message)); fprintf (pFile, "\r\n"); fprintf (pFile, "%s Thank you. \r\n", zmailer_msg_request(self->message)); fclose (pFile); FILE * pMail; pMail = fopen (filename,"r"); assert (pMail); curl = curl_easy_init(); if(curl) { /* Set username and password */ curl_easy_setopt(curl, CURLOPT_USERNAME, zconfig_get (self->server->config, "zmailer_server/user", "")); curl_easy_setopt(curl, CURLOPT_PASSWORD, zconfig_get (self->server->config, "zmailer_server/password", "")); /* This is the URL for your mailserver. Note the use of port 587 here, * instead of the normal SMTP port (25). Port 587 is commonly used for * secure mail submission (see RFC4403), but you should use whatever * matches your server configuration. */ curl_easy_setopt(curl, CURLOPT_URL, zconfig_get (self->server->config, "zmailer_server/server", "")); /* In this example, we'll start with a plain text connection, and upgrade * to Transport Layer Security (TLS) using the STARTTLS command. Be careful * of using CURLUSESSL_TRY here, because if TLS upgrade fails, the transfer * will continue anyway - see the security discussion in the libcurl * tutorial for more details. */ curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL); /* If your server doesn't have a valid certificate, then you can disable * part of the Transport Layer Security protection by setting the * CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to 0 (false).*/ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); /* Note that this option isn't strictly required, omitting it will result in * libcurl sending the MAIL FROM command with empty sender data. All * autoresponses should have an empty reverse-path, and should be directed * to the address in the reverse-path which triggered them. Otherwise, they * could cause an endless loop. See RFC 5321 Section 4.5.5 for more details. */ curl_easy_setopt(curl, CURLOPT_MAIL_FROM, zmailer_msg_from (self->message)); /* Add two recipients, in this particular case they correspond to the * To: and Cc: addressees in the header, but they could be any kind of * recipient. */ recipients = curl_slist_append(recipients, zmailer_msg_to (self->message)); curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients); /* We're using a callback function to specify the payload (the headers and * body of the message). You could just use the CURLOPT_READDATA option to * specify a FILE pointer to read from. */ curl_easy_setopt(curl, CURLOPT_READDATA, pMail); curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L); /* Since the traffic will be encrypted, it is very useful to turn on debug * information within libcurl to see what is happening during the transfer. */ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); /* Send the message */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); /* Free the list of recipients */ curl_slist_free_all(recipients); /* Always cleanup */ curl_easy_cleanup(curl); } fclose (pMail); }
void zwr_client_test (bool verbose) { printf (" * zwr_client: "); if (verbose) printf ("\n"); // @selftest zwr_client_verbose = verbose; // Start a server to test against, and bind to endpoint zactor_t *server = zactor_new (zwr_server, "zwr_client_test"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "LOAD", "src/zwr_client.cfg", NULL); // Create clients for testing zwr_client_t *client = zwr_client_new (); zwr_client_t *handler = zwr_client_new (); assert (client); assert (handler); // Connect clients to server int rc = zwr_client_connect (client, "tcp://127.0.0.1:9999", 1000, "client"); assert (rc == 0); assert (zwr_client_connected (client) == true); rc = zwr_client_connect (handler, "tcp://127.0.0.1:9999", 1000, "handler"); assert (rc == 0); assert (zwr_client_connected (handler) == true); // Provide Rubbish Offering rc = zwr_client_set_handler (handler, "GET", "///"); assert (rc != 0); assert (zwr_client_status (handler) == XRAP_TRAFFIC_CONFLICT); // Provide Offering rc = zwr_client_set_handler (handler, "GET", "/foo/{[^/]}"); assert (rc == 0); // Send Request xrap_msg_t *xmsg = xrap_msg_new (XRAP_MSG_GET); xrap_msg_set_resource (xmsg, "%s", "/foo/bar"); zmsg_t *msg = xrap_msg_encode (&xmsg); rc = zwr_client_request (client, 0, &msg); assert (rc == 0); // Receive Request msg = zwr_client_recv (handler); xmsg = xrap_msg_decode (&msg); assert (xrap_msg_id (xmsg) == XRAP_MSG_GET); assert (streq ("/foo/bar", xrap_msg_resource (xmsg))); xrap_msg_destroy (&xmsg); // Send Response xmsg = xrap_msg_new (XRAP_MSG_GET_OK); xrap_msg_set_status_code (xmsg, 200); xrap_msg_set_content_type (xmsg, "text/hello"); xrap_msg_set_content_body (xmsg, "Hello World!"); msg = xrap_msg_encode (&xmsg); zwr_client_deliver (handler, handler->sender, &msg); zuuid_t *sender = zwr_client_sender (handler); zuuid_destroy (&sender); // Receive Response msg = zwr_client_recv (client); xmsg = xrap_msg_decode (&msg); assert (xrap_msg_id (xmsg) == XRAP_MSG_GET_OK); assert (xrap_msg_status_code (xmsg) == 200); assert (streq ("text/hello", xrap_msg_content_type (xmsg))); assert (streq ("Hello World!", xrap_msg_content_body (xmsg))); xrap_msg_destroy (&xmsg); sender = zwr_client_sender (client); zuuid_destroy (&sender); // Send Request 2 xmsg = xrap_msg_new (XRAP_MSG_GET); xrap_msg_set_resource (xmsg, "%s", "/fou/baz"); msg = xrap_msg_encode (&xmsg); rc = zwr_client_request (client, 0, &msg); assert (rc == XRAP_TRAFFIC_NOT_FOUND); zwr_client_destroy (&client); zwr_client_destroy (&handler); // Done, shut down zactor_destroy (&server); // @end printf ("OK\n"); }