int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); broker_t *self = s_broker_new (verbose); s_broker_bind (self, "tcp://*:5555"); // Get and process messages forever or until interrupted while (TRUE) { zmq_pollitem_t items [] = { { self->socket, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 1, HEARTBEAT_INTERVAL * ZMQ_POLL_MSEC); if (rc == -1) break; // Interrupted // Process next input message, if any if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv (self->socket); if (!msg) break; // Interrupted if (self->verbose) { zclock_log ("I: received message:"); zmsg_dump (msg); } zframe_t *sender = zmsg_pop (msg); zframe_t *empty = zmsg_pop (msg); zframe_t *header = zmsg_pop (msg); if (zframe_streq (header, MDPC_CLIENT)) s_client_process (self, sender, msg); else if (zframe_streq (header, MDPW_WORKER)) s_worker_process (self, sender, msg); else { zclock_log ("E: invalid message:"); zmsg_dump (msg); zmsg_destroy (&msg); } zframe_destroy (&sender); zframe_destroy (&empty); zframe_destroy (&header); } // Disconnect and delete any expired workers // Send heartbeats to idle workers if needed if (zclock_time () > self->heartbeat_at) { s_broker_purge_workers (self); worker_t *worker = (worker_t *) zlist_first (self->waiting); while (worker) { s_worker_send (self, worker, MDPW_HEARTBEAT, NULL, NULL); worker = (worker_t *) zlist_next (self->waiting); } self->heartbeat_at = zclock_time () + HEARTBEAT_INTERVAL; } } if (zctx_interrupted) printf ("W: interrupt received, shutting down...\n"); s_broker_destroy (&self); return 0; }
int main (int argc, char *argv[]) { int verbose = (argc > 1 && streq(argv[1], "-v")); broker_t *self = s_broker_new(verbose); s_broker_bind(self, "tcp://127.0.0.1:5555"); while(true){ zmq_pollitem_t items [] = { {self->socket, 0, ZMQ_POLLIN, 0} }; int rc = zmq_poll(items, 1, HEARTBEAT_INTERVAL * ZMQ_POLL_MSEC); if (rc == -1) break; if (items[0].revents & ZMQ_POLLIN){ zmsg_t *msg = zmsg_recv(self->socket); if (!msg) break; if (self->verbose){ zclock_log("I: received message"); zmsg_dump(msg); } zframe_t *sender = zmsg_pop(msg); zframe_t *empty = zmsg_pop(msg); zframe_t *header = zmsg_pop(msg); if (zframe_streq(header, MDPC_CLIENT)) s_broker_client_msg(self, sender, msg); else if (zframe_streq(header, MDPW_WORKER)) s_broker_worker_msg(self, sender, msg); else{ zclock_log("E: invalid message:"); zmsg_dump(msg); zmsg_destroy(&msg); } zframe_destroy(&sender); zframe_destroy(&empty); zframe_destroy(&header); } if (zclock_time() > self->heartbeat_at){ s_broker_purge(self); worker_t *worker = (worker_t *)zlist_first(self->waiting); while (worker){ s_worker_send(worker, MDPW_HEARTBEAT, NULL, NULL); worker = (worker_t *)zlist_next(self->waiting); } self->heartbeat_at = zclock_time() + HEARTBEAT_INTERVAL; } } if (zctx_interrupted) printf("W: interrupt receivde, shutting down...\n"); s_broker_destroy(&self); return 0; }
int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); s_version_assert (2, 1); s_catch_signals (); broker_t *self = s_broker_new (verbose); s_broker_bind (self, "tcp://*:5555"); // Get and process messages forever or until interrupted while (!s_interrupted) { zmq_pollitem_t items [] = { { self->socket, 0, ZMQ_POLLIN, 0 } }; zmq_poll (items, 1, HEARTBEAT_INTERVAL * 1000); // Process next input message, if any if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv (self->socket); if (self->verbose) { s_console ("I: received message:"); zmsg_dump (msg); } char *sender = zmsg_pop (msg); char *empty = zmsg_pop (msg); char *header = zmsg_pop (msg); if (streq (header, MDPC_CLIENT)) s_client_process (self, sender, msg); else if (streq (header, MDPW_WORKER)) s_worker_process (self, sender, msg); else { s_console ("E: invalid message:"); zmsg_dump (msg); zmsg_destroy (&msg); } free (sender); free (empty); free (header); } // Disconnect and delete any expired workers // Send heartbeats to idle workers if needed if (s_clock () > self->heartbeat_at) { s_broker_purge_workers (self); worker_t *worker = zlist_first (self->waiting); while (worker) { s_worker_send (self, worker, MDPW_HEARTBEAT, NULL, NULL); worker = zlist_next (self->waiting); } self->heartbeat_at = s_clock () + HEARTBEAT_INTERVAL; } } if (s_interrupted) printf ("W: interrupt received, shutting down...\n"); s_broker_destroy (&self); return 0; }
static void s_worker_delete(worker_t *self, int disconnect) { assert(self); if (disconnect) s_worker_send(self, MDPW_DISCONNECT, NULL, NULL); if (self->service){ zlist_remove(self->service->waiting, self); self->service->workers--; } zlist_remove(self->broker->waiting, self); zhash_delete(self->broker->workers, self->id_string); }
static void s_worker_delete (worker_t *self, int disconnect) { assert (self); if (disconnect) s_worker_send (self, MDPW_DISCONNECT, NULL, NULL); if (self->service) { zlist_remove (self->service->waiting, self); self->service->workers--; } zlist_remove (self->broker->waiting, self); // This implicitly calls s_worker_destroy zhash_delete (self->broker->workers, self->identity); }
static void s_service_dispatch(service_t *self, zmsg_t *msg) { assert(self); if (msg) { // Queue message if any zlist_append(self->requests, msg); } s_broker_purge(self->broker); while (zlist_size(self->waiting) && zlist_size(self->requests)) { worker_t *worker = zlist_pop(self->waiting); zlist_remove(self->broker->waiting, worker); zmsg_t *msg = zlist_pop(self->requests); s_worker_send(worker, MDPW_REQUEST, msg); zmsg_destroy (&msg); } }
static void s_worker_delete(worker_t *self, int disconnect) { assert (self); if (disconnect) { s_worker_send (self, MDPW_DISCONNECT, NULL); // ! Расширить до отключение через mmi } if (self->service) { zlist_remove (self->service->waiting, self); self->service->workers--; } zlist_remove (self->broker->waiting, self); // This implicitly calls s_worker_destroy zhash_delete (self->broker->workers, self->id_string); }
// The dispatch method sends request to the worker. static void s_service_dispatch (service_t *self) { assert (self); s_broker_purge (self->broker); if (zlist_size (self->waiting) == 0) return; while (zlist_size (self->requests) > 0) { worker_t *worker = (worker_t*)zlist_pop (self->waiting); zlist_remove (self->waiting, worker); zmsg_t *msg = (zmsg_t*)zlist_pop (self->requests); s_worker_send (worker, MDPW_REQUEST, NULL, msg); // Workers are scheduled in the round-robin fashion zlist_append (self->waiting, worker); zmsg_destroy (&msg); } }
int main (int argc, char *argv []) { int verbose = 0; int daemonize = 0; for (int i = 1; i < argc; i++) { if (streq(argv[i], "-v")) verbose = 1; else if (streq(argv[i], "-d")) daemonize = 1; else if (streq(argv[i], "-h")) { printf("%s [-h] | [-d] [-v] [broker url]\n\t-h This help message\n\t-d Daemon mode.\n\t-v Verbose output\n\tbroker url defaults to tcp://*:5555\n", argv[0]); return -1; } } if (daemonize != 0) { daemon(0, 0); } broker_t *self = s_broker_new (verbose); /* did the user specify a bind address? */ if (argc > 1) { s_broker_bind (self, argv[argc-1]); printf("Bound to %s\n", argv[argc-1]); } else { /* default */ s_broker_bind (self, "tcp://*:5555"); printf("Bound to tcp://*:5555\n"); } // Get and process messages forever or until interrupted while (true) { zmq_pollitem_t items [] = { { self->socket, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 1, HEARTBEAT_INTERVAL * ZMQ_POLL_MSEC); if (rc == -1) break; // Interrupted // Process next input message, if any if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv (self->socket); if (!msg) break; // Interrupted if (self->verbose) { zclock_log ("I: received message:"); zmsg_dump (msg); } zframe_t *sender = zmsg_pop (msg); zframe_t *empty = zmsg_pop (msg); zframe_t *header = zmsg_pop (msg); if (zframe_streq (header, MDPC_CLIENT)) s_broker_client_msg (self, sender, msg); else if (zframe_streq (header, MDPW_WORKER)) s_broker_worker_msg (self, sender, msg); else { zclock_log ("E: invalid message:"); zmsg_dump (msg); zmsg_destroy (&msg); } zframe_destroy (&sender); zframe_destroy (&empty); zframe_destroy (&header); } // Disconnect and delete any expired workers // Send heartbeats to idle workers if needed if (zclock_time () > self->heartbeat_at) { s_broker_purge (self); worker_t *worker = (worker_t *) zlist_first (self->waiting); while (worker) { s_worker_send (worker, MDPW_HEARTBEAT, NULL, NULL); worker = (worker_t *) zlist_next (self->waiting); } self->heartbeat_at = zclock_time () + HEARTBEAT_INTERVAL; } } if (zctx_interrupted) printf ("W: interrupt received, shutting down...\n"); s_broker_destroy (&self); return 0; }
int main () { broker_t *self = s_broker_new(1); zsocket_bind(self, "tcp://*:5555"); while (true) { zmq_pollitem_t items[] = { { self->socket, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll(items, 1, HEARTBEAT_INTERVAL * ZMQ_POLL_MSEC); if (rc == -1) { break; } if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv(self->socket); if (!msg) { break; } if (self->verbose) { zclock_log("I: received message:"); zmsg_dump(msg); } zframe_t *sender = zmsg_pop(msg); zframe_t *empty = zmsg_pop(msg); zframe_t *header = zmsg_pop(msg); if (zframe_streq(header, MDPC_CLIENT)) { s_broker_client_msg(self, sender, msg); } else { if (zframe_streq(header, MDPW_WORKER)) { s_broker_worker_msg(self, sender, msg); } else { zclock_log("E: invalid message:"); zmsg_dump(msg); zmsg_destroy(&msg); } } zframe_destroy(&sender); zframe_destroy(&empty); zframe_destroy(&header); } // Disconnect and delete any expired workers // Send heartbeats to idle workers if needed // Зачем посылать им, пусть они нам присылают if (zclock_time() > self->heartbeat_at) { s_broker_purge(self); worker_t *worker = (worker_t *) zlist_first(self->waiting); while (worker) { s_worker_send(worker, MDPW_HEARTBEAT, NULL); worker = (worker_t *) zlist_next(self->waiting); } self->heartbeat_at = zclock_time () + HEARTBEAT_INTERVAL; } } if (zctx_interrupted) printf ("W: interrupt received, shutting down...\n"); zctx_destroy(&self->ctx); zhash_destroy(&self->services); zhash_destroy(&self->workers); zlist_destroy(&self->waiting); free(self); return 0; }