void subscriber(zsock_t *pipe, void *args) { set_thread_name("subscriber[0]"); int rc; zconfig_t* config = args; subscriber_state_t *state = subscriber_state_new(pipe, config, hosts); // signal readyiness after sockets have been created zsock_signal(pipe, 0); // subscribe to either all messages, or a subset setup_subscriptions(state); // set up event loop zloop_t *loop = zloop_new(); assert(loop); zloop_set_verbose(loop, 0); // setup handler for actor messages rc = zloop_reader(loop, state->pipe, actor_command, state); assert(rc == 0); // setup handler for the sub socket rc = zloop_reader(loop, state->sub_socket, read_request_and_forward, state); assert(rc == 0); // setup handler for the router socket rc = zloop_reader(loop, state->router_socket, read_router_request_forward, state); assert(rc == 0); // setup handler for the pull socket rc = zloop_reader(loop, state->pull_socket, read_request_and_forward, state); assert(rc == 0); // run the loop if (!quiet) fprintf(stdout, "[I] subscriber: listening\n"); bool should_continue_to_run = getenv("CPUPROFILE") != NULL; do { rc = zloop_start(loop); should_continue_to_run &= errno == EINTR; log_zmq_error(rc, __FILE__, __LINE__); } while (should_continue_to_run); if (!quiet) fprintf(stdout, "[I] subscriber: shutting down\n"); // shutdown subscriber_state_destroy(&state); zloop_destroy(&loop); assert(loop == NULL); if (!quiet) fprintf(stdout, "[I] subscriber: terminated\n"); }
void zdir_watch (zsock_t *pipe, void *unused) { zdir_watch_t *watch = s_zdir_watch_new (pipe); assert (watch); watch->loop = zloop_new (); assert (watch->loop); watch->subs = zhash_new (); assert (watch->subs); zloop_reader (watch->loop, pipe, s_on_command, watch); zloop_reader_set_tolerant (watch->loop, pipe); // command pipe needs to be tolerant, otherwise we'd have a hard time shutting down s_zdir_watch_timeout (watch, 250); // default poll time of 250ms // Signal initialization zsock_signal (pipe, 0); // Dispatch above handlers zloop_start (watch->loop); if (watch->verbose) zsys_info ("zdir_watch: Complete"); // signal destruction zsock_signal (watch->pipe, 0); // Done - cleanup and exit s_zdir_watch_destroy (&watch); }
int main (void) { zsys_set_ipv6 (1); zactor_t *root = zactor_new (zgossip, "root"); assert (root); int rc = 0; rc = zstr_sendx (root, "BIND", "tcp://*:5670", NULL); assert (rc == 0); zloop_t *reactor = zloop_new (); assert (reactor); zloop_set_verbose (reactor, true); rc = zloop_reader (reactor, root, handle_pipe, NULL); assert (rc == 0); zloop_start (reactor); zloop_destroy (&reactor); zactor_destroy (&root); return 0; }
static void graylog_forwarder_subscriber(zsock_t *pipe, void *args) { set_thread_name("graylog-forwarder-subscriber"); int rc; subscriber_state_t *state = subscriber_state_new(pipe, args); // signal readyiness after sockets have been created zsock_signal(pipe, 0); // set up event loop zloop_t *loop = zloop_new(); assert(loop); zloop_set_verbose(loop, 0); // we rely on the controller shutting us down zloop_ignore_interrupts(loop); // setup handler for actor messages rc = zloop_reader(loop, state->pipe, actor_command, state); assert(rc == 0); // setup handler for the sub socket rc = zloop_reader(loop, state->sub_socket, read_request_and_forward, state); assert(rc == 0); // run the loop fprintf(stdout, "[I] subscriber: listening\n"); bool should_continue_to_run = getenv("CPUPROFILE") != NULL; do { rc = zloop_start(loop); should_continue_to_run &= errno == EINTR; if (!zsys_interrupted) log_zmq_error(rc, __FILE__, __LINE__); } while (should_continue_to_run); fprintf(stdout, "[I] subscriber: shutting down\n"); // shutdown subscriber_state_destroy(&state); zloop_destroy(&loop); assert(loop == NULL); fprintf(stdout, "[I] subscriber: terminated\n"); }
void client_proxy(zsock_t *pipe, void *arg) { client_proxy_t *self = s_new(pipe); assert(self); zsock_signal(self->pipe, 0); zloop_reader(self->loop, self->pipe, pipe_loop_handler, self); zloop_start(self->loop); zloop_reader_end(self->loop, self->pipe); if (self->rep != NULL) zloop_reader_end(self->loop, self->rep); s_destroy(&self); }
void ticket_serial_printer(zsock_t *pipe, void *arg) { char *port = (char *) arg; ticket_printer_t *self = s_new_serial(pipe, port); zsock_signal(self->pipe, 0); int main_serial_id = zloop_timer(self->loop, 300, 0, timer_main_serial_loop, self); zloop_reader(self->loop, self->pipe, reader_pipe_event, self); zloop_start(self->loop); zloop_reader_end(self->loop, self->pipe); zloop_timer_end(self->loop, main_serial_id); s_destroy(&self); }
void ticket_hid_printer(zsock_t *pipe, void *arg) { char *device_path = (char *) arg; ticket_printer_t *self = s_new(pipe, device_path); zsock_signal(self->pipe, 0); int main_timer_id = zloop_timer(self->loop, 300, 0, timer_main_loop, self); zloop_reader(self->loop, self->pipe, reader_pipe_event, self); zloop_start(self->loop); zloop_timer_end(self->loop, main_timer_id); zloop_reader_end(self->loop, self->pipe); s_destroy(&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"); }
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); }
/* ================ run_client() ================ */ int run_client(const char *endpoint, int op_code, int total_threads, uint32_t total_files, const char *key, const char *filename, int verbose) { info_log("run_client() with op_code:%d endpoint:%s threads:%d count:%d key:%s filename:%s", op_code, endpoint, total_threads, total_files, key, filename); if ( prepare_file_data(filename) != 0 ){ return -1; } total_actors = total_threads; client_t **clients = (client_t**)malloc(sizeof(client_t*) * total_actors); for ( int i = 0 ; i < total_actors ; i++ ){ client_t *client = client_new(i, endpoint); client->op_code = op_code; client->total_threads = total_threads; client->total_files = total_files; client->key = key; client->filename = filename; client->file_data = file_data; client->file_size = file_size; client_create_actor(client); clients[i] = client; } zloop_t *loop = zloop_new(); zloop_set_verbose(loop, verbose); for ( int i = 0 ; i < total_actors ; i++ ){ zactor_t *actor = clients[i]->actor; zloop_reader(loop, (zsock_t*)zactor_resolve(actor), handle_pullin_on_client_pipe, clients[i]); } zloop_start(loop); zloop_destroy(&loop); for ( int i = 0 ; i < total_actors ; i++ ){ client_free(clients[i]); } return 0; }
static void receiver_actor (zsock_t *pipe, void *args) { // initialization ubx_block_t *b = (ubx_block_t *) args; struct zmq_receiver_info *inf = (struct zmq_receiver_info*) b->private_data; printf("zmq_receiver: actor started.\n"); // send signal on pipe socket to acknowledge initialisation zsock_signal (pipe, 0); zloop_t *loop = zloop_new (); assert (loop); int rc = zloop_reader (loop, inf->subscriber, handle_event, args); assert (rc == 0); zloop_start (loop); zloop_destroy (&loop); }
void watchdog(zsock_t *pipe, void *args) { set_thread_name("watchdog[0]"); int rc; watchdog_state_t state = { .credit = CREDIT, .received_term_cmd = false }; // signal readyiness zsock_signal(pipe, 0); // set up event loop zloop_t *loop = zloop_new(); assert(loop); zloop_set_verbose(loop, 0); // we rely on the controller shutting us down zloop_ignore_interrupts(loop); // decrease credit every second rc = zloop_timer(loop, 1000, 0, timer_event, &state); assert(rc != -1); // setup handler for actor messages rc = zloop_reader(loop, pipe, actor_command, &state); assert(rc == 0); // run the loop bool should_continue_to_run = getenv("CPUPROFILE") != NULL; do { rc = zloop_start(loop); should_continue_to_run &= errno == EINTR; if (!state.received_term_cmd) log_zmq_error(rc, __FILE__, __LINE__); } while (should_continue_to_run); if (!quiet) printf("[I] watchdog[0]: shutting down\n"); // shutdown zloop_destroy(&loop); assert(loop == NULL); if (!quiet) printf("[I] watchdog[0]: terminated\n"); }
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; }
static int pipe_loop_handler(zloop_t *loop, zsock_t *pipe, void *arg) { int ret = 0; client_proxy_t *self = arg; zmsg_t *request = zmsg_recv(pipe); if (!request) { return ret; } char *command = zmsg_popstr(request); if (self->verbose) { zsys_debug("client proxy: API command %s", command); } if (streq(command, "START")) { char *endpoint = zmsg_popstr(request); self->rep = zsock_new_rep(endpoint); assert(self->rep); zsys_info("client proxy: is listening on %s", endpoint); zloop_reader(self->loop, self->rep, reader_rep_event, self); zstr_free(&endpoint); char *pub_ep = zmsg_popstr(request); self->pub = zsock_new_pub(pub_ep); assert(self->pub); zsys_info("client proxy: is publishing on %s", pub_ep); zstr_free(&pub_ep); zsock_signal(self->pipe, 0); } else if (streq(command, "SETTICKETSTORE")) { char *ep = zmsg_popstr(request); self->ticket_store_req = zsock_new_req(ep); assert(self->ticket_store_req); zsys_info("client proxy: ticket api using %s", ep); zstr_free(&ep); char *pub_ep = zmsg_popstr(request); self->ticket_store_sub = zsock_new_sub(pub_ep, ""); zloop_reader(self->loop, self->ticket_store_sub, reader_internal_sub_event, self); zsys_info("client proxy: republishing messages from %s", pub_ep); zstr_free(&pub_ep); zsock_signal(self->pipe, 0); } else if (streq(command, "SETPRINTERSTORE")) { char *ep = zmsg_popstr(request); self->printer_store_req = zsock_new_req(ep); assert(self->printer_store_req); zsys_info("client proxy: printer api is ready from %s", ep); zstr_free(&ep); char *subep = zmsg_popstr(request); self->printer_store_sub = zsock_new_sub(subep, ""); zloop_reader(self->loop, self->printer_store_sub, reader_internal_sub_event, self); zsys_info("client proxy: republishing message from %s", subep); zstr_free(&subep); zsock_signal(self->pipe, 0); } else if (streq(command, "$TERM")) { ret = -1; } else if (streq(command, "VERBOSE")) { self->verbose = true; } zstr_free(&command); zmsg_destroy(&request); return ret; }
/// // Register socket reader with the reactor. When the reader has messages, // the reactor will call the handler, passing the arg. Returns 0 if OK, -1 // if there was an error. If you register the same socket more than once, // each instance will invoke its corresponding handler. int QZloop::reader (QZsock *sock, zloop_reader_fn handler, void *arg) { int rv = zloop_reader (self, sock->self, handler, arg); return rv; }
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); // Check whether loop properly ignores zsys_interrupted flag // when asked to zloop_destroy (&loop); loop = zloop_new (); bool timer_event_called = false; zloop_timer (loop, 1, 1, s_timer_event3, &timer_event_called); zsys_interrupted = 1; zloop_start (loop); // zloop returns immediately without giving any handler a chance to run assert (!timer_event_called); zloop_set_nonstop (loop, true); zloop_start (loop); // zloop runs the handler which will terminate the loop assert (timer_event_called); zsys_interrupted = 0; // Check if reader removed in timer is not called zloop_destroy (&loop); loop = zloop_new (); bool socket_event_called = false; zloop_reader (loop, output, s_socket_event1, &socket_event_called); zloop_timer (loop, 0, 1, s_timer_event5, output); zstr_send (input, "PING"); zloop_start (loop); assert (!socket_event_called); // cleanup zloop_destroy (&loop); assert (loop == NULL); zsock_destroy (&input); zsock_destroy (&output); #if defined (__WINDOWS__) zsys_shutdown(); #endif // @end printf ("OK\n"); }
/// // Register socket reader with the reactor. When the reader has messages, // the reactor will call the handler, passing the arg. Returns 0 if OK, -1 // if there was an error. If you register the same socket more than once, // each instance will invoke its corresponding handler. int QmlZloop::reader (QmlZsock *sock, zloop_reader_fn handler, void *arg) { return zloop_reader (self, sock->self, handler, arg); };