GroupNodeImpl::GroupNodeImpl(const std::string& desc, bool promiscuous) : event_thread_(NULL), prom_thread_(NULL), promiscuous_(promiscuous), stopped_(false) { // create the zyre node node_ = zyre_new((GroupNode::name() + "/" + desc).c_str()); // verbose output zyre_set_verbose(node_); // set the interface to use, otherwise zyre just guesses (default "") zyre_set_interface(node_, GroupNode::iface().c_str()); // set the headers zyre_set_header(node_, "desc", "%s", desc.c_str()); // access our uuid std::string uuid = zyre_uuid(node_); // access our name node_name_ = zyre_name(node_); // start the node if (zyre_start(node_)) { throw std::runtime_error("Could not start the zyre node"); } // join the control group - too heavy if (zyre_join(node_, GroupNode::control_.c_str())) { throw std::runtime_error("Error joining CONTROL group"); } // create our self peer self_.reset(new Peer(uuid, desc)); /* we want to minimize the groups joined and messages sent - especially by other non-promiscuous components; in order to do so, we need to periodically join all the known groups. */ if (promiscuous_) { prom_thread_ = new std::thread(&GroupNodeImpl::update_groups, this); BOOST_LOG_TRIVIAL(debug) << "Started promiscuous group thread..." << std::endl; } }
static void node_actor (zsock_t *pipe, void *args) { zyre_t *node = zyre_new (NULL); if (!node) return; // Could not create new node zyre_set_verbose (node); zyre_set_endpoint (node, "inproc://%s", (char *) args); free (args); // Connect to test hub zyre_gossip_connect (node, "inproc://zyre-hub"); zyre_start (node); zsock_signal (pipe, 0); // Signal "ready" to caller int counter = 0; char *to_peer = NULL; // Either of these set, char *to_group = NULL; // and we set a message char *cookie = NULL; zpoller_t *poller = zpoller_new (pipe, zyre_socket (node), NULL); int64_t trigger = zclock_mono () + 1000; while (true) { void *which = zpoller_wait (poller, randof (1000)); if (!which) break; // Interrupted // $TERM from parent means exit; anything else is breach of // contract so we should assert if (which == pipe) { char *command = zstr_recv (pipe); assert (streq (command, "$TERM")); zstr_free (&command); break; // Finished } // Process an event from node if (which == zyre_socket (node)) { zmsg_t *incoming = zyre_recv (node); if (!incoming) break; // Interrupted char *event = zmsg_popstr (incoming); char *peer = zmsg_popstr (incoming); char *name = zmsg_popstr (incoming); if (streq (event, "ENTER")) // Always say hello to new peer to_peer = strdup (peer); else if (streq (event, "EXIT")) // Always try talk to departed peer to_peer = strdup (peer); else if (streq (event, "WHISPER")) { // Send back response 1/2 the time if (randof (2) == 0) { to_peer = strdup (peer); cookie = zmsg_popstr (incoming); } } else if (streq (event, "SHOUT")) { to_peer = strdup (peer); to_group = zmsg_popstr (incoming); cookie = zmsg_popstr (incoming); // Send peer response 1/3rd the time if (randof (3) > 0) { free (to_peer); to_peer = NULL; } // Send group response 1/3rd the time if (randof (3) > 0) { free (to_group); to_group = NULL; } } else if (streq (event, "JOIN")) { char *group = zmsg_popstr (incoming); printf ("I: %s joined %s\n", name, group); free (group); } else if (streq (event, "LEAVE")) { char *group = zmsg_popstr (incoming); printf ("I: %s left %s\n", name, group); free (group); } free (event); free (peer); free (name); zmsg_destroy (&incoming); // Send outgoing messages if needed if (to_peer) { zyre_whispers (node, to_peer, "%d", counter++); free (to_peer); to_peer = NULL; } if (to_group) { zyre_shouts (node, to_group, "%d", counter++); free (to_group); to_group = NULL; } if (cookie) { free (cookie); cookie = NULL; } } if (zclock_mono () >= trigger) { trigger = zclock_mono () + 1000; char group [10]; sprintf (group, "GROUP%03d", randof (MAX_GROUP)); if (randof (4) == 0) zyre_join (node, group); else if (randof (3) == 0) zyre_leave (node, group); } } zpoller_destroy (&poller); zyre_destroy (&node); }
void zyre_event_test (bool verbose) { printf (" * zyre_event: "); // @selftest // Create two nodes zyre_t *node1 = zyre_new ("node1"); assert (node1); zyre_set_header (node1, "X-HELLO", "World"); zyre_set_verbose (node1); if (zyre_start (node1)) { zyre_destroy (&node1); printf ("OK (skipping test, no UDP discovery)\n"); return; } zyre_join (node1, "GLOBAL"); zyre_t *node2 = zyre_new ("node2"); assert (node2); zyre_set_verbose (node2); int rc = zyre_start (node2); assert (rc == 0); zyre_join (node2, "GLOBAL"); // Give time for them to interconnect zclock_sleep (250); // One node shouts to GLOBAL zmsg_t *msg = zmsg_new (); zmsg_addstr (msg, "Hello, World"); zyre_shout (node1, "GLOBAL", &msg); zclock_sleep (100); // Parse ENTER zyre_event_t *event = zyre_event_new (node2); assert (zyre_event_type (event) == ZYRE_EVENT_ENTER); char *sender = zyre_event_sender (event); assert (sender); char *name = zyre_event_name (event); assert (name); assert (streq (name, "node1")); char *address = zyre_event_address (event); assert (address); char *header = zyre_event_header (event, "X-HELLO"); assert (header); zyre_event_destroy (&event); // Parse JOIN event = zyre_event_new (node2); assert (zyre_event_type (event) == ZYRE_EVENT_JOIN); zyre_event_destroy (&event); // Parse SHOUT event = zyre_event_new (node2); assert (zyre_event_type (event) == ZYRE_EVENT_SHOUT); assert (streq (zyre_event_group (event), "GLOBAL")); msg = zyre_event_msg (event); char *string = zmsg_popstr (msg); assert (streq (string, "Hello, World")); free (string); zyre_event_destroy (&event); zyre_destroy (&node1); zyre_destroy (&node2); // @end printf ("OK\n"); }
static void node_task (void *args, zctx_t *ctx, void *pipe) { zyre_t *node = zyre_new (ctx); if (!node) return; // Could not create new node zyre_set_verbose (node); zyre_start (node); int64_t counter = 0; char *to_peer = NULL; // Either of these set, char *to_group = NULL; // and we set a message char *cookie = NULL; zpoller_t *poller = zpoller_new (pipe, zyre_socket (node), NULL); int64_t trigger = zclock_time () + 1000; while (!zctx_interrupted) { void *which = zpoller_wait (poller, randof (1000)); // Any command from parent means EXIT if (which == pipe) break; // Process an event from node if (which == zyre_socket (node)) { zmsg_t *incoming = zyre_recv (node); if (!incoming) break; // Interrupted char *event = zmsg_popstr (incoming); if (streq (event, "ENTER")) { // Always say hello to new peer to_peer = zmsg_popstr (incoming); } else if (streq (event, "EXIT")) { // Always try talk to departed peer to_peer = zmsg_popstr (incoming); } else if (streq (event, "WHISPER")) { // Send back response 1/2 the time if (randof (2) == 0) { to_peer = zmsg_popstr (incoming); cookie = zmsg_popstr (incoming); } } else if (streq (event, "SHOUT")) { to_peer = zmsg_popstr (incoming); to_group = zmsg_popstr (incoming); cookie = zmsg_popstr (incoming); // Send peer response 1/3rd the time if (randof (3) > 0) { free (to_peer); to_peer = NULL; } // Send group response 1/3rd the time if (randof (3) > 0) { free (to_group); to_group = NULL; } } else if (streq (event, "JOIN")) { char *from_peer = zmsg_popstr (incoming); char *group = zmsg_popstr (incoming); printf ("I: %s joined %s\n", from_peer, group); free (from_peer); free (group); } else if (streq (event, "LEAVE")) { char *from_peer = zmsg_popstr (incoming); char *group = zmsg_popstr (incoming); printf ("I: %s left %s\n", from_peer, group); free (from_peer); free (group); } else if (streq (event, "DELIVER")) { char *filename = zmsg_popstr (incoming); char *fullname = zmsg_popstr (incoming); printf ("I: received file %s\n", fullname); free (fullname); free (filename); } free (event); zmsg_destroy (&incoming); // Send outgoing messages if needed if (to_peer) { zyre_whispers (node, to_peer, "%lu", counter++); free (to_peer); to_peer = NULL; } if (to_group) { zyre_shouts (node, to_group, "%lu", counter++); free (to_group); to_group = NULL; } if (cookie) { free (cookie); cookie = NULL; } } if (zclock_time () >= trigger) { trigger = zclock_time () + 1000; char group [10]; sprintf (group, "GROUP%03d", randof (MAX_GROUP)); if (randof (4) == 0) zyre_join (node, group); else if (randof (3) == 0) zyre_leave (node, group); } } zpoller_destroy (&poller); zyre_destroy (&node); }
void zyre_event_test (bool verbose) { printf (" * zyre_event: "); // @selftest // Create two nodes zyre_t *node1 = zyre_new ("node1"); assert (node1); zyre_set_header (node1, "X-HELLO", "World"); int rc = zyre_set_endpoint (node1, "inproc://zyre-node1"); assert (rc == 0); // use gossiping instead of beaconing, suits Travis better zyre_gossip_bind (node1, "inproc://gossip-hub"); if (verbose) zyre_set_verbose (node1); if (zyre_start (node1)) { zyre_destroy (&node1); printf ("OK (skipping test, no UDP discovery)\n"); return; } zyre_join (node1, "GLOBAL"); zyre_t *node2 = zyre_new ("node2"); assert (node2); if (verbose) zyre_set_verbose (node2); rc = zyre_set_endpoint (node2, "inproc://zyre-node2"); assert (rc == 0); // use gossiping instead of beaconing, suits Travis better zyre_gossip_connect (node2, "inproc://gossip-hub"); rc = zyre_start (node2); assert (rc == 0); zyre_join (node2, "GLOBAL"); // Give time for them to interconnect zclock_sleep (250); // One node shouts to GLOBAL zmsg_t *msg = zmsg_new (); zmsg_addstr (msg, "Hello, World"); zyre_shout (node1, "GLOBAL", &msg); zclock_sleep (100); // Parse ENTER zyre_event_t *event = zyre_event_new (node2); assert (streq (zyre_event_type (event), "ENTER")); const char *sender = zyre_event_peer_uuid (event); assert (sender); const char *name = zyre_event_peer_name (event); assert (name); assert (streq (name, "node1")); const char *address = zyre_event_peer_addr (event); assert (address); const char *header = zyre_event_header (event, "X-HELLO"); assert (header); zyre_event_destroy (&event); // Parse JOIN // We tolerate other events, which we can get if there are instances // of Zyre running somewhere on the network. event = zyre_event_new (node2); if (streq (zyre_event_type (event), "JOIN")) { // Parse SHOUT zyre_event_destroy (&event); event = zyre_event_new (node2); if (streq (zyre_event_type (event), "SHOUT")) { assert (streq (zyre_event_group (event), "GLOBAL")); zmsg_t *msg = zyre_event_get_msg (event); char *string = zmsg_popstr (msg); zmsg_destroy (&msg); assert (streq (string, "Hello, World")); free (string); } zyre_event_destroy (&event); } zyre_destroy (&node1); zyre_destroy (&node2); // @end printf ("OK\n"); }
void zyre_test (bool verbose) { printf (" * zyre: "); // @selftest // We'll use inproc gossip discovery so that this works without networking int major, minor, patch; zyre_version (&major, &minor, &patch); assert (major == ZYRE_VERSION_MAJOR); assert (minor == ZYRE_VERSION_MINOR); assert (patch == ZYRE_VERSION_PATCH); // Create two nodes zyre_t *node1 = zyre_new ("node1"); assert (node1); assert (streq (zyre_name (node1), "node1")); zyre_set_header (node1, "X-HELLO", "World"); zyre_set_verbose (node1); // Set inproc endpoint for this node zyre_set_endpoint (node1, "inproc://zyre-node1"); // Set up gossip network for this node zyre_gossip_bind (node1, "inproc://gossip-hub"); int rc = zyre_start (node1); assert (rc == 0); zyre_t *node2 = zyre_new ("node2"); assert (node2); assert (streq (zyre_name (node2), "node2")); zyre_set_verbose (node2); // Set inproc endpoint for this node // First, try to use existing name, it'll fail zyre_set_endpoint (node2, "inproc://zyre-node1"); assert (streq (zyre_endpoint (node2), "")); // Now use available name and confirm that it succeeds zyre_set_endpoint (node2, "inproc://zyre-node2"); assert (streq (zyre_endpoint (node2), "inproc://zyre-node2")); // Set up gossip network for this node zyre_gossip_connect (node2, "inproc://gossip-hub"); rc = zyre_start (node2); assert (rc == 0); assert (strneq (zyre_uuid (node1), zyre_uuid (node2))); zyre_join (node1, "GLOBAL"); zyre_join (node2, "GLOBAL"); // Give time for them to interconnect zclock_sleep (100); // One node shouts to GLOBAL zyre_shouts (node1, "GLOBAL", "Hello, World"); // Second node should receive ENTER, JOIN, and SHOUT zmsg_t *msg = zyre_recv (node2); assert (msg); char *command = zmsg_popstr (msg); assert (streq (command, "ENTER")); zstr_free (&command); assert (zmsg_size (msg) == 4); char *peerid = zmsg_popstr (msg); zstr_free (&peerid); char *name = zmsg_popstr (msg); assert (streq (name, "node1")); zstr_free (&name); zframe_t *headers_packed = zmsg_pop (msg); char *peeraddress = zmsg_popstr (msg); zstr_free (&peeraddress); assert (headers_packed); zhash_t *headers = zhash_unpack (headers_packed); assert (headers); zframe_destroy (&headers_packed); assert (streq ((char*)zhash_lookup (headers, "X-HELLO"), "World")); zhash_destroy (&headers); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "JOIN")); zstr_free (&command); assert (zmsg_size (msg) == 3); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "SHOUT")); zstr_free (&command); zmsg_destroy (&msg); zyre_stop (node1); zyre_stop (node2); zyre_destroy (&node1); zyre_destroy (&node2); // @end printf ("OK\n"); }
JNIEXPORT void JNICALL Java_org_zeromq_zyre_Zyre__1_1setVerbose (JNIEnv *env, jclass c, jlong self) { zyre_set_verbose ((zyre_t *) (intptr_t) self); }
/// // Set verbose mode; this tells the node to log all traffic as well as // all major events. void QmlZyre::setVerbose () { zyre_set_verbose (self); };
void zyre_test (bool verbose) { printf (" * zyre: "); if (verbose) printf ("\n"); // @selftest // We'll use inproc gossip discovery so that this works without networking uint64_t version = zyre_version (); assert ((version / 10000) % 100 == ZYRE_VERSION_MAJOR); assert ((version / 100) % 100 == ZYRE_VERSION_MINOR); assert (version % 100 == ZYRE_VERSION_PATCH); // Create two nodes zyre_t *node1 = zyre_new ("node1"); assert (node1); assert (streq (zyre_name (node1), "node1")); zyre_set_header (node1, "X-HELLO", "World"); if (verbose) zyre_set_verbose (node1); // Set inproc endpoint for this node int rc = zyre_set_endpoint (node1, "inproc://zyre-node1"); assert (rc == 0); // Set up gossip network for this node zyre_gossip_bind (node1, "inproc://gossip-hub"); rc = zyre_start (node1); assert (rc == 0); zyre_t *node2 = zyre_new ("node2"); assert (node2); assert (streq (zyre_name (node2), "node2")); if (verbose) zyre_set_verbose (node2); // Set inproc endpoint for this node // First, try to use existing name, it'll fail rc = zyre_set_endpoint (node2, "inproc://zyre-node1"); assert (rc == -1); // Now use available name and confirm that it succeeds rc = zyre_set_endpoint (node2, "inproc://zyre-node2"); assert (rc == 0); // Set up gossip network for this node zyre_gossip_connect (node2, "inproc://gossip-hub"); rc = zyre_start (node2); assert (rc == 0); assert (strneq (zyre_uuid (node1), zyre_uuid (node2))); zyre_join (node1, "GLOBAL"); zyre_join (node2, "GLOBAL"); // Give time for them to interconnect zclock_sleep (250); if (verbose) zyre_dump (node1); zlist_t *peers = zyre_peers (node1); assert (peers); assert (zlist_size (peers) == 1); zlist_destroy (&peers); zyre_join (node1, "node1 group of one"); zyre_join (node2, "node2 group of one"); // Give them time to join their groups zclock_sleep (250); zlist_t *own_groups = zyre_own_groups (node1); assert (own_groups); assert (zlist_size (own_groups) == 2); zlist_destroy (&own_groups); zlist_t *peer_groups = zyre_peer_groups (node1); assert (peer_groups); assert (zlist_size (peer_groups) == 2); zlist_destroy (&peer_groups); char *value = zyre_peer_header_value (node2, zyre_uuid (node1), "X-HELLO"); assert (streq (value, "World")); zstr_free (&value); // One node shouts to GLOBAL zyre_shouts (node1, "GLOBAL", "Hello, World"); // Second node should receive ENTER, JOIN, and SHOUT zmsg_t *msg = zyre_recv (node2); assert (msg); char *command = zmsg_popstr (msg); assert (streq (command, "ENTER")); zstr_free (&command); assert (zmsg_size (msg) == 4); char *peerid = zmsg_popstr (msg); char *name = zmsg_popstr (msg); assert (streq (name, "node1")); zstr_free (&name); zframe_t *headers_packed = zmsg_pop (msg); char *address = zmsg_popstr (msg); char *endpoint = zyre_peer_address (node2, peerid); assert (streq (address, endpoint)); zstr_free (&peerid); zstr_free (&endpoint); zstr_free (&address); assert (headers_packed); zhash_t *headers = zhash_unpack (headers_packed); assert (headers); zframe_destroy (&headers_packed); assert (streq ((char *) zhash_lookup (headers, "X-HELLO"), "World")); zhash_destroy (&headers); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "JOIN")); zstr_free (&command); assert (zmsg_size (msg) == 3); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "JOIN")); zstr_free (&command); assert (zmsg_size (msg) == 3); zmsg_destroy (&msg); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "SHOUT")); zstr_free (&command); zmsg_destroy (&msg); zyre_stop (node2); msg = zyre_recv (node2); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "STOP")); zstr_free (&command); zmsg_destroy (&msg); zyre_stop (node1); zyre_destroy (&node1); zyre_destroy (&node2); printf ("OK\n"); #ifdef ZYRE_BUILD_DRAFT_API if (zsys_has_curve()){ printf (" * zyre-curve: "); if (verbose) printf ("\n"); if (verbose) zsys_debug("----------------TESTING CURVE --------------"); zactor_t *speaker = zactor_new (zbeacon, NULL); assert (speaker); if (verbose) zstr_sendx (speaker, "VERBOSE", NULL); // ensuring we have a broadcast address zsock_send (speaker, "si", "CONFIGURE", 9999); char *hostname = zstr_recv (speaker); if (!*hostname) { printf ("OK (skipping test, no UDP broadcasting)\n"); zactor_destroy (&speaker); freen (hostname); return; } freen (hostname); zactor_destroy (&speaker); // zap setup zactor_t *auth = zactor_new(zauth, NULL); assert (auth); if (verbose) { zstr_sendx(auth, "VERBOSE", NULL); zsock_wait(auth); } zstr_sendx (auth, "CURVE", CURVE_ALLOW_ANY, NULL); zsock_wait (auth); zyre_t *node3 = zyre_new ("node3"); zyre_t *node4 = zyre_new ("node4"); assert (node3); assert (node4); zyre_set_verbose (node3); zyre_set_verbose (node4); zyre_set_zap_domain(node3, "TEST"); zyre_set_zap_domain(node4, "TEST"); zsock_set_rcvtimeo(node3->inbox, 10000); zsock_set_rcvtimeo(node4->inbox, 10000); zcert_t *node3_cert = zcert_new (); zcert_t *node4_cert = zcert_new (); assert (node3_cert); assert (node4_cert); zyre_set_zcert(node3, node3_cert); zyre_set_zcert(node4, node4_cert); zyre_set_header(node3, "X-PUBLICKEY", "%s", zcert_public_txt(node3_cert)); zyre_set_header(node4, "X-PUBLICKEY", "%s", zcert_public_txt(node4_cert)); // test beacon if (verbose) zsys_debug ("----------------TESTING BEACON----------------"); rc = zyre_start(node3); assert (rc == 0); rc = zyre_start(node4); assert (rc == 0); zyre_join (node3, "GLOBAL"); zyre_join (node4, "GLOBAL"); zclock_sleep (1500); if (verbose) { zyre_dump (node3); zyre_dump (node4); } zyre_shouts (node3, "GLOBAL", "Hello, World"); // Second node should receive ENTER, JOIN, and SHOUT msg = zyre_recv (node4); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "ENTER")); zstr_free (&command); char *peerid = zmsg_popstr (msg); assert (peerid); name = zmsg_popstr (msg); assert (streq (name, "node3")); zmsg_destroy (&msg); msg = zyre_recv (node4); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "JOIN")); zstr_free (&command); zmsg_destroy(&msg); msg = zyre_recv (node4); assert (msg); command = zmsg_popstr (msg); assert (streq (command, "SHOUT")); zstr_free (&command); zmsg_destroy(&msg); zyre_leave(node3, "GLOBAL"); zyre_leave(node4, "GLOBAL"); zstr_free (&name); zstr_free (&peerid); zstr_free (&command); zyre_stop (node3); zyre_stop (node4); // give things a chance to settle... zclock_sleep (250); zyre_destroy(&node3); zyre_destroy(&node4); zcert_destroy(&node3_cert); zcert_destroy(&node4_cert); // test gossip if (verbose) zsys_debug ("----------------TESTING GOSSIP----------------"); zyre_t *node5 = zyre_new ("node5"); zyre_t *node6 = zyre_new ("node6"); assert (node5); assert (node6); if (verbose) { zyre_set_verbose (node5); zyre_set_verbose (node6); } // if it takes more than 10s, something probably went terribly wrong zsock_set_rcvtimeo(node5->inbox, 10000); zsock_set_rcvtimeo(node6->inbox, 10000); zcert_t *node5_cert = zcert_new (); zcert_t *node6_cert = zcert_new (); assert (node5_cert); assert (node6_cert); zyre_set_zcert(node5, node5_cert); zyre_set_zcert(node6, node6_cert); zyre_set_header(node5, "X-PUBLICKEY", "%s", zcert_public_txt(node5_cert)); zyre_set_header(node6, "X-PUBLICKEY", "%s", zcert_public_txt(node6_cert)); const char *gossip_cert = zcert_public_txt (node5_cert); // TODO- need to add zyre_gossip_port functions to get port from gossip bind(?) zyre_gossip_bind(node5, "tcp://127.0.0.1:9001"); zyre_gossip_connect_curve(node6, gossip_cert, "tcp://127.0.0.1:9001"); zyre_start(node5); zsock_wait(node5); zyre_start(node6); zsock_wait(node6); zyre_join (node5, "GLOBAL"); zyre_join (node6, "GLOBAL"); // give things a chance to settle... zclock_sleep (1500); if (verbose) { zyre_dump (node5); zyre_dump (node6); } zyre_shouts (node5, "GLOBAL", "Hello, World"); // Second node should receive ENTER, JOIN, and SHOUT msg = zyre_recv (node6); assert (msg); command = zmsg_popstr (msg); zsys_info(command); assert (streq (command, "ENTER")); zstr_free (&command); peerid = zmsg_popstr (msg); assert (peerid); name = zmsg_popstr (msg); zmsg_destroy (&msg); assert (streq (name, "node5")); zstr_free (&name); zyre_leave(node5, "GLOBAL"); zyre_leave(node6, "GLOBAL"); zyre_stop (node5); zyre_stop (node6); // give things a chance to settle... zclock_sleep (250); zstr_free (&peerid); zcert_destroy (&node5_cert); zcert_destroy (&node6_cert); zyre_destroy(&node5); zyre_destroy(&node6); zactor_destroy(&auth); printf ("OK\n"); } #endif }
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; }
void set_verbose() const { zyre_set_verbose(m_self); }
static void ipc_actor (zsock_t *pipe, void *args) { // Do some initialization char* name = (char*) args; node = zyre_new (name); assert(node); if (!node) // Could not create new node { sim_printf("Couldn't create IPC node ... %s\n", name); return; } ipc_printf("Starting IPC node %s ...", name); if (ipc_verbose) zyre_set_verbose (node); // uncomment to watch the events zyre_start (node); zyre_join (node, fnpGroup); zsock_signal (pipe, 0); // Signal "ready" to caller terminated = false; poller = zpoller_new (pipe, zyre_socket (node), NULL); assert(poller); ipc_printf(" done\n"); while (!terminated) { void *which = zpoller_wait (poller, -1); // no timeout if (!terminated && which == pipe) { zmsg_t *msg = zmsg_recv (which); if (!msg) break; // Interrupted char *command = zmsg_popstr (msg); if (streq (command, "$TERM")) { terminated = true; } else if (streq (command, "SHOUT")) { char *string = zmsg_popstr (msg); zyre_shouts (node, fnpGroup, "%s", string); } else if (streq (command, "WHISPER")) { char *string = zmsg_popstr (msg); zyre_whispers (node, lastPeer, "%s", string); } else { sim_debug (DBG_IPCVERBOSE, &ipc_dev,"ipc_actor(): E: invalid message to actor"); //assert (false); } free (command); zmsg_destroy (&msg); } else if (!terminated && which == zyre_socket (node)) { zmsg_t *msg = zmsg_recv (which); char *event = zmsg_popstr (msg); char *peer = zmsg_popstr (msg); char *name = zmsg_popstr (msg); char *group = zmsg_popstr (msg); char *message = zmsg_popstr (msg); // change to zmsg_popmsg (zmsg_t *self) later if (streq (event, "ENTER")) { ipc(ipcEnter, name, peer, message, 0); } else if (streq (event, "EXIT")) { ipc(ipcExit, name, peer, 0, 0); } if (streq (event, "SHOUT")) { ipc(ipcShoutRx, name, peer, message, 0); } if (streq (event, "WHISPER")) { ipc(ipcWhisperRx, name, peer, group, 0); } if (ipc_verbose) { ipc_printf("Message from node\n"); ipc_printf("event: %s peer: %s name: %s group: %s message: %s\n", event, peer, name, group, message); } free (event); free (peer); free (name); free (group); free (message); zmsg_destroy (&msg); } } }