static void collabclient_remakeSockets( cloneclient_t *cc ) { cc->snapshot = zsocket_new (cc->ctx, ZMQ_DEALER); zsocket_connect (cc->snapshot, collabclient_makeAddressString( cc->address, cc->port + socket_offset_snapshot)); cc->subscriber = zsocket_new (cc->ctx, ZMQ_SUB); zsocket_set_subscribe (cc->subscriber, ""); zsocket_connect (cc->subscriber, collabclient_makeAddressString( cc->address, cc->port + socket_offset_subscriber)); zsocket_set_subscribe (cc->subscriber, SUBTREE); cc->publisher = zsocket_new (cc->ctx, ZMQ_PUSH); zsocket_connect (cc->publisher, collabclient_makeAddressString( cc->address, cc->port + socket_offset_publisher)); int fd = 0; size_t fdsz = sizeof(fd); int rc = zmq_getsockopt( cc->subscriber, ZMQ_FD, &fd, &fdsz ); printf("subscriber rc:%d fd:%d\n", rc, fd ); GDrawAddReadFD( 0, fd, cc, zeromq_subscriber_fd_callback ); }
int main(int argc, const char **argv){ zctx_t *ctx = zctx_new(); void *sub = zsocket_new(ctx, ZMQ_SUB); zsocket_set_subscribe(sub, ""); zsocket_connect(sub, "tcp://127.0.0.1:3000"); printf("connected to :3000\n"); struct timeval start; gettimeofday(&start, NULL); while (1) { char *msg = zstr_recv(sub); if (0 == n--) { struct timeval end; gettimeofday(&end, NULL); int duration = end.tv_sec - start.tv_sec; printf("\n"); printf(" pub/sub:\n"); printf(" %d msgs\n", ops); printf(" %d ops/s\n", ops / duration); printf(" %d s\n", duration); printf("\n"); exit(0); } } return 0; }
bstar_t * bstar_new (int primary, char *local, char *remote) { bstar_t *self; self = (bstar_t *) zmalloc (sizeof (bstar_t)); // Initialize the Binary Star self->ctx = zctx_new (); self->loop = zloop_new (); self->state = primary? STATE_PRIMARY: STATE_BACKUP; // Create publisher for state going to peer self->statepub = zsocket_new (self->ctx, ZMQ_PUB); zsocket_bind (self->statepub, local); // Create subscriber for state coming from peer self->statesub = zsocket_new (self->ctx, ZMQ_SUB); zsocket_set_subscribe (self->statesub, ""); zsocket_connect (self->statesub, remote); // Set-up basic reactor events zloop_timer (self->loop, BSTAR_HEARTBEAT, 0, s_send_state, self); zmq_pollitem_t poller = { self->statesub, 0, ZMQ_POLLIN }; zloop_poller (self->loop, &poller, s_recv_state, self); return self; }
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"); return 0; } 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); zsocket_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 (true) { // 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; }
static void subscriber_thread (void *args, zctx_t *ctx, void *pipe) { // Subscribe to "A" and "B" void *subscriber = zsocket_new (ctx, ZMQ_SUB); zsocket_connect (subscriber, "tcp://localhost:6001"); zsocket_set_subscribe (subscriber, "A"); zsocket_set_subscribe (subscriber, "B"); int count = 0; while (count < 5) { char *string = zstr_recv (subscriber); if (!string) break; // Interrupted free (string); count++; } zsocket_destroy (ctx, subscriber); }
/** * Setup the location to receive messages. * @param location */ void Alien::PrepareToBeShot(const std::string& location) { //Subscribe to everything char dummy = '\0'; zsocket_set_subscribe(mBody, &dummy); zsocket_set_rcvhwm(mBody, 32 * 1024); zsocket_set_sndhwm(mBody, 32 * 1024); int rc = zsocket_connect(mBody, location.c_str()); if (rc == -1) { LOG(WARNING) << "location: " << location; LOG(WARNING) << "connect socket rc == " << rc; throw std::string("Failed to connect to socket"); } }
static void ReConnectSignals() { int err = 0; const proto::ServerInfo& sinfo = gServerInfo; err |= zsocket_disconnect(gSignals, "tcp://%s:%d", sinfo.host().c_str(), sinfo.pub()); err |= zsocket_connect(gSignals, "tcp://%s:%d", sinfo.host().c_str(), sinfo.pub()); const char one[2] = {1, 0}; zsocket_set_subscribe(gSignals, one); if(err){ printf("ReConnectSignals(): FAIL\n"); } }
int main (int argc, char *argv []) { zctx_t *ctx = zctx_new (); // Use the CZMQ zbeacon class to make sure we listen on the // same network interface as our peers void *collector = zsocket_new (ctx, ZMQ_SUB); zbeacon_t *beacon = zbeacon_new (ZRE_DISCOVERY_PORT); char *host = zbeacon_hostname (beacon); // Bind to an ephemeral port int port = zsocket_bind (collector, "tcp://%s:*", host); // Announce this to all peers we connect to zre_node_t *node = zre_node_new (); zre_node_header_set (node, "X-ZRELOG", "tcp://%s:%d", host, port); // Get all log messages (don't filter) zsocket_set_subscribe (collector, ""); zmq_pollitem_t pollitems [] = { { collector, 0, ZMQ_POLLIN, 0 }, { zre_node_handle (node), 0, ZMQ_POLLIN, 0 } }; while (!zctx_interrupted) { if (zmq_poll (pollitems, 2, 1000 * ZMQ_POLL_MSEC) == -1) break; // Interrupted // Handle input on collector if (pollitems [0].revents & ZMQ_POLLIN) s_print_log_msg (collector); // Handle event from node (ignore it) if (pollitems [1].revents & ZMQ_POLLIN) { zmsg_t *msg = zre_node_recv (node); if (!msg) break; // Interrupted zmsg_destroy (&msg); } } zre_node_destroy (&node); zbeacon_destroy (&beacon); zctx_destroy (&ctx); return 0; }
int main (void) { zctx_t *ctx = zctx_new (); void *subscriber = zsocket_new (ctx, ZMQ_SUB); zsocket_connect (subscriber, "tcp://localhost:30001"); zsocket_set_subscribe (subscriber, ""); while (true) { zmsg_t *msg = zmsg_recv (subscriber); if (!msg) break; zmsg_dump (msg); zmsg_destroy (&msg); } zctx_destroy (&ctx); return 0; }
static void listener_task (void *args, zctx_t *ctx, void *pipe) { void *listener = zsocket_new (ctx, ZMQ_SUB); int address; for (address = 1; address < 255; address++) { int rc = zsocket_connect (listener, "tcp://%s%d:9000", (char *) args, address); assert (rc == 0); } zsocket_set_subscribe (listener, ""); while (!zctx_interrupted) { char *message = zstr_recv (listener); if (message) { printf ("%s", message); free (message); } } }
static bool ConnectSignals() { const proto::ServerInfo& sinfo = gServerInfo; printf("Connecting to signals: %s:%d ...\n", sinfo.host().c_str(), sinfo.pub()); zsocket_destroy(gCtx, gSignals); gSignals = zsocket_new(gCtx, ZMQ_SUB); int err = zsocket_connect(gSignals, "tcp://%s:%d", sinfo.host().c_str(), sinfo.pub()); if(err){ printf("ERROR: invalid hostname and/or port.\n"); return false; } const char one[2] = {1, 0}; zsocket_set_subscribe(gSignals, one); return true; }
void zyre_log_test (bool verbose) { printf (" * zyre_log: "); // @selftest zctx_t *ctx = zctx_new (); // Get all incoming log messages void *collector = zsocket_new (ctx, ZMQ_SUB); zsocket_bind (collector, "tcp://127.0.0.1:5555"); zsocket_set_subscribe (collector, ""); // Create a log instance to send log messages zyre_log_t *log = zyre_log_new (ctx, "this is me"); zyre_log_connect (log, "tcp://127.0.0.1:5555"); // Workaround for issue 270; give time for connect to // happen and subscriptions to go to pub socket; 200 // msec should be enough for under valgrind on a slow PC zpoller_t *poller = zpoller_new (collector, NULL); zpoller_wait (poller, 200); // Send some messages zyre_log_info (log, ZRE_LOG_MSG_EVENT_JOIN, NULL, "this is you"); zyre_log_info (log, ZRE_LOG_MSG_EVENT_EXIT, "Pizza time", "this is you"); zyre_log_warning (log, "this is you", "Time flies like an %s", "arrow"); zyre_log_error (log, "this is you", "Fruit flies like a %s", "banana"); int count = 0; while (count < 4) { zre_log_msg_t *msg = zre_log_msg_recv (collector); assert (msg); if (verbose) zre_log_msg_dump (msg); zre_log_msg_destroy (&msg); count++; } zpoller_destroy (&poller); zyre_log_destroy (&log); zctx_destroy (&ctx); // @end printf ("OK\n"); }
void *zeromq_create_socket (zctx_t *context, const char *endpoint, int type, const char *topic, bool connect, int linger, int hwm) { void *socket = NULL; assert(context); assert(endpoint); if (endpoint[0] == 0) { errorLog("Trying to open NULL output socket !"); return NULL; } socket = zsocket_new (context, type); if (hwm != -1) { zsocket_set_sndhwm (socket, hwm); zsocket_set_rcvhwm (socket, hwm); } if (linger != -1) { zsocket_set_linger (socket, linger); } else { // Set linger to 0 as default, safety measure zsocket_set_linger (socket, 0); } // Allow for exponential backoff: up to 5 minutes and 1s minimum zsocket_set_reconnect_ivl(socket, 1 * 1000); zsocket_set_reconnect_ivl_max(socket, 5 * 60 * 1000); if (type == ZMQ_SUB) zsocket_set_subscribe (socket, (char*)(topic == NULL ? "" : topic)); if (connect) { zsocket_connect (socket, "%s", endpoint); } else { zsocket_bind (socket, "%s", endpoint); } return socket; }
int main (int argc, char *argv []) { zctx_t *ctx = zctx_new (); // Use the CZMQ zbeacon class to make sure we listen on the // same network interface as our peers zbeacon_t *beacon = zbeacon_new (ctx, ZRE_DISCOVERY_PORT); char *host = zbeacon_hostname (beacon); // Bind to an ephemeral port void *collector = zsocket_new (ctx, ZMQ_SUB); int port = zsocket_bind (collector, "tcp://%s:*", host); zsocket_set_subscribe (collector, ""); // Announce this to all peers we connect to zyre_t *node = zyre_new (ctx); zyre_set_header (node, "X-ZRELOG", "tcp://%s:%d", host, port); zyre_start (node); zpoller_t *poller = zpoller_new (collector, zyre_socket (node), NULL); while (!zctx_interrupted) { void *which = zpoller_wait (poller, -1); if (which == collector) s_print_log_msg (collector); else if (which == zyre_socket (node)) { zmsg_t *msg = zyre_recv (node); if (!msg) break; // Interrupted zmsg_destroy (&msg); } else break; // Interrupted } zyre_destroy (&node); zbeacon_destroy (&beacon); zctx_destroy (&ctx); return 0; }
bstar_t * bstar_new(int primary, char *local, char *remote) { bstar_t *self; self = (bstar_t *)zmalloc(sizeof(bstar_t)); self->ctx = zctx_new(); self->loop = zloop_new(); self->state = primary ? STATE_PRIMARY : STATE_BACKUP; self->statepub = zsocket_new(self->ctx, ZMQ_PUB); zsocket_bind(self->statepub, local); self->statesub = zsocket_new(self->ctx, ZMQ_SUB); zsocket_set_subscribe(self->statesub, ""); zsocket_connect(self->statesub, remote); zloop_timer(self->loop, BSTAR_HEARTBEAT, 0, s_send_state, self); zmq_pollitem_t poller = {self->statesub, 0, ZMQ_POLLIN}; zloop_poller(self->loop, &poller, s_recv_state, self); return self; }
void *client_task (void *args) { zctx_t *ctx = zctx_new (); void *subscriber = zsocket_new (ctx, ZMQ_SUB); void *pusher = zsocket_new (ctx, ZMQ_PUSH); zsocket_connect (subscriber, "tcp://localhost:5563"); zsocket_connect (pusher, "tcp://localhost:5564"); int i; for (i = 0; i < 1000000; i++) { zsocket_set_subscribe (subscriber, "A"); zmq_pollitem_t polla [] = { { pusher, 0, ZMQ_POLLOUT, 0 } }; if (zmq_poll (polla, 1, 100) == 1) zstr_send (pusher, "B"); zmq_pollitem_t pollb [] = { { subscriber, 0, ZMQ_POLLIN, 0 } }; if (zmq_poll (pollb, 1, 100) == 1) free (zstr_recv (subscriber)); zsocket_set_unsubscribe (subscriber, "A"); } return NULL; }
void PrimeWorker::Work(zctx_t *ctx, void *pipe) { printf("PrimeWorker started.\n"); mBackend = zsocket_new(ctx, ZMQ_DEALER); void* frontend = zsocket_new(ctx, ZMQ_DEALER); void* input = zsocket_new(ctx, ZMQ_SUB); mServer = zsocket_new(ctx, ZMQ_ROUTER); mSignals = zsocket_new(ctx, ZMQ_PUB); zsocket_set_sndhwm(mBackend, 1*1000*1000); int err = 0; err = zsocket_bind(mServer, "tcp://*:%d", mServerPort); if(!err) printf("zsocket_bind(mServer, tcp://*:*) failed.\n"); err = zsocket_bind(mSignals, "tcp://*:%d", mSignalPort); if(!err) printf("zsocket_bind(mSignals, tcp://*:*) failed.\n"); printf("PrimeWorker: mServerPort=%d mSignalPort=%d\n", mServerPort, mSignalPort); err = zsocket_connect(mBackend, "tcp://localhost:8888"); assert(!err); err = zsocket_connect(frontend, "tcp://localhost:7777"); assert(!err); err = zsocket_connect(input, "inproc://bitcoin"); assert(!err); const char one[2] = {1, 0}; zsocket_set_subscribe(input, one); zloop_t* wloop = zloop_new(); zmq_pollitem_t item_input = {input, 0, ZMQ_POLLIN, 0}; err = zloop_poller(wloop, &item_input, &PrimeWorker::InvokeInput, this); assert(!err); zmq_pollitem_t item_server = {mServer, 0, ZMQ_POLLIN, 0}; err = zloop_poller(wloop, &item_server, &PrimeWorker::InvokeRequest, this); assert(!err); zmq_pollitem_t item_frontend = {frontend, 0, ZMQ_POLLIN, 0}; err = zloop_poller(wloop, &item_frontend, &PrimeWorker::InvokeRequest, this); assert(!err); err = zloop_timer(wloop, 60000, 0, &PrimeWorker::InvokeTimerFunc, this); assert(err >= 0); zsocket_signal(pipe); zloop_start(wloop); zloop_destroy(&wloop); zsocket_destroy(ctx, mServer); zsocket_destroy(ctx, mSignals); zsocket_destroy(ctx, mBackend); zsocket_destroy(ctx, frontend); zsocket_destroy(ctx, input); zsocket_signal(pipe); printf("PrimeWorker exited.\n"); }
void worker_fn (void *arg, zctx_t * ctx, void *pipe) { int rc; worker_args_t *args = (worker_args_t *) arg; //connect void *pull = zsocket_new (ctx, ZMQ_PULL); rc = zsocket_connect (pull, "inproc://bind_point"); //connect void *sub = zsocket_new (ctx, ZMQ_SUB); rc = zsocket_connect (sub, "inproc://pub_point"); //connect void *dealer = zsocket_new (ctx, ZMQ_DEALER); rc = zsocket_connect (dealer, "inproc://response_point"); //subscribing to all messages zsocket_set_subscribe (sub, ""); unsigned int counter = 0; size_t vallen; char *errptr; int stop = 0; int timeout = 0; zmq_pollitem_t pollitem[] = { {pull, 0, ZMQ_POLLIN, 0}, {sub, 0, ZMQ_POLLIN, 0}, }; while (1) { rc = zmq_poll (pollitem, 2, timeout); assert (rc != -1); if (pollitem[0].revents & ZMQ_POLLIN) { unsigned int key; zframe_t *frame = zframe_recv_nowait (pull); memcpy (&key, zframe_data (frame), 4); zframe_destroy (&frame); free (leveldb_get (*(args->db), args->readoptions, (const char *) &key, 4, &vallen, &errptr)); counter++; } else { if (stop) { zframe_t *frame = zframe_new (&counter, 4); zframe_send (&frame, dealer, 0); counter = 0; stop = 0; } else { //request more zframe_t *frame = zframe_new ("m", strlen ("m")); zframe_send (&frame, dealer, 0); zclock_sleep(1); } } if (pollitem[1].revents & ZMQ_POLLIN) { zframe_t *frame = zframe_recv_nowait (sub); memcpy (&stop, zframe_data (frame), 4); zframe_destroy (&frame); } } }
static rsRetVal createSocket(socket_info* info, void** sock) { size_t ii; int rv; *sock = zsocket_new(s_context, info->type); if (!sock) { errmsg.LogError(0, RS_RET_INVALID_PARAMS, "zsocket_new failed: %s, for type %d", strerror(errno),info->type); /* DK: invalid params seems right here */ return RS_RET_INVALID_PARAMS; } /* Set options *before* the connect/bind. */ if (info->identity) zsocket_set_identity(*sock, info->identity); if (info->sndBuf > -1) zsocket_set_sndbuf(*sock, info->sndBuf); if (info->rcvBuf > -1) zsocket_set_rcvbuf(*sock, info->rcvBuf); if (info->linger > -1) zsocket_set_linger(*sock, info->linger); if (info->backlog > -1) zsocket_set_backlog(*sock, info->backlog); if (info->sndTimeout > -1) zsocket_set_sndtimeo(*sock, info->sndTimeout); if (info->rcvTimeout > -1) zsocket_set_rcvtimeo(*sock, info->rcvTimeout); if (info->maxMsgSize > -1) zsocket_set_maxmsgsize(*sock, info->maxMsgSize); if (info->rate > -1) zsocket_set_rate(*sock, info->rate); if (info->recoveryIVL > -1) zsocket_set_recovery_ivl(*sock, info->recoveryIVL); if (info->multicastHops > -1) zsocket_set_multicast_hops(*sock, info->multicastHops); if (info->reconnectIVL > -1) zsocket_set_reconnect_ivl(*sock, info->reconnectIVL); if (info->reconnectIVLMax > -1) zsocket_set_reconnect_ivl_max(*sock, info->reconnectIVLMax); if (info->ipv4Only > -1) zsocket_set_ipv4only(*sock, info->ipv4Only); if (info->affinity > -1) zsocket_set_affinity(*sock, info->affinity); /* since HWM have defaults, we always set them. No return codes to check, either.*/ zsocket_set_sndhwm(*sock, info->sndHWM); zsocket_set_rcvhwm(*sock, info->rcvHWM); /* Set subscriptions.*/ if (info->type == ZMQ_SUB) { for (ii = 0; ii < sizeof(info->subscriptions)/sizeof(char*); ++ii) zsocket_set_subscribe(*sock, info->subscriptions[ii]); } /* Do the bind/connect... */ if (info->action==ACTION_CONNECT) { rv = zsocket_connect(*sock, info->description); if (rv < 0) { errmsg.LogError(0, RS_RET_INVALID_PARAMS, "zmq_connect using %s failed: %s", info->description, strerror(errno)); return RS_RET_INVALID_PARAMS; } } else { rv = zsocket_bind(*sock, info->description); if (rv <= 0) { errmsg.LogError(0, RS_RET_INVALID_PARAMS, "zmq_bind using %s failed: %s", info->description, strerror(errno)); return RS_RET_INVALID_PARAMS; } } return RS_RET_OK; }
int zsockopt_test (bool verbose) { printf (" * zsockopt: "); // @selftest zctx_t *ctx = zctx_new (); assert (ctx); void *zocket; #if (ZMQ_VERSION_MAJOR == 2) # if defined (ZMQ_HWM) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_hwm (zocket, 1); assert (zsocket_hwm (zocket) == 1); zsocket_hwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SWAP) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_swap (zocket, 1); assert (zsocket_swap (zocket) == 1); zsocket_swap (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_AFFINITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_affinity (zocket, 1); assert (zsocket_affinity (zocket) == 1); zsocket_affinity (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IDENTITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_identity (zocket, "test"); char *identity = zsocket_identity (zocket); assert (identity); free (identity); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RATE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rate (zocket, 1); assert (zsocket_rate (zocket) == 1); zsocket_rate (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl (zocket, 1); assert (zsocket_recovery_ivl (zocket) == 1); zsocket_recovery_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL_MSEC) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl_msec (zocket, 1); assert (zsocket_recovery_ivl_msec (zocket) == 1); zsocket_recovery_ivl_msec (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MCAST_LOOP) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_mcast_loop (zocket, 1); assert (zsocket_mcast_loop (zocket) == 1); zsocket_mcast_loop (zocket); zsocket_destroy (ctx, zocket); # endif # if (ZMQ_VERSION_MINOR == 2) # if defined (ZMQ_RCVTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvtimeo (zocket, 1); assert (zsocket_rcvtimeo (zocket) == 1); zsocket_rcvtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # endif # if (ZMQ_VERSION_MINOR == 2) # if defined (ZMQ_SNDTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndtimeo (zocket, 1); assert (zsocket_sndtimeo (zocket) == 1); zsocket_sndtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # endif # if defined (ZMQ_SNDBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndbuf (zocket, 1); assert (zsocket_sndbuf (zocket) == 1); zsocket_sndbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvbuf (zocket, 1); assert (zsocket_rcvbuf (zocket) == 1); zsocket_rcvbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LINGER) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_linger (zocket, 1); assert (zsocket_linger (zocket) == 1); zsocket_linger (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl (zocket, 1); assert (zsocket_reconnect_ivl (zocket) == 1); zsocket_reconnect_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL_MAX) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl_max (zocket, 1); assert (zsocket_reconnect_ivl_max (zocket) == 1); zsocket_reconnect_ivl_max (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_BACKLOG) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_backlog (zocket, 1); assert (zsocket_backlog (zocket) == 1); zsocket_backlog (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_subscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_UNSUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_unsubscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_TYPE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_type (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVMORE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_rcvmore (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_FD) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_fd (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_EVENTS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_events (zocket); zsocket_destroy (ctx, zocket); # endif #endif #if (ZMQ_VERSION_MAJOR == 3) # if defined (ZMQ_TYPE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_type (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDHWM) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_sndhwm (zocket, 1); assert (zsocket_sndhwm (zocket) == 1); zsocket_sndhwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVHWM) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvhwm (zocket, 1); assert (zsocket_rcvhwm (zocket) == 1); zsocket_rcvhwm (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_AFFINITY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_affinity (zocket, 1); assert (zsocket_affinity (zocket) == 1); zsocket_affinity (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_subscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_UNSUBSCRIBE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_unsubscribe (zocket, "test"); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IDENTITY) zocket = zsocket_new (ctx, ZMQ_DEALER); assert (zocket); zsocket_set_identity (zocket, "test"); char *identity = zsocket_identity (zocket); assert (identity); free (identity); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RATE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rate (zocket, 1); assert (zsocket_rate (zocket) == 1); zsocket_rate (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECOVERY_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_recovery_ivl (zocket, 1); assert (zsocket_recovery_ivl (zocket) == 1); zsocket_recovery_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDBUF) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_sndbuf (zocket, 1); assert (zsocket_sndbuf (zocket) == 1); zsocket_sndbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVBUF) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvbuf (zocket, 1); assert (zsocket_rcvbuf (zocket) == 1); zsocket_rcvbuf (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LINGER) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_linger (zocket, 1); assert (zsocket_linger (zocket) == 1); zsocket_linger (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl (zocket, 1); assert (zsocket_reconnect_ivl (zocket) == 1); zsocket_reconnect_ivl (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RECONNECT_IVL_MAX) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_reconnect_ivl_max (zocket, 1); assert (zsocket_reconnect_ivl_max (zocket) == 1); zsocket_reconnect_ivl_max (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_BACKLOG) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_backlog (zocket, 1); assert (zsocket_backlog (zocket) == 1); zsocket_backlog (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MAXMSGSIZE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_maxmsgsize (zocket, 1); assert (zsocket_maxmsgsize (zocket) == 1); zsocket_maxmsgsize (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_MULTICAST_HOPS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_multicast_hops (zocket, 1); assert (zsocket_multicast_hops (zocket) == 1); zsocket_multicast_hops (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_rcvtimeo (zocket, 1); assert (zsocket_rcvtimeo (zocket) == 1); zsocket_rcvtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_SNDTIMEO) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_sndtimeo (zocket, 1); assert (zsocket_sndtimeo (zocket) == 1); zsocket_sndtimeo (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_IPV4ONLY) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_set_ipv4only (zocket, 1); assert (zsocket_ipv4only (zocket) == 1); zsocket_ipv4only (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_DELAY_ATTACH_ON_CONNECT) zocket = zsocket_new (ctx, ZMQ_PUB); assert (zocket); zsocket_set_delay_attach_on_connect (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_ROUTER_MANDATORY) zocket = zsocket_new (ctx, ZMQ_ROUTER); assert (zocket); zsocket_set_router_mandatory (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_ROUTER_RAW) zocket = zsocket_new (ctx, ZMQ_ROUTER); assert (zocket); zsocket_set_router_raw (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_XPUB_VERBOSE) zocket = zsocket_new (ctx, ZMQ_XPUB); assert (zocket); zsocket_set_xpub_verbose (zocket, 1); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_RCVMORE) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_rcvmore (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_FD) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_fd (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_EVENTS) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); zsocket_events (zocket); zsocket_destroy (ctx, zocket); # endif # if defined (ZMQ_LAST_ENDPOINT) zocket = zsocket_new (ctx, ZMQ_SUB); assert (zocket); char *last_endpoint = zsocket_last_endpoint (zocket); assert (last_endpoint); free (last_endpoint); zsocket_destroy (ctx, zocket); # endif zocket = zsocket_new (ctx, ZMQ_SUB); zsocket_set_hwm (zocket, 1); #endif zctx_destroy (&ctx); // @end printf ("OK\n"); return 0; }
int main (void) { zctx_t *context = zctx_new (); void *frontend = zsocket_new (context, ZMQ_SUB); zsocket_bind (frontend, "tcp://*:5557"); void *backend = zsocket_new (context, ZMQ_XPUB); zsocket_bind (backend, "tcp://*:5558"); // Subscribe to every single topic from publisher zsocket_set_subscribe (frontend, ""); // Store last instance of each topic in a cache zhash_t *cache = zhash_new (); // .split main poll loop // We route topic updates from frontend to backend, and // we handle subscriptions by sending whatever we cached, // if anything: while (true) { zmq_pollitem_t items [] = { { frontend, 0, ZMQ_POLLIN, 0 }, { backend, 0, ZMQ_POLLIN, 0 } }; if (zmq_poll (items, 2, 1000 * ZMQ_POLL_MSEC) == -1) break; // Interrupted // Any new topic data we cache and then forward if (items [0].revents & ZMQ_POLLIN) { char *topic = zstr_recv (frontend); char *current = zstr_recv (frontend); if (!topic) break; char *previous = zhash_lookup (cache, topic); if (previous) { zhash_delete (cache, topic); free (previous); } zhash_insert (cache, topic, current); zstr_sendm (backend, topic); zstr_send (backend, current); free (topic); } // .split handle subscriptions // When we get a new subscription we pull data from the cache: if (items [1].revents & ZMQ_POLLIN) { zframe_t *frame = zframe_recv (backend); if (!frame) break; // Event is one byte 0=unsub or 1=sub, followed by topic byte *event = zframe_data (frame); if (event [0] == 1) { char *topic = zmalloc (zframe_size (frame)); memcpy (topic, event + 1, zframe_size (frame) - 1); printf ("Sending cached topic %s\n", topic); char *previous = zhash_lookup (cache, topic); if (previous) { zstr_sendm (backend, topic); zstr_send (backend, previous); } free (topic); } zframe_destroy (&frame); } } zctx_destroy (&context); zhash_destroy (&cache); return 0; }
int main (int argc, char *argv []) { clonesrv_t *self = (clonesrv_t *) zmalloc (sizeof (clonesrv_t)); if (argc == 2 && streq (argv [1], "-p")) { zclock_log ("I: primary active, waiting for backup (passive)"); self->bstar = bstar_new (BSTAR_PRIMARY, "tcp://*:5003", "tcp://localhost:5004"); bstar_voter (self->bstar, "tcp://*:5556", ZMQ_ROUTER, s_snapshots, self); self->port = 5556; self->peer = 5566; self->primary = true; } else if (argc == 2 && streq (argv [1], "-b")) { zclock_log ("I: backup passive, waiting for primary (active)"); self->bstar = bstar_new (BSTAR_BACKUP, "tcp://*:5004", "tcp://localhost:5003"); bstar_voter (self->bstar, "tcp://*:5566", ZMQ_ROUTER, s_snapshots, self); self->port = 5566; self->peer = 5556; self->primary = false; } else { printf ("Usage: clonesrv4 { -p | -b }\n"); free (self); exit (0); } // Primary server will become first active if (self->primary) self->kvmap = zhash_new (); self->ctx = zctx_new (); self->pending = zlist_new (); bstar_set_verbose (self->bstar, true); // Set up our clone server sockets self->publisher = zsocket_new (self->ctx, ZMQ_PUB); self->collector = zsocket_new (self->ctx, ZMQ_SUB); zsocket_set_subscribe (self->collector, ""); zsocket_bind (self->publisher, "tcp://*:%d", self->port + 1); zsocket_bind (self->collector, "tcp://*:%d", self->port + 2); // Set up our own clone client interface to peer self->subscriber = zsocket_new (self->ctx, ZMQ_SUB); zsocket_set_subscribe (self->subscriber, ""); zsocket_connect (self->subscriber, "tcp://localhost:%d", self->peer + 1); // .split main task body // After we've setup our sockets, we register our binary star // event handlers, and then start the bstar reactor. This finishes // when the user presses Ctrl-C or when the process receives a SIGINT // interrupt: // Register state change handlers bstar_new_active (self->bstar, s_new_active, self); bstar_new_passive (self->bstar, s_new_passive, self); // Register our other handlers with the bstar reactor zmq_pollitem_t poller = { self->collector, 0, ZMQ_POLLIN }; zloop_poller (bstar_zloop (self->bstar), &poller, s_collector, self); zloop_timer (bstar_zloop (self->bstar), 1000, 0, s_flush_ttl, self); zloop_timer (bstar_zloop (self->bstar), 1000, 0, s_send_hugz, self); // Start the bstar reactor bstar_start (self->bstar); // Interrupted, so shut down while (zlist_size (self->pending)) { kvmsg_t *kvmsg = (kvmsg_t *) zlist_pop (self->pending); kvmsg_destroy (&kvmsg); } zlist_destroy (&self->pending); bstar_destroy (&self->bstar); zhash_destroy (&self->kvmap); zctx_destroy (&self->ctx); free (self); return 0; }
int main (int argc, char *argv []) { // Arguments can be either of: // -p primary server, at tcp://localhost:5001 // -b backup server, at tcp://localhost:5002 zctx_t *ctx = zctx_new (); void *statepub = zsocket_new (ctx, ZMQ_PUB); void *statesub = zsocket_new (ctx, ZMQ_SUB); zsocket_set_subscribe (statesub, ""); void *frontend = zsocket_new (ctx, ZMQ_ROUTER); bstar_t fsm = { 0 }; if (argc == 2 && streq (argv [1], "-p")) { printf ("I: Primary active, waiting for backup (passive)\n"); zsocket_bind (frontend, "tcp://*:5001"); zsocket_bind (statepub, "tcp://*:5003"); zsocket_connect (statesub, "tcp://localhost:5004"); fsm.state = STATE_PRIMARY; } else if (argc == 2 && streq (argv [1], "-b")) { printf ("I: Backup passive, waiting for primary (active)\n"); zsocket_bind (frontend, "tcp://*:5002"); zsocket_bind (statepub, "tcp://*:5004"); zsocket_connect (statesub, "tcp://localhost:5003"); fsm.state = STATE_BACKUP; } else { printf ("Usage: bstarsrv { -p | -b }\n"); zctx_destroy (&ctx); exit (0); } // We now process events on our two input sockets, and process these // events one at a time via our finite-state machine. Our "work" for // a client request is simply to echo it back: // Set timer for next outgoing state message int64_t send_state_at = zclock_time () + HEARTBEAT; while (!zctx_interrupted) { zmq_pollitem_t items [] = { { frontend, 0, ZMQ_POLLIN, 0 }, { statesub, 0, ZMQ_POLLIN, 0 } }; int time_left = (int) ((send_state_at - zclock_time ())); if (time_left < 0) time_left = 0; int rc = zmq_poll (items, 2, time_left * ZMQ_POLL_MSEC); if (rc == -1) break; // Context has been shut down if (items [0].revents & ZMQ_POLLIN) { // Have a client request zmsg_t *msg = zmsg_recv (frontend); fsm.event = CLIENT_REQUEST; if (s_state_machine (&fsm) == false) // Answer client by echoing request back zmsg_send (&msg, frontend); else zmsg_destroy (&msg); } if (items [1].revents & ZMQ_POLLIN) { // Have state from our peer, execute as event char *message = zstr_recv (statesub); fsm.event = atoi (message); free (message); if (s_state_machine (&fsm)) break; // Error, so exit fsm.peer_expiry = zclock_time () + 2 * HEARTBEAT; } // If we timed out, send state to peer if (zclock_time () >= send_state_at) { char message [2]; sprintf (message, "%d", fsm.state); zstr_send (statepub, message); send_state_at = zclock_time () + HEARTBEAT; } } if (zctx_interrupted) printf ("W: interrupted\n"); // Shutdown sockets and context zctx_destroy (&ctx); return 0; }
int main (void) { zctx_t *context = zctx_new (); void *frontend = zsocket_new (context, ZMQ_SUB); zsocket_bind (frontend, "ipc://frontend"); void *backend = zsocket_new (context, ZMQ_XPUB); zsocket_bind (backend, "ipc://backend"); // .split main poll loop // We route topic updates from frontend to backend, and // we handle subscriptions by sending whatever we cached, // if anything: while (true) { printf("Polling...\r\n"); zmq_pollitem_t items [] = { { frontend, 0, ZMQ_POLLIN, 0 }, { backend, 0, ZMQ_POLLIN, 0 } }; if (zmq_poll (items, 2, -1) <= 0) break; // Interrupted(-1) or no event signaled (0) printf("Something read...\r\n"); // Any new topic data we cache and then forward if (items [0].revents & ZMQ_POLLIN) { printf("Received... "); char *topic = zstr_recv (frontend); char *current = zstr_recv (frontend); if (!topic) break; printf("%s : %s \r\n", topic, current); zstr_sendm (backend, topic); zstr_send (backend, current); free (topic); } // .split handle subscriptions // When we get a new subscription, we pull data from the cache: if (items [1].revents & ZMQ_POLLIN) { printf("Received new subscription...\r\n"); zframe_t *frame = zframe_recv (backend); if (!frame) break; // Event is one byte 0=unsub or 1=sub, followed by topic byte *event = zframe_data (frame); char *topic = zmalloc (zframe_size (frame)); memcpy (topic, event + 1, zframe_size (frame) - 1); printf ("Topic is %s\n", topic); if (event [0] == 0) { printf ("Unsubscribing topic %s\r\n", topic); zsocket_set_unsubscribe (frontend, topic); } if (event [0] == 1) { printf ("Subscribing topic %s\r\n", topic); zsocket_set_subscribe (frontend, topic); } free(topic); zframe_destroy (&frame); } } zctx_destroy (&context); return 0; }