JNIEXPORT void JNICALL Java_org_zeromq_zyre_Zyre__1_1setHeader (JNIEnv *env, jclass c, jlong self, jstring name, jstring format) { char *name_ = (char *) (*env)->GetStringUTFChars (env, name, NULL); char *format_ = (char *) (*env)->GetStringUTFChars (env, format, NULL); zyre_set_header ((zyre_t *) (intptr_t) self, name_, "%s", format_); (*env)->ReleaseStringUTFChars (env, name, name_); (*env)->ReleaseStringUTFChars (env, format, format_); }
int main (int argc, char *argv []) { zctx_t *ctx = zctx_new (); // Use the CZMQ zbeacon class to make sure we listen on the // same network interface as our peers zbeacon_t *beacon = zbeacon_new (ctx, ZRE_DISCOVERY_PORT); char *host = zbeacon_hostname (beacon); // Bind to an ephemeral port void *collector = zsocket_new (ctx, ZMQ_SUB); int port = zsocket_bind (collector, "tcp://%s:*", host); zsocket_set_subscribe (collector, ""); // Announce this to all peers we connect to zyre_t *node = zyre_new (ctx); zyre_set_header (node, "X-ZRELOG", "tcp://%s:%d", host, port); zyre_start (node); zpoller_t *poller = zpoller_new (collector, zyre_socket (node), NULL); while (!zctx_interrupted) { void *which = zpoller_wait (poller, -1); if (which == collector) s_print_log_msg (collector); else if (which == zyre_socket (node)) { zmsg_t *msg = zyre_recv (node); if (!msg) break; // Interrupted zmsg_destroy (&msg); } else break; // Interrupted } zyre_destroy (&node); zbeacon_destroy (&beacon); zctx_destroy (&ctx); return 0; }
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; } }
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 int mrb_actor_pipe_reader(zloop_t* reactor, zsock_t* pipe, void* args) { errno = 0; self_t* self = (self_t*)args; int rc = 0; zmsg_t* msg = zmsg_recv(pipe); if (!msg) return -1; char* command = zmsg_popstr(msg); zsys_debug("command: %s", command); if (streq(command, "$TERM")) { rc = -1; } else if (streq(command, "BIND ROUTER")) { char* endpoint = zmsg_popstr(msg); if (zsock_bind(self->router, "%s", endpoint) >= 0) { const char* boundendpoint = zsock_endpoint(self->router); zyre_set_header(self->discovery, "mrb-actor-v1-router", "%s", boundendpoint); zsock_signal(pipe, 0); zsock_send(pipe, "s", boundendpoint); } else { zsock_signal(pipe, 1); zsock_send(pipe, "i", errno); } zstr_free(&endpoint); } else if (streq(command, "BIND PULL")) { char* endpoint = zmsg_popstr(msg); if (zsock_bind(self->pull, "%s", endpoint) >= 0) { const char* boundendpoint = zsock_endpoint(self->pull); zyre_set_header(self->discovery, "mrb-actor-v1-pull", "%s", boundendpoint); zsock_signal(pipe, 0); zsock_send(pipe, "s", boundendpoint); } else { zsock_signal(pipe, 1); zsock_send(pipe, "i", errno); } zstr_free(&endpoint); } else if (streq(command, "ZYRE SET ENDPOINT")) { char* endpoint = zmsg_popstr(msg); if (zyre_set_endpoint(self->discovery, "%s", endpoint) == -1) { zsock_signal(pipe, 1); } else { zsock_signal(pipe, 0); } zstr_free(&endpoint); } else if (streq(command, "ZYRE GOSSIP BIND")) { char* endpoint = zmsg_popstr(msg); zyre_gossip_bind(self->discovery, "%s", endpoint); zstr_free(&endpoint); } else if (streq(command, "ZYRE GOSSIP CONNECT")) { char* endpoint = zmsg_popstr(msg); zyre_gossip_connect(self->discovery, "%s", endpoint); zstr_free(&endpoint); } else if (streq(command, "ZYRE START")) { if (zyre_start(self->discovery) == -1) { zsock_signal(pipe, 1); } else { zyre_join(self->discovery, "mrb-actor-v1"); zsock_signal(pipe, 0); } } else if (streq(command, "LOAD IREP FILE")) { char* mrb_file = zmsg_popstr(msg); mrb_state* mrb = self->mrb; int ai = mrb_gc_arena_save(mrb); struct mrb_jmpbuf* prev_jmp = mrb->jmp; struct mrb_jmpbuf c_jmp; MRB_TRY(&c_jmp) { mrb->jmp = &c_jmp; FILE* fp = fopen(mrb_file, "rb"); if (!fp) { mrb_sys_fail(mrb, "fopen"); } mrb_load_irep_file(mrb, fp); fclose(fp); if (mrb->exc) { mrb_exc_raise(mrb, mrb_obj_value(mrb->exc)); } zsock_signal(pipe, 0); mrb->jmp = prev_jmp; } MRB_CATCH(&c_jmp) { mrb->jmp = prev_jmp; mrb_print_error(mrb); zsock_signal(pipe, 1); mrb->exc = NULL; } MRB_END_EXC(&c_jmp); mrb_gc_arena_restore(mrb, ai); zstr_free(&mrb_file); }
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"); }
/// // Set node header; these are provided to other nodes during discovery // and come in each ENTER message. void QmlZyre::setHeader (const QString &name, const QString &format) { zyre_set_header (self, name.toUtf8().data(), "%s", format.toUtf8().data()); };
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 }
/// // Set node header; these are provided to other nodes during discovery // and come in each ENTER message. void QZyre::setHeader (const QString &name, const QString ¶m) { zyre_set_header (self, name.toUtf8().data(), "%s", param.toUtf8().data()); }
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_header(const std::string key, const std::string& value) const { zyre_set_header(m_self, key.c_str(), "%s", value.c_str()); }
void ztask_monitor_api_loop (ztask_monitor_api_t *self) { zyre_t *node = zyre_new(); zyre_set_header(node, "X-ZTASK-MON", "TASK MON"); zyre_start(node); zyre_dump(node); if (self->pub_port >= 0) zclock_log("Publisher is listening on port %d ...", self->pub_port); zpoller_t *poller = zpoller_new (zyre_socket (node), NULL); uint64_t reap_at = zclock_time () + self->pub_timeout; zyre_event_t *event; while (!zsys_interrupted) { void *which = zpoller_wait (poller, self->pub_timeout); if (zpoller_terminated (poller)) { break; } if (which == zyre_socket (node)) { event = zyre_event_new(node); if (!event) break; if (zyre_event_type(event) == ZYRE_EVENT_ENTER) { } else if (zyre_event_type(event) == ZYRE_EVENT_EXIT) { char *data = (char *) zhash_lookup(self->nodes, (const char *) zyre_event_sender(event)); if (data) { zhash_delete(self->nodes, (const char *) zyre_event_sender(event)); free(data); } } else if (zyre_event_type(event) == ZYRE_EVENT_WHISPER) { zmsg_t *msg = zyre_event_msg(event); char *cpu_str = zmsg_popstr(msg); char *mem_rss_str = zmsg_popstr(msg); if (self->pub_sock) { char *data = (char *) zhash_lookup(self->nodes, (const char *) zyre_event_sender(event)); if (data) { zhash_delete(self->nodes, (const char *) zyre_event_sender(event)); free(data); } if (asprintf(&data, "{\"id\":\"%s\",%s,%s}", zyre_event_sender(event), cpu_str, mem_rss_str) < 0) data = NULL; zhash_insert(self->nodes, (const char *) zyre_event_sender(event), data); } else { zclock_log("From %s", zyre_event_sender(event)); zclock_log(" %s", cpu_str); zclock_log(" %s", mem_rss_str); } free(cpu_str); free(mem_rss_str); } zyre_event_destroy(&event); } if (zclock_time() >= reap_at) { reap_at = zclock_time() + self->pub_timeout; if (self->pub_sock) { char *data = ztask_monitor_api_json (self); if (data) { zmsg_t *msg = zmsg_new(); zmsg_addstr(msg, data); zclock_log("Sending %s ...", data); zmsg_send(&msg, self->pub_sock); free(data); } } } } zyre_stop(node); zpoller_destroy (&poller); zyre_destroy(&node); // Cleanup of self->nodes // char *wk = (char *) zhash_first (self->nodes); // while (wk) { // zhash_delete (self->nodes, wk); //// free (data); // wk = (char *) zhash_first (self->nodes); // } }
void ztask_run_manager_loop (ztask_run_manager_t *self) { assert (self); if (!self->packetizer) { zclock_log ("E: No packetizer set !!!"); return; } // Setting network interface if neede if (self->node_interface) zyre_set_interface(self->node, self->node_interface); zyre_set_header (self->node, "X-ZTASK-RUN", "ZTASK RUN"); zyre_start (self->node); zyre_dump (self->node); zclock_sleep (10); // ztask_packetizer_dump (self->packetizer); ztask_packet_t *packet; int request_sent = 0; zyre_event_t *event; while (!zsys_interrupted) { if (ztask_packetizer_is_finished (self->packetizer)) break; event = zyre_event_new (self->node); if (!event) break; if (zyre_event_type (event) == ZYRE_EVENT_ENTER) { // Ignoring nodes which don't have service X-ZTASK-NODE defined if (zyre_event_header (event, "X-ZTASK-NODE")) { zhash_insert (self->nodes, zyre_event_sender (event),""); ztask_log_debug (self->log, "Adding node -> workers=%ld", zhash_size (self->nodes)); } else { // TODO disconnect worker (zyre peer) somehow } } else if (zyre_event_type (event) == ZYRE_EVENT_EXIT) { if (zhash_lookup (self->nodes, zyre_event_sender (event))) { ztask_log_debug (self->log, "Removing node -> workers=%ld", zhash_size (self->nodes)); zhash_delete (self->nodes, zyre_event_sender (event)); // Cleanup packetizer in case ztask_node was killed request_sent -= ztask_packetizer_running_node_cleanup (self->packetizer, zyre_event_sender (event)); } } else if (zyre_event_type (event) == ZYRE_EVENT_WHISPER) { // Ingoring whispers when they are not from our nodes if (!zhash_lookup(self->nodes, zyre_event_sender (event))) { ztask_log_warning (self->log, "W: Ingoring whisper from %s", zyre_event_sender (event)); zyre_event_destroy (&event); continue; } zmsg_t *msg_report = zyre_event_msg(event); char *str_msg = zmsg_popstr (msg_report); if (streq(str_msg, "REQUEST")) { // Let's handle request packet = ztask_packetizer_next_packet (self->packetizer); if (packet) { char *cmd; if ( asprintf (&cmd, "%s", ztask_packet_cmd (packet)) < 0) cmd = NULL; assert (cmd); zmsg_t *msg = zmsg_new (); zmsg_addstr (msg, "REQUEST"); zmsg_addstrf (msg, "%s_%ld", zyre_uuid (self->node), ztask_packet_id (packet)); zmsg_addstr (msg, cmd); zyre_whisper (self->node, zyre_event_sender (event), &msg); ztask_packet_set_node (packet, zyre_event_sender (event)); request_sent++; ztask_packetizer_info (self->packetizer, request_sent); ztask_log_debug (self->log, "ID=%s_%ld cmd='%s' running=%d", zyre_uuid (self->node), ztask_packet_id (packet), cmd, request_sent); free (cmd); } else { zmsg_t *msg = zmsg_new (); zmsg_addstr (msg, "NO_PACKETS"); zyre_whisper (self->node, zyre_event_sender (event), &msg); } } else if (streq(str_msg, "REPORT")) { // It's report char *str_id = zmsg_popstr (msg_report); char *str_pid_rc = zmsg_popstr (msg_report); ztask_log_debug (self->log, "REPORT ID=%s rc=%s", str_id, str_pid_rc); ztask_packetizer_report (self->packetizer, str_id, str_pid_rc); request_sent--; ztask_packetizer_info (self->packetizer, request_sent); free (str_id); free (str_pid_rc); } else { ztask_log_error (self->log, "E: ztask_run_manager_loop : Wrong message %s", str_msg); assert (false); } free (str_msg); } zyre_event_destroy (&event); } // FIXME : simplify zhash_foreach() zlist_t *keys = zhash_keys (self->nodes); char *key = (char *) zlist_first (keys); while (key) { zmsg_t *msg = zmsg_new (); zmsg_addstr (msg, "END"); zyre_whisper (self->node, key, &msg); key = (char *) zlist_next (keys); } zlist_destroy (&keys); // wait for shout to be delivered zclock_sleep (100); ztask_packetizer_summary(self->packetizer, 0); zyre_stop (self->node); }