void fmq_server_set_anonymous (fmq_server_t *self, long enabled) { assert (self); zstr_sendm (self->pipe, "SET ANONYMOUS"); zstr_sendf (self->pipe, "%ld", enabled); }
void zyre_set_port (zyre_t *self, int port_nbr) { assert (self); zstr_sendm (self->actor, "SET PORT"); zstr_sendf (self->actor, "%d", port_nbr); }
zmonitor_t * zmonitor_new (zctx_t *ctx, void *socket, int events) { zmonitor_t *self = (zmonitor_t *) zmalloc (sizeof (zmonitor_t)); assert (self); // Start background agent to connect to the inproc monitor socket assert (ctx); self->pipe = zthread_fork (ctx, s_agent_task, NULL); if (self->pipe) { self->socket = socket; // Register a monitor endpoint on the socket char *monitor_endpoint = (char *) zmalloc (100); sprintf (monitor_endpoint, "inproc://zmonitor-%p", self->socket); int rc = zmq_socket_monitor (self->socket, monitor_endpoint, events); assert (rc == 0); // Configure backend agent with monitor endpoint zstr_sendf (self->pipe, "%s", monitor_endpoint); free (monitor_endpoint); char *status = zstr_recv (self->pipe); if (strneq (status, "OK")) zmonitor_destroy (&self); zstr_free (&status); } else { free (self); self = NULL; } return self; }
void curve_server_set_verbose (curve_server_t *self, bool verbose) { assert (self); zstr_sendm (self->control, "VERBOSE"); zstr_sendf (self->control, "%d", verbose); }
// -------------------------------------------------------------------------- // Set the node expiration timeout, in milliseconds. Default is 30000. // This can be tuned in order to deal with expected network conditions // and the response time expected by the application. This is tied to // the beacon interval and rate of messages received. void zyre_set_expired_timeout (zyre_t *self, int interval) { assert (self); zstr_sendm (self->actor, "SET EXPIRED TIMEOUT"); zstr_sendf (self->actor, "%d", interval); }
void zmonitor_set_verbose (zmonitor_t *self, bool verbose) { assert (self); zstr_sendm (self->pipe, "VERBOSE"); zstr_sendf (self->pipe, "%d", verbose); }
static void test_tcp_pub (void *args, zctx_t *ctx, void *pipe) { vtx_t *vtx = vtx_new (ctx); int rc = vtx_tcp_load (vtx, FALSE); assert (rc == 0); char *port = zstr_recv (pipe); // Create publisher socket and bind to all network interfaces void *publisher = vtx_socket (vtx, ZMQ_PUB); assert (publisher); rc = vtx_bind (vtx, publisher, "tcp://*:%s", port); assert (rc == 0); int sent = 0; while (!zctx_interrupted) { zstr_sendf (publisher, "NOM %04x", randof (0x10000)); sent++; char *end = zstr_recv_nowait (pipe); if (end) { free (end); zstr_send (pipe, "OK"); break; } } zclock_log ("I: PUB: sent=%d", sent); free (port); vtx_destroy (&vtx); }
// -------------------------------------------------------------------------- // Set the node evasiveness timeout, in milliseconds. Default is 5000. // This can be tuned in order to deal with expected network conditions // and the response time expected by the application. This is tied to // the beacon interval and rate of messages received. void zyre_set_evasive_timeout (zyre_t *self, int interval) { assert (self); zstr_sendm (self->actor, "SET EVASIVE TIMEOUT"); zstr_sendf (self->actor, "%d", interval); }
void curve_server_set_max_pending (curve_server_t *self, int limit) { assert (self); zstr_sendm (self->control, "MAX PENDING"); zstr_sendf (self->control, "%d", limit); }
void curve_server_set_max_clients (curve_server_t *self, int limit) { assert (self); zstr_sendm (self->control, "MAX CLIENTS"); zstr_sendf (self->control, "%d", limit); }
void curve_server_set_client_ttl (curve_server_t *self, int limit) { assert (self); zstr_sendm (self->control, "CLIENT TTL"); zstr_sendf (self->control, "%d", limit); }
void curve_server_set_pending_ttl (curve_server_t *self, int limit) { assert (self); zstr_sendm (self->control, "PENDING TTL"); zstr_sendf (self->control, "%d", limit); }
void fmq_client_set_resync (fmq_client_t *self, long enabled) { assert (self); zstr_sendm (self->pipe, "SET RESYNC"); zstr_sendf (self->pipe, "%ld", enabled); }
void zyre_set_interval (zyre_t *self, size_t interval) { assert (self); zstr_sendm (self->actor, "SET INTERVAL"); zstr_sendf (self->actor, "%zd", interval); }
void fmq_client_set_inbox (fmq_client_t *self, const char *path) { assert (self); assert (path); zstr_sendm (self->pipe, "SET INBOX"); zstr_sendf (self->pipe, "%s", path); }
void fmq_client_subscribe (fmq_client_t *self, const char *path) { assert (self); assert (path); zstr_sendm (self->pipe, "SUBSCRIBE"); zstr_sendf (self->pipe, "%s", path); }
void zauth_set_verbose (zauth_t *self, bool verbose) { assert (self); zstr_sendm (self->pipe, "VERBOSE"); zstr_sendf (self->pipe, "%d", verbose); zsocket_wait (self->pipe); }
// 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); }
void zauth_set_verbose (zauth_t *self, bool verbose) { assert (self); zstr_sendm (self->pipe, "VERBOSE"); zstr_sendf (self->pipe, "%d", verbose); // Wait for completion free (zstr_recv (self->pipe)); }
int main (int argc, char *argv []) { // First argument is this broker's name // Other arguments are our peers' names // if (argc < 2) { printf ("syntax: peering1 me {you}...\n"); exit (EXIT_FAILURE); } char *self = argv [1]; printf ("I: preparing broker at %s...\n", self); srandom ((unsigned) time (NULL)); zctx_t *ctx = zctx_new (); // Bind state backend to endpoint void *statebe = zsocket_new (ctx, ZMQ_PUB); zsocket_bind (statebe, "ipc://%s-state.ipc", self); // Connect statefe to all peers void *statefe = zsocket_new (ctx, ZMQ_SUB); zsockopt_set_subscribe (statefe, ""); int argn; for (argn = 2; argn < argc; argn++) { char *peer = argv [argn]; printf ("I: connecting to state backend at '%s'\n", peer); zsocket_connect (statefe, "ipc://%s-state.ipc", peer); } // .split main loop // The main loop sends out status messages to peers, and collects // status messages back from peers. The zmq_poll timeout defines // our own heartbeat: while (1) { // Poll for activity, or 1 second timeout zmq_pollitem_t items [] = { { statefe, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (items, 1, 1000 * ZMQ_POLL_MSEC); if (rc == -1) break; // Interrupted // Handle incoming status messages if (items [0].revents & ZMQ_POLLIN) { char *peer_name = zstr_recv (statefe); char *available = zstr_recv (statefe); printf ("%s - %s workers free\n", peer_name, available); free (peer_name); free (available); } else { // Send random values for worker availability zstr_sendm (statebe, self); zstr_sendf (statebe, "%d", randof (10)); } } zctx_destroy (&ctx); return EXIT_SUCCESS; }
void fmq_server_publish (fmq_server_t *self, const char *location, const char *alias) { assert (self); assert (location); assert (alias); zstr_sendm (self->pipe, "PUBLISH"); zstr_sendfm (self->pipe, "%s", location); zstr_sendf (self->pipe, "%s", alias); }
int fmq_server_bind (fmq_server_t *self, const char *endpoint) { assert (self); assert (endpoint); zstr_sendm (self->pipe, "BIND"); zstr_sendf (self->pipe, "%s", endpoint); char *reply = zstr_recv (self->pipe); int rc = atoi (reply); free (reply); return rc; }
int game_thread( void * _parms ) { GameThreadParms * parms = (GameThreadParms*)_parms; GameState gs; SharedRenderState rs; game_init( gs, rs ); gs.zmq_control_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR ); { int ret = zsocket_connect( gs.zmq_control_socket, "inproc://control_game" ); assert( ret == 0 ); } gs.zmq_render_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR ); zsocket_bind( gs.zmq_render_socket, "inproc://game_render" ); gs.zmq_input_req = zsocket_new( parms->zmq_context, ZMQ_REQ ); { int ret = zsocket_connect( gs.zmq_input_req, "inproc://input" ); assert( ret == 0 ); } unsigned int baseline = SDL_GetTicks(); int framenum = 0; while ( true ) { unsigned int now = SDL_GetTicks(); unsigned int target_frame = ( now - baseline ) / GAME_DELAY; if ( framenum <= target_frame ) { framenum++; // NOTE: build the state of the world at t = framenum * GAME_DELAY, // under normal conditions that's a time in the future // (the exception to that is if we are catching up on ticking game frames) game_tick( now, gs, rs ); // notify the render thread that a new game state is ready. // on the next render frame, it will start interpolating between the previous state and this new one zstr_sendf( gs.zmq_render_socket, "%d %f %f %f %f %f %f %f %f %f", baseline + framenum * GAME_DELAY, rs.position.x, rs.position.y, rs.orientation.w, rs.orientation.x, rs.orientation.y, rs.orientation.z, rs.smoothed_angular.x, rs.smoothed_angular.y, rs.smoothed_angular.z ); } else { int ahead = framenum * GAME_DELAY - ( now - baseline ); assert( ahead > 0 ); printf( "game sleep %d ms\n", ahead ); SDL_Delay( ahead ); } char * cmd = zstr_recv_nowait( gs.zmq_control_socket ); if ( cmd != NULL ) { assert( strcmp( cmd, "stop" ) == 0 ); free( cmd ); break; } } return 0; }
int main (int argn, char *argv []) { if (argn < 2) { puts ("syntax: ./zyrechat myname"); exit (0); } zctx_t *ctx = zctx_new (); void *chat_pipe = zthread_fork (ctx, chat_task, NULL); while (!zctx_interrupted) { char message [1024]; if (!fgets (message, 1024, stdin)) break; zstr_sendf (chat_pipe, "%s:%s", argv [1], message); } zctx_destroy (&ctx); return 0; }
// Request-reply client using REQ socket // To simulate load, clients issue a burst of requests and then // sleep for a random period. // static void * client_task (void *args) { zctx_t *ctx = zctx_new (); void *client = zsocket_new (ctx, ZMQ_REQ); zsocket_connect (client, "ipc://%s-localfe.ipc", self); void *monitor = zsocket_new (ctx, ZMQ_PUSH); zsocket_connect (monitor, "ipc://%s-monitor.ipc", self); while (1) { sleep (randof (5)); int burst = randof (15); while (burst--) { char task_id [5]; sprintf (task_id, "%04X", randof (0x10000)); // Send request with random hex ID zstr_send (client, task_id); // Wait max ten seconds for a reply, then complain zmq_pollitem_t pollset [1] = { { client, 0, ZMQ_POLLIN, 0 } }; int rc = zmq_poll (pollset, 1, 10 * 1000 * ZMQ_POLL_MSEC); if (rc == -1) break; // Interrupted if (pollset [0].revents & ZMQ_POLLIN) { char *reply = zstr_recv (client); if (!reply) break; // Interrupted // Worker is supposed to answer us with our task id puts (reply); assert (streq (reply, task_id)); free (reply); } else { zstr_sendf (monitor, "E: CLIENT EXIT - lost task %s", task_id); return NULL; } } } zctx_destroy (&ctx); return NULL; }
static void process_the_patch (client_t *self, server_t *server) { char *inbox = fmq_config_resolve (self->config, "client/inbox", ".inbox"); char *filename = fmq_msg_filename (server->reply); // Filenames from server must start with slash, which we skip assert (*filename == '/'); filename++; if (fmq_msg_operation (server->reply) == FMQ_MSG_FILE_CREATE) { if (server->file == NULL) { server->file = fmq_file_new (inbox, filename); if (fmq_file_output (server->file)) { // File not writeable, skip patch fmq_file_destroy (&server->file); return; } } // Try to write, ignore errors in this version zframe_t *frame = fmq_msg_chunk (server->reply); fmq_chunk_t *chunk = fmq_chunk_new (zframe_data (frame), zframe_size (frame)); if (fmq_chunk_size (chunk) > 0) { fmq_file_write (server->file, chunk, fmq_msg_offset (server->reply)); server->credit -= fmq_chunk_size (chunk); } else { // Zero-sized chunk means end of file, so report back to caller zstr_sendm (self->pipe, "DELIVER"); zstr_sendm (self->pipe, filename); zstr_sendf (self->pipe, "%s/%s", inbox, filename); fmq_file_destroy (&server->file); } fmq_chunk_destroy (&chunk); } else if (fmq_msg_operation (server->reply) == FMQ_MSG_FILE_DELETE) { zclock_log ("I: delete %s/%s", inbox, filename); fmq_file_t *file = fmq_file_new (inbox, filename); fmq_file_remove (file); fmq_file_destroy (&file); } }
int recvHandler(zloop_t *loop, zmq_pollitem_t *item, void *client) { zmsg_t *msg = zmsg_recv(client); zmsg_dump(msg); zstr_sendm(client, zmsg_popstr(msg)); zmsg_destroy(&msg); received++; zstr_sendm(client, ""); zstr_sendf(client, "pong:%d", ++sent); if (sent >= count) { return -1; } return 0; }
rsRetVal outputCZMQ(uchar* msg, instanceData* pData) { DEFiRet; /* if auth or socket is missing, initialize */ if((NULL == pData->sock) || (NULL == pData->authActor)) { CHKiRet(initCZMQ(pData)); } /* if we have a ZMQ_PUB sock and topics, send once per topic */ if (pData->sockType == ZMQ_PUB && pData->topics) { char *topic = zlist_first(pData->topics); while (topic) { if (pData->topicFrame) { int rc = zstr_sendx(pData->sock, topic, (char*)msg, NULL); if (rc != 0) { pData->sendError = true; ABORT_FINALIZE(RS_RET_SUSPENDED); } } else { int rc = zstr_sendf(pData->sock, "%s%s", topic, (char*)msg); if (rc != 0) { pData->sendError = true; ABORT_FINALIZE(RS_RET_SUSPENDED); } } topic = zlist_next(pData->topics); } } /* otherwise do a normal send */ else { int rc = zstr_send(pData->sock, (char*)msg); if (rc != 0) { pData->sendError = true; ABORT_FINALIZE(RS_RET_SUSPENDED); } } finalize_it: RETiRet; }
int main (void) { zctx_t *ctx = zctx_new (); void *dealer = zsocket_new (ctx, ZMQ_DEALER); zsocket_connect (dealer, "tcp://127.0.0.1:6000"); // We'll allow up to N chunks in transit at once size_t credit = PIPELINE; size_t total = 0; // Total bytes received size_t chunks = 0; // Total chunks received size_t offset = 0; // Offset of next chunk request size_t offset_expc = 0; while (true) { while (credit) { // Ask for next chunk zstr_sendfm (dealer, "fetch"); zstr_sendfm (dealer, "%ld", offset); zstr_sendf (dealer, "%ld", CHUNK_SIZE); offset += CHUNK_SIZE; credit--; } zframe_t *chunk = zframe_recv (dealer); if (!chunk) break; // Shutting down, quit chunks++; credit++; size_t size = zframe_size (chunk); zframe_destroy (&chunk); total += size; if (size < CHUNK_SIZE) break; // Last chunk received; exit } printf ("%zd chunks received, %zd bytes\n", chunks, total); zctx_destroy (&ctx); return total == 102400? 0: -1; }
int main (void) { zctx_t *ctx = zctx_new (); void *mailbox = zsocket_new (ctx, ZMQ_DEALER); zsocket_set_sndhwm (mailbox, 4); zsocket_set_sndtimeo (mailbox, 0); zsocket_connect (mailbox, "tcp://localhost:9876"); int count; for (count = 0; count < 10; count++) { printf ("Sending message %d\n", count); int rc = zstr_sendf (mailbox, "message %d", count); if (rc == -1) { printf ("%s\n", strerror (errno)); break; } } zctx_destroy (&ctx); return 0; }