static zsock_t* subscriber_pull_socket_new(zconfig_t* config) { zsock_t *socket = zsock_new(ZMQ_PULL); assert(socket); zsock_set_linger(socket, 0); zsock_set_reconnect_ivl(socket, 100); // 100 ms zsock_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s char *pull_spec = zconfig_resolve(config, "frontend/endpoints/subscriber/pull", "tcp://*"); char *full_spec = augment_zmq_connection_spec(pull_spec, pull_port); if (!quiet) printf("[I] subscriber: binding PULL socket to %s\n", full_spec); int rc = zsock_bind(socket, "%s", full_spec); assert(rc != -1); free(full_spec); const char *inproc_binding = "inproc://subscriber-pull"; if (!quiet) printf("[I] subscriber: binding PULL socket to %s\n", inproc_binding); rc = zsock_bind(socket, "%s", inproc_binding); assert(rc != -1); return socket; }
int main(int argc, char const * const *argv) { int rc; zsys_set_sndhwm(1); zsys_set_linger(100); void *pusher = zsock_new(ZMQ_PUSH); assert(pusher); zsock_set_sndhwm(pusher, 1000); zsock_set_linger(pusher, 500); rc = zsock_connect(pusher, "tcp://localhost:12345"); assert(rc==0); void *puller = zsock_new(ZMQ_PULL); assert(puller); zsock_set_rcvhwm(puller, 1000); zsock_set_linger(puller, 500); rc = zsock_bind(puller, "tcp://*:12345"); if (rc != 12345){ printf("bind failed: %s\n", zmq_strerror(errno)); } assert(rc == 12345); void *publisher = zsock_new(ZMQ_PUB); assert(publisher); zsock_set_sndhwm(publisher, 1000); zsock_set_linger(publisher, 500); rc = zsock_bind(publisher, "tcp://*:12346"); assert(rc==12346); // set up event loop zloop_t *loop = zloop_new(); assert(loop); zloop_set_verbose(loop, 0); // push data every 10 ms rc = zloop_timer(loop, 1, 0, timer_event, pusher); assert(rc != -1); zmq_pollitem_t item; item.socket = puller; item.events = ZMQ_POLLIN; rc = zloop_poller(loop, &item, forward, publisher); assert(rc == 0); rc = zloop_start(loop); printf("zloop return: %d", rc); zloop_destroy(&loop); assert(loop == NULL); return 0; }
zactor_t * zactor_new (zactor_fn *actor, void *args) { zactor_t *self = (zactor_t *) zmalloc (sizeof (zactor_t)); if (!self) return NULL; self->tag = ZACTOR_TAG; shim_t *shim = (shim_t *) zmalloc (sizeof (shim_t)); if (!shim) return NULL; // Create front-to-back pipe pair self->pipe = zsock_new (ZMQ_PAIR); assert (self->pipe); char endpoint [32]; while (true) { sprintf (endpoint, "inproc://zactor-%04x-%04x\n", randof (0x10000), randof (0x10000)); if (zsock_bind (self->pipe, "%s", endpoint) == 0) break; } shim->pipe = zsock_new (ZMQ_PAIR); assert (shim->pipe); int rc = zsock_connect (shim->pipe, "%s", endpoint); assert (rc != -1); shim->handler = actor; shim->args = args; #if defined (__UNIX__) pthread_t thread; pthread_create (&thread, NULL, s_thread_shim, shim); pthread_detach (thread); #elif defined (__WINDOWS__) HANDLE handle = (HANDLE) _beginthreadex ( NULL, // Handle is private to this process 0, // Use a default stack size for new thread &s_thread_shim, // Start real thread function via this shim shim, // Which gets arguments shim CREATE_SUSPENDED, // Set thread priority before starting it NULL); // We don't use the thread ID assert (handle); // Set child thread priority to same as current int priority = GetThreadPriority (GetCurrentThread ()); SetThreadPriority (handle, priority); // Start thread & release resources ResumeThread (handle); CloseHandle (handle); #endif // Mandatory handshake for new actor so that constructor returns only // when actor has also initialized. This eliminates timing issues at // application start up. zsock_wait (self->pipe); return self; }
zyre_t * zyre_new (const char *name) { zyre_t *self = (zyre_t *) zmalloc (sizeof (zyre_t)); assert (self); // Create front-to-back pipe pair for data traffic self->inbox = zsock_new (ZMQ_PAIR); assert (self->inbox); char endpoint [32]; while (true) { sprintf (endpoint, "inproc://zyre-%04x-%04x\n", randof (0x10000), randof (0x10000)); if (zsock_bind (self->inbox, "%s", endpoint) == 0) break; } // Create other half of traffic pipe zsock_t *outbox = zsock_new_pair (endpoint); assert (outbox); // Start node engine and wait for it to be ready self->actor = zactor_new (zyre_node_actor, outbox); assert (self->actor); // Send name, if any, to node ending if (name) zstr_sendx (self->actor, "SET NAME", name, NULL); return self; }
int main (void) { printf("Starting dealer...\n"); // Socket to talk to clients zsock_t *server = zsock_new (ZMQ_DEALER); int rc = zsock_bind (server, "tcp://*:5556"); checkRetCode(rc); printf("Hiding from others...\n"); char* msg = "LucTeo (blue shirt + red)"; int caught = 0; // while ( 1 ) { rc = zstr_send (server, msg); checkRetCode(rc); if ( !caught ) { printf("I'm caught. :(\n"); msg = "Too late, secret was secret but it's taken"; caught = 1; } } // zsock_destroy (&server); return 0; }
// Checks whether client can connect to server static bool s_can_connect (zsock_t **server, zsock_t **client) { int port_nbr = zsock_bind (*server, "tcp://127.0.0.1:*"); assert (port_nbr > 0); int rc = zsock_connect (*client, "tcp://127.0.0.1:%d", port_nbr); assert (rc == 0); // Give the connection time to fail if that's the plan zclock_sleep (200); // By default PUSH sockets block if there's no peer zsock_set_sndtimeo (*server, 200); zstr_send (*server, "Hello, World"); zpoller_t *poller = zpoller_new (*client, NULL); assert (poller); bool success = (zpoller_wait (poller, 400) == *client); zpoller_destroy (&poller); zsock_destroy (client); zsock_destroy (server); *server = zsock_new (ZMQ_PUSH); assert (*server); *client = zsock_new (ZMQ_PULL); assert (*client); return success; }
static self_t * s_self_new (zsock_t *pipe, zcertstore_t *certstore) { self_t *self = (self_t *) zmalloc (sizeof (self_t)); assert (self); if (certstore) { self->certstore = certstore; self->allow_any = false; } self->pipe = pipe; self->whitelist = zhashx_new (); assert (self->whitelist); self->blacklist = zhashx_new (); // Create ZAP handler and get ready for requests assert (self->blacklist); self->handler = zsock_new (ZMQ_REP); assert (self->handler); int rc = zsock_bind (self->handler, ZAP_ENDPOINT); assert (rc == 0); self->poller = zpoller_new (self->pipe, self->handler, NULL); assert (self->poller); return self; }
JNIEXPORT jint JNICALL Java_org_zeromq_czmq_Zsock__1_1bind (JNIEnv *env, jclass c, jlong self, jstring format) { char *format_ = (char *) (*env)->GetStringUTFChars (env, format, NULL); jint bind_ = (jint) zsock_bind ((zsock_t *) (intptr_t) self, "%s", format_); (*env)->ReleaseStringUTFChars (env, format, format_); return bind_; }
static zsock_t* subscriber_push_socket_new() { zsock_t *socket = zsock_new(ZMQ_PUSH); assert(socket); int rc = zsock_bind(socket, "inproc://graylog-forwarder-subscriber"); assert(rc == 0); return socket; }
static zsock_t* subscriber_push_socket_new(zconfig_t* config) { zsock_t *socket = zsock_new(ZMQ_PUSH); assert(socket); zsock_set_sndtimeo(socket, 10); int rc = zsock_bind(socket, "inproc://subscriber"); assert(rc == 0); return socket; }
void zmailer_msg_test (bool verbose) { printf (" * zmailer_msg:"); if (verbose) printf ("\n"); // @selftest // Simple create/destroy test zmailer_msg_t *self = zmailer_msg_new (); assert (self); zmailer_msg_destroy (&self); // Create pair of sockets we can send through // We must bind before connect if we wish to remain compatible with ZeroMQ < v4 zsock_t *output = zsock_new (ZMQ_DEALER); assert (output); int rc = zsock_bind (output, "inproc://selftest-zmailer_msg"); assert (rc == 0); zsock_t *input = zsock_new (ZMQ_ROUTER); assert (input); rc = zsock_connect (input, "inproc://selftest-zmailer_msg"); assert (rc == 0); // Encode/send/decode and verify each message type int instance; self = zmailer_msg_new (); zmailer_msg_set_id (self, ZMAILER_MSG_MAIL); zmailer_msg_set_from (self, "Life is short but Now lasts for ever"); zmailer_msg_set_to (self, "Life is short but Now lasts for ever"); zmailer_msg_set_subject (self, " test subject "); zmailer_msg_set_request (self, " this is the text to be sent "); // Send twice zmailer_msg_send (self, output); zmailer_msg_send (self, output); for (instance = 0; instance < 2; instance++) { zmailer_msg_recv (self, input); assert (zmailer_msg_routing_id (self)); assert (streq (zmailer_msg_from (self), "Life is short but Now lasts for ever")); assert (streq (zmailer_msg_to (self), "Life is short but Now lasts for ever")); assert (streq (zmailer_msg_subject (self), " test subject ")); assert (streq (zmailer_msg_request (self), " this is the text to be sent ")); } zmailer_msg_destroy (&self); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); }
STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) { socket_obj_t *socket = self_in; socket_check_closed(socket); struct sockaddr sockaddr; parse_inet_addr(socket, addr_in, &sockaddr); int res = zsock_bind(socket->ctx, &sockaddr, sizeof(sockaddr)); RAISE_SOCK_ERRNO(res); return mp_const_none; }
void zhttp_client_test (bool verbose) { #if defined(HAVE_LIBCURL) && defined(ZMQ_STREAM) printf (" * zhttp_client: "); zsock_t *server = zsock_new_stream (NULL); int port = zsock_bind (server, "tcp://127.0.0.1:*"); char url[255]; sprintf (url, "http://127.0.0.1:%d", port); // @selftest // Simple create/destroy test zhttp_client_t *self = zhttp_client_new (verbose); assert (self); // Send the get request zlistx_t *headers = zlistx_new (); zlistx_add_end (headers, "Host: zeromq.org"); zhttp_client_get (self, url, headers, NULL); zlistx_destroy (&headers); // Receive request on the server zchunk_t *routing_id; char *request; int rc = zsock_recv (server, "cs", &routing_id, &request); assert (rc == 0); // Send the response char* response = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nHello"; zsock_send (server, "cs", routing_id, response); // Receive the response on the http client int code; zchunk_t *data; zhttp_client_recv (self, &code, &data, NULL); assert (zchunk_streq (data, "Hello")); // Sending another request, without being answer // Checking the client ability to stop while request are inprogres zhttp_client_get (self, url, NULL, NULL); zchunk_destroy (&data); zchunk_destroy (&routing_id); zstr_free (&request); zhttp_client_destroy (&self); zsock_destroy (&server); // @end printf ("OK\n"); #endif }
void zmonitor_test (bool verbose) { printf (" * zmonitor: "); if (verbose) printf ("\n"); #if defined (ZMQ_EVENT_ALL) // @selftest zsock_t *client = zsock_new (ZMQ_DEALER); assert (client); zactor_t *clientmon = zactor_new (zmonitor, client); assert (clientmon); if (verbose) zstr_sendx (clientmon, "VERBOSE", NULL); zstr_sendx (clientmon, "LISTEN", "LISTENING", "ACCEPTED", NULL); zstr_sendx (clientmon, "START", NULL); zsock_wait (clientmon); zsock_t *server = zsock_new (ZMQ_DEALER); assert (server); zactor_t *servermon = zactor_new (zmonitor, server); assert (servermon); if (verbose) zstr_sendx (servermon, "VERBOSE", NULL); zstr_sendx (servermon, "LISTEN", "CONNECTED", "DISCONNECTED", NULL); zstr_sendx (servermon, "START", NULL); zsock_wait (servermon); // Allow a brief time for the message to get there... zmq_poll (NULL, 0, 200); // Check client is now listening int port_nbr = zsock_bind (client, "tcp://127.0.0.1:*"); assert (port_nbr != -1); s_assert_event (clientmon, "LISTENING"); // Check server connected to client zsock_connect (server, "tcp://127.0.0.1:%d", port_nbr); s_assert_event (servermon, "CONNECTED"); // Check client accepted connection s_assert_event (clientmon, "ACCEPTED"); zactor_destroy (&clientmon); zactor_destroy (&servermon); zsock_destroy (&client); zsock_destroy (&server); #endif // @end printf ("OK\n"); }
int zsock_attach (zsock_t *self, const char *endpoints, bool serverish) { assert (self); if (!endpoints) return 0; // We hold each individual endpoint here char endpoint [256]; while (*endpoints) { const char *delimiter = strchr (endpoints, ','); if (!delimiter) delimiter = endpoints + strlen (endpoints); if (delimiter - endpoints > 255) return -1; memcpy (endpoint, endpoints, delimiter - endpoints); endpoint [delimiter - endpoints] = 0; int rc; if (endpoint [0] == '@') rc = zsock_bind (self, "%s", endpoint + 1); else if (endpoint [0] == '>') rc = zsock_connect (self, "%s", endpoint + 1); else if (serverish) rc = zsock_bind (self, "%s", endpoint); else rc = zsock_connect (self, "%s", endpoint); if (rc == -1) return -1; // Bad endpoint syntax if (*delimiter == 0) break; endpoints = delimiter + 1; } return 0; }
static zsock_t* subscriber_pub_socket_new(zconfig_t* config) { zsock_t *socket = zsock_new(ZMQ_PUB); assert(socket); zsock_set_sndhwm(socket, 10000); char *pub_spec = zconfig_resolve(config, "frontend/endpoints/subscriber/pub", "tcp://127.0.0.1:9651"); if (!quiet) printf("[I] subscriber: binding PUB socket to %s\n", pub_spec); int rc = zsock_bind(socket, "%s", pub_spec); assert(rc != -1); return socket; }
static int zyre_node_start (zyre_node_t *self) { // If application didn't bind explicitly, we grab an ephemeral port // on all available network interfaces. This is orthogonal to // beaconing, since we can connect to other peers and they will // gossip our endpoint to others. if (!self->bound) { self->port = zsock_bind (self->inbox, "tcp://*:*"); if (self->port < 0) return 1; // Could not get new port to bind to? self->bound = true; } // Start UDP beaconing, if the application didn't disable it if (self->beacon_port) { assert (!self->beacon); self->beacon = zbeacon_new (NULL, self->beacon_port); if (!self->beacon) return 1; // Not possible to start beacon if (self->interval) zbeacon_set_interval (self->beacon, self->interval); zpoller_add (self->poller, zbeacon_socket (self->beacon)); // Set broadcast/listen beacon beacon_t beacon; beacon.protocol [0] = 'Z'; beacon.protocol [1] = 'R'; beacon.protocol [2] = 'E'; beacon.version = BEACON_VERSION; beacon.port = htons (self->port); zuuid_export (self->uuid, beacon.uuid); zbeacon_noecho (self->beacon); zbeacon_publish (self->beacon, (byte *) &beacon, sizeof (beacon_t)); zbeacon_subscribe (self->beacon, (byte *) "ZRE", 3); // Our own host endpoint is provided by the beacon assert (!self->endpoint); self->endpoint = zsys_sprintf ("tcp://%s:%d", zbeacon_hostname (self->beacon), self->port); } else if (!self->endpoint) { char *hostname = zsys_hostname (); self->endpoint = zsys_sprintf ("tcp://%s:%d", hostname, self->port); zstr_free (&hostname); } // Start polling on inbox zpoller_add (self->poller, self->inbox); return 0; }
static void s_upstream_handle_pipe (upstream_t *self) { char *command = zstr_recv (self->pipe); if (streq (command, "$TERM")) { self->terminated = true; } else if (streq (command, "CONNECT")) { char *endpoint = zstr_recv (self->pipe); int rc = zsock_connect (self->push, "%s", endpoint); if (rc != -1) self->connected = true; zstr_free (&endpoint); zsock_bsend (self->pipe, "2", rc); } else if (streq (command, "BIND")) { char *endpoint = zstr_recv (self->pipe); int rc = zsock_bind (self->push, "%s", endpoint); if (rc != -1) self->connected = true; zstr_free (&endpoint); zsock_bsend (self->pipe, "2", rc); } else if (streq (command, "SET_SIZE")) { int64_t size; zsock_brecv (self->pipe, "8", &size); self->size = size; } else if (streq (command, "GET_SIZE")) { zsock_bsend (self->pipe, "8", self->size); } else if (streq (command, "SET_VARIANCE")) { int64_t variance; zsock_brecv (self->pipe, "8", &variance); self->variance = variance; } else if (streq (command, "GET_VARIANCE")) { zsock_bsend (self->pipe, "8", self->variance); } else { zsys_error ("upstream: invalid command: %s", command); assert (false); } zstr_free (&command); }
ztask_monitor_api_t * ztask_monitor_api_new (char *publisher) { ztask_monitor_api_t *self = (ztask_monitor_api_t *) zmalloc (sizeof (ztask_monitor_api_t)); self->nodes = zhash_new (); self->pub_timeout = 1000; // 1 sec if (publisher) { zclock_log ("%s", publisher); self->pub_sock = zsock_new (ZMQ_PUB); self->pub_port = zsock_bind (self->pub_sock, "%s", publisher); assert (self->pub_port >= 0); } return self; }
void zloop_test (bool verbose) { printf (" * zloop: "); int rc = 0; // @selftest // Create two PAIR sockets and connect over inproc zsock_t *output = zsock_new (ZMQ_PAIR); assert (output); zsock_bind (output, "inproc://zloop.test"); zsock_t *input = zsock_new (ZMQ_PAIR); assert (input); zsock_connect (input, "inproc://zloop.test"); zloop_t *loop = zloop_new (); assert (loop); zloop_set_verbose (loop, verbose); // Create a timer that will be cancelled int timer_id = zloop_timer (loop, 1000, 1, s_timer_event, NULL); zloop_timer (loop, 5, 1, s_cancel_timer_event, &timer_id); // After 20 msecs, send a ping message to output3 zloop_timer (loop, 20, 1, s_timer_event, output); // Set up some tickets that will never expire zloop_set_ticket_delay (loop, 10000); void *ticket1 = zloop_ticket (loop, s_timer_event, NULL); void *ticket2 = zloop_ticket (loop, s_timer_event, NULL); void *ticket3 = zloop_ticket (loop, s_timer_event, NULL); // When we get the ping message, end the reactor rc = zloop_reader (loop, input, s_socket_event, NULL); assert (rc == 0); zloop_reader_set_tolerant (loop, input); zloop_start (loop); zloop_ticket_delete (loop, ticket1); zloop_ticket_delete (loop, ticket2); zloop_ticket_delete (loop, ticket3); zloop_destroy (&loop); assert (loop == NULL); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); }
static int zyre_node_bind (zyre_node_t *self, const char *endpoint) { assert (!self->endpoint); self->port = zsock_bind (self->inbox, "%s", endpoint); if (self->port < 0) return 1; // Endpoint is occupied, or invalid // Successful bind, save endpoint and disable beaconing self->endpoint = strdup (endpoint); self->bound = true; self->beacon_port = 0; return 0; }
static self_t * s_self_new (zsock_t *pipe) { self_t *self = (self_t *) zmalloc (sizeof (self_t)); self->pipe = pipe; self->whitelist = zhash_new (); self->blacklist = zhash_new (); // Create ZAP handler and get ready for requests self->handler = zsock_new (ZMQ_REP); assert (self->handler); int rc = zsock_bind (self->handler, "inproc://zeromq.zap.01"); assert (rc == 0); self->poller = zpoller_new (self->pipe, self->handler, NULL); return self; }
void MessageProcessor::init(int port, zcert_t* transportKey) { if (port == 0) { OT_FAIL; } if (!zsys_has_curve()) { Log::vError("Error: libzmq has no libsodium support"); OT_FAIL; } zstr_sendx(zmqAuth_, "CURVE", CURVE_ALLOW_ANY, NULL); zsock_wait(zmqAuth_); zsock_set_zap_domain(zmqSocket_, "global"); zsock_set_curve_server(zmqSocket_, 1); zcert_apply(transportKey, zmqSocket_); zsock_bind(zmqSocket_, "tcp://*:%d", port); }
int zpubsub_filter_test (bool verbose) { printf (" * zpubsub_filter: "); // @selftest // Simple create/destroy test zpubsub_filter_t *self = zpubsub_filter_new (); assert (self); zpubsub_filter_destroy (&self); // Create pair of sockets we can send through zsock_t *input = zsock_new (ZMQ_ROUTER); assert (input); zsock_connect (input, "inproc://selftest-zpubsub_filter"); zsock_t *output = zsock_new (ZMQ_DEALER); assert (output); zsock_bind (output, "inproc://selftest-zpubsub_filter"); // Encode/send/decode and verify each message type int instance; self = zpubsub_filter_new (); zpubsub_filter_set_id (self, ZPUBSUB_FILTER_FILTER); zpubsub_filter_set_partition (self, "Life is short but Now lasts for ever"); zpubsub_filter_set_topic (self, "Life is short but Now lasts for ever"); // Send twice zpubsub_filter_send (self, output); zpubsub_filter_send (self, output); for (instance = 0; instance < 2; instance++) { zpubsub_filter_recv (self, input); assert (zpubsub_filter_routing_id (self)); assert (streq (zpubsub_filter_partition (self), "Life is short but Now lasts for ever")); assert (streq (zpubsub_filter_topic (self), "Life is short but Now lasts for ever")); } zpubsub_filter_destroy (&self); zsock_destroy (&input); zsock_destroy (&output); // @end printf ("OK\n"); return 0; }
// Checks whether client can connect to server static bool s_can_connect (zsock_t **server, zsock_t **client) { int port_nbr = zsock_bind (*server, "tcp://127.0.0.1:*"); assert (port_nbr > 0); int rc = zsock_connect (*client, "tcp://127.0.0.1:%d", port_nbr); assert (rc == 0); zstr_send (*server, "Hello, World"); zpoller_t *poller = zpoller_new (*client, NULL); bool success = (zpoller_wait (poller, 200) == *client); zpoller_destroy (&poller); zsock_destroy (client); zsock_destroy (server); *server = zsock_new (ZMQ_PUSH); *client = zsock_new (ZMQ_PULL); return success; }
static zsock_t* subscriber_router_socket_new(zconfig_t* config) { zsock_t *socket = zsock_new(ZMQ_ROUTER); assert(socket); zsock_set_linger(socket, 0); zsock_set_reconnect_ivl(socket, 100); // 100 ms zsock_set_reconnect_ivl_max(socket, 10 * 1000); // 10 s char *router_spec = zconfig_resolve(config, "frontend/endpoints/subscriber/router", "tcp://*"); char *full_spec = augment_zmq_connection_spec(router_spec, router_port); if (!quiet) printf("[I] subscriber: binding ROUTER socket to %s\n", full_spec); int rc = zsock_bind(socket, "%s", full_spec); assert(rc != -1); free(full_spec); return socket; }
int main (void) { // Create and bind server socket zsock_t *server = zsock_new (ZMQ_PUSH); zsock_bind (server, "tcp://*:9000"); // Create and connect client socket zsock_t *client = zsock_new (ZMQ_PULL); zsock_connect (client, "tcp://127.0.0.1:9000"); // Send a single message from server to client zstr_send (server, "Hello"); char *message = zstr_recv (client); assert (streq (message, "Hello")); free (message); puts ("Grasslands test OK"); zsock_destroy (&client); zsock_destroy (&server); return 0; }
static int zyre_node_start (zyre_node_t *self) { if (self->beacon_port) { // Start beacon discovery // ------------------------------------------------------------------ assert (!self->beacon); self->beacon = zactor_new (zbeacon, NULL); if (!self->beacon) return 1; // Not possible to start beacon if (self->verbose) zsock_send (self->beacon, "s", "VERBOSE"); } else { // Start gossip discovery // ------------------------------------------------------------------ // If application didn't set an endpoint explicitly, grab ephemeral // port on all available network interfaces. if (!self->endpoint) { const char *iface = zsys_interface (); if (streq (iface, "")) iface = "*"; self->port = zsock_bind (self->inbox, "tcp://%s:*", iface); assert (self->port > 0); // Die on bad interface or port exhaustion char *hostname = zsys_hostname (); self->endpoint = zsys_sprintf ("tcp://%s:%d", hostname, self->port); zstr_free (&hostname); } assert (self->gossip); zstr_sendx (self->gossip, "PUBLISH", zuuid_str (self->uuid), self->endpoint, NULL); // Start polling on zgossip zpoller_add (self->poller, self->gossip); // Start polling on inbox zpoller_add(self->poller, self->inbox); } return 0; }
static self_t * s_self_new (zsock_t *pipe) { self_t *self = (self_t *) zmalloc (sizeof (self_t)); int rc = -1; if (self) { self->pipe = pipe; self->whitelist = zhashx_new (); if (self->whitelist) self->blacklist = zhashx_new (); // Create ZAP handler and get ready for requests if (self->blacklist) self->handler = zsock_new (ZMQ_REP); if (self->handler) rc = zsock_bind (self->handler, ZAP_ENDPOINT); if (rc == 0) self->poller = zpoller_new (self->pipe, self->handler, NULL); if (!self->poller) s_self_destroy (&self); } return self; }
static int s_get_available_port () { int port_nbr = -1; int attempts = 0; // Choosing a random port for better results in case multiple tests are running // in parallel on the same machine, such as during CI runs while (port_nbr == -1 && attempts++ < 10) { port_nbr = 49152 + randof (16383); zsock_t *server = zsock_new (ZMQ_PUSH); assert (server); port_nbr = zsock_bind (server, LOCALENDPOINT, port_nbr); zsock_destroy (&server); } if (port_nbr < 0) { zsys_error ("zproxy: failed to find an available port number"); assert (false); } return port_nbr; }