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_engine_handle_request (engine_t *self, zmsg_t *request, zframe_t *reply_to) { assert (zmsg_size (request) >= 3); zframe_t *operation = zmsg_pop (request); zframe_t *price = zmsg_pop (request); zframe_t *volume = zmsg_pop (request); if (zframe_streq (operation, "SELL")) s_engine_handle_sell_request (self, price, volume, reply_to); else if (zframe_streq (operation, "BUY")) s_engine_handle_buy_request (self, price, volume, reply_to); else { zclock_log ("E: invalid message: "); zmsg_dump (request); } zframe_destroy (&operation); zframe_destroy (&price); zframe_destroy (&volume); zmsg_destroy (&request); }
static void s_broker_worker_msg(broker_t *self, zframe_t *sender, zmsg_t *msg) { assert (zmsg_size(msg) >= 1); // At least, command zframe_t *command = zmsg_pop(msg); char *id_string = zframe_strhex(sender); int worker_ready = (zhash_lookup(self->workers, id_string) != NULL); free (id_string); worker_t *worker = s_worker_require(self, sender); if (zframe_streq(command, MDPW_READY)) { if (worker_ready) { // Not first command in session s_worker_delete(worker, 1); // Додумать, по идеи синоним сердцебиения } else { if (zframe_size(sender) >= 4 && memcmp(zframe_data (sender), "mmi.", 4) == 0) { s_worker_delete(worker, 1); // Додумать, по идеи синоним сердцебиения } else { // Attach worker to service and mark as idle zframe_t *service_frame = zmsg_pop(msg); worker->service = s_service_require(self, service_frame); worker->service->workers++; s_worker_waiting(worker); zframe_destroy(&service_frame); } } } else if (zframe_streq(command, MDPW_REPLY)) { if (worker_ready) { // Remove and save client return envelope and insert the // protocol header and service name, then rewrap envelope. zframe_t *client = zmsg_unwrap(msg); zmsg_pushstr(msg, worker->service->name); zmsg_pushstr(msg, MDPC_CLIENT); zmsg_wrap(msg, client); zmsg_send(&msg, self->socket); s_worker_waiting(worker); } else { // Просто обрыв связи между воркером и брокером // синоним сердцебиения s_worker_delete(worker, 1); } } else if (zframe_streq(command, MDPW_HEARTBEAT)) { if (worker_ready) { worker->expiry = zclock_time () + HEARTBEAT_EXPIRY; } else { // Просто обрыв связи между воркером и брокером // синоним сердцебиения s_worker_delete(worker, 1); } } else if (zframe_streq (command, MDPW_DISCONNECT)) { s_worker_delete(worker, 0); } else { zclock_log ("E: invalid input message"); zmsg_dump (msg); } free (command); zmsg_destroy (&msg); }
static void s_worker_process (broker_t *self, zframe_t *sender, zmsg_t *msg) { assert (zmsg_size (msg) >= 1); // At least, command zframe_t *command = zmsg_pop (msg); char *identity = zframe_strhex (sender); int worker_ready = (zhash_lookup (self->workers, identity) != NULL); free (identity); worker_t *worker = s_worker_require (self, sender); if (zframe_streq (command, MDPW_READY)) { if (worker_ready) // Not first command in session s_worker_delete (self, worker, 1); else if (zframe_size (sender) >= 4 // Reserved service name && memcmp (zframe_data (sender), "mmi.", 4) == 0) s_worker_delete (self, worker, 1); else { // Attach worker to service and mark as idle zframe_t *service_frame = zmsg_pop (msg); worker->service = s_service_require (self, service_frame); worker->service->workers++; s_worker_waiting (self, worker); zframe_destroy (&service_frame); } } else if (zframe_streq (command, MDPW_REPLY)) { if (worker_ready) { // Remove & save client return envelope and insert the // protocol header and service name, then rewrap envelope. zframe_t *client = zmsg_unwrap (msg); zmsg_pushstr (msg, worker->service->name); zmsg_pushstr (msg, MDPC_CLIENT); zmsg_wrap (msg, client); zmsg_send (&msg, self->socket); s_worker_waiting (self, worker); } else s_worker_delete (self, worker, 1); } else if (zframe_streq (command, MDPW_HEARTBEAT)) { if (worker_ready) worker->expiry = zclock_time () + HEARTBEAT_EXPIRY; else s_worker_delete (self, worker, 1); } else if (zframe_streq (command, MDPW_DISCONNECT)) s_worker_delete (self, worker, 0); else { zclock_log ("E: invalid input message"); zmsg_dump (msg); } free (command); zmsg_destroy (&msg); }
int main (int argc, char *argv []) { int verbose = (argc > 1 && streq (argv [1], "-v")); zctx_t *ctx = zctx_new (); // Prepare server socket with predictable identity char *bind_endpoint = "tcp://*:5555"; char *connect_endpoint = "tcp://localhost:5555"; void *server = zsocket_new (ctx, ZMQ_ROUTER); zmq_setsockopt (server, ZMQ_IDENTITY, connect_endpoint, strlen (connect_endpoint)); zsocket_bind (server, bind_endpoint); printf ("I: service is ready at %s\n", bind_endpoint); while (!zctx_interrupted) { zmsg_t *request = zmsg_recv (server); if (verbose && request) zmsg_dump (request); if (!request) break; // Interrupted // Frame 0: identity of client // Frame 1: PING, or client control frame // Frame 2: request body zframe_t *identity = zmsg_pop (request); zframe_t *control = zmsg_pop (request); zmsg_t *reply = zmsg_new (); if (zframe_streq (control, "PING")) zmsg_addstr (reply, "PONG"); else { zmsg_add (reply, control); zmsg_addstr (reply, "OK"); } zmsg_destroy (&request); zmsg_push (reply, identity); if (verbose && reply) zmsg_dump (reply); zmsg_send (&reply, server); } if (zctx_interrupted) printf ("W: interrupted\n"); zctx_destroy (&ctx); return 0; }
static void s_worker_process (broker_t *self, char *sender, zmsg_t *msg) { assert (zmsg_parts (msg) >= 1); // At least, command char *command = zmsg_pop (msg); int worker_ready = (zhash_lookup (self->workers, sender) != NULL); worker_t *worker = s_worker_require (self, sender); if (streq (command, MDPW_READY)) { if (worker_ready) // Not first command in session s_worker_delete (self, worker, 1); else if (strlen (sender) >= 4 // Reserved service name && memcmp (sender, "mmi.", 4) == 0) s_worker_delete (self, worker, 1); else { // Attach worker to service and mark as idle char *service_name = zmsg_pop (msg); worker->service = s_service_require (self, service_name); worker->service->workers++; s_worker_waiting (self, worker); free (service_name); } } else if (streq (command, MDPW_REPLY)) { if (worker_ready) { // Remove & save client return envelope and insert the // protocol header and service name, then rewrap envelope. char *client = zmsg_unwrap (msg); zmsg_wrap (msg, MDPC_CLIENT, worker->service->name); zmsg_wrap (msg, client, ""); free (client); zmsg_send (&msg, self->socket); s_worker_waiting (self, worker); } else s_worker_delete (self, worker, 1); } else if (streq (command, MDPW_HEARTBEAT)) { if (worker_ready) worker->expiry = s_clock () + HEARTBEAT_EXPIRY; else s_worker_delete (self, worker, 1); } else if (streq (command, MDPW_DISCONNECT)) s_worker_delete (self, worker, 0); else { s_console ("E: invalid input message (%d)", (int) *command); zmsg_dump (msg); } free (command); zmsg_destroy (&msg); }
zmsg_t * mdwrk_recv (mdwrk_t *self, zmsg_t *reply) { // Format and send the reply if we were provided one assert (reply || !self->expect_reply); if (reply) { zmsg_t *msg = zmsg_dup (reply); zmsg_push (msg, MDPS_REPLY); zmsg_push (msg, MDPS_HEADER); zmsg_send (&msg, self->worker); } self->expect_reply = 1; while (1) { zmq_pollitem_t items [] = { { self->worker, 0, ZMQ_POLLIN, 0 } }; zmq_poll (items, 1, HEARTBEAT_INTERVAL * 1000); if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv (self->worker); self->liveness = HEARTBEAT_LIVENESS; // Don't try to handle errors, just assert noisily assert (zmsg_parts (msg) >= 3); char *header = zmsg_pop (msg); assert (strcmp (header, MDPS_HEADER) == 0); free (header); char *command = zmsg_pop (msg); if (strcmp (command, MDPS_REQUEST) == 0) return msg; // We have a request to process else if (strcmp (command, MDPS_HEARTBEAT) == 0) ; // Do nothing for heartbeats else if (strcmp (command, MDPS_DISCONNECT) == 0) break; // Return empty handed else { printf ("E: invalid input message (%d)\n", (int) command [1]); zmsg_dump (msg); } free (command); } else if (--self->liveness == 0) { s_sleep (RECONNECT_INTERVAL); s_connect_to_broker (self); } // Send HEARTBEAT if it's time if (s_clock () > self->heartbeat_at) { self->heartbeat_at = s_clock () + HEARTBEAT_INTERVAL; s_send (self->worker, "HEARTBEAT"); } } // We exit if we've been disconnected return NULL; }
static void client_execute (client_t *self, int event) { self->next_event = event; while (self->next_event) { self->event = self->next_event; self->next_event = 0; printf ("State=%s, event=%s\n", s_state_name [self->state], s_event_name [self->event]); switch (self->state) { case start_state: if (self->event == ohai_event) { check_credentials_action (self); self->state = authenticated_state; } break; case authenticated_state: if (self->event == ok_event) { zmsg_addstr (self->reply, "OHAI-OK"); self->state = ready_state; } else if (self->event == error_event) { zmsg_addstr (self->reply, "WTF"); self->state = start_state; } break; case ready_state: if (self->event == icanhaz_event) { zmsg_addstr (self->reply, "CHEEZBURGER"); } else if (self->event == hugz_event) { zmsg_addstr (self->reply, "HUGZ-OK"); } else if (self->event == heartbeat_event) { zmsg_addstr (self->reply, "HUGZ"); } break; case stopped_state: // Discard all events silently break; } if (zmsg_size (self->reply) > 1) { puts ("Send message to client"); zmsg_dump (self->reply); zmsg_send (&self->reply, self->router); self->reply = zmsg_new (); zmsg_add (self->reply, zframe_dup (self->address)); } } }
static void s_broker_worker_msg(broker_t *self, zframe_t *sender, zmsg_t *msg) { assert(zmsg_size(msg) >= 1); zframe_t *command = zmsg_pop(msg); char *id_string = zframe_strhex(sender); int worker_ready = (zhash_lookup(self->workers, id_string) != NULL); free(id_string); worker_t *worker = s_worker_require(self, sender); if (zframe_streq(command, MDPW_READY)){ if (worker_ready) s_worker_delete(worker, 1); else if (zframe_size(sender) >= 4 && memcmp(zframe_data(sender), "mmi.", 4) == 0) s_worker_delete(worker, 1); else { zframe_t *service_frame = zmsg_pop(msg); worker->service = s_service_require(self, service_frame); worker->service->workers++; s_worker_waiting(worker); zframe_destroy(&service_frame); } } else if (zframe_streq(command, MDPW_REPLY)){ if (worker_ready){ zframe_t *client = zmsg_unwrap(msg); zmsg_pushstr(msg, worker->service->name); zmsg_pushstr(msg, MDPC_CLIENT); zmsg_wrap(msg, client); zmsg_send(&msg, self->socket); s_worker_waiting(worker); } else s_worker_delete(worker, 1); } else if (zframe_streq(command, MDPW_HEARTBEAT)){ if (worker_ready) worker->expiry = zclock_time() + HEARTBEAT_EXPIRY; else s_worker_delete(worker, 1); } else if (zframe_streq(command, MDPW_DISCONNECT)) s_worker_delete(worker, 0); else { zclock_log("E: invalid input message"); zmsg_dump(msg); } free(command); zmsg_destroy(&msg); }
int main (void) { interface_t *interface = interface_new (); while (true) { zmsg_t *msg = interface_recv (interface); if (!msg) break; // Interrupted zmsg_dump (msg); } interface_destroy (&interface); return 0; }
int main (int argc, char *argv []) { zre_interface_t *interface = zre_interface_new (); while (true) { zmsg_t *incoming = zre_interface_recv (interface); if (!incoming) break; zmsg_dump (incoming); zmsg_destroy (&incoming); } zre_interface_destroy (&interface); return 0; }
static void handle_frontend (GPPWorker *self) { GPPWorkerPrivate *priv = GET_PRIV (self); GPPWorkerClass *klass = GPP_WORKER_GET_CLASS (self); zmsg_t *msg = zmsg_recv (priv->frontend); if (!msg) return; if (zmsg_size (msg) == 3) { char *request = zframe_strdup (zmsg_last (msg)); g_info ("I: normal reply\n"); priv->liveness = HEARTBEAT_LIVENESS; priv->current_task = msg; if (!klass->handle_request (self, request)) gpp_worker_set_task_done (self, NULL, FALSE); free (request); } else { if (zmsg_size (msg) == 1) { zframe_t *frame = zmsg_first (msg); if (memcmp (zframe_data (frame), PPP_HEARTBEAT, 1) == 0) { priv->liveness = HEARTBEAT_LIVENESS; g_debug ("got heartbeat from queue !\n"); } else { g_warning ("E: invalid message\n"); zmsg_dump (msg); } zmsg_destroy (&msg); } else { g_warning ("E: invalid message\n"); zmsg_dump (msg); } } priv->interval = INTERVAL_INIT; }
static void s_worker_send(worker_t *self, char *command, zmsg_t *msg) { msg = (msg ? zmsg_dup(msg): zmsg_new()); zmsg_pushstr(msg, command); zmsg_pushstr(msg, MDPW_WORKER); // Stack routing envelope to start of message zmsg_wrap(msg, zframe_dup(self->identity)); if (self->broker->verbose) { zclock_log ("I: sending %s to worker", mdps_commands [(int) *command]); zmsg_dump(msg); } zmsg_send(&msg, self->broker->socket); }
zmsg_t * mdp_client_recv (mdp_client_t *self, char **command_p, char **service_p) { assert (self); zmsg_t *msg = zmsg_recv (self->client); if (msg == NULL) // Interrupt return NULL; if (self->verbose) { zclock_log ("I: received reply:"); zmsg_dump (msg); } // Message format: // Frame 1: empty frame (delimiter) // Frame 2: "MDPCxy" (six bytes, MDP/Client x.y) // Frame 3: REPORT|NAK // Frame 4: Service name (printable string) // Frame 5..n: Application frames // We would handle malformed replies better in real code assert (zmsg_size (msg) >= 5); zframe_t *empty = zmsg_pop (msg); assert (zframe_streq (empty, "")); zframe_destroy (&empty); zframe_t *header = zmsg_pop (msg); assert (zframe_streq (header, MDPC_CLIENT)); zframe_destroy (&header); zframe_t *command = zmsg_pop (msg); assert (zframe_streq (command, MDPC_REPORT) || zframe_streq (command, MDPC_NAK)); if (command_p) *command_p = zframe_strdup (command); zframe_destroy (&command); zframe_t *service = zmsg_pop (msg); if (service_p) *service_p = zframe_strdup (service); zframe_destroy (&service); return msg; // Success }
int main (void) { zctx_t *ctx = zctx_new (); void *subscriber = zsocket_new (ctx, ZMQ_SUB); zsocket_connect (subscriber, "tcp://localhost:30001"); zsocket_set_subscribe (subscriber, ""); while (true) { zmsg_t *msg = zmsg_recv (subscriber); if (!msg) break; zmsg_dump (msg); zmsg_destroy (&msg); } zctx_destroy (&ctx); return 0; }
zlist_t * fetch_rules(void * sock, char * channel) { zlist_t * rules = zlist_new(); /* if(!endpoint) { zclock_log("W: no reup endpoint defined, won't try te refresh rules"); return rules; }*/ // zsocket_connect(sock, endpoint); zstr_sendm(sock, ""); zstr_send(sock, channel); zmsg_t * tmp; // while(tmp=zmsg_recv(sock)) { // zmsg_dump(tmp); //} // exit(1); zmsg_t * msg = zmsg_recv(sock); kill_envelope(msg); char * status = zmsg_popstr(msg); if(strcmp("200", status) != 0) { zclock_log("W: reloading rules for %s failed: got |%s|", channel, status); return rules; } free(status); zmsg_destroy(&msg); while((msg=zmsg_recv(sock))) { zclock_log("once"); kill_envelope(msg); zmsg_dump(msg); zframe_t * header = zmsg_pop(msg); if(zframe_streq(header, "")) { // we're done zclock_log("got a null header, we're out"); zframe_destroy(&header); zmsg_destroy(&msg); break; } zmsg_push(msg, header); zlist_push(rules, msg); } return rules; }
static void s_worker_send(worker_t *self, char *command, char *option, zmsg_t *msg) { msg = msg ? zmsg_dup(msg) : zmsg_new(); if (option) zmsg_pushstr(msg, option); zmsg_pushstr(msg, command); zmsg_pushstr(msg, MDPW_WORKER); zmsg_wrap(msg, zframe_dup(self->identity)); if (self->broker->verbose){ zclock_log("I: sending %s to worker", mdps_commands[(int) *command]); zmsg_dump(msg); } zmsg_send(&msg, self->broker->socket); }
int main (int argc, char *argv []) { if (argc < 2) { puts ("Syntax: sender filename virtualname"); return 0; } printf ("Publishing %s as %s\n", argv [1], argv [2]); zre_node_t *node = zre_node_new (); zre_node_publish (node, argv [1], argv [2]); while (true) { zmsg_t *incoming = zre_node_recv (node); if (!incoming) break; zmsg_dump (incoming); zmsg_destroy (&incoming); } zre_node_destroy (&node); return 0; }
int recvHandler(zloop_t *loop, zmq_pollitem_t *item, void *client) { zmsg_t *msg = zmsg_recv(client); zmsg_dump(msg); zstr_sendm(client, zmsg_popstr(msg)); zmsg_destroy(&msg); received++; zstr_sendm(client, ""); zstr_sendf(client, "pong:%d", ++sent); if (sent >= count) { return -1; } return 0; }
zmsg_t * mdcli_recv (mdcli_t *self) { assert (self); // Poll socket for a reply, with timeout zmq_pollitem_t items [] = { { self->client, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 1, self->timeout * ZMQ_POLL_MSEC); if (rc == -1) return NULL; // Interrupted // If we got a reply, process it if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv (self->client); if (self->verbose) { zclock_log ("I: received reply:"); zmsg_dump (msg); } // Don't try to handle errors, just assert noisily assert (zmsg_size (msg) >= 4); zframe_t *empty = zmsg_pop (msg); assert (zframe_streq (empty, "")); zframe_destroy (&empty); zframe_t *header = zmsg_pop (msg); assert (zframe_streq (header, MDPC_CLIENT)); zframe_destroy (&header); zframe_t *service = zmsg_pop (msg); zframe_destroy (&service); return msg; // Success } if (zctx_interrupted) printf ("W: interrupt received, killing client...\n"); else if (self->verbose) zclock_log ("W: permanent error, abandoning request"); return NULL; }
static void s_mdp_worker_send_to_broker (mdp_worker_t *self, char *command, char *option, zmsg_t *msg) { msg = msg? zmsg_dup (msg): zmsg_new (); // Stack protocol envelope to start of message if (option) zmsg_pushstr (msg, option); zmsg_pushstr (msg, command); zmsg_pushstr (msg, MDPW_WORKER); zmsg_pushstr (msg, ""); if (self->verbose) { zclock_log ("I: sending %s to broker", mdpw_commands [(int) *command]); zmsg_dump (msg); } zmsg_send (&msg, self->worker); }
void mdp_client_send (mdp_client_t *self, char *service, zmsg_t **request_p) { assert (self); assert (request_p); zmsg_t *request = *request_p; // Prefix request with protocol frames // Frame 1: empty frame (delimiter) // Frame 2: "MDPCxy" (six bytes, MDP/Client x.y) // Frame 3: Service name (printable string) zmsg_pushstr (request, service); zmsg_pushstr (request, MDPC_CLIENT); zmsg_pushstr (request, ""); if (self->verbose) { zclock_log ("I: send request to '%s' service:", service); zmsg_dump (request); } zmsg_send (request_p, self->client); }
int main (void) { zctx_t *ctx = zctx_new (); void *frontend = zsocket_new (ctx, ZMQ_DEALER); zsocket_bind (frontend, "tcp://*:30000"); void *backend = zsocket_new (ctx, ZMQ_PUB); zsocket_bind (backend, "tcp://*:30001"); while (true) { zmsg_t *msg = zmsg_recv (frontend); if (!msg) break; zmsg_dump (msg); zmsg_send (&msg, backend); } zctx_destroy (&ctx); return 0; }
zmsg_t * mdcli_recv (mdcli_t *self) { assert (self); // Poll socket for a reply, with timeout zmq_pollitem_t items [] = { { self->client, 0, ZMQ_POLLIN, 0 } }; zmq_poll (items, 1, self->timeout * 1000); // If we got a reply, process it if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv (self->client); if (self->verbose) { s_console ("I: received reply:"); zmsg_dump (msg); } // Don't try to handle errors, just assert noisily assert (zmsg_parts (msg) >= 4); char *empty = zmsg_pop (msg); assert (streq (empty, "")); free (empty); char *header = zmsg_pop (msg); assert (streq (header, MDPC_CLIENT)); free (header); char *service = zmsg_pop (msg); assert (streq (service, service)); free (service); return msg; // Success } if (s_interrupted) printf ("W: interrupt received, killing client...\n"); else if (self->verbose) s_console ("W: permanent error, abandoning request"); return NULL; }
int _tmain(int argc, _TCHAR* argv[]) { zclock_sleep(20000); char* broker_loc = "tcp://localhost:5555"; zctx_t* ctx = zctx_new(); void* scket = zsocket_new(ctx,ZMQ_REQ); zsocket_connect(scket,broker_loc); zmsg_t* msg = zmsg_new(); zmsg_addstr(msg,TWRK_CLI_VER); zmsg_addstr(msg,"Echo"); zmsg_addstr(msg,TMSG_TYPE_REQUEST); int64_t sleep = 10*1000; zclock_sleep(sleep); zmsg_add(msg,zframe_new(&sleep,sizeof(sleep))); zmsg_send(&msg,scket); zmsg_t* reply = zmsg_recv(scket); zmsg_dump(reply); return 0; }
int mdcli_send (mdcli_t *self, char *service, zmsg_t **request_p) { assert (self); assert (request_p); zmsg_t *request = *request_p; // Prefix request with protocol frames // Frame 0: empty (REQ emulation) // Frame 1: "MDPCxy" (six bytes, MDP/Client x.y) // Frame 2: Service name (printable string) zmsg_push (request, service); zmsg_push (request, MDPC_CLIENT); zmsg_push (request, ""); if (self->verbose) { s_console ("I: send request to '%s' service:", service); zmsg_dump (request); } zmsg_send (&request, self->client); return 0; }
static void s_worker_send (broker_t *self, worker_t *worker, char *command, char *option, zmsg_t *msg) { msg = msg? zmsg_dup (msg): zmsg_new (); // Stack protocol envelope to start of message if (option) zmsg_pushstr (msg, option); zmsg_pushstr (msg, command); zmsg_pushstr (msg, MDPW_WORKER); // Stack routing envelope to start of message zmsg_wrap (msg, zframe_dup (worker->address)); if (self->verbose) { zclock_log ("I: sending %s to worker", mdps_commands [(int) *command]); zmsg_dump (msg); } zmsg_send (&msg, self->socket); }
static void chat_task (void *args, zctx_t *ctx, void *pipe) { zyre_t *node = zyre_new (ctx); zyre_start (node); zyre_join (node, "CHAT"); zmq_pollitem_t items [] = { { pipe, 0, ZMQ_POLLIN, 0 }, { zyre_socket (node), 0, ZMQ_POLLIN, 0 } }; while (true) { if (zmq_poll (items, 2, -1) == -1) break; // Interrupted // Activity on my pipe if (items [0].revents & ZMQ_POLLIN) { zmsg_t *msg = zmsg_recv (pipe); zyre_shout (node, "CHAT", &msg); } // Activity on my node handle if (items [1].revents & ZMQ_POLLIN) { zmsg_t *msg = zyre_recv (node); zmsg_dump (msg); char *command = zmsg_popstr (msg); if (streq (command, "SHOUT")) { // Discard sender and group name free (zmsg_popstr (msg)); free (zmsg_popstr (msg)); char *message = zmsg_popstr (msg); printf ("%s", message); free (message); } free (command); zmsg_destroy (&msg); } } zyre_destroy (&node); }