static void dispatch_the_service (client_t *self) { service_t *service = s_service_require (self, mlm_proto_address (self->message)); assert (service); s_service_dispatch (service); }
static void handle_ready (client_t *self) { mdp_msg_t *msg = self->message; const char *service_name = mdp_msg_service(msg); // zsys_debug("handle_ready: service=%s\n", service_name); zframe_t *routing_id = mdp_msg_routing_id(msg); assert(routing_id); char *identity = zframe_strhex(routing_id); int worker_ready = (zhash_lookup(self->server->workers, identity) != NULL); free(identity); worker_t *worker = s_worker_require(self->server, routing_id); if (worker_ready) // Not first command in session. s_worker_delete(worker, 1); else { service_t *service = s_service_require(self->server, service_name); worker->service = service; zlist_append(service->broker->waiting, worker); zlist_append(service->waiting, worker); worker->service->workers++; s_service_dispatch(service); } }
static void s_broker_client_msg(broker_t *self, zframe_t *sender, zmsg_t *msg) { assert(zmsg_size(msg) >= 2); zframe_t *service_frame = zmsg_pop(msg); service_t *service = s_service_require(self, service_frame); zmsg_wrap(msg, zframe_dup(sender)); if (zframe_size(service_frame) >= 4 && memcmp(zframe_data(service_frame), "mmi.", 4) == 0){ char *return_code; if (zframe_streq(service_frame, "mmi.service")){ char *name = zframe_strdup(zmsg_last(msg)); service_t *service = (service_t *)zhash_lookup(self->services, name); return_code = service && service->workers ? "200" : "404"; free(name); } else return_code = "501"; zframe_reset(zmsg_last(msg), return_code, strlen(return_code)); zframe_t *client = zmsg_unwrap(msg); zmsg_prepend(msg, &service_frame); zmsg_pushstr(msg, MDPC_CLIENT); zmsg_wrap(msg, client); zmsg_send(&msg, self->socket); } else s_service_dispatch(service, msg); zframe_destroy(&service_frame); }
static void handle_request (client_t *self) { const char *service_name = mdp_msg_service(self->message); if(strstr(service_name, "mmi.")) { handle_mmi(self, service_name); return; } // Create a fresh instance of mdp_msg_t to append to the list of requests. mdp_msg_t *msg = mdp_msg_new(); // routing id, messageid, service, body mdp_msg_set_routing_id(msg, mdp_msg_routing_id(self->message)); mdp_msg_set_id(msg, mdp_msg_id(self->message)); mdp_msg_set_service(msg, service_name); zmsg_t *body = mdp_msg_get_body(self->message); mdp_msg_set_body(msg, &body); service_t *service = s_service_require(self->server, service_name); zlist_append(service->requests, msg); s_service_dispatch(service); }
static void s_worker_waiting(worker_t *self) { assert(self->broker); zlist_append(self->broker->waiting, self); zlist_append(self->service->waiting, self); self->expiry = zclock_time() + HEARTBEAT_EXPIRY; s_service_dispatch(self->service, NULL); }
static void s_worker_waiting (broker_t *self, worker_t *worker) { // Queue to broker and service waiting lists zlist_append (self->waiting, worker); zlist_append (worker->service->waiting, worker); worker->expiry = zclock_time () + HEARTBEAT_EXPIRY; s_service_dispatch (self, worker->service, NULL); }
static void s_broker_client_msg(broker_t *self, zframe_t *sender, zmsg_t *msg) { assert (zmsg_size(msg) >= 2); // Service name + body zframe_t *service_frame = zmsg_pop(msg); service_t *service = s_service_require(self, service_frame); // Не должен создавать сервис, в случаи запроса от клиента // Set reply return identity to client sender zmsg_wrap(msg, zframe_dup(sender)); // If we got a MMI service request, process that internally if (zframe_size(service_frame) >= 4 && memcmp(zframe_data(service_frame), "mmi.", 4) == 0) { char *return_code; if (zframe_streq(service_frame, "mmi.service")) { char *name = zframe_strdup (zmsg_last (msg)); service_t *service = (service_t *) zhash_lookup(self->services, name); if (service) { if (service->workers) { return_code = "200"; } else { return_code = "404"; } } else { return_code = "401"; } free(name); } else { return_code = "501"; } zframe_reset(zmsg_last(msg), return_code, strlen(return_code)); // Remove & save client return envelope and insert the // protocol header and service name, then rewrap envelope. zframe_t *client = zmsg_unwrap (msg); zmsg_push(msg, zframe_dup(service_frame)); zmsg_pushstr(msg, MDPC_CLIENT); zmsg_wrap(msg, client); zmsg_send(&msg, self->socket); } else { // Else dispatch the message to the requested service s_service_dispatch(service, msg); } zframe_destroy(&service_frame); }
static void write_message_to_service (client_t *self) { mlm_msg_t *msg = mlm_msg_new ( self->address, mlm_proto_address (self->message), mlm_proto_subject (self->message), mlm_proto_tracker (self->message), mlm_proto_timeout (self->message), mlm_proto_get_content (self->message)); service_t *service = s_service_require (self, mlm_proto_address (self->message)); assert (service); zlistx_add_end (service->queue, msg); s_service_dispatch (service); }
static void s_client_process (broker_t *self, char *sender, zmsg_t *msg) { assert (zmsg_parts (msg) >= 2); // Service name + body char *service_name = zmsg_pop (msg); service_t *service = s_service_require (self, service_name); // Set reply return address to client sender zmsg_wrap (msg, sender, ""); if (strlen (service_name) >= 4 && memcmp (service_name, "mmi.", 4) == 0) s_service_internal (self, service_name, msg); else s_service_dispatch (self, service, msg); free (service_name); }
static void s_client_process (broker_t *self, zframe_t *sender, zmsg_t *msg) { assert (zmsg_size (msg) >= 2); // Service name + body zframe_t *service_frame = zmsg_pop (msg); service_t *service = s_service_require (self, service_frame); // Set reply return address to client sender zmsg_wrap (msg, zframe_dup (sender)); if (zframe_size (service_frame) >= 4 && memcmp (zframe_data (service_frame), "mmi.", 4) == 0) s_service_internal (self, service_frame, msg); else s_service_dispatch (self, service, msg); zframe_destroy (&service_frame); }
static void s_broker_client_msg (broker_t *self, zframe_t *sender, zmsg_t *msg) { assert (zmsg_size (msg) >= 2); // Service name + body zframe_t *service_frame = zmsg_pop (msg); service_t *service = s_service_require (self, service_frame); // If we got a MMI service request, process that internally if (zframe_size (service_frame) >= 4 && memcmp (zframe_data (service_frame), "mmi.", 4) == 0) { char *return_code; if (zframe_streq (service_frame, "mmi.service")) { char *name = zframe_strdup (zmsg_last (msg)); service_t *service = (service_t *) zhash_lookup (self->services, name); return_code = service && service->workers? "200": "404"; free (name); } else // The filter service that can be used to manipulate // the command filter table. if (zframe_streq (service_frame, "mmi.filter") && zmsg_size (msg) == 3) { zframe_t *operation = zmsg_pop (msg); zframe_t *service_frame = zmsg_pop (msg); zframe_t *command_frame = zmsg_pop (msg); char *command_str = zframe_strdup (command_frame); if (zframe_streq (operation, "enable")) { service_t *service = s_service_require (self, service_frame); s_service_enable_command (service, command_str); return_code = "200"; } else if (zframe_streq (operation, "disable")) { service_t *service = s_service_require (self, service_frame); s_service_disable_command (service, command_str); return_code = "200"; } else return_code = "400"; zframe_destroy (&operation); zframe_destroy (&service_frame); zframe_destroy (&command_frame); free (command_str); // Add an empty frame; it will be replaced by the return code. zmsg_pushstr (msg, ""); } else return_code = "501"; zframe_reset (zmsg_last (msg), return_code, strlen (return_code)); // Insert the protocol header and service name, then rewrap envelope. zmsg_push (msg, zframe_dup (service_frame)); zmsg_pushstr (msg, MDPC_REPORT); zmsg_pushstr (msg, MDPC_CLIENT); zmsg_wrap (msg, zframe_dup (sender)); zmsg_send (&msg, self->socket); } else { int enabled = 1; if (zmsg_size (msg) >= 1) { zframe_t *cmd_frame = zmsg_first (msg); char *cmd = zframe_strdup (cmd_frame); enabled = s_service_is_command_enabled (service, cmd); free (cmd); } // Forward the message to the worker. if (enabled) { zmsg_wrap (msg, zframe_dup (sender)); zlist_append (service->requests, msg); s_service_dispatch (service); } // Send a NAK message back to the client. else { zmsg_push (msg, zframe_dup (service_frame)); zmsg_pushstr (msg, MDPC_NAK); zmsg_pushstr (msg, MDPC_CLIENT); zmsg_wrap (msg, zframe_dup (sender)); zmsg_send (&msg, self->socket); } } zframe_destroy (&service_frame); }
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 *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 (worker, 1); else if (zframe_size (sender) >= 4 // Reserved service name && 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); zlist_append (self->waiting, worker); zlist_append (worker->service->waiting, worker); worker->service->workers++; worker->expiry = zclock_time () + HEARTBEAT_EXPIRY; s_service_dispatch (worker->service); zframe_destroy (&service_frame); zclock_log ("worker created"); } } else if (zframe_streq (command, MDPW_REPORT)) { 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_REPORT); zmsg_pushstr (msg, MDPC_CLIENT); zmsg_wrap (msg, client); zmsg_send (&msg, self->socket); } else s_worker_delete (worker, 1); } else if (zframe_streq (command, MDPW_HEARTBEAT)) { if (worker_ready) { if (zlist_size (self->waiting) > 1) { // Move worker to the end of the waiting queue, // so s_broker_purge will only check old worker(s) zlist_remove (self->waiting, worker); zlist_append (self->waiting, worker); } 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); } zframe_destroy (&command); zmsg_destroy (&msg); }