int zgossip_msg_send (zgossip_msg_t *self, zsock_t *output) { assert (self); assert (output); if (zsock_type (output) == ZMQ_ROUTER) zframe_send (&self->routing_id, output, ZFRAME_MORE + ZFRAME_REUSE); size_t frame_size = 2 + 1; // Signature and message ID switch (self->id) { case ZGOSSIP_MSG_HELLO: frame_size += 1; // version break; case ZGOSSIP_MSG_PUBLISH: frame_size += 1; // version frame_size += 1 + strlen (self->key); frame_size += 4; if (self->value) frame_size += strlen (self->value); frame_size += 4; // ttl break; case ZGOSSIP_MSG_PING: frame_size += 1; // version break; case ZGOSSIP_MSG_PONG: frame_size += 1; // version break; case ZGOSSIP_MSG_INVALID: frame_size += 1; // version break; } // Now serialize message into the frame zmq_msg_t frame; zmq_msg_init_size (&frame, frame_size); self->needle = (byte *) zmq_msg_data (&frame); PUT_NUMBER2 (0xAAA0 | 0); PUT_NUMBER1 (self->id); size_t nbr_frames = 1; // Total number of frames to send switch (self->id) { case ZGOSSIP_MSG_HELLO: PUT_NUMBER1 (1); break; case ZGOSSIP_MSG_PUBLISH: PUT_NUMBER1 (1); PUT_STRING (self->key); if (self->value) { PUT_LONGSTR (self->value); } else PUT_NUMBER4 (0); // Empty string PUT_NUMBER4 (self->ttl); break; case ZGOSSIP_MSG_PING: PUT_NUMBER1 (1); break; case ZGOSSIP_MSG_PONG: PUT_NUMBER1 (1); break; case ZGOSSIP_MSG_INVALID: PUT_NUMBER1 (1); break; } // Now send the data frame zmq_msg_send (&frame, zsock_resolve (output), --nbr_frames? ZMQ_SNDMORE: 0); return 0; }
/** * Called by Java's Socket::send(byte [] msg, int offset, int flags). */ JNIEXPORT jboolean JNICALL Java_org_zeromq_ZMQ_00024Socket_send (JNIEnv *env, jobject obj, jbyteArray msg, jint offset, jint flags) { void *s = get_socket (env, obj, 0); jsize size = env->GetArrayLength (msg) - offset; if (size < 0) { raise_exception(env, EINVAL); return JNI_FALSE; } zmq_msg_t message; int rc = zmq_msg_init_size (&message, size); int err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } jbyte *data = env->GetByteArrayElements (msg, 0); if (! data) { raise_exception (env, EINVAL); return JNI_FALSE; } memcpy (zmq_msg_data (&message), data + offset, size); env->ReleaseByteArrayElements (msg, data, 0); #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) rc = zmq_sendmsg (s, &message, flags); #else rc = zmq_send (s, &message, flags); #endif err = zmq_errno(); if (rc < 0 && err == EAGAIN) { rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } return JNI_FALSE; } if (rc < 0) { raise_exception (env, err); rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } return JNI_FALSE; } rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } return JNI_TRUE; }
inline void *data () { return zmq_msg_data (&msg); }
int main (int argc, char *argv []) { const char *connect_to; int message_count; int message_size; void *ctx; void *s; int rc; int i; zmq_msg_t msg; if (argc != 4) { printf ("usage: remote_thr <connect-to> <message-size> " "<message-count>\n"); return 1; } connect_to = argv [1]; message_size = atoi (argv [2]); message_count = atoi (argv [3]); ctx = zmq_init (1); if (!ctx) { printf ("error in zmq_init: %s\n", zmq_strerror (errno)); return -1; } s = zmq_socket (ctx, ZMQ_PUSH); if (!s) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } // Add your socket options here. // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. rc = zmq_connect (s, connect_to); if (rc != 0) { printf ("error in zmq_connect: %s\n", zmq_strerror (errno)); return -1; } for (i = 0; i != message_count; i++) { rc = zmq_msg_init_size (&msg, message_size); if (rc != 0) { printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); return -1; } #if defined ZMQ_MAKE_VALGRIND_HAPPY memset (zmq_msg_data (&msg), 0, message_size); #endif rc = zmq_sendmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } } rc = zmq_close (s); if (rc != 0) { printf ("error in zmq_close: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_term (ctx); if (rc != 0) { printf ("error in zmq_term: %s\n", zmq_strerror (errno)); return -1; } return 0; }
inline const void* data () const { return zmq_msg_data (const_cast<zmq_msg_t*>(&msg)); }
int main(int argc, char **argv) { InitializeEnvironment(); IncrementGCLocks(); Load("src/main.clp"); void * module = FindDefmodule("Hypnotoad"); SetCurrentModule(module); zmq = zmq_init(1); pub = zmq_socket(zmq, ZMQ_PUB); zmq_bind(pub, "tcp://*:5554"); void * sub = zmq_socket(zmq, ZMQ_SUB); zmq_bind(sub, "tcp://*:5555"); zmq_setsockopt(sub, ZMQ_SUBSCRIBE, "", 0); while (1) { char * msg, * topic; int topic_size; char * remote_filename; zmq_msg_t request; zmq_msg_init (&request); zmq_recv (sub, &request, 0); topic = zmq_msg_data (&request); topic_size = zmq_msg_size(&request); int fact = !strncmp(topic, "fact", 4); if (!fact) { remote_filename = (char *) malloc(topic_size + 1); strncpy(remote_filename, topic, topic_size); } zmq_msg_close (&request); zmq_msg_init (&request); zmq_recv (sub, &request, 0); msg = zmq_msg_data (&request); if (fact) { LoadFactsFromString(msg, zmq_msg_size(&request)); Focus(module); Run(-1); } else { char * filename = tmpnam(0); /* dump file */ FILE * f = fopen(filename,"w+"); fwrite(msg, zmq_msg_size(&request), 1, f); fclose(f); char * fact = (char *) malloc(1024); sprintf(fact, "(remote-file \"%s\" \"%s\")\n",remote_filename,filename); /* load file mapping */ Focus(module); LoadFactsFromString(fact, -1); Focus(module); Run(-1); #if 0 DATA_OBJECT res; Eval("(facts)",&res); fflush(0); #endif /* cleanup */ free(fact); free(remote_filename); unlink(filename); free(filename); } zmq_msg_close (&request); } zmq_close(pub); zmq_close(sub); zmq_term(zmq); DecrementGCLocks(); }
const void* data() const { return zmq_msg_data(const_cast< ZMessage* >(this)); }
int uwsgi_proto_zeromq_accept(struct wsgi_request *wsgi_req, int fd) { zmq_msg_t message; char *req_uuid = NULL; size_t req_uuid_len = 0; char *req_id = NULL; size_t req_id_len = 0; char *req_path = NULL; size_t req_path_len = 0; #ifdef UWSGI_JSON json_t *root; json_error_t error; #endif char *mongrel2_req = NULL; size_t mongrel2_req_size = 0; int resp_id_len; uint32_t events = 0; char *message_ptr; size_t message_size = 0; char *post_data; #ifdef ZMQ_EVENTS size_t events_len = sizeof(uint32_t); if (uwsgi.edge_triggered == 0) { if (zmq_getsockopt(pthread_getspecific(uwsgi.zmq_pull), ZMQ_EVENTS, &events, &events_len) < 0) { uwsgi_error("zmq_getsockopt()"); uwsgi.edge_triggered = 0; return -1; } } #endif if (events & ZMQ_POLLIN || uwsgi.edge_triggered) { wsgi_req->do_not_add_to_async_queue = 1; wsgi_req->proto_parser_status = 0; zmq_msg_init(&message); if (zmq_recv(pthread_getspecific(uwsgi.zmq_pull), &message, uwsgi.zeromq_recv_flag) < 0) { if (errno == EAGAIN) { uwsgi.edge_triggered = 0; } else { uwsgi_error("zmq_recv()"); } zmq_msg_close(&message); return -1; } uwsgi.edge_triggered = 1; message_size = zmq_msg_size(&message); //uwsgi_log("%.*s\n", (int) wsgi_req->proto_parser_pos, zmq_msg_data(&message)); if (message_size > 0xffff) { uwsgi_log("too much big message %d\n", message_size); zmq_msg_close(&message); wsgi_req->do_not_log = 1; return -1; } message_ptr = zmq_msg_data(&message); // warning mongrel2_req_size will contains a bad value, but this is not a problem... post_data = uwsgi_split4(message_ptr, message_size, ' ', &req_uuid, &req_uuid_len, &req_id, &req_id_len, &req_path, &req_path_len, &mongrel2_req, &mongrel2_req_size); if (post_data == NULL) { uwsgi_log("cannot parse message (split4 phase)\n"); zmq_msg_close(&message); wsgi_req->do_not_log = 1; return -1; } // fix post_data, mongrel2_req and mongrel2_req_size post_data = uwsgi_netstring(mongrel2_req, message_size - (mongrel2_req - message_ptr), &mongrel2_req, &mongrel2_req_size); if (post_data == NULL) { uwsgi_log("cannot parse message (body netstring phase)\n"); zmq_msg_close(&message); wsgi_req->do_not_log = 1; return -1; } // ok ready to parse tnetstring/json data and build uwsgi request if (mongrel2_req[mongrel2_req_size] == '}') { if (uwsgi_mongrel2_tnetstring_parse(wsgi_req, mongrel2_req, mongrel2_req_size)) { zmq_msg_close(&message); wsgi_req->do_not_log = 1; return -1; } } else { #ifdef UWSGI_JSON #ifdef UWSGI_DEBUG uwsgi_log("JSON %d: %.*s\n", mongrel2_req_size, mongrel2_req_size, mongrel2_req); #endif // add a zero to the end of buf mongrel2_req[mongrel2_req_size] = 0; root = json_loads(mongrel2_req, 0, &error); if (!root) { uwsgi_log("error parsing JSON data: line %d %s\n", error.line, error.text); zmq_msg_close(&message); wsgi_req->do_not_log = 1; return -1; } if (uwsgi_mongrel2_json_parse(root, wsgi_req)) { json_decref(root); zmq_msg_close(&message); wsgi_req->do_not_log = 1; return -1; } json_decref(root); #else uwsgi_log("JSON support not enabled (recompile uWSGI with libjansson support, or re-configure mongrel2 with \"protocol='tnetstring'\". skip request\n"); #endif } // pre-build the mongrel2 response_header wsgi_req->proto_parser_buf = uwsgi_malloc(req_uuid_len + 1 + 11 + 1 + req_id_len + 1 + 1); memcpy(wsgi_req->proto_parser_buf, req_uuid, req_uuid_len); ((char *) wsgi_req->proto_parser_buf)[req_uuid_len] = ' '; resp_id_len = uwsgi_num2str2(req_id_len, wsgi_req->proto_parser_buf + req_uuid_len + 1); ((char *) wsgi_req->proto_parser_buf)[req_uuid_len + 1 + resp_id_len] = ':'; memcpy((char *) wsgi_req->proto_parser_buf + req_uuid_len + 1 + resp_id_len + 1, req_id, req_id_len); memcpy((char *) wsgi_req->proto_parser_buf + req_uuid_len + 1 + resp_id_len + 1 + req_id_len, ", ", 2); wsgi_req->proto_parser_pos = (uint64_t) req_uuid_len + 1 + resp_id_len + 1 + req_id_len + 1 + 1; // handle post data if (wsgi_req->post_cl > 0 && !wsgi_req->async_post) { if (uwsgi_netstring(post_data, message_size - (post_data - message_ptr), &message_ptr, &wsgi_req->post_cl)) { #ifdef UWSGI_DEBUG uwsgi_log("post_size: %d\n", wsgi_req->post_cl); #endif wsgi_req->async_post = tmpfile(); if (fwrite(message_ptr, wsgi_req->post_cl, 1, wsgi_req->async_post) != 1) { uwsgi_error("fwrite()"); zmq_msg_close(&message); return -1; } rewind(wsgi_req->async_post); wsgi_req->body_as_file = 1; } } zmq_msg_close(&message); return 0; } return -1; }
int main (int argc, char *argv []) { //main_req_file_trans(); //main_req_file_trans_pack(); main_req_db_trans_pack(); return 0; const char *connect_to; int message_count; int message_size; void *ctx; void *send; void *receive; int rc; int i; zmq_msg_t msg; //if (argc != 4) { // printf ("usage: remote_thr <connect-to> <message-size> " // "<message-count>\n"); // return 1; //} //connect_to = argv [1]; //message_size = atoi (argv [2]); //message_count = atoi (argv [3]); connect_to = argc>1 ? argv [1] : "tcp://127.0.0.1:2015"; message_size = argc>3 ? atoi (argv [3]) : 50; message_count = argc>2 ? atoi (argv [2]) : 1000; ctx = zmq_init (1); if (!ctx) { printf ("error in zmq_init: %send\n", zmq_strerror (errno)); return -1; } send = zmq_socket (ctx, ZMQ_PUSH); if (!send) { printf ("error in zmq_socket: %send\n", zmq_strerror (errno)); return -1; } // Add your socket options here. // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. rc = zmq_connect (send, connect_to); if (rc != 0) { printf ("error in zmq_connect: %s\n", zmq_strerror (errno)); return -1; } receive = zmq_socket (ctx, ZMQ_SUB); if (!receive) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } zmq_setsockopt(receive, ZMQ_SUBSCRIBE, "", 0); int s1; size_t r1 = sizeof(int); zmq_getsockopt (send, ZMQ_RCVHWM, &s1, &r1); zmq_getsockopt (send, ZMQ_RCVBUF, &s1, &r1); int jsendsize2 = 100000 * 200; zmq_setsockopt(send, ZMQ_RCVBUF, &jsendsize2, sizeof(jsendsize2)); zmq_getsockopt (send, ZMQ_RCVBUF, &s1, &r1); // Add your socket options here. // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM. const char *connect_to2 = "tcp://127.0.0.1:2016"; rc = zmq_connect (receive, connect_to2); if (rc != 0) { printf ("error in zmq_connect: %s\n", zmq_strerror (errno)); return -1; } Sleep(1000); for (i = 0; i != message_count; i++) { zmq_msg_close(&msg); //if (rc != 0) { // printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); // return -1; //} //printf("send %d: \n", i); char str[100]; memset(str, 0, 100); sprintf(str, "val: %-3d", i+1); sprintf(str, "%s", "Lock:20485:-100:0:290774416"); rc = zmq_msg_init_size (&msg, strlen(str)); memcpy(zmq_msg_data(&msg), str, strlen(str)); rc = zmq_sendmsg (send, &msg, 0); if (rc < 0) { printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } } zmq_pollitem_t items [] = { { receive, 0, ZMQ_POLLIN, 0 } }; int nCount = 1; int nTryTimes = 0; while (true){//nTryTimes<3) { zmq_msg_t message; zmq_poll (items, 1, 1000); if (items [0].revents & ZMQ_POLLIN) { zmq_msg_init (&message); zmq_recvmsg (receive, &message, 0); char *val = (char *)(zmq_msg_data(&message)); printf ("%d in zmq_recvmsg: %s\n", nCount++, val); zmq_msg_close (&message); nTryTimes = 0; } else { nTryTimes++; } } // for (i = 0; i != message_count-1; i++) { // printf ("in zmq_recvmsg: %d\n", i); // zmq_msg_t msg2; // rc = zmq_msg_init (&msg2); // rc = zmq_recvmsg (receive, &msg2, 0); // if (rc < 0) { // printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); // return -1; // } // int message_size = zmq_msg_size (&msg2); //} rc = zmq_close (receive); if (rc != 0) { printf ("error in zmq_close: %send\n", zmq_strerror (errno)); return -1; } rc = zmq_close (send); if (rc != 0) { printf ("error in zmq_close: %send\n", zmq_strerror (errno)); return -1; } rc = zmq_term (ctx); if (rc != 0) { printf ("error in zmq_term: %send\n", zmq_strerror (errno)); return -1; } return 0; }
static ssize_t s_recv_wire (peering_t *self) { vocket_t *vocket = self->vocket; driver_t *driver = self->driver; // Read into buffer and dump what we got // TODO: only read as much as we have space in input queue // implement exception strategy here // - drop oldest, drop newest, pushback byte buffer [VTX_TCP_BUFSIZE]; ssize_t size = recv (self->handle, buffer, VTX_TCP_BUFSIZE, MSG_DONTWAIT); if (size == 0) // Other side closed TCP socket, so our peering is down self->exception = TRUE; else if (size == -1) { if (s_handle_io_error ("recv") == -1) // Hard error on socket, so peering is down self->exception = TRUE; } else { if (driver->verbose) zclock_log ("I: (tcp) recv %zd bytes from %s", size, self->address); int rc = vtx_codec_bin_put (self->input, buffer, size); assert (rc == 0); Bool first_part = !self->more; Bool more; zmq_msg_t msg; zmq_msg_init (&msg); rc = vtx_codec_msg_get (self->input, &msg, &more); while (rc == 0) { if (vocket->routing == VTX_ROUTING_REQUEST) { // TODO state check // TODO Is reply expected to come from thes peering? int flags = ZMQ_DONTWAIT; if (more) flags |= ZMQ_SNDMORE; zmq_sendmsg (vocket->msgpipe, &msg, flags); } else if (vocket->routing == VTX_ROUTING_REPLY) { // TODO state check vocket->current_peering = self; int flags = ZMQ_DONTWAIT; if (more) flags |= ZMQ_SNDMORE; zmq_sendmsg (vocket->msgpipe, &msg, flags); } else if (vocket->routing == VTX_ROUTING_ROUTER) { // Send peering's ID if (first_part) { size_t id_size = strlen(driver->scheme) + strlen("://") + strlen(self->address); zmq_msg_t msg; rc = zmq_msg_init_size (&msg, id_size + 1); assert (rc == 0); strcpy (zmq_msg_data (&msg), driver->scheme); strcat (zmq_msg_data (&msg), "://"); strcat (zmq_msg_data (&msg), self->address); zmq_sendmsg (vocket->msgpipe, &msg, ZMQ_DONTWAIT|ZMQ_SNDMORE); zmq_msg_close (&msg); } int flags = ZMQ_DONTWAIT; if (more) flags |= ZMQ_SNDMORE; zmq_sendmsg (vocket->msgpipe, &msg, flags); } else if (vocket->nomnom) { int flags = ZMQ_DONTWAIT; if (more) flags |= ZMQ_SNDMORE; zmq_sendmsg (vocket->msgpipe, &msg, flags); } zmq_msg_close (&msg); zmq_msg_init (&msg); self->more = more; first_part = !more; rc = vtx_codec_msg_get (self->input, &msg, &more); } #if 0 else zclock_log ("W: unexpected message from %s - dropping", address); char *colon = strchr (address, ':'); assert (colon); *colon = 0; strcpy (vocket->sender, address); #endif }
static int s_vocket_input (zloop_t *loop, zmq_pollitem_t *item, void *arg) { vocket_t *vocket = (vocket_t *) arg; driver_t *driver = vocket->driver; // It's remotely possible we just lost a peering, in which case // don't take the message off the pipe, leave it for next time if (zlist_size (vocket->live_peerings) < vocket->min_peerings) return 0; // Pull message parts off socket assert (item->socket == vocket->msgpipe); zmq_msg_t msg; zmq_msg_init (&msg); Bool more = vocket->more; int rc = zmq_recvmsg (vocket->msgpipe, &msg, 0); while (rc >= 0) { vocket->outpiped++; Bool first = !more; more = zsockopt_rcvmore (vocket->msgpipe); // Route message to active peerings as appropriate if (vocket->routing == VTX_ROUTING_NONE) zclock_log ("W: send() not allowed - dropping"); else if (vocket->routing == VTX_ROUTING_REQUEST) { // First part of message // Round-robin to next peering if (first) { vocket->current_peering = (peering_t *) zlist_pop (vocket->live_peerings); zlist_append (vocket->live_peerings, vocket->current_peering); } peering_t *peering = vocket->current_peering; if (peering) s_queue_output (peering, &msg, more); } else if (vocket->routing == VTX_ROUTING_REPLY) { peering_t *peering = vocket->current_peering; if (peering) s_queue_output (peering, &msg, more); } else if (vocket->routing == VTX_ROUTING_DEALER) { // First part of message // Round-robin to next peering if (first) { vocket->current_peering = (peering_t *) zlist_pop (vocket->live_peerings); zlist_append (vocket->live_peerings, vocket->current_peering); } peering_t *peering = vocket->current_peering; if (peering) s_queue_output (peering, &msg, more); } else if (vocket->routing == VTX_ROUTING_ROUTER) { peering_t *peering = vocket->current_peering; // Look-up peering using first message part if (first) { // Parse and check schemed identity size_t size = zmq_msg_size (&msg); char *address = (char *) malloc (size + 1); memcpy (address, zmq_msg_data (&msg), size); address [size] = 0; int scheme_size = strlen (driver->scheme); if (memcmp (address, driver->scheme, scheme_size) == 0 && memcmp (address + scheme_size, "://", 3) == 0) { peering = (peering_t *) zhash_lookup ( vocket->peering_hash, address + scheme_size + 3); if (!peering || !peering->alive) zclock_log ("W: no route to '%s' - dropping", address); vocket->current_peering = peering; } else zclock_log ("E: bad address '%s' - dropping", address); free (address); } else if (peering) s_queue_output (peering, &msg, more); } else if (vocket->routing == VTX_ROUTING_PUBLISH) { // This is the first part of possibly multi-part message // Subscribe all peerings that are alive if (first) { peering_t *peering = (peering_t *) zlist_first (vocket->live_peerings); while (peering) { peering->subscribed = TRUE; peering = (peering_t *) zlist_next (vocket->live_peerings); } } if (zlist_size (vocket->live_peerings) > 1) { // Duplicate frames to all subscribers peering_t *peering = (peering_t *) zlist_first (vocket->live_peerings); while (peering) { if (peering->subscribed) s_queue_output (peering, &msg, more); peering = (peering_t *) zlist_next (vocket->live_peerings); } } else if (zlist_size (vocket->live_peerings) == 1) { // Send frames straight through to single subscriber peering_t *peering = (peering_t *) zlist_first (vocket->live_peerings); if (peering->subscribed) s_queue_output (peering, &msg, more); } } else if (vocket->routing == VTX_ROUTING_SINGLE) { if (first) vocket->current_peering = (peering_t *) zlist_first (vocket->live_peerings); peering_t *peering = vocket->current_peering; if (peering) s_queue_output (peering, &msg, more); } else zclock_log ("E: unknown routing mechanism - dropping"); zmq_msg_close (&msg); zmq_msg_init (&msg); rc = zmq_recvmsg (vocket->msgpipe, &msg, ZMQ_DONTWAIT); } // Save state vocket->more = more; return 0; }
int zproto_example_send (zproto_example_t *self, zsock_t *output) { assert (self); assert (output); if (zsock_type (output) == ZMQ_ROUTER) zframe_send (&self->routing_id, output, ZFRAME_MORE + ZFRAME_REUSE); size_t frame_size = 2 + 1; // Signature and message ID switch (self->id) { case ZPROTO_EXAMPLE_LOG: frame_size += 2; // sequence frame_size += 2; // version frame_size += 1; // level frame_size += 1; // event frame_size += 2; // node frame_size += 2; // peer frame_size += 8; // time frame_size += 1 + strlen (self->host); frame_size += 4; if (self->data) frame_size += strlen (self->data); break; case ZPROTO_EXAMPLE_STRUCTURES: frame_size += 2; // sequence frame_size += 4; // Size is 4 octets if (self->aliases) { char *aliases = (char *) zlist_first (self->aliases); while (aliases) { frame_size += 4 + strlen (aliases); aliases = (char *) zlist_next (self->aliases); } } frame_size += 4; // Size is 4 octets if (self->headers) { self->headers_bytes = 0; char *item = (char *) zhash_first (self->headers); while (item) { self->headers_bytes += 1 + strlen (zhash_cursor (self->headers)); self->headers_bytes += 4 + strlen (item); item = (char *) zhash_next (self->headers); } } frame_size += self->headers_bytes; break; case ZPROTO_EXAMPLE_BINARY: frame_size += 2; // sequence frame_size += 4; // flags frame_size += 4; // Size is 4 octets if (self->public_key) frame_size += zchunk_size (self->public_key); frame_size += ZUUID_LEN; // identifier break; case ZPROTO_EXAMPLE_TYPES: frame_size += 2; // sequence frame_size += 1 + strlen (self->client_forename); frame_size += 1 + strlen (self->client_surname); frame_size += 1 + strlen (self->client_mobile); frame_size += 1 + strlen (self->client_email); frame_size += 1 + strlen (self->supplier_forename); frame_size += 1 + strlen (self->supplier_surname); frame_size += 1 + strlen (self->supplier_mobile); frame_size += 1 + strlen (self->supplier_email); break; } // Now serialize message into the frame zmq_msg_t frame; zmq_msg_init_size (&frame, frame_size); self->needle = (byte *) zmq_msg_data (&frame); PUT_NUMBER2 (0xAAA0 | 0); PUT_NUMBER1 (self->id); bool have_content = false; size_t nbr_frames = 1; // Total number of frames to send switch (self->id) { case ZPROTO_EXAMPLE_LOG: PUT_NUMBER2 (self->sequence); PUT_NUMBER2 (3); PUT_NUMBER1 (self->level); PUT_NUMBER1 (self->event); PUT_NUMBER2 (self->node); PUT_NUMBER2 (self->peer); PUT_NUMBER8 (self->time); PUT_STRING (self->host); if (self->data) { PUT_LONGSTR (self->data); } else PUT_NUMBER4 (0); // Empty string break; case ZPROTO_EXAMPLE_STRUCTURES: PUT_NUMBER2 (self->sequence); if (self->aliases) { PUT_NUMBER4 (zlist_size (self->aliases)); char *aliases = (char *) zlist_first (self->aliases); while (aliases) { PUT_LONGSTR (aliases); aliases = (char *) zlist_next (self->aliases); } } else PUT_NUMBER4 (0); // Empty string array if (self->headers) { PUT_NUMBER4 (zhash_size (self->headers)); char *item = (char *) zhash_first (self->headers); while (item) { PUT_STRING (zhash_cursor (self->headers)); PUT_LONGSTR (item); item = (char *) zhash_next (self->headers); } } else PUT_NUMBER4 (0); // Empty hash break; case ZPROTO_EXAMPLE_BINARY: PUT_NUMBER2 (self->sequence); PUT_OCTETS (self->flags, 4); if (self->public_key) { PUT_NUMBER4 (zchunk_size (self->public_key)); memcpy (self->needle, zchunk_data (self->public_key), zchunk_size (self->public_key)); self->needle += zchunk_size (self->public_key); } else PUT_NUMBER4 (0); // Empty chunk if (self->identifier) zuuid_export (self->identifier, self->needle); else memset (self->needle, 0, ZUUID_LEN); self->needle += ZUUID_LEN; nbr_frames++; nbr_frames += self->content? zmsg_size (self->content): 1; have_content = true; break; case ZPROTO_EXAMPLE_TYPES: PUT_NUMBER2 (self->sequence); PUT_STRING (self->client_forename); PUT_STRING (self->client_surname); PUT_STRING (self->client_mobile); PUT_STRING (self->client_email); PUT_STRING (self->supplier_forename); PUT_STRING (self->supplier_surname); PUT_STRING (self->supplier_mobile); PUT_STRING (self->supplier_email); break; } // Now send the data frame zmq_msg_send (&frame, zsock_resolve (output), --nbr_frames? ZMQ_SNDMORE: 0); // Now send any frame fields, in order if (self->id == ZPROTO_EXAMPLE_BINARY) { // If address isn't set, send an empty frame if (self->address) zframe_send (&self->address, output, ZFRAME_REUSE + (--nbr_frames? ZFRAME_MORE: 0)); else zmq_send (zsock_resolve (output), NULL, 0, (--nbr_frames? ZMQ_SNDMORE: 0)); } // Now send the content if necessary if (have_content) { if (self->content) { zframe_t *frame = zmsg_first (self->content); while (frame) { zframe_send (&frame, output, ZFRAME_REUSE + (--nbr_frames? ZFRAME_MORE: 0)); frame = zmsg_next (self->content); } } else zmq_send (zsock_resolve (output), NULL, 0, 0); } return 0; }
int zproto_example_recv (zproto_example_t *self, zsock_t *input) { assert (input); if (zsock_type (input) == ZMQ_ROUTER) { zframe_destroy (&self->routing_id); self->routing_id = zframe_recv (input); if (!self->routing_id || !zsock_rcvmore (input)) { zsys_warning ("zproto_example: no routing ID"); return -1; // Interrupted or malformed } } zmq_msg_t frame; zmq_msg_init (&frame); int size = zmq_msg_recv (&frame, zsock_resolve (input), 0); if (size == -1) { zsys_warning ("zproto_example: interrupted"); goto malformed; // Interrupted } // Get and check protocol signature self->needle = (byte *) zmq_msg_data (&frame); self->ceiling = self->needle + zmq_msg_size (&frame); uint16_t signature; GET_NUMBER2 (signature); if (signature != (0xAAA0 | 0)) { zsys_warning ("zproto_example: invalid signature"); // TODO: discard invalid messages and loop, and return // -1 only on interrupt goto malformed; // Interrupted } // Get message id and parse per message type GET_NUMBER1 (self->id); switch (self->id) { case ZPROTO_EXAMPLE_LOG: GET_NUMBER2 (self->sequence); { uint16_t version; GET_NUMBER2 (version); if (version != 3) { zsys_warning ("zproto_example: version is invalid"); goto malformed; } } GET_NUMBER1 (self->level); GET_NUMBER1 (self->event); GET_NUMBER2 (self->node); GET_NUMBER2 (self->peer); GET_NUMBER8 (self->time); GET_STRING (self->host); GET_LONGSTR (self->data); break; case ZPROTO_EXAMPLE_STRUCTURES: GET_NUMBER2 (self->sequence); { size_t list_size; GET_NUMBER4 (list_size); self->aliases = zlist_new (); zlist_autofree (self->aliases); while (list_size--) { char *string = NULL; GET_LONGSTR (string); zlist_append (self->aliases, string); free (string); } } { size_t hash_size; GET_NUMBER4 (hash_size); self->headers = zhash_new (); zhash_autofree (self->headers); while (hash_size--) { char key [256]; char *value = NULL; GET_STRING (key); GET_LONGSTR (value); zhash_insert (self->headers, key, value); free (value); } } break; case ZPROTO_EXAMPLE_BINARY: GET_NUMBER2 (self->sequence); GET_OCTETS (self->flags, 4); { size_t chunk_size; GET_NUMBER4 (chunk_size); if (self->needle + chunk_size > (self->ceiling)) { zsys_warning ("zproto_example: public_key is missing data"); goto malformed; } zchunk_destroy (&self->public_key); self->public_key = zchunk_new (self->needle, chunk_size); self->needle += chunk_size; } if (self->needle + ZUUID_LEN > (self->ceiling)) { zsys_warning ("zproto_example: identifier is invalid"); goto malformed; } zuuid_destroy (&self->identifier); self->identifier = zuuid_new_from (self->needle); self->needle += ZUUID_LEN; // Get next frame off socket if (!zsock_rcvmore (input)) { zsys_warning ("zproto_example: address is missing"); goto malformed; } zframe_destroy (&self->address); self->address = zframe_recv (input); // Get zero or more remaining frames zmsg_destroy (&self->content); if (zsock_rcvmore (input)) self->content = zmsg_recv (input); else self->content = zmsg_new (); break; case ZPROTO_EXAMPLE_TYPES: GET_NUMBER2 (self->sequence); GET_STRING (self->client_forename); GET_STRING (self->client_surname); GET_STRING (self->client_mobile); GET_STRING (self->client_email); GET_STRING (self->supplier_forename); GET_STRING (self->supplier_surname); GET_STRING (self->supplier_mobile); GET_STRING (self->supplier_email); break; default: zsys_warning ("zproto_example: bad message ID"); goto malformed; } // Successful return zmq_msg_close (&frame); return 0; // Error returns malformed: zsys_warning ("zproto_example: zproto_example malformed message, fail"); zmq_msg_close (&frame); return -1; // Invalid message }
int main (int, char **) { setup_test_environment (); size_t len = MAX_SOCKET_STRING; char bind_endpoint[MAX_SOCKET_STRING]; char connect_endpoint[MAX_SOCKET_STRING]; void *context = zmq_ctx_new (); void *sockets[2]; int rc = 0; sockets[SERVER] = zmq_socket (context, ZMQ_STREAM); int enabled = 1; rc = zmq_setsockopt (sockets[SERVER], ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)); assert (rc == 0); rc = zmq_bind (sockets[SERVER], "tcp://0.0.0.0:*"); assert (rc == 0); rc = zmq_getsockopt (sockets[SERVER], ZMQ_LAST_ENDPOINT, bind_endpoint, &len); assert (rc == 0); // Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome. #ifdef ZMQ_HAVE_WINDOWS sprintf (connect_endpoint, "tcp://127.0.0.1:%s", strrchr (bind_endpoint, ':') + 1); #else strcpy (connect_endpoint, bind_endpoint); #endif sockets[CLIENT] = zmq_socket (context, ZMQ_STREAM); rc = zmq_setsockopt (sockets[CLIENT], ZMQ_STREAM_NOTIFY, &enabled, sizeof (enabled)); assert (rc == 0); rc = zmq_connect (sockets[CLIENT], connect_endpoint); assert (rc == 0); // wait for connect notification // Server: Grab the 1st frame (peer routing id). zmq_msg_t peer_frame; rc = zmq_msg_init (&peer_frame); assert (rc == 0); rc = zmq_msg_recv (&peer_frame, sockets[SERVER], 0); assert (rc != -1); assert (zmq_msg_size (&peer_frame) > 0); assert (has_more (sockets[SERVER])); rc = zmq_msg_close (&peer_frame); assert (rc == 0); // Server: Grab the 2nd frame (actual payload). zmq_msg_t data_frame; rc = zmq_msg_init (&data_frame); assert (rc == 0); rc = zmq_msg_recv (&data_frame, sockets[SERVER], 0); assert (rc != -1); assert (zmq_msg_size (&data_frame) == 0); rc = zmq_msg_close (&data_frame); assert (rc == 0); // Client: Grab the 1st frame (peer routing id). rc = zmq_msg_init (&peer_frame); assert (rc == 0); rc = zmq_msg_recv (&peer_frame, sockets[CLIENT], 0); assert (rc != -1); assert (zmq_msg_size (&peer_frame) > 0); assert (has_more (sockets[CLIENT])); rc = zmq_msg_close (&peer_frame); assert (rc == 0); // Client: Grab the 2nd frame (actual payload). rc = zmq_msg_init (&data_frame); assert (rc == 0); rc = zmq_msg_recv (&data_frame, sockets[CLIENT], 0); assert (rc != -1); assert (zmq_msg_size (&data_frame) == 0); rc = zmq_msg_close (&data_frame); assert (rc == 0); // Send initial message. char blob_data[256]; size_t blob_size = sizeof (blob_data); rc = zmq_getsockopt (sockets[CLIENT], ZMQ_ROUTING_ID, blob_data, &blob_size); assert (rc != -1); assert (blob_size > 0); zmq_msg_t msg; rc = zmq_msg_init_size (&msg, blob_size); assert (rc == 0); memcpy (zmq_msg_data (&msg), blob_data, blob_size); rc = zmq_msg_send (&msg, sockets[dialog[0].turn], ZMQ_SNDMORE); assert (rc != -1); rc = zmq_msg_close (&msg); assert (rc == 0); rc = zmq_msg_init_size (&msg, strlen (dialog[0].text)); assert (rc == 0); memcpy (zmq_msg_data (&msg), dialog[0].text, strlen (dialog[0].text)); rc = zmq_msg_send (&msg, sockets[dialog[0].turn], ZMQ_SNDMORE); assert (rc != -1); rc = zmq_msg_close (&msg); assert (rc == 0); // TODO: make sure this loop doesn't loop forever if something is wrong // with the test (or the implementation). int step = 0; while (step < steps) { // Wait until something happens. zmq_pollitem_t items[] = { {sockets[SERVER], 0, ZMQ_POLLIN, 0}, {sockets[CLIENT], 0, ZMQ_POLLIN, 0}, }; int rc = zmq_poll (items, 2, 100); assert (rc >= 0); // Check for data received by the server. if (items[SERVER].revents & ZMQ_POLLIN) { assert (dialog[step].turn == CLIENT); // Grab the 1st frame (peer routing id). zmq_msg_t peer_frame; rc = zmq_msg_init (&peer_frame); assert (rc == 0); rc = zmq_msg_recv (&peer_frame, sockets[SERVER], 0); assert (rc != -1); assert (zmq_msg_size (&peer_frame) > 0); assert (has_more (sockets[SERVER])); // Grab the 2nd frame (actual payload). zmq_msg_t data_frame; rc = zmq_msg_init (&data_frame); assert (rc == 0); rc = zmq_msg_recv (&data_frame, sockets[SERVER], 0); assert (rc != -1); // Make sure payload matches what we expect. const char *const data = (const char *) zmq_msg_data (&data_frame); const size_t size = zmq_msg_size (&data_frame); // 0-length frame is a disconnection notification. The server // should receive it as the last step in the dialogue. if (size == 0) { ++step; assert (step == steps); } else { assert (size == strlen (dialog[step].text)); int cmp = memcmp (dialog[step].text, data, size); assert (cmp == 0); ++step; assert (step < steps); // Prepare the response. rc = zmq_msg_close (&data_frame); assert (rc == 0); rc = zmq_msg_init_size (&data_frame, strlen (dialog[step].text)); assert (rc == 0); memcpy (zmq_msg_data (&data_frame), dialog[step].text, zmq_msg_size (&data_frame)); // Send the response. rc = zmq_msg_send (&peer_frame, sockets[SERVER], ZMQ_SNDMORE); assert (rc != -1); rc = zmq_msg_send (&data_frame, sockets[SERVER], ZMQ_SNDMORE); assert (rc != -1); } // Release resources. rc = zmq_msg_close (&peer_frame); assert (rc == 0); rc = zmq_msg_close (&data_frame); assert (rc == 0); } // Check for data received by the client. if (items[CLIENT].revents & ZMQ_POLLIN) { assert (dialog[step].turn == SERVER); // Grab the 1st frame (peer routing id). zmq_msg_t peer_frame; rc = zmq_msg_init (&peer_frame); assert (rc == 0); rc = zmq_msg_recv (&peer_frame, sockets[CLIENT], 0); assert (rc != -1); assert (zmq_msg_size (&peer_frame) > 0); assert (has_more (sockets[CLIENT])); // Grab the 2nd frame (actual payload). zmq_msg_t data_frame; rc = zmq_msg_init (&data_frame); assert (rc == 0); rc = zmq_msg_recv (&data_frame, sockets[CLIENT], 0); assert (rc != -1); assert (zmq_msg_size (&data_frame) > 0); // Make sure payload matches what we expect. const char *const data = (const char *) zmq_msg_data (&data_frame); const size_t size = zmq_msg_size (&data_frame); assert (size == strlen (dialog[step].text)); int cmp = memcmp (dialog[step].text, data, size); assert (cmp == 0); ++step; // Prepare the response (next line in the dialog). assert (step < steps); rc = zmq_msg_close (&data_frame); assert (rc == 0); rc = zmq_msg_init_size (&data_frame, strlen (dialog[step].text)); assert (rc == 0); memcpy (zmq_msg_data (&data_frame), dialog[step].text, zmq_msg_size (&data_frame)); // Send the response. rc = zmq_msg_send (&peer_frame, sockets[CLIENT], ZMQ_SNDMORE); assert (rc != -1); rc = zmq_msg_send (&data_frame, sockets[CLIENT], ZMQ_SNDMORE); assert (rc != -1); // Release resources. rc = zmq_msg_close (&peer_frame); assert (rc == 0); rc = zmq_msg_close (&data_frame); assert (rc == 0); } } assert (step == steps); rc = zmq_close (sockets[CLIENT]); assert (rc == 0); rc = zmq_close (sockets[SERVER]); assert (rc == 0); rc = zmq_ctx_term (context); assert (rc == 0); return 0; }
int main (int argc, char *argv []) { #if defined ZMQ_HAVE_WINDOWS HANDLE local_thread; #else pthread_t local_thread; #endif void *ctx; void *s; int rc; int i; zmq_msg_t msg; void *watch; unsigned long elapsed; double latency; if (argc != 3) { printf ("usage: inproc_lat <message-size> <roundtrip-count>\n"); return 1; } message_size = atoi (argv [1]); roundtrip_count = atoi (argv [2]); ctx = zmq_init (1); if (!ctx) { printf ("error in zmq_init: %s\n", zmq_strerror (errno)); return -1; } s = zmq_socket (ctx, ZMQ_REQ); if (!s) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_bind (s, "inproc://lat_test"); if (rc != 0) { printf ("error in zmq_bind: %s\n", zmq_strerror (errno)); return -1; } #if defined ZMQ_HAVE_WINDOWS local_thread = (HANDLE) _beginthreadex (NULL, 0, worker, ctx, 0 , NULL); if (local_thread == 0) { printf ("error in _beginthreadex\n"); return -1; } #else rc = pthread_create (&local_thread, NULL, worker, ctx); if (rc != 0) { printf ("error in pthread_create: %s\n", zmq_strerror (rc)); return -1; } #endif rc = zmq_msg_init_size (&msg, message_size); if (rc != 0) { printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); return -1; } memset (zmq_msg_data (&msg), 0, message_size); printf ("message size: %d [B]\n", (int) message_size); printf ("roundtrip count: %d\n", (int) roundtrip_count); watch = zmq_stopwatch_start (); for (i = 0; i != roundtrip_count; i++) { rc = zmq_sendmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_recvmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } } elapsed = zmq_stopwatch_stop (watch); rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } latency = (double) elapsed / (roundtrip_count * 2); #if defined ZMQ_HAVE_WINDOWS DWORD rc2 = WaitForSingleObject (local_thread, INFINITE); if (rc2 == WAIT_FAILED) { printf ("error in WaitForSingleObject\n"); return -1; } BOOL rc3 = CloseHandle (local_thread); if (rc3 == 0) { printf ("error in CloseHandle\n"); return -1; } #else rc = pthread_join (local_thread, NULL); if (rc != 0) { printf ("error in pthread_join: %s\n", zmq_strerror (rc)); return -1; } #endif printf ("average latency: %.3f [us]\n", (double) latency); rc = zmq_close (s); if (rc != 0) { printf ("error in zmq_close: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_term (ctx); if (rc != 0) { printf ("error in zmq_term: %s\n", zmq_strerror (errno)); return -1; } return 0; }
/** * Called by Java's Socket::send(byte [] msg, int offset, int flags). */ JNIEXPORT jboolean JNICALL Java_org_zeromq_ZMQ_00024Socket_send (JNIEnv *env, jobject obj, jbyteArray msg, jint offset, jint length, jint flags) { void *s = get_socket (env, obj); if (length < 0) { raise_exception(env, EINVAL); return JNI_FALSE; } zmq_msg_t message; int rc = zmq_msg_init_size (&message, length); int err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } void* pd = zmq_msg_data (&message); env->GetByteArrayRegion(msg, offset, length, (jbyte*) pd); #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) rc = zmq_sendmsg (s, &message, flags); #else rc = zmq_send (s, &message, flags); #endif err = zmq_errno(); if (rc < 0 && err == EAGAIN) { rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } return JNI_FALSE; } if (rc < 0) { raise_exception (env, err); rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } return JNI_FALSE; } rc = zmq_msg_close (&message); err = zmq_errno(); if (rc != 0) { raise_exception (env, err); return JNI_FALSE; } return JNI_TRUE; }
int main (int argc, char *argv[]) { /* Our process ID and Session ID */ pid_t pid, sid; if (argc != 3) { printf("%s [pubsub]\n\nEx.\n%s tcp://127.0.0.1:7817 /path/to/storage\n", argv[0], argv[0]); exit(-1); } /* Fork off the parent process */ pid = fork(); // pid = 0; /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { exit(EXIT_SUCCESS); } /* If forking actually didn't work */ if (pid < 0) { /* Change the file mode mask */ umask(022); /* Create a new SID for the child process */ sid = setsid(); /* Close out the standard file descriptors */ close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } /* Security is important, first we move to our target directory */ chdir(argv[2]); /* Setup the connection to the PubSub and subscribe to the envelopes */ void *context = zmq_init (1); void *subscriber = zmq_socket (context, ZMQ_SUB); zmq_connect (subscriber, argv[1]); zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "/GOVI/KV7calendar", 17); zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, "/GOVI/KV7planning", 17); /* Check if we are root */ if (getuid() == 0 || geteuid() == 0) { struct passwd *pw; uid_t puid = 65534; /* just use the traditional value */ gid_t pgid = 65534; if ((pw = getpwnam("nobody"))) { puid = pw->pw_uid; pgid = pw->pw_gid; } /* Now we chroot to this directory, preventing any write access outside it */ chroot(argv[2]); /* After we bind to the socket and chrooted, we drop priviledges to nobody */ setuid(puid); setgid(pgid); } /* First make our destination directories */ mkdir("GOVI/KV7calendar", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); mkdir("GOVI/KV7planning", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); while (1) { #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) int more; #else int64_t more; #endif size_t more_size = sizeof(more); FILE *fd = NULL; do { /* Create an empty 0MQ message to hold the message part */ zmq_msg_t part; int rc = zmq_msg_init (&part); assert (rc == 0); /* Block until a message is available to be received from socket */ #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) rc = zmq_msg_recv (&part, subscriber, 0); #else rc = zmq_recv (subscriber, &part, 0); #endif assert (rc != -1); if (fd == NULL) { char path[222]; size_t len = zmq_msg_size(&part); assert (len < 200); memcpy(path, zmq_msg_data(&part), len); if (strncmp(path, "/GOVI/KV7calendar", 17) == 0 && len != 17) { snprintf(path + len, 4, ".gz"); } else { struct timeval tv; gettimeofday(&tv,NULL); snprintf(path + 17, 22, "/%u.%06u.gz", (unsigned int) tv.tv_sec, (unsigned int) tv.tv_usec); } fd = fopen(path + 1, "w"); assert (fd != NULL); } else { fwrite(zmq_msg_data(&part), zmq_msg_size(&part), 1, fd); } /* Determine if more message parts are to follow */ rc = zmq_getsockopt (subscriber, ZMQ_RCVMORE, &more, &more_size); assert (rc == 0); zmq_msg_close (&part); } while (more); if (fd) { fclose(fd); } } }
// create the logpipe void create_logpipe(void) { #if defined(SOCK_SEQPACKET) && defined(__linux__) if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, uwsgi.shared->worker_log_pipe)) { #else if (socketpair(AF_UNIX, SOCK_DGRAM, 0, uwsgi.shared->worker_log_pipe)) { #endif uwsgi_error("socketpair()\n"); exit(1); } uwsgi_socket_nb(uwsgi.shared->worker_log_pipe[0]); uwsgi_socket_nb(uwsgi.shared->worker_log_pipe[1]); if (uwsgi.shared->worker_log_pipe[1] != 1) { if (dup2(uwsgi.shared->worker_log_pipe[1], 1) < 0) { uwsgi_error("dup2()"); exit(1); } } if (dup2(1, 2) < 0) { uwsgi_error("dup2()"); exit(1); } } #ifdef UWSGI_ZEROMQ // the zeromq logger ssize_t uwsgi_zeromq_logger(struct uwsgi_logger *ul, char *message, size_t len) { if (!ul->configured) { if (!ul->arg) { uwsgi_log_safe("invalid zeromq syntax\n"); exit(1); } void *ctx = uwsgi_zeromq_init(); ul->data = zmq_socket(ctx, ZMQ_PUSH); if (ul->data == NULL) { uwsgi_error_safe("zmq_socket()"); exit(1); } if (zmq_connect(ul->data, ul->arg) < 0) { uwsgi_error_safe("zmq_connect()"); exit(1); } ul->configured = 1; } zmq_msg_t msg; if (zmq_msg_init_size(&msg, len) == 0) { memcpy(zmq_msg_data(&msg), message, len); #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3,0,0) zmq_sendmsg(ul->data, &msg, 0); #else zmq_send(ul->data, &msg, 0); #endif zmq_msg_close(&msg); } return 0; } #endif // log to the specified file or udp address void logto(char *logfile) { int fd; #ifdef UWSGI_UDP char *udp_port; struct sockaddr_in udp_addr; udp_port = strchr(logfile, ':'); if (udp_port) { udp_port[0] = 0; if (!udp_port[1] || !logfile[0]) { uwsgi_log("invalid udp address\n"); exit(1); } fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { uwsgi_error("socket()"); exit(1); } memset(&udp_addr, 0, sizeof(struct sockaddr_in)); udp_addr.sin_family = AF_INET; udp_addr.sin_port = htons(atoi(udp_port + 1)); char *resolved = uwsgi_resolve_ip(logfile); if (resolved) { udp_addr.sin_addr.s_addr = inet_addr(resolved); } else { udp_addr.sin_addr.s_addr = inet_addr(logfile); } if (connect(fd, (const struct sockaddr *) &udp_addr, sizeof(struct sockaddr_in)) < 0) { uwsgi_error("connect()"); exit(1); } } else { #endif if (uwsgi.log_truncate) { fd = open(logfile, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP); } else { fd = open(logfile, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP); } if (fd < 0) { uwsgi_error_open(logfile); exit(1); } uwsgi.logfile = logfile; if (uwsgi.chmod_logfile_value) { if (chmod(uwsgi.logfile, uwsgi.chmod_logfile_value)) { uwsgi_error("chmod()"); } } #ifdef UWSGI_UDP } #endif /* stdout */ if (fd != 1) { if (dup2(fd, 1) < 0) { uwsgi_error("dup2()"); exit(1); } close(fd); } /* stderr */ if (dup2(1, 2) < 0) { uwsgi_error("dup2()"); exit(1); } }
/* * Function: * zbx_module_vpoller() * * Purpose: * Sends task requests to vPoller for processing * * The `vpoller` key expects the following parameters * when called through Zabbix: * * vpoller[method, hostname, name, properties, <key>, <username>, <password>, <counter-name>, <instance>, <perf-interval>] * * And the parameters that it expects are these: * * method - vPoller method to be processed * hostname - VMware vSphere server hostname * name - Name of the vSphere object (e.g. VM name, ESXi name) * properties - vSphere properties to be collected * <key> - Additional information passed as a 'key' to vPoller * <username> - Username to use when logging into the guest system * <password> - Password to use when logging into the guest system * <counter-name> - Performance counter name * <instance> - Performance counter instance * <perf-interval> - Historical performance interval */ int zbx_module_vpoller(AGENT_REQUEST *request, AGENT_RESULT *result) { unsigned int i; void *zsocket = NULL; /* ZeroMQ socket */ zmq_msg_t msg_in; /* Incoming ZeroMQ message from vPoller */ char *params[PARAM_NUM]; /* Params received from Zabbix */ char *key_esc; bool got_reply = false; /* A flag to indicate whether a reply from vPoller was received or not */ int retries = CONFIG_VPOLLER_RETRIES; /* Number of retries */ int linger = 0; /* Set the ZeroMQ socket option ZMQ_LINGER to 0 */ int msg_len = 0; /* Length of the received message */ char msg_buf[MAX_BUFFER_LEN]; /* Buffer to hold the final message we send out to vPoller */ for (i = 0; i < PARAM_NUM; i++) params[i] = "(null)"; /* * The Zabbix `vpoller` key expects these parameters * in the following order: * * vpoller[method, hostname, name, properties, <key>, <username>, <password>, <counter-name>, <instance>, <perf-interval>] */ if ((request->nparam < 4) || (request->nparam > PARAM_NUM)) { SET_MSG_RESULT(result, strdup("Invalid number of arguments")); return (SYSINFO_RET_FAIL); } for (i = 0; i < request->nparam; i++) params[i] = get_rparam(request, i); /* * Create the task request which we send to vPoller */ key_esc = zbx_dyn_escape_string(params[PARAM_KEY], "\\"); zbx_snprintf(msg_buf, sizeof(msg_buf), VPOLLER_TASK_TEMPLATE, params[PARAM_METHOD], params[PARAM_HOSTNAME], params[PARAM_NAME], params[PARAM_PROPERTIES], key_esc, params[PARAM_USERNAME], params[PARAM_PASSWORD], params[PARAM_COUNTER_NAME], params[PARAM_INSTANCE], params[PARAM_PERF_INTERVAL]); zbx_free(key_esc); zabbix_log(LOG_LEVEL_DEBUG, "Creating a ZeroMQ socket for connecting to vPoller"); if ((zsocket = zmq_socket(zcontext, ZMQ_REQ)) == NULL) { SET_MSG_RESULT(result, strdup("Cannot create a ZeroMQ socket")); return (SYSINFO_RET_FAIL); } /* * Connect to the vPoller Proxy */ zabbix_log(LOG_LEVEL_DEBUG, "Connecting to vPoller endpoint at %s", CONFIG_VPOLLER_PROXY); zmq_connect(zsocket, CONFIG_VPOLLER_PROXY); zmq_setsockopt(zsocket, ZMQ_LINGER, &linger, sizeof(linger)); zmq_msg_init(&msg_in); /* * Send the task request to vPoller, using a retry mechanism */ while (retries > 0) { zabbix_log(LOG_LEVEL_DEBUG, "Sending task request to vPoller: %s", msg_buf); zmq_pollitem_t items[] = { { zsocket, 0, ZMQ_POLLIN, 0 }, }; zmq_send(zsocket, msg_buf, strlen(msg_buf), 0); zmq_poll(items, 1, CONFIG_VPOLLER_TIMEOUT); /* Do we have a reply? */ if (items[0].revents & ZMQ_POLLIN) { zabbix_log(LOG_LEVEL_DEBUG, "Received reply from vPoller"); if ((msg_len = zmq_msg_recv(&msg_in, zsocket, 0)) != -1) { got_reply = true; break; } } else { /* We didn't get a reply from the server, let's retry */ retries--; if (retries > 0) { zabbix_log(LOG_LEVEL_WARNING, "Did not receive response from vPoller, retrying..."); } else { zabbix_log(LOG_LEVEL_WARNING, "Did not receive response from vPoller, giving up."); } /* Socket is confused, close and remove it */ zabbix_log(LOG_LEVEL_DEBUG, "Closing socket and re-establishing connection to vPoller..."); zmq_close(zsocket); if ((zsocket = zmq_socket(zcontext, ZMQ_REQ)) == NULL) { SET_MSG_RESULT(result, strdup("Cannot create a ZeroMQ socket")); zmq_msg_close(&msg_in); return (SYSINFO_RET_FAIL); } zmq_connect(zsocket, CONFIG_VPOLLER_PROXY); zmq_setsockopt(zsocket, ZMQ_LINGER, &linger, sizeof(linger)); } } /* Do we have any result? */ if (got_reply == false) { zmq_msg_close(&msg_in); zmq_close(zsocket); SET_MSG_RESULT(result, strdup("Did not receive response from vPoller")); return (SYSINFO_RET_FAIL); } SET_STR_RESULT(result, strdup(zmq_msg_data(&msg_in))); zmq_msg_close(&msg_in); zmq_close(zsocket); return (SYSINFO_RET_OK); }
int main(int, char**) { setup_test_environment(); void* context = zmq_ctx_new(); void* pubSocket; void* subSocket; (pubSocket = zmq_socket(context, ZMQ_XPUB)) || printf("zmq_socket: %s\n", zmq_strerror(errno)); (subSocket = zmq_socket(context, ZMQ_SUB)) || printf("zmq_socket: %s\n", zmq_strerror(errno)); zmq_setsockopt(subSocket, ZMQ_SUBSCRIBE, "foo", 3) && printf("zmq_setsockopt: %s\n",zmq_strerror(errno)); zmq_bind(pubSocket, "inproc://someInProcDescriptor") && printf("zmq_bind: %s\n", zmq_strerror(errno)); //zmq_bind(pubSocket, "tcp://127.0.0.1:30010") && printf("zmq_bind: %s\n", zmq_strerror(errno)); int more; size_t more_size = sizeof(more); int iteration = 0; while (1) { zmq_pollitem_t items [] = { { subSocket, 0, ZMQ_POLLIN, 0 }, // read publications { pubSocket, 0, ZMQ_POLLIN, 0 }, // read subscriptions }; int rc = zmq_poll (items, 2, 100); if (items [1].revents & ZMQ_POLLIN) { while (1) { zmq_msg_t msg; zmq_msg_init (&msg); zmq_msg_recv (&msg, pubSocket, 0); char* buffer = (char*)zmq_msg_data(&msg); if (buffer[0] == 0) { assert(isSubscribed); isSubscribed = false; } else { assert(!isSubscribed); isSubscribed = true; } zmq_getsockopt (pubSocket, ZMQ_RCVMORE, &more, &more_size); zmq_msg_close (&msg); if (!more) break; // Last message part } } if (items[0].revents & ZMQ_POLLIN) { while (1) { zmq_msg_t msg; zmq_msg_init (&msg); zmq_msg_recv (&msg, subSocket, 0); zmq_getsockopt (subSocket, ZMQ_RCVMORE, &more, &more_size); zmq_msg_close (&msg); if (!more) { publicationsReceived++; break; // Last message part } } } if (iteration == 1) { zmq_connect(subSocket, "inproc://someInProcDescriptor") && printf("zmq_connect: %s\n", zmq_strerror(errno)); //zmq_connect(subSocket, "tcp://127.0.0.1:30010") && printf("zmq_connect: %s\n", zmq_strerror(errno)); } if (iteration == 4) { zmq_disconnect(subSocket, "inproc://someInProcDescriptor") && printf("zmq_disconnect(%d): %s\n", errno, zmq_strerror(errno)); //zmq_disconnect(subSocket, "tcp://127.0.0.1:30010") && printf("zmq_disconnect: %s\n", zmq_strerror(errno)); } if (iteration > 4 && rc == 0) break; zmq_msg_t channelEnvlp; ZMQ_PREPARE_STRING(channelEnvlp, "foo", 3); zmq_msg_send (&channelEnvlp, pubSocket, ZMQ_SNDMORE) >= 0 || printf("zmq_msg_send: %s\n",zmq_strerror(errno)); zmq_msg_close(&channelEnvlp) && printf("zmq_msg_close: %s\n",zmq_strerror(errno)); zmq_msg_t message; ZMQ_PREPARE_STRING(message, "this is foo!", 12); zmq_msg_send (&message, pubSocket, 0) >= 0 || printf("zmq_msg_send: %s\n",zmq_strerror(errno)); zmq_msg_close(&message) && printf("zmq_msg_close: %s\n",zmq_strerror(errno)); iteration++; } assert(publicationsReceived == 3); assert(!isSubscribed); zmq_close(pubSocket) && printf("zmq_close: %s", zmq_strerror(errno)); zmq_close(subSocket) && printf("zmq_close: %s", zmq_strerror(errno)); zmq_ctx_term(context); return 0; }
int main (int argc, char *argv []) { const char *connect_to; int roundtrip_count; ssize_t message_size; void *ctx; void *s; int rc; int i; zmq_msg_t msg; void *watch; unsigned long elapsed; double latency; if (argc != 4) { printf ("usage: remote_lat <connect-to> <message-size> " "<roundtrip-count>\n"); return 1; } connect_to = argv [1]; message_size = atoi (argv [2]); roundtrip_count = atoi (argv [3]); ctx = zmq_init (1); if (!ctx) { printf ("error in zmq_init: %s\n", zmq_strerror (errno)); return -1; } s = zmq_socket (ctx, ZMQ_REQ); if (!s) { printf ("error in zmq_socket: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_connect (s, connect_to); if (rc != 0) { printf ("error in zmq_connect: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_msg_init_size (&msg, message_size); if (rc != 0) { printf ("error in zmq_msg_init_size: %s\n", zmq_strerror (errno)); return -1; } memset (zmq_msg_data (&msg), 0, message_size); watch = zmq_stopwatch_start (); for (i = 0; i != roundtrip_count; i++) { rc = zmq_sendmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_sendmsg: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_recvmsg (s, &msg, 0); if (rc < 0) { printf ("error in zmq_recvmsg: %s\n", zmq_strerror (errno)); return -1; } if (zmq_msg_size (&msg) != message_size) { printf ("message of incorrect size received\n"); return -1; } } elapsed = zmq_stopwatch_stop (watch); rc = zmq_msg_close (&msg); if (rc != 0) { printf ("error in zmq_msg_close: %s\n", zmq_strerror (errno)); return -1; } latency = (double) elapsed / (roundtrip_count * 2); printf ("message size: %d [B]\n", (int) message_size); printf ("roundtrip count: %d\n", (int) roundtrip_count); printf ("average latency: %.3f [us]\n", (double) latency); rc = zmq_close (s); if (rc != 0) { printf ("error in zmq_close: %s\n", zmq_strerror (errno)); return -1; } rc = zmq_term (ctx); if (rc != 0) { printf ("error in zmq_term: %s\n", zmq_strerror (errno)); return -1; } return 0; }
int barrier(role *grp_role, char *at_rolename) { if (grp_role->type != SESSION_ROLE_GRP) { fprintf(stderr, "Error: cannot perform barrier synchronisation with non group role!\n"); return -1; } int rc = 0; zmq_msg_t msg; int i; if (strcmp(grp_role->s->name, at_rolename) == 0) { // Master role rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_UNSUBSCRIBE, "", 0); if (rc != 0) perror(__FUNCTION__); rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_SUBSCRIBE, "S1", 2); if (rc != 0) perror(__FUNCTION__); // Wait for S1 (Phase 1) messages. for (i=0; i<grp_role->grp->nendpoint; ++i) { zmq_msg_init(&msg); rc |= zmq_msg_recv(grp_role->grp->in->ptr, &msg, 0); if (rc != 0) perror(__FUNCTION__); zmq_msg_close (&msg); } // Reset filters. rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_UNSUBSCRIBE, "S1", 2); if (rc != 0) perror(__FUNCTION__); rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_SUBSCRIBE, "", 0); if (rc != 0) perror(__FUNCTION__); zmq_msg_init_size(&msg, 2); memcpy(zmq_msg_data(&msg), "S2", 2); rc |= zmq_msg_send(grp_role->grp->out->ptr, &msg, 0); if (rc != 0) perror(__FUNCTION__); zmq_msg_close(&msg); // Synchronised. } else { // Slave role // Send S1 (Phase 1) messages. zmq_msg_init_size(&msg, 2); memcpy(zmq_msg_data(&msg), "S1", 2); rc |= zmq_msg_send(grp_role->grp->out->ptr, &msg, 0); if (rc != 0) perror(__FUNCTION__); zmq_msg_close(&msg); rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_UNSUBSCRIBE, "", 0); if (rc != 0) perror(__FUNCTION__); rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_SUBSCRIBE, "S2", 2); if (rc != 0) perror(__FUNCTION__); // Wait for S2 (Phase 2) messages. zmq_msg_init(&msg); rc |= zmq_msg_recv(grp_role->grp->in->ptr, &msg, 0); if (rc != 0) perror(__FUNCTION__); zmq_msg_close(&msg); // Reset filters. rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_UNSUBSCRIBE, "S2", 2); if (rc != 0) perror(__FUNCTION__); rc |= zmq_setsockopt(grp_role->grp->in->ptr, ZMQ_SUBSCRIBE, "", 0); if (rc != 0) perror(__FUNCTION__); // Synchronised. } return rc; }
void test_pull_fair_queue_in (void *ctx) { void *pull = zmq_socket (ctx, ZMQ_PULL); assert (pull); int rc = zmq_bind (pull, bind_address); assert (rc == 0); const size_t services = 5; void *pushs [services]; for (size_t peer = 0; peer < services; ++peer) { pushs [peer] = zmq_socket (ctx, ZMQ_PUSH); assert (pushs [peer]); rc = zmq_connect (pushs [peer], connect_address); assert (rc == 0); } // Wait for connections. msleep (SETTLE_TIME); int first_half = 0; int second_half = 0; // Send 2N messages for (size_t peer = 0; peer < services; ++peer) { char *str = strdup("A"); str [0] += peer; s_send_seq (pushs [peer], str, SEQ_END); first_half += str [0]; str [0] += services; s_send_seq (pushs [peer], str, SEQ_END); second_half += str [0]; free (str); } // Wait for data. msleep (SETTLE_TIME); zmq_msg_t msg; rc = zmq_msg_init (&msg); assert (rc == 0); // Expect to pull one from each first for (size_t peer = 0; peer < services; ++peer) { rc = zmq_msg_recv (&msg, pull, 0); assert (rc == 2); const char *str = (const char *)zmq_msg_data (&msg); first_half -= str [0]; } assert (first_half == 0); // And then get the second batch for (size_t peer = 0; peer < services; ++peer) { rc = zmq_msg_recv (&msg, pull, 0); assert (rc == 2); const char *str = (const char *)zmq_msg_data (&msg); second_half -= str [0]; } assert (second_half == 0); rc = zmq_msg_close (&msg); assert (rc == 0); close_zero_linger (pull); for (size_t peer = 0; peer < services; ++peer) close_zero_linger (pushs [peer]); // Wait for disconnects. msleep (SETTLE_TIME); }
static void parser_thread(void *args, zctx_t* context, void *pipe){ void *dispatch_socket = zsocket_new (context, ZMQ_PUB); zsocket_set_hwm(dispatch_socket, 10000); int rc = zsocket_connect(dispatch_socket, DISPATCH_SOCKET); assert(rc == 0); void *work_receiver = zsocket_new(context, ZMQ_PULL); rc = zsocket_connect(work_receiver, WORKER_SOCKET); assert(rc == 0); while(1) { zmq_msg_t message; zmq_msg_init (&message); int size = zmq_msg_recv (&message, work_receiver, 0); assert (size != -1); char *buff = malloc (size + 1); memcpy (buff, zmq_msg_data (&message), size); zmq_msg_close (&message); buff [size] = 0; ssize_t i = 0; char action[81+FILENAME_MAX] = {0}; while (i < size) { struct inotify_event *pevent = (struct inotify_event *)&buff[i]; if (pevent->len){ int serial_length = sizeof(struct inotify_event) + pevent->len; dispatch(&buff[i], serial_length, dispatch_socket); strcpy (action, pevent->name); }else{ strcpy (action, "some random directory"); } if (pevent->mask & IN_ACCESS) strcat(action, " was read"); if (pevent->mask & IN_ATTRIB) strcat(action, " Metadata changed"); if (pevent->mask & IN_CLOSE_WRITE) strcat(action, " opened for writing was closed"); if (pevent->mask & IN_CLOSE_NOWRITE) strcat(action, " not opened for writing was closed"); if (pevent->mask & IN_CREATE) strcat(action, " created in watched directory"); if (pevent->mask & IN_DELETE) strcat(action, " deleted from watched directory"); if (pevent->mask & IN_DELETE_SELF) strcat(action, "Watched file/directory was itself deleted"); if (pevent->mask & IN_MODIFY) strcat(action, " was modified"); if (pevent->mask & IN_MOVE_SELF) strcat(action, "Watched file/directory was itself moved"); if (pevent->mask & IN_MOVED_FROM) strcat(action, " moved out of watched directory"); if (pevent->mask & IN_MOVED_TO) strcat(action, " moved into watched directory"); if (pevent->mask & IN_OPEN) strcat(action, " was opened"); //printf ("wd=%d mask=%d cookie=%d len=%d dir=%s\n",pevent->wd, pevent->mask, pevent->cookie, pevent->len, (pevent->mask & IN_ISDIR)?"yes":"no"); if (pevent->len) printf ("name=%s\n", pevent->name); //printf ("%s\n", action); i += sizeof(struct inotify_event) + pevent->len; } free (buff); } //zmq_close(dispatch_socket); //zmq_close(work_receiver); }
bool Van::recv(mltools::Message *msg, size_t *recvBytes) { size_t dataSize = 0; msg->clear_data(); for (int i = 0;; ++i) { zmq_msg_t *zmsg = new zmq_msg_t; CHECK(zmq_msg_init(zmsg) == 0) << zmq_strerror(errno); while (true) { if (zmq_msg_recv(zmsg, receiver_, 0) != -1) { break; } if (errno == EINTR) { continue; } LOG(WARNING) << "failed to receive message, error : " << zmq_strerror(errno); return false; } char *buf = CHECK_NOTNULL((char *)zmq_msg_data(zmsg)); size_t size = zmq_msg_size(zmsg); dataSize += size; if (i == 0) { // first message is the identity of the sender msg->sender_ = std::string(buf, size); msg->recver_ = myNode_.id(); zmq_msg_close(zmsg); delete zmsg; } else if (i == 1) { CHECK(msg->task_.ParseFromArray(buf, size)) << "failed to parse string from " << msg->sender_ << ". this is " << myNode_.id() << size; if (isScheduler() && msg->task_.control() && msg->task_.ctrl().cmd() == Control::REQUEST_APP) { // In the start time of every work, they will send such message to // scheduler to get the config. The scheduler will store the file // descriptor for performance monitoring. int val[64]; size_t valLen = sizeof(val); std::memset(val, 0, sizeof(val)); CHECK(!zmq_getsockopt(receiver_, ZMQ_FD, (char *)val, &valLen)) << "Failed to get the file descriptor of " << msg->sender_ << ". with error: " << zmq_strerror(errno); CHECK_EQ(valLen, 4); int fd = val[0]; VLOG(1) << "node [" << msg->sender_ << "] is on file descriptor " << fd; Lock l(fdToNodeIdMu_); fdToNodeId_[fd] = msg->sender_; } zmq_msg_close(zmsg); delete zmsg; } else { // keys & values from sender's message DArray<char> data(buf, size, false); data.pointer().reset(buf, [zmsg](char *) { zmq_msg_close(zmsg); delete zmsg; }); if (i == 2 && msg->task_.has_key()) { msg->key_ = data; } else { msg->value_.push_back(data); } } if (!zmq_msg_more(zmsg)) { CHECK_GT(i, 0); break; } } *recvBytes += dataSize; if (hostnames_[msg->sender_] == myNode_.hostname()) { receivedFromLocal_ += dataSize; } else { receivedFromOthers_ += dataSize; } VLOG(1) << "FROM : " << msg->sender_ << msg->ShortDebugString(); return true; }
int main (int argc, char **argv) { char *endpoint = NULL; void *context, *socket; zmq_pollitem_t pollitems[] = { { NULL, 0, ZMQ_POLLIN, 0 } }; if (argc <= 1) { _usage(argv[0]); return -1; } endpoint = argv[1]; context = zmq_ctx_new(); if (!context) { _ERR("ZeroMQ context: %s\n", zmq_strerror(errno)); return -1; } socket = zmq_socket(context, ZMQ_PULL); if (!socket) { _ERR("ZeroMQ socket: %s\n", zmq_strerror(errno)); zmq_ctx_destroy(context); return -1; } if (zmq_connect(socket, endpoint) == -1) { _ERR("ZeroMQ connect: %s: %s\n", endpoint, zmq_strerror(errno)); zmq_close(socket); zmq_ctx_destroy(context); return -1; } pollitems[0].socket = socket; _signals(); while (!_interrupted) { if (zmq_poll(pollitems, 1, -1) == -1) { break; } if (pollitems[0].revents & ZMQ_POLLIN) { int more; size_t moresz = sizeof(more); while (!_interrupted) { zmq_msg_t zmsg; if (zmq_msg_init(&zmsg) != 0) { break; } if (zmq_recvmsg(socket, &zmsg, 0) == -1) { _ERR("ZeroMQ receive: %s\n", zmq_strerror(errno)); zmq_msg_close(&zmsg); break; } if (zmq_getsockopt(socket, ZMQ_RCVMORE, &more, &moresz) == -1) { _ERR("ZeroMQ option receive: %s\n", zmq_strerror(errno)); more = 0; } fprintf(stdout, "%.*s\n", (int)zmq_msg_size(&zmsg), (char *)zmq_msg_data(&zmsg)); zmq_msg_close(&zmsg); if (!more) { break; } } } } zmq_close(socket); zmq_ctx_destroy(context); return 0; }
inline void *data () { return zmq_msg_data (this); }
int main(int argc, char *argv[]) { zmq_rcvmore_t more; size_t more_sz = sizeof(more); char *exe = argv[0], *filter = "", *pub; int part_num,opt,rc=-1; void *msg_data, *sp, *set=NULL; size_t msg_len; zmq_msg_t part; while ( (opt = getopt(argc, argv, "sd:v+")) != -1) { switch (opt) { case 'v': verbose++; break; case 's': pull_mode++; break; case 'd': dir=strdup(optarg); break; default: usage(exe); break; } } if (!dir) usage(exe); if (optind >= argc) usage(exe); sp = kv_spoolwriter_new(dir); if (!sp) usage(exe); set = kv_set_new(); /* connect socket to each publisher. yes, zeromq lets you connect n times */ if ( !(context = zmq_init(1))) goto done; if ( !(socket = zmq_socket(context, pull_mode?ZMQ_PULL:ZMQ_SUB))) goto done; while (optind < argc) { pub = argv[optind++]; if (zmq_connect(socket, pub)) goto done; } if (!pull_mode) { if (zmq_setsockopt(socket, ZMQ_SUBSCRIBE, filter, strlen(filter))) goto done; } while(1) { /* receive a multi-part message */ part_num=1; do { if ( (rc= zmq_msg_init(&part))) goto done; if ( ((rc= zmq_recvmsg(socket, &part, 0)) == -1) || ((rc= zmq_getsockopt(socket, ZMQ_RCVMORE, &more,&more_sz)) != 0)) { zmq_msg_close(&part); goto done; } msg_data = zmq_msg_data(&part); msg_len = zmq_msg_size(&part); switch(part_num) { /* part 1 has serialized frame */ case 1: if (json_to_frame(sp,set,msg_data,msg_len)) goto done; break; default: assert(0); } zmq_msg_close(&part); part_num++; } while(more); } rc = 0; /* not reached TODO under clean shutdown on signal */ done: if (rc) fprintf(stderr,"%s: %s\n", exe, zmq_strerror(errno)); if(socket) zmq_close(socket); if(context) zmq_term(context); kv_spoolwriter_free(sp); if (set) kv_set_free(set); return rc; }
static int log_send(struct evbuffer *res_buf, thd_data *thd, struct evkeyvalq *get_args) { uint64_t millisec; int threshold, limit; const char *callback, *types, *query, *client_id, *target_name, *learn_target_name; parse_keyval(get_args, &query, &types, &client_id, &target_name, &learn_target_name, &callback, &millisec, &threshold, &limit); /* send data to learn client */ if (thd->zmq_sock && millisec && client_id && query && learn_target_name) { char c; size_t l; msgpack_packer pk; msgpack_sbuffer sbuf; int cnt, submit_flag = 0; msgpack_sbuffer_init(&sbuf); msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); cnt = 4; if (types && !strcmp(types, "submit")) { cnt++; types = NULL; submit_flag = 1; } msgpack_pack_map(&pk, cnt); c = 'i'; msgpack_pack_raw(&pk, 1); msgpack_pack_raw_body(&pk, &c, 1); l = strlen(client_id); msgpack_pack_raw(&pk, l); msgpack_pack_raw_body(&pk, client_id, l); c = 'q'; msgpack_pack_raw(&pk, 1); msgpack_pack_raw_body(&pk, &c, 1); l = strlen(query); msgpack_pack_raw(&pk, l); msgpack_pack_raw_body(&pk, query, l); c = 's'; msgpack_pack_raw(&pk, 1); msgpack_pack_raw_body(&pk, &c, 1); msgpack_pack_uint64(&pk, millisec); c = 'l'; msgpack_pack_raw(&pk, 1); msgpack_pack_raw_body(&pk, &c, 1); l = strlen(learn_target_name); msgpack_pack_raw(&pk, l); msgpack_pack_raw_body(&pk, learn_target_name, l); if (submit_flag) { c = 't'; msgpack_pack_raw(&pk, 1); msgpack_pack_raw_body(&pk, &c, 1); msgpack_pack_true(&pk); } { zmq_msg_t msg; if (!zmq_msg_init_size(&msg, sbuf.size)) { memcpy((void *)zmq_msg_data(&msg), sbuf.data, sbuf.size); if (zmq_send(thd->zmq_sock, &msg, 0)) { print_error("zmq_send() error"); } zmq_msg_close(&msg); } } msgpack_sbuffer_destroy(&sbuf); } /* make result */ { int content_length; if (callback) { content_length = strlen(callback); evbuffer_add(res_buf, callback, content_length); evbuffer_add(res_buf, "(", 1); content_length += suggest_result(res_buf, types, query, target_name, threshold, limit, &(thd->cmd_buf), thd->ctx) + 3; evbuffer_add(res_buf, ");", 2); } else { content_length = suggest_result(res_buf, types, query, target_name, threshold, limit, &(thd->cmd_buf), thd->ctx); } return content_length; } }
int zgossip_msg_recv (zgossip_msg_t *self, zsock_t *input) { assert (input); if (zsock_type (input) == ZMQ_ROUTER) { zframe_destroy (&self->routing_id); self->routing_id = zframe_recv (input); if (!self->routing_id || !zsock_rcvmore (input)) { zsys_warning ("zgossip_msg: no routing ID"); return -1; // Interrupted or malformed } } zmq_msg_t frame; zmq_msg_init (&frame); int size = zmq_msg_recv (&frame, zsock_resolve (input), 0); if (size == -1) { zsys_warning ("zgossip_msg: interrupted"); goto malformed; // Interrupted } // Get and check protocol signature self->needle = (byte *) zmq_msg_data (&frame); self->ceiling = self->needle + zmq_msg_size (&frame); uint16_t signature; GET_NUMBER2 (signature); if (signature != (0xAAA0 | 0)) { zsys_warning ("zgossip_msg: invalid signature"); // TODO: discard invalid messages and loop, and return // -1 only on interrupt goto malformed; // Interrupted } // Get message id and parse per message type GET_NUMBER1 (self->id); switch (self->id) { case ZGOSSIP_MSG_HELLO: { byte version; GET_NUMBER1 (version); if (version != 1) { zsys_warning ("zgossip_msg: version is invalid"); goto malformed; } } break; case ZGOSSIP_MSG_PUBLISH: { byte version; GET_NUMBER1 (version); if (version != 1) { zsys_warning ("zgossip_msg: version is invalid"); goto malformed; } } GET_STRING (self->key); GET_LONGSTR (self->value); GET_NUMBER4 (self->ttl); break; case ZGOSSIP_MSG_PING: { byte version; GET_NUMBER1 (version); if (version != 1) { zsys_warning ("zgossip_msg: version is invalid"); goto malformed; } } break; case ZGOSSIP_MSG_PONG: { byte version; GET_NUMBER1 (version); if (version != 1) { zsys_warning ("zgossip_msg: version is invalid"); goto malformed; } } break; case ZGOSSIP_MSG_INVALID: { byte version; GET_NUMBER1 (version); if (version != 1) { zsys_warning ("zgossip_msg: version is invalid"); goto malformed; } } break; default: zsys_warning ("zgossip_msg: bad message ID"); goto malformed; } // Successful return zmq_msg_close (&frame); return 0; // Error returns malformed: zsys_warning ("zgossip_msg: zgossip_msg malformed message, fail"); zmq_msg_close (&frame); return -1; // Invalid message }