/* send 100 messages over PAIR sockets * sender in one event handler, receiver in another */ void test_ev_zmq (void) { struct ev_loop *loop; void *zctx; void *zin, *zout; int i; ev_zmq win, wout; ok ((loop = ev_loop_new (EVFLAG_AUTO)) != NULL, "ev_loop_new works"); ok ((zctx = zmq_init (1)) != NULL, "initialized zmq context"); ok ((zout = zmq_socket (zctx, ZMQ_PAIR)) != NULL && zmq_bind (zout, "inproc://eventloop_test") == 0, "PAIR socket bind ok"); ok ((zin = zmq_socket (zctx, ZMQ_PAIR)) != NULL && zmq_connect (zin, "inproc://eventloop_test") == 0, "PAIR socket connect ok"); i = 0; ev_zmq_init (&win, zsock_rx_cb, zin, EV_READ); win.data = &i; ev_zmq_init (&wout, zsock_tx_cb, zout, EV_WRITE); ev_zmq_start (loop, &win); ev_zmq_start (loop, &wout); ok (ev_run (loop, 0) == 0, "both watchers removed themselves and ev_run exited"); ev_zmq_stop (loop, &win); ev_zmq_stop (loop, &wout); cmp_ok (i, "==", 100, "ev_zmq handler ran 100 times"); ev_loop_destroy (loop); zmq_close (zin); zmq_close (zout); zmq_ctx_destroy (zctx); }
void zsock_tx_cb (struct ev_loop *loop, ev_zmq *w, int revents) { static int count = 50; /* send two per invocation */ if ((revents & EV_WRITE)) { if (zstr_send (w->zsock, "PING") < 0) fprintf (stderr, "zstr_send: %s", strerror (errno)); if (zstr_send (w->zsock, "PING") < 0) fprintf (stderr, "zstr_send: %s", strerror (errno)); if (--count == 0) ev_zmq_stop (loop, w); } if ((revents & EV_ERROR)) ev_break (loop, EVBREAK_ALL); }
void zsock_rx_cb (struct ev_loop *loop, ev_zmq *w, int revents) { int *iter = w->data; char *s; static int count = 100; if ((revents & EV_READ)) { (*iter)++; if (!(s = zstr_recv (w->zsock))) fprintf (stderr, "zstr_recv: %s", strerror (errno)); else free (s); if (--count == 0) ev_zmq_stop (loop, w); } if ((revents & EV_ERROR)) ev_break (loop, EVBREAK_ALL); }
static void zmq_stop (void *impl, flux_watcher_t *w) { assert (w->signature == ZMQ_SIG); ev_zmq_stop (w->r->loop, (ev_zmq *)impl); }