static int zyre_handler (zloop_t *loop, zsock_t *reader, void *argument) { server_t *self = (server_t *) argument; zmsg_t *msg = zyre_recv (self->zyre); if (!msg) return -1; // Interrupted char *command = zmsg_popstr (msg); char *peer_id = zmsg_popstr (msg); char *peer_name = zmsg_popstr (msg); if (streq (command, "ENTER")) zsys_info ("ZPIPES server appeared at %s", peer_name); else if (streq (command, "EXIT")) zsys_info ("ZPIPES server vanished from %s", peer_name); else if (streq (command, "SHOUT")) { char *group = zmsg_popstr (msg); if (streq (group, "ZPIPES")) server_process_cluster_command (self, peer_id, peer_name, msg, false); zstr_free (&group); } else if (streq (command, "WHISPER")) server_process_cluster_command (self, peer_id, peer_name, msg, true); zstr_free (&command); zstr_free (&peer_id); zstr_free (&peer_name); zmsg_destroy (&msg); return 0; }
static bool s_node_recv (zre_node_t *node, char* command, char* expected) { bool result = false; zmsg_t *incoming = zre_node_recv (node); assert (incoming); char *event = zmsg_popstr (incoming); if (streq (event, command)) { char *peer = zmsg_popstr (incoming); char *group = NULL; if (streq (command, "SHOUT")) group = zmsg_popstr (incoming); char *cookie = zmsg_popstr (incoming); if (streq (cookie, expected)) { result = true; } free (peer); if (group) free (group); free (cookie); } free (event); zmsg_destroy (&incoming); return result; }
/** * * @param foundId * @param foundReply * @return */ bool BoomStick::ReadFromReadySocket(std::string& foundId, std::string& foundReply) { if (0 == mUtilizedThread) { mUtilizedThread = pthread_self(); } else { CHECK(pthread_self() == mUtilizedThread); } if (!mChamber) { LOG(WARNING) << "Invalid socket"; return false; } bool success = false; zmsg_t* msg = zmsg_recv(mChamber); if (!msg) { foundReply = zmq_strerror(zmq_errno()); } else if (zmsg_size(msg) == 2) { char* msgChar; msgChar = zmsg_popstr(msg); foundId = msgChar; free(msgChar); msgChar = zmsg_popstr(msg); foundReply = msgChar; free(msgChar); success = true; } else { foundReply = "Malformed reply, expecting 2 parts"; } if (msg) { zmsg_destroy(&msg); } return success; }
static zmsg_t * server_method (server_t *self, const char *method, zmsg_t *msg) { // Connect to a remote zmsg_t *reply = NULL; if (streq (method, "CONNECT")) { char *endpoint = zmsg_popstr (msg); assert (endpoint); server_connect (self, endpoint); zstr_free (&endpoint); } else if (streq (method, "PUBLISH")) { char *key = zmsg_popstr (msg); char *value = zmsg_popstr (msg); server_accept (self, key, value); zstr_free (&key); zstr_free (&value); } else if (streq (method, "STATUS")) { // Return number of tuples we have stored reply = zmsg_new (); assert (reply); zmsg_addstr (reply, "STATUS"); zmsg_addstrf (reply, "%d", (int) zhashx_size (self->tuples)); } else zsys_error ("unknown zgossip method '%s'", method); return reply; }
static int s_recv_from_zyre (s_agent_t *self) { zyre_event_t *event = zyre_event_new (self->zyre); if (zyre_event_type (event) == ZYRE_EVENT_SHOUT && streq (zyre_event_group (event), "DROPS")) { zmsg_t *msg = zyre_event_msg (event); char *operation = zmsg_popstr (msg); if (streq (operation, "CREATE")) { char *filename = zmsg_popstr (msg); zframe_t *frame = zmsg_pop (msg); zfile_t *file = zfile_new (self->path, filename); zfile_output (file); fwrite (zframe_data (frame), 1, zframe_size (frame), zfile_handle (file)); zfile_destroy (&file); zframe_destroy (&frame); zstr_send (self->pipe, filename); free (filename); } free (operation); } zyre_event_destroy (&event); return 0; }
int get_request(zsock_t *sock, struct request *req) { zmsg_t *msg; int size; char *str; int Y, m, d, H, M, S; struct tm tm; msg = zmsg_recv(sock); size = zmsg_size(msg); if (size != 2) { // something's wrong zmsg_destroy(&msg); return -1; } str = zmsg_popstr(msg); req->ip = inet_addr(str); str = zmsg_popstr(msg); sscanf(str, "%04d%02d%02d%02d%02d%02d", &Y, &m, &d, &H, &M, &S); tm.tm_year = Y - 1900; tm.tm_mon = m; tm.tm_mday = d; tm.tm_hour = H; tm.tm_min = M; tm.tm_sec = S; req->timestamp = mktime(&tm); zmsg_destroy(&msg); return 0; }
static void server_process_cluster_command ( server_t *self, const char *peer_id, const char *peer_name, zmsg_t *msg, bool unicast) { char *request = zmsg_popstr (msg); char *pipename = zmsg_popstr (msg); zsys_info ("peer=%s command=%s pipe=%s unicast=%d", peer_name, request, pipename? pipename: "-", unicast); // Lookup or create pipe // TODO: remote pipes need cleaning up with some timeout pipe_t *pipe = NULL; if (pipename) { pipe = (pipe_t *) zhash_lookup (self->pipes, pipename); if (!pipe) pipe = pipe_new (self, pipename); } if (pipe && streq (request, "HAVE WRITER")) pipe_attach_remote_writer (pipe, peer_id, unicast); else if (pipe && streq (request, "HAVE READER")) pipe_attach_remote_reader (pipe, peer_id, unicast); else if (pipe && streq (request, "DATA")) { // TODO encode these commands as proper protocol zframe_t *frame = zmsg_pop (msg); zchunk_t *chunk = zchunk_new (zframe_data (frame), zframe_size (frame)); if (pipe->writer == REMOTE_NODE && pipe->reader) { zsys_info ("send %d bytes to pipe", (int) zchunk_size (chunk)); pipe_send_data (pipe, &chunk); } else zsys_info ("discard %d bytes, unroutable", (int) zchunk_size (chunk)); zframe_destroy (&frame); zchunk_destroy (&chunk); } else if (pipe && streq (request, "DROP READER")) pipe_drop_remote_reader (&pipe, peer_id); else if (pipe && streq (request, "DROP WRITER")) pipe_drop_remote_writer (&pipe, peer_id); else if (streq (request, "DUMP")) zyre_dump (self->zyre); else zsys_warning ("bad request %s from %s", request, peer_name); zstr_free (&pipename); zstr_free (&request); }
static int s_agent_handle_control (agent_t *self) { // Get the whole message off the control socket in one go zmsg_t *request = zmsg_recv (self->control); char *command = zmsg_popstr (request); if (!command) return -1; // Interrupted if (streq (command, "SET")) { char *name = zmsg_popstr (request); char *value = zmsg_popstr (request); curve_codec_set_metadata (self->codec, name, value); free (name); free (value); } else if (streq (command, "CONNECT")) { assert (!self->endpoint); self->endpoint = zmsg_popstr (request); int rc = zsocket_connect (self->dealer, "%s", self->endpoint); assert (rc != -1); zframe_t *server_key = zmsg_pop (request); zframe_t *output = curve_codec_execute (self->codec, &server_key); zframe_send (&output, self->dealer, 0); self->state = connecting; } else if (streq (command, "DISCONNECT")) { if (self->endpoint) { int rc = zsocket_disconnect (self->dealer, "%s", self->endpoint); assert (rc != -1); free (self->endpoint); } } else if (streq (command, "VERBOSE")) { char *verbose = zmsg_popstr (request); curve_codec_set_verbose (self->codec, *verbose == '1'); free (verbose); } else if (streq (command, "TERMINATE")) { self->state = terminated; zstr_send (self->control, "OK"); } else { puts ("E: invalid command from API"); assert (false); } free (command); zmsg_destroy (&request); return 0; }
static void s_self_configure (self_t *self, zsock_t **sock_p, zmsg_t *request, char *name) { char *type_name = zmsg_popstr (request); char *endpoints = zmsg_popstr (request); if (self->verbose) zsys_info ("zmonitor: - %s type=%s attach=%s", name, type_name, endpoints); assert (*sock_p == NULL); *sock_p = s_create_socket (type_name, endpoints); assert (*sock_p); zpoller_add (self->poller, *sock_p); zstr_free (&type_name); zstr_free (&endpoints); }
process_item *utils_msg2processitem(zmsg_t *message) { process_item *item = malloc(sizeof(process_item)); char *msgid = zmsg_popstr(message); char *command = zmsg_popstr(message); zframe_t *frame = zmsg_pop(message); pid_t *pid = (pid_t*)frame; memcpy(&item->pid,pid,sizeof(pid_t)); item->message_id = msgid; item->command = command; return item; }
static void s_alerts ( zsock_t *pipe, void *args) { const char *name = "ALERT"; mlm_client_t *cl = mlm_client_new (); mlm_client_connect (cl, endpoint, 5000, __PRETTY_FUNCTION__); mlm_client_set_producer (cl, stream); zsock_t *msgpipe = mlm_client_msgpipe (cl); zpoller_t *poller = zpoller_new (pipe, msgpipe, NULL); char *alert_state = strdup ("NEW"); zsock_signal (pipe, 0); while (!zsys_interrupted) { zsock_t *which = zpoller_wait (poller, 1000); if (!which) { mlm_client_sendx (cl, "alert://upsonbattery@ups1", alert_state, NULL); continue; } if (which == pipe) break; //which == msgpipe zmsg_t *msg = mlm_client_recv (cl); if (!streq (mlm_client_command (cl), "MAILBOX DELIVER")) goto msg_destroy; char *alert_name = zmsg_popstr (msg); zstr_free (&alert_state); alert_state = zmsg_popstr (msg); zsys_info ("%s: Alert '%s' new state is '%s'", name, alert_name, alert_state); zstr_free (&alert_name); msg_destroy: zmsg_destroy (&msg); } zstr_free (&alert_state); zpoller_destroy (&poller); mlm_client_destroy (&cl); }
// Process message from pipe static void server_control_message (server_t *self) { zmsg_t *msg = zmsg_recv (self->pipe); char *method = zmsg_popstr (msg); if (streq (method, "BIND")) { char *endpoint = zmsg_popstr (msg); self->port = zsocket_bind (self->router, endpoint); zstr_sendf (self->pipe, "%d", self->port); free (endpoint); } else if (streq (method, "PUBLISH")) { char *location = zmsg_popstr (msg); char *alias = zmsg_popstr (msg); mount_t *mount = mount_new (location, alias); zlist_append (self->mounts, mount); free (location); free (alias); } else if (streq (method, "SET ANONYMOUS")) { char *enabled_string = zmsg_popstr (msg); long enabled = atoi (enabled_string); free (enabled_string); // Enable anonymous access without a config file zconfig_put (self->config, "security/anonymous", enabled? "1" :"0"); } else if (streq (method, "CONFIG")) { char *config_file = zmsg_popstr (msg); zconfig_destroy (&self->config); self->config = zconfig_load (config_file); if (self->config) server_apply_config (self); else { printf ("E: cannot load config file '%s'\n", config_file); self->config = zconfig_new ("root", NULL); } free (config_file); } else if (streq (method, "SETOPTION")) { char *path = zmsg_popstr (msg); char *value = zmsg_popstr (msg); zconfig_put (self->config, path, value); server_config_self (self); free (path); free (value); } else if (streq (method, "STOP")) { zstr_send (self->pipe, "OK"); self->stopped = true; } free (method); zmsg_destroy (&msg); }
int zstr_recvx (void *source, char **string_p, ...) { assert (source); void *handle = zsock_resolve (source); zmsg_t *msg = zmsg_recv (handle); if (!msg) return -1; // Filter a signal that may come from a dying actor if (zmsg_signal (msg) >= 0) { zmsg_destroy (&msg); return -1; } int count = 0; va_list args; va_start (args, string_p); while (string_p) { *string_p = zmsg_popstr (msg); string_p = va_arg (args, char **); count++; } va_end (args); zmsg_destroy (&msg); return count; }
static void echo_actor (zsock_t *pipe, void *args) { // Do some initialization assert (streq ((char *) args, "Hello, World")); zsock_signal (pipe, 0); bool terminated = false; while (!terminated) { zmsg_t *msg = zmsg_recv (pipe); if (!msg) break; // Interrupted char *command = zmsg_popstr (msg); // All actors must handle $TERM in this way if (streq (command, "$TERM")) terminated = true; else // This is an example command for our test actor if (streq (command, "ECHO")) zmsg_send (&msg, pipe); else { puts ("E: invalid message to actor"); assert (false); } free (command); zmsg_destroy (&msg); } }
static void handle_mmi (client_t *self, const char *service_name) { const char *result = "501"; zmsg_t *mmibody = mdp_msg_get_body(self->message); if(mmibody) { if(strstr(service_name, "mmi.service")) { char *svc_lookup = zmsg_popstr(mmibody); if(svc_lookup) { service_t *service = (service_t *) zhash_lookup(self->server->services, svc_lookup); result = service && service->workers ? "200" : "404"; zstr_free(&svc_lookup); } } zmsg_destroy(&mmibody); } // Set routing id, messageid, service, body mdp_msg_t *client_msg = mdp_msg_new(); mdp_msg_set_routing_id(client_msg, mdp_msg_routing_id(self->message)); mdp_msg_set_id(client_msg, MDP_MSG_CLIENT_FINAL); mdp_msg_set_service(client_msg, service_name); zmsg_t *rep_body = zmsg_new(); zmsg_pushstr(rep_body, result); mdp_msg_set_body(client_msg, &rep_body); mdp_msg_send(client_msg, self->server->router); mdp_msg_destroy(&client_msg); }
triggerconfig_t * add_trigger(rulepackage_t * rpkg, zmsg_t * request,zmsg_t * reply) { char * rule_id = zmsg_popstr(request); zclock_log("new trigger!"); if (remove_rule(rpkg->context, rpkg->triggers, rule_id)) { // already have a rule with that id! zclock_log("Received duplicate rule %s - killing old trigger", rule_id); } triggerconfig_t * tconf = malloc(sizeof(triggerconfig_t)); tconf->base_config = rpkg->base_config; create_triggerconfig(tconf, request, rpkg->channel, rule_id); // when we create this trigger, what guarantee do we have that // the line listener is active? FIX // basically we have none. crap. char * created = create_trigger(rpkg->triggers, rule_id, rpkg->context, tconf); if(NULL == created) { // happy path, so add to db zmsg_pushstr(reply, "ok"); } else { zclock_log("create_trigger failed: %s", created); free(tconf); tconf=NULL; zmsg_pushstr(reply, created); } free(created); return tconf; }
static int actor_command(zloop_t *loop, zsock_t *socket, void *arg) { int rc = 0; watchdog_state_t *state = arg; zmsg_t *msg = zmsg_recv(socket); if (msg) { char *cmd = zmsg_popstr(msg); if (streq(cmd, "$TERM")) { state->received_term_cmd = true; // fprintf(stderr, "[D] watchdog[0]: received $TERM command\n"); rc = -1; } else if (streq(cmd, "tick")) { if (verbose) printf("[I] watchdog: credit: %d\n", state->credit); state->credit = CREDIT; } else { fprintf(stderr, "[E] watchdog[0]: received unknown actor command: %s\n", cmd); } free(cmd); zmsg_destroy(&msg); } return rc; }
static int s_agent_handle_data (agent_t *self) { // First frame is client address (hashkey) // If caller sends unknown client address, we discard the message // For testing, we'll abort in this case, since it cannot happen // The assert disappears when we start to timeout clients... zmsg_t *request = zmsg_recv (self->data); char *hashkey = zmsg_popstr (request); client_t *client = (client_t *) zhash_lookup (self->clients, hashkey); free (hashkey); if (client) { // Encrypt and send all frames of request // Each frame is a full ZMQ message with identity frame while (zmsg_size (request)) { zframe_t *cleartext = zmsg_pop (request); if (zmsg_size (request)) zframe_set_more (cleartext, 1); zframe_t *encrypted = curve_codec_encode (client->codec, &cleartext); if (encrypted) { zframe_send (&client->address, self->router, ZFRAME_MORE + ZFRAME_REUSE); zframe_send (&encrypted, self->router, 0); } else client_set_exception (client); } } zmsg_destroy (&request); return 0; }
static int actor_command(zloop_t *loop, zsock_t *socket, void *callback_data) { static size_t ticks = 0; int rc = 0; subscriber_state_t *state = callback_data; zmsg_t *msg = zmsg_recv(socket); if (msg) { char *cmd = zmsg_popstr(msg); if (streq(cmd, "$TERM")) { fprintf(stderr, "[D] subscriber: received $TERM command\n"); rc = -1; } else if (streq(cmd, "tick")) { printf("[I] subscriber: %5zu messages " "(gap_size: %zu, no_info: %zu, dev_zero: %zu, blocks: %zu, drops: %zu)\n", state->message_count, state->message_gap_size, state->meta_info_failures, state->messages_dev_zero, state->message_blocks, state->message_drops); state->message_count = 0; state->message_gap_size = 0; state->meta_info_failures = 0; state->messages_dev_zero = 0; state->message_drops = 0; if (++ticks % HEART_BEAT_INTERVAL == 0) device_tracker_reconnect_stale_devices(state->tracker); } else { fprintf(stderr, "[E] subscriber: received unknown actor command: %s\n", cmd); } free(cmd); zmsg_destroy(&msg); } return rc; }
/// // Pop frame off front of message, return as fresh string. If there were // no more frames in the message, returns NULL. QString QZmsg::popstr () { char *retStr_ = zmsg_popstr (self); QString rv = QString (retStr_); free (retStr_);; return rv; }
static void someactor_recv_api (someactor_t *self) { // Get the whole message of the pipe in one go zmsg_t *request = zmsg_recv (self->pipe); if (!request) return; // Interrupted char *command = zmsg_popstr (request); if (streq (command, "START")) zsock_signal (self->pipe, someactor_start (self)); else if (streq (command, "STOP")) zsock_signal (self->pipe, someactor_stop (self)); else if (streq (command, "VERBOSE")) { self->verbose = true; zsock_signal (self->pipe, 0); } else if (streq (command, "$TERM")) // The $TERM command is send by zactor_destroy() method self->terminated = true; else { zsys_error ("invalid command '%s'", command); assert (false); } }
static zap_request_t * s_zap_request_new (zsock_t *handler, bool verbose) { zap_request_t *self = (zap_request_t *) zmalloc (sizeof (zap_request_t)); if (!self) return NULL; // Store handler socket so we can send a reply easily self->handler = handler; self->verbose = verbose; zmsg_t *request = zmsg_recv (handler); if (!request) { // interrupted s_zap_request_destroy (&self); return NULL; } // Get all standard frames off the handler socket self->version = zmsg_popstr (request); self->sequence = zmsg_popstr (request); self->domain = zmsg_popstr (request); self->address = zmsg_popstr (request); self->identity = zmsg_popstr (request); self->mechanism = zmsg_popstr (request); // If the version is wrong, we're linked with a bogus libzmq, so die assert (streq (self->version, "1.0")); // Get mechanism-specific frames if (streq (self->mechanism, "PLAIN")) { self->username = zmsg_popstr (request); self->password = zmsg_popstr (request); } else if (streq (self->mechanism, "CURVE")) { zframe_t *frame = zmsg_pop (request); assert (zframe_size (frame) == 32); self->client_key = (char *) zmalloc (41); #if (ZMQ_VERSION_MAJOR == 4) zmq_z85_encode (self->client_key, zframe_data (frame), 32); #endif zframe_destroy (&frame); } else if (streq (self->mechanism, "GSSAPI")) self->principal = zmsg_popstr (request); if (self->verbose) zsys_info ("zauth: ZAP request mechanism=%s ipaddress=%s", self->mechanism, self->address); zmsg_destroy (&request); 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; }
zyre_event_t * zyre_event_new (zyre_t *node) { zmsg_t *msg = zyre_recv (node); if (!msg) return NULL; // Interrupted zyre_event_t *self = (zyre_event_t *) zmalloc (sizeof (zyre_event_t)); assert (self); char *type = zmsg_popstr (msg); self->sender = zmsg_popstr (msg); self->name = zmsg_popstr (msg); if (streq (type, "ENTER")) { self->type = ZYRE_EVENT_ENTER; zframe_t *headers = zmsg_pop (msg); if (headers) { self->headers = zhash_unpack (headers); zframe_destroy (&headers); } self->address = zmsg_popstr (msg); } else if (streq (type, "EXIT")) self->type = ZYRE_EVENT_EXIT; else if (streq (type, "JOIN")) { self->type = ZYRE_EVENT_JOIN; self->group = zmsg_popstr (msg); } else if (streq (type, "LEAVE")) { self->type = ZYRE_EVENT_LEAVE; self->group = zmsg_popstr (msg); } else if (streq (type, "WHISPER")) { self->type = ZYRE_EVENT_WHISPER; self->msg = msg; msg = NULL; } else if (streq (type, "SHOUT")) { self->type = ZYRE_EVENT_SHOUT; self->group = zmsg_popstr (msg); self->msg = msg; msg = NULL; } else if (streq (type, "STOP")) { self->type = ZYRE_EVENT_STOP; } else zsys_warning ("bad message received from node: %s\n", type); free (type); zmsg_destroy (&msg); return self; }
int zmsg_popint (zmsg_t *self) { assert (self); char *string = zmsg_popstr (self); int rc = string? atoi (string): 0; zstr_free (&string); return rc; }
static void s_self_configure (self_t *self, zsock_t **sock_p, zmsg_t *request, proxy_socket selected_socket) { char *type_name = zmsg_popstr (request); assert (type_name); char *endpoints = zmsg_popstr (request); assert (endpoints); if (self->verbose) zsys_info ("zproxy: - %s type=%s attach=%s authentication=%s", s_self_selected_socket_name (selected_socket), type_name, endpoints, s_self_selected_socket_auth (self->auth_type [selected_socket])); assert (*sock_p == NULL); *sock_p = s_self_create_socket (self, type_name, endpoints, selected_socket); assert (*sock_p); zpoller_add (self->poller, *sock_p); zstr_free (&type_name); zstr_free (&endpoints); }
static void check_credentials_action (client_t *self) { char *body = zmsg_popstr (self->request); if (body && streq (body, "Joe")) self->next_event = ok_event; else self->next_event = error_event; free (body); }
int receive_number(zmsg_t *response) { char *nr_s = zmsg_popstr(response); int nr; int rc = sscanf(nr_s, "%d", &nr); assert(rc == 1); (void) rc; RTfree(nr_s); return nr; }
static void s_assert_event (zactor_t *self, char *expected) { zmsg_t *msg = zmsg_recv (self); assert (msg); char *event = zmsg_popstr (msg); assert (streq (event, expected)); free (event); zmsg_destroy (&msg); }
void kill_envelope(zmsg_t * msg) { char * frame; while(1) { frame = zmsg_popstr(msg); if(strcmp(frame, "") == 0) { break; } free(frame); } }