int main (int argc, char *argv []) { zctx_t *ctx = zctx_new (); // Use the Zyre UDP class to make sure we listen on the same // network interface as our peers void *collector = zsocket_new (ctx, ZMQ_SUB); zre_udp_t *udp = zre_udp_new (PING_PORT_NUMBER); char *host = zre_udp_host (udp); // Bind to an ephemeral port int port = zsocket_bind (collector, "tcp://%s:*", host); // Announce this to all peers we connect to zre_interface_t *interface = zre_interface_new (); zre_interface_header_set (interface, "X-ZRELOG", "tcp://%s:%d", host, port); // Get all log messages (don't filter) zsocket_set_subscribe (collector, ""); zmq_pollitem_t pollitems [] = { { collector, 0, ZMQ_POLLIN, 0 }, { zre_interface_handle (interface), 0, ZMQ_POLLIN, 0 } }; while (!zctx_interrupted) { if (zmq_poll (pollitems, 2, 1000 * ZMQ_POLL_MSEC) == -1) break; // Interrupted // Handle input on collector if (pollitems [0].revents & ZMQ_POLLIN) s_print_log_msg (collector); // Handle event from interface (ignore it) if (pollitems [1].revents & ZMQ_POLLIN) { zmsg_t *msg = zre_interface_recv (interface); if (!msg) break; // Interrupted zmsg_destroy (&msg); } } zre_interface_destroy (&interface); zre_udp_destroy (&udp); zctx_destroy (&ctx); return 0; }
static void interface_task (void *args, zctx_t *ctx, void *pipe) { zre_interface_t *interface = zre_interface_new (); int64_t counter = 0; char *to_peer = NULL; // Either of these set, char *to_group = NULL; // and we set a message char *cookie = NULL; // received message char *sending_cookie = NULL; // sending message zmq_pollitem_t pollitems [] = { { pipe, 0, ZMQ_POLLIN, 0 }, { zre_interface_handle (interface), 0, ZMQ_POLLIN, 0 } }; // all interface joins GLOBAL zre_interface_join (interface, "GLOBAL"); while (!zctx_interrupted) { if (zmq_poll (pollitems, 2, randof (1000) * ZMQ_POLL_MSEC) == -1) break; // Interrupted if (pollitems [0].revents & ZMQ_POLLIN) break; // Any command from parent means EXIT // Process an event from interface if (pollitems [1].revents & ZMQ_POLLIN) { zmsg_t *incoming = zre_interface_recv (interface); if (!incoming) break; // Interrupted char *event = zmsg_popstr (incoming); if (streq (event, "ENTER")) { // Always say hello to new peer to_peer = zmsg_popstr (incoming); sending_cookie = "R:HELLO"; } else if (streq (event, "EXIT")) { // Do nothing } else if (streq (event, "WHISPER")) { to_peer = zmsg_popstr (incoming); cookie = zmsg_popstr (incoming); // if a message comes from zre_perf_local, send back a special response if (streq (cookie, "S:WHISPER")) { sending_cookie = "R:WHISPER"; } else { free (to_peer); free (cookie); to_peer = NULL; cookie = NULL; } } else if (streq (event, "SHOUT")) { to_peer = zmsg_popstr (incoming); to_group = zmsg_popstr (incoming); cookie = zmsg_popstr (incoming); // if a message comes from zre_perf_local, send back a special response if (streq (cookie, "S:SHOUT")) { free (to_peer); to_peer = NULL; sending_cookie = "R:SHOUT"; } else { free (to_peer); free (to_group); to_peer = NULL; to_group = NULL; } } free (event); zmsg_destroy (&incoming); // Send outgoing messages if needed if (to_peer) { zmsg_t *outgoing = zmsg_new (); zmsg_addstr (outgoing, to_peer); zmsg_addstr (outgoing, sending_cookie); zre_interface_whisper (interface, &outgoing); free (to_peer); to_peer = NULL; } if (to_group) { zmsg_t *outgoing = zmsg_new (); zmsg_addstr (outgoing, to_group); zmsg_addstr (outgoing, sending_cookie); zre_interface_shout (interface, &outgoing); free (to_group); to_group = NULL; } if (cookie) { free (cookie); cookie = NULL; } } } zre_interface_destroy (&interface); }
int main (int argc, char *argv []) { // Get number of remote interfaces to simulate, default 100 // If we run multiple zre_perf_remote on multiple machines, // max_interface must be sum of all the remote interface counts. int max_interface = 100; int max_message = 10000; int nbr_interface = 0; int nbr_hello_response = 0; int nbr_message = 0; int nbr_message_response = 0; if (argc > 1) max_interface = atoi (argv [1]); if (argc > 2) max_message = atoi (argv [2]); zre_interface_t *interface = zre_interface_new (); zre_interface_join (interface, "GLOBAL"); int64_t start = zclock_time (); int64_t elapse; char **peers = zmalloc (sizeof (char *) * max_interface); while (true) { zmsg_t *incoming = zre_interface_recv (interface); if (!incoming) break; // Interrupted // If new peer, say hello to it and wait for it to answer us char *event = zmsg_popstr (incoming); if (streq (event, "ENTER")) { char *peer = zmsg_popstr (incoming); peers[nbr_interface++] = peer; if (nbr_interface == max_interface) { // got HELLO from the all remote interfaces elapse = zclock_time () - start; printf ("Took %ld ms to coordinate with all remote\n", (long)elapse); } } else if (streq (event, "WHISPER")) { char *peer = zmsg_popstr (incoming); char *cookie = zmsg_popstr (incoming); if (streq (cookie, "R:HELLO")) { if (++nbr_hello_response == max_interface) { // got HELLO from the all remote interfaces elapse = zclock_time () - start; printf ("Took %ld ms to get greeting from all remote\n", (long)elapse); } } free (peer); free (cookie); } free (event); zmsg_destroy (&incoming); if (nbr_interface == max_interface && nbr_hello_response == max_interface) break; } zmq_pollitem_t pollitems [] = { { zre_interface_handle (interface), 0, ZMQ_POLLIN, 0 } }; // send WHISPER message start = zclock_time (); for (nbr_message = 0; nbr_message < max_message; nbr_message++) { zmsg_t *outgoing = zmsg_new (); zmsg_addstr (outgoing, peers [nbr_message % max_interface]); zmsg_addstr (outgoing, "S:WHISPER"); zre_interface_whisper (interface, &outgoing); while (zmq_poll (pollitems, 1, 0) > 0) { if (s_interface_recv (interface, "WHISPER", "R:WHISPER")) nbr_message_response++; } } while (nbr_message_response < max_message) if (s_interface_recv (interface, "WHISPER", "R:WHISPER")) nbr_message_response++; // got WHISPER response from the all remote interfaces elapse = zclock_time () - start; printf ("Took %ld ms to send/receive %d message. %.2f msg/s \n", (long)elapse, max_message, (float) max_message * 1000 / elapse); // send SPOUT message start = zclock_time (); nbr_message = 0; nbr_message_response = 0; max_message = max_message / max_interface; for (nbr_message = 0; nbr_message < max_message; nbr_message++) { zmsg_t *outgoing = zmsg_new (); zmsg_addstr (outgoing, "GLOBAL"); zmsg_addstr (outgoing, "S:SHOUT"); zre_interface_shout (interface, &outgoing); while (zmq_poll (pollitems, 1, 0) > 0) { if (s_interface_recv (interface, "SHOUT", "R:SHOUT")) nbr_message_response++; } } while (nbr_message_response < max_message * max_interface) if (s_interface_recv (interface, "SHOUT", "R:SHOUT")) nbr_message_response++; // got SHOUT response from the all remote interfaces elapse = zclock_time () - start; printf ("Took %ld ms to send %d, recv %d GROUP message. %.2f msg/s \n", (long) elapse, max_message, max_interface * max_message, (float) max_interface * max_message * 1000 / elapse); zre_interface_destroy (&interface); for (nbr_interface = 0; nbr_interface < max_interface; nbr_interface++) { free (peers[nbr_interface]); } free (peers); return 0; }