static void test_tcp_pub (void *args, zctx_t *ctx, void *pipe) { vtx_t *vtx = vtx_new (ctx); int rc = vtx_tcp_load (vtx, FALSE); assert (rc == 0); char *port = zstr_recv (pipe); // Create publisher socket and bind to all network interfaces void *publisher = vtx_socket (vtx, ZMQ_PUB); assert (publisher); rc = vtx_bind (vtx, publisher, "tcp://*:%s", port); assert (rc == 0); int sent = 0; while (!zctx_interrupted) { zstr_sendf (publisher, "NOM %04x", randof (0x10000)); sent++; char *end = zstr_recv_nowait (pipe); if (end) { free (end); zstr_send (pipe, "OK"); break; } } zclock_log ("I: PUB: sent=%d", sent); free (port); vtx_destroy (&vtx); }
static void test_tcp_pair_cli (void *args, zctx_t *ctx, void *pipe) { vtx_t *vtx = vtx_new (ctx); int rc = vtx_tcp_load (vtx, FALSE); assert (rc == 0); char *port = zstr_recv (pipe); void *pair = vtx_socket (vtx, ZMQ_PAIR); assert (pair); rc = vtx_connect (vtx, pair, "tcp://localhost:%s", port); assert (rc == 0); int sent = 0; int recd = 0; while (!zctx_interrupted) { zstr_send (pair, "ICANHAZ?"); sent++; char *reply = zstr_recv_nowait (pair); if (reply) { recd++; free (reply); } char *end = zstr_recv_nowait (pipe); if (end) { free (end); zstr_send (pipe, "OK"); break; } } zclock_log ("I: PAIR CLI: sent=%d recd=%d", sent, recd); free (port); vtx_destroy (&vtx); }
static void client_thread (void *args, zctx_t *ctx, void *pipe) { // Initialize virtual transport interface vtx_t *vtx = vtx_new (ctx); int rc = vtx_register (vtx, "udp", vtx_udp_driver); assert (rc == 0); // Create client socket and connect to broadcast address void *client = vtx_socket (vtx, ZMQ_REQ); rc = vtx_connect (vtx, client, "udp://127.0.0.255:32000"); assert (rc == 0); while (TRUE) { // Look for name server anywhere on LAN zstr_send (client, "hello?"); puts ("hello?"); // Wait for at most 1000msec for reply before retrying zmq_pollitem_t items [] = { { client, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 1, 1000 * ZMQ_POLL_MSEC); if (rc == -1) break; // Context has been shut down if (items [0].revents & ZMQ_POLLIN) { char *input = zstr_recv (client); puts (input); free (input); sleep (1); } } vtx_destroy (&vtx); }
static void test_tcp_req (void *args, zctx_t *ctx, void *pipe) { vtx_t *vtx = vtx_new (ctx); int rc = vtx_tcp_load (vtx, FALSE); assert (rc == 0); char *port = zstr_recv (pipe); void *client = vtx_socket (vtx, ZMQ_REQ); assert (client); rc = vtx_connect (vtx, client, "tcp://localhost:%s", port); assert (rc == 0); int sent = 0; int recd = 0; while (!zctx_interrupted) { zstr_send (client, "ICANHAZ?"); sent++; zmq_pollitem_t items [] = { { pipe, 0, ZMQ_POLLIN, 0 }, { client, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 2, 500 * ZMQ_POLL_MSEC); if (rc == -1) break; // Context has been shut down if (items [0].revents & ZMQ_POLLIN) { free (zstr_recv (pipe)); zstr_send (pipe, "OK"); break; } if (items [1].revents & ZMQ_POLLIN) { free (zstr_recv (client)); recd++; } else { // No response, close socket and start a new one vtx_close (vtx, client); client = vtx_socket (vtx, ZMQ_REQ); rc = vtx_connect (vtx, client, "tcp://localhost:%s", port); } } zclock_log ("I: REQ: sent=%d recd=%d", sent, recd); free (port); vtx_destroy (&vtx); }
static void test_tcp_router (void *args, zctx_t *ctx, void *pipe) { vtx_t *vtx = vtx_new (ctx); int rc = vtx_tcp_load (vtx, FALSE); assert (rc == 0); char *port = zstr_recv (pipe); void *router = vtx_socket (vtx, ZMQ_ROUTER); assert (router); rc = vtx_bind (vtx, router, "tcp://*:%s", port); assert (rc == 0); int sent = 0; while (!zctx_interrupted) { zmq_pollitem_t items [] = { { pipe, 0, ZMQ_POLLIN, 0 }, { router, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 2, 500 * ZMQ_POLL_MSEC); if (rc == -1) break; // Context has been shut down if (items [1].revents & ZMQ_POLLIN) { char *address = zstr_recv (router); free (zstr_recv (router)); zstr_sendm (router, address); zstr_send (router, "CHEEZBURGER"); free (address); sent++; } if (items [0].revents & ZMQ_POLLIN) { free (zstr_recv (pipe)); zstr_send (pipe, "OK"); break; } } zclock_log ("I: ROUTER: sent=%d", sent); free (port); vtx_destroy (&vtx); }
static void server_thread (void *args, zctx_t *ctx, void *pipe) { // Initialize virtual transport interface vtx_t *vtx = vtx_new (ctx); int rc = vtx_register (vtx, "udp", vtx_udp_driver); assert (rc == 0); // Create server socket and bind to all network interfaces void *server = vtx_socket (vtx, ZMQ_REP); rc = vtx_bind (vtx, server, "udp://*:32000"); assert (rc == 0); while (TRUE) { char *input = zstr_recv (server); if (!input) break; // Interrupted puts (input); free (input); zstr_send (server, "ack"); } vtx_destroy (&vtx); }
static void test_tcp_sub (void *args, zctx_t *ctx, void *pipe) { vtx_t *vtx = vtx_new (ctx); int rc = vtx_tcp_load (vtx, FALSE); assert (rc == 0); char *port = zstr_recv (pipe); void *subscriber = vtx_socket (vtx, ZMQ_SUB); assert (subscriber); rc = vtx_connect (vtx, subscriber, "tcp://localhost:%s", port); assert (rc == 0); int recd = 0; while (!zctx_interrupted) { zmq_pollitem_t items [] = { { pipe, 0, ZMQ_POLLIN, 0 }, { subscriber, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 2, 500 * ZMQ_POLL_MSEC); if (rc == -1) break; // Context has been shut down if (items [0].revents & ZMQ_POLLIN) { free (zstr_recv (pipe)); zstr_send (pipe, "OK"); break; } if (items [1].revents & ZMQ_POLLIN) { free (zstr_recv (subscriber)); recd++; } } zclock_log ("I: SUB: recd=%d", recd); free (port); vtx_destroy (&vtx); }