static self_t * s_self_new (zsock_t *pipe) { self_t *self = (self_t *) zmalloc (sizeof (self_t)); if (self) { self->pipe = pipe; self->poller = zpoller_new (self->pipe, NULL); if (!self->poller) s_self_destroy (&self); } return self; }
static self_t * s_self_new (zsock_t *pipe, void *sock) { self_t *self = (self_t *) zmalloc (sizeof (self_t)); if (!self) return NULL; self->pipe = pipe; self->monitored = zsock_resolve (sock); self->poller = zpoller_new (self->pipe, NULL); if (!self->poller) s_self_destroy (&self); return self; }
static int s_self_handle_pipe (self_t *self) { // Get the whole message off the pipe in one go zmsg_t *request = zmsg_recv (self->pipe); if (!request) return -1; // Interrupted char *command = zmsg_popstr (request); if (!command) { s_self_destroy (&self); return -1; } if (self->verbose) zsys_info ("zmonitor: API command=%s", command); if (streq (command, "LISTEN")) { char *event = zmsg_popstr (request); while (event) { if (self->verbose) zsys_info ("zmonitor: - listening to event=%s", event); s_self_listen (self, event); zstr_free (&event); event = zmsg_popstr (request); } } else if (streq (command, "START")) { s_self_start (self); zsock_signal (self->pipe, 0); } else if (streq (command, "VERBOSE")) self->verbose = true; else if (streq (command, "$TERM")) self->terminated = true; else { zsys_error ("zmonitor: - invalid command: %s", command); assert (false); } zstr_free (&command); zmsg_destroy (&request); return 0; }
void zmonitor (zsock_t *pipe, void *args) { self_t *self = s_self_new (pipe, (zsock_t *) args); // Signal successful initialization zsock_signal (pipe, 0); while (!self->terminated) { zsock_t *which = zpoller_wait (self->poller, -1); if (which == self->pipe) s_self_handle_pipe (self); else if (which == self->sink) s_self_handle_sink (self); else if (zpoller_terminated (self->poller)) break; // Interrupted } s_self_destroy (&self); }
void zauth (zsock_t *pipe, void *unused) { self_t *self = s_self_new (pipe); // Signal successful initialization zsock_signal (pipe, 0); while (!self->terminated) { zsock_t *which = (zsock_t *) zpoller_wait (self->poller, -1); if (which == self->pipe) s_self_handle_pipe (self); else if (which == self->handler) s_self_authenticate (self); else if (zpoller_terminated (self->poller)) break; // Interrupted } s_self_destroy (&self); }
void zbeacon (zsock_t *pipe, void *args) { self_t *self = s_self_new (pipe); assert (self); // Signal successful initialization zsock_signal (pipe, 0); while (!self->terminated) { // Poll on API pipe and on UDP socket zmq_pollitem_t pollitems [] = { { zsock_resolve (self->pipe), 0, ZMQ_POLLIN, 0 }, { NULL, self->udpsock, ZMQ_POLLIN, 0 } }; long timeout = -1; if (self->transmit) { timeout = (long) (self->ping_at - zclock_mono ()); if (timeout < 0) timeout = 0; } int pollset_size = self->udpsock? 2: 1; if (zmq_poll (pollitems, pollset_size, timeout * ZMQ_POLL_MSEC) == -1) break; // Interrupted if (pollitems [0].revents & ZMQ_POLLIN) s_self_handle_pipe (self); if (pollitems [1].revents & ZMQ_POLLIN) s_self_handle_udp (self); if (self->transmit && zclock_mono () >= self->ping_at) { // Send beacon to any listening peers if (zsys_udp_send (self->udpsock, self->transmit, &self->broadcast, sizeof (inaddr_t))) // Try to recreate UDP socket on interface s_self_prepare_udp (self); self->ping_at = zclock_mono () + self->interval; } } s_self_destroy (&self); }
static self_t * s_self_new (zsock_t *pipe) { self_t *self = (self_t *) zmalloc (sizeof (self_t)); int rc = -1; if (self) { self->pipe = pipe; self->whitelist = zhashx_new (); if (self->whitelist) self->blacklist = zhashx_new (); // Create ZAP handler and get ready for requests if (self->blacklist) self->handler = zsock_new (ZMQ_REP); if (self->handler) rc = zsock_bind (self->handler, ZAP_ENDPOINT); if (rc == 0) self->poller = zpoller_new (self->pipe, self->handler, NULL); if (!self->poller) s_self_destroy (&self); } return self; }
void zproxy (zsock_t *pipe, void *unused) { self_t *self = s_self_new (pipe); assert (self); // Signal successful initialization zsock_signal (pipe, 0); while (!self->terminated) { zsock_t *which = (zsock_t *) zpoller_wait (self->poller, -1); if (zpoller_terminated (self->poller)) break; // Interrupted else if (which == self->pipe) s_self_handle_pipe (self); else if (which == self->frontend) s_self_switch (self, self->frontend, self->backend); else if (which == self->backend) s_self_switch (self, self->backend, self->frontend); } s_self_destroy (&self); }