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; }
static void server_connect (server_t *self, const char *endpoint) { zsock_t *remote = zsock_new (ZMQ_DEALER); assert (remote); // No recovery if exhausted // Never block on sending; we use an infinite HWM and buffer as many // messages as needed in outgoing pipes. Note that the maximum number // is the overall tuple set size. zsock_set_sndhwm (remote, 0); if (zsock_connect (remote, "%s", endpoint)) { zsys_error ("bad zgossip endpoint '%s'", endpoint); zsock_destroy (&remote); return; } // Send HELLO and then PUBLISH for each tuple we have zgossip_msg_send_hello (remote); tuple_t *tuple = (tuple_t *) zhash_first (self->tuples); while (tuple) { int rc = zgossip_msg_send_publish (remote, tuple->key, tuple->value, 0); assert (rc == 0); tuple = (tuple_t *) zhash_next (self->tuples); } // Now monitor this remote for incoming messages engine_handle_socket (self, remote, remote_handler); zlist_append (self->remotes, remote); }
void zsock_set_unbounded (zsock_t *self) { #if (ZMQ_VERSION_MAJOR == 2) zsock_set_hwm (self, 0); #else zsock_set_sndhwm (self, 0); zsock_set_rcvhwm (self, 0); #endif }
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; }
void zyre_peer_connect (zyre_peer_t *self, zuuid_t *from, const char *endpoint) { assert (self); assert (!self->connected); // Create new outgoing socket (drop any messages in transit) self->mailbox = zsock_new (ZMQ_DEALER); if (!self->mailbox) return; // Null when we're shutting down // Set our own identity on the socket so that receiving node // knows who each message came from. Note that we cannot use // the UUID directly as the identity since it may contain a // zero byte at the start, which libzmq does not like for // historical and arguably bogus reasons that it nonetheless // enforces. byte routing_id [ZUUID_LEN + 1] = { 1 }; memcpy (routing_id + 1, zuuid_data (from), ZUUID_LEN); int rc = zmq_setsockopt (zsock_resolve (self->mailbox), ZMQ_IDENTITY, routing_id, ZUUID_LEN + 1); assert (rc == 0); // Set a high-water mark that allows for reasonable activity zsock_set_sndhwm (self->mailbox, PEER_EXPIRED * 100); // Send messages immediately or return EAGAIN zsock_set_sndtimeo (self->mailbox, 0); // Connect through to peer node rc = zsock_connect (self->mailbox, "%s", endpoint); if (rc != 0) { zsys_error ("(%s) cannot connect to endpoint=%s", self->origin, endpoint); // Don't really have any error handling yet; if connect // fails, there's something wrong with connect endpoint? assert (false); } assert (rc == 0); if (self->verbose) zsys_info ("(%s) connect to peer: endpoint=%s", self->origin, endpoint); self->endpoint = strdup (endpoint); self->connected = true; self->ready = false; }
void rabbitmq_listener(zsock_t *pipe, void* args) { set_thread_name("rabbit-consumer"); // signal readyiness immediately so that zmq publishers are already processed // while the rabbitmq exchanges/queues/bindings are created zsock_signal(pipe, 0); amqp_connection_state_t conn = setup_amqp_connection(); int last_channel = rabbitmq_setup_queues(conn, (zlist_t*)args); // connect to the receiver socket zsock_t *receiver = zsock_new(ZMQ_PUSH); zsock_set_sndhwm(receiver, 10000); zsock_connect(receiver, "inproc://receiver"); // set up event loop zloop_t *loop = zloop_new(); assert(loop); zloop_set_verbose(loop, 0); zloop_ignore_interrupts(loop); // register actor command handler int rc = zloop_reader(loop, pipe, pipe_command, NULL); assert(rc==0); // register rabbitmq socket for pollin events zmq_pollitem_t rabbit_item = { .fd = amqp_get_sockfd(conn), .events = ZMQ_POLLIN }; rabbit_listener_state_t listener_state = { .conn = conn, .receiver = zsock_resolve(receiver) }; rc = zloop_poller(loop, &rabbit_item, rabbitmq_consume_message_and_forward, &listener_state); assert(rc==0); // start event loop zloop_start(loop); // shutdown zloop_destroy(&loop); zsock_destroy(&receiver); shutdown_amqp_connection(conn, 0, last_channel); }
int zyre_peer_connect (zyre_peer_t *self, zuuid_t *from, const char *endpoint, uint64_t expired_timeout) { assert (self); assert (!self->connected); // Create new outgoing socket (drop any messages in transit) self->mailbox = zsock_new (ZMQ_DEALER); if (!self->mailbox) return -1; // Null when we're shutting down // Set our own identity on the socket so that receiving node // knows who each message came from. Note that we cannot use // the UUID directly as the identity since it may contain a // zero byte at the start, which libzmq does not like for // historical and arguably bogus reasons that it nonetheless // enforces. byte routing_id [ZUUID_LEN + 1] = { 1 }; memcpy (routing_id + 1, zuuid_data (from), ZUUID_LEN); int rc = zmq_setsockopt (zsock_resolve (self->mailbox), ZMQ_IDENTITY, routing_id, ZUUID_LEN + 1); assert (rc == 0); // Set a high-water mark that allows for reasonable activity zsock_set_sndhwm (self->mailbox, expired_timeout * 100); // Send messages immediately or return EAGAIN zsock_set_sndtimeo (self->mailbox, 0); // If the peer is a link-local IPv6 address but the interface is not set, // use ZSYS_INTERFACE_ADDRESS if provided zrex_t *rex = zrex_new (NULL); char endpoint_iface [NI_MAXHOST] = {0}; if (zsys_ipv6 () && zsys_interface () && strlen(zsys_interface ()) && !streq (zsys_interface (), "*") && zrex_eq (rex, endpoint, "^tcp://(fe80[^%]+)(:\\d+)$")) { const char *hostname, *port; zrex_fetch (rex, &hostname, &port, NULL); strcat (endpoint_iface, "tcp://"); strcat (endpoint_iface, hostname); strcat (endpoint_iface, "%"); strcat (endpoint_iface, zsys_interface ()); strcat (endpoint_iface, port); } else strcat (endpoint_iface, endpoint); zrex_destroy (&rex); // Connect through to peer node rc = zsock_connect (self->mailbox, "%s", endpoint_iface); if (rc != 0) { zsys_debug ("(%s) cannot connect to endpoint=%s", self->origin, endpoint_iface); zsock_destroy (&self->mailbox); return -1; } if (self->verbose) zsys_info ("(%s) connect to peer: endpoint=%s", self->origin, endpoint_iface); self->endpoint = strdup (endpoint_iface); self->connected = true; self->ready = false; return 0; }
int main(int argc, char * const *argv) { int rc = 0; process_arguments(argc, argv); setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); if (!quiet) printf("[I] started %s\n" "[I] sub-port: %d\n" "[I] push-port: %d\n" "[I] io-threads: %lu\n" "[I] rcv-hwm: %d\n" "[I] snd-hwm: %d\n" , argv[0], pull_port, pub_port, io_threads, rcv_hwm, snd_hwm); // load config config_file_exists = zsys_file_exists(config_file_name); if (config_file_exists) { config_file_init(); config = zconfig_load((char*)config_file_name); } // set global config zsys_init(); zsys_set_rcvhwm(10000); zsys_set_sndhwm(10000); zsys_set_pipehwm(1000); zsys_set_linger(100); zsys_set_io_threads(io_threads); // create socket to receive messages on zsock_t *receiver = zsock_new(ZMQ_SUB); assert_x(receiver != NULL, "sub socket creation failed", __FILE__, __LINE__); zsock_set_rcvhwm(receiver, rcv_hwm); // bind externally char* host = zlist_first(hosts); while (host) { if (!quiet) printf("[I] connecting to: %s\n", host); rc = zsock_connect(receiver, "%s", host); assert_x(rc == 0, "sub socket connect failed", __FILE__, __LINE__); host = zlist_next(hosts); } tracker = device_tracker_new(hosts, receiver); // create socket for publishing zsock_t *publisher = zsock_new(ZMQ_PUSH); assert_x(publisher != NULL, "pub socket creation failed", __FILE__, __LINE__); zsock_set_sndhwm(publisher, snd_hwm); rc = zsock_bind(publisher, "tcp://%s:%d", "*", pub_port); assert_x(rc == pub_port, "pub socket bind failed", __FILE__, __LINE__); // create compressor sockets zsock_t *compressor_input = zsock_new(ZMQ_PUSH); assert_x(compressor_input != NULL, "compressor input socket creation failed", __FILE__, __LINE__); rc = zsock_bind(compressor_input, "inproc://compressor-input"); assert_x(rc==0, "compressor input socket bind failed", __FILE__, __LINE__); zsock_t *compressor_output = zsock_new(ZMQ_PULL); assert_x(compressor_output != NULL, "compressor output socket creation failed", __FILE__, __LINE__); rc = zsock_bind(compressor_output, "inproc://compressor-output"); assert_x(rc==0, "compressor output socket bind failed", __FILE__, __LINE__); // create compressor agents zactor_t *compressors[MAX_COMPRESSORS]; for (size_t i = 0; i < num_compressors; i++) compressors[i] = message_decompressor_new(i); // set up event loop zloop_t *loop = zloop_new(); assert(loop); zloop_set_verbose(loop, 0); // calculate statistics every 1000 ms int timer_id = zloop_timer(loop, 1000, 0, timer_event, NULL); assert(timer_id != -1); // setup handler for the receiver socket publisher_state_t publisher_state = { .receiver = zsock_resolve(receiver), .publisher = zsock_resolve(publisher), .compressor_input = zsock_resolve(compressor_input), .compressor_output = zsock_resolve(compressor_output), }; // setup handler for compression results rc = zloop_reader(loop, compressor_output, read_zmq_message_and_forward, &publisher_state); assert(rc == 0); zloop_reader_set_tolerant(loop, compressor_output); // setup handdler for messages incoming from the outside or rabbit_listener rc = zloop_reader(loop, receiver, read_zmq_message_and_forward, &publisher_state); assert(rc == 0); zloop_reader_set_tolerant(loop, receiver); // initialize clock global_time = zclock_time(); // setup subscriptions if (subscriptions == NULL || zlist_size(subscriptions) == 0) { if (!quiet) printf("[I] subscribing to all log messages\n"); zsock_set_subscribe(receiver, ""); } else { char *subscription = zlist_first(subscriptions); while (subscription) { if (!quiet) printf("[I] subscribing to %s\n", subscription); zsock_set_subscribe(receiver, subscription); subscription = zlist_next(subscriptions); } zsock_set_subscribe(receiver, "heartbeat"); } // run the loop if (!zsys_interrupted) { if (verbose) printf("[I] starting main event loop\n"); bool should_continue_to_run = getenv("CPUPROFILE") != NULL; do { rc = zloop_start(loop); should_continue_to_run &= errno == EINTR && !zsys_interrupted; log_zmq_error(rc, __FILE__, __LINE__); } while (should_continue_to_run); if (verbose) printf("[I] main event zloop terminated with return code %d\n", rc); } zloop_destroy(&loop); assert(loop == NULL); if (!quiet) { printf("[I] received %zu messages\n", received_messages_count); printf("[I] shutting down\n"); } zlist_destroy(&hosts); zlist_destroy(&subscriptions); zsock_destroy(&receiver); zsock_destroy(&publisher); zsock_destroy(&compressor_input); zsock_destroy(&compressor_output); device_tracker_destroy(&tracker); for (size_t i = 0; i < num_compressors; i++) zactor_destroy(&compressors[i]); zsys_shutdown(); if (!quiet) printf("[I] terminated\n"); return rc; }
/// // Set socket option `sndhwm`. void QZsock::setSndhwm (int sndhwm) { zsock_set_sndhwm (self, sndhwm); }
JNIEXPORT void JNICALL Java_org_zeromq_czmq_Zsock__1_1setSndhwm (JNIEnv *env, jclass c, jlong self, jint sndhwm) { zsock_set_sndhwm ((zsock_t *) (intptr_t) self, (int) sndhwm); }
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) { zactor_destroy (&self); return NULL; } // Create front-to-back pipe pair self->pipe = zsock_new (ZMQ_PAIR); assert (self->pipe); shim->pipe = zsock_new (ZMQ_PAIR); assert (shim->pipe); #if (ZMQ_VERSION_MAJOR == 2) zsock_set_hwm (self->pipe, zsys_pipehwm ()); zsock_set_hwm (shim->pipe, zsys_pipehwm ()); #else zsock_set_sndhwm (self->pipe, (int) zsys_pipehwm ()); zsock_set_sndhwm (shim->pipe, (int) zsys_pipehwm ()); #endif // Now bind and connect pipe ends 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; } 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; }