Ejemplo n.º 1
0
static void s_destroy(client_proxy_t **self_p) {
    assert(self_p);
    if (*self_p) {
        client_proxy_t *self = *self_p;

        if (self->ticket_store_req != NULL) {
            zsock_destroy(&self->ticket_store_req);
        }
        if (self->ticket_store_sub != NULL) {
            zsock_destroy(&self->ticket_store_sub);
        }
        if (self->printer_store_req != NULL) {
            zsock_destroy(&self->printer_store_req);
        }
        if (self->pub != NULL) {
            zsock_destroy(&self->pub);
        }
        if (self->rep != NULL) {
            zsock_destroy(&self->rep);
        }
        if (self->printer_store_sub != NULL) {
            zsock_destroy(&self->printer_store_sub);
        }
        zloop_destroy(&self->loop);
        free(self);
        *self_p = NULL;
    }
}
Ejemplo n.º 2
0
int main (void)
{
    clonesrv_t *self = (clonesrv_t *) zmalloc (sizeof (clonesrv_t));

    self->port = 5556;
    self->ctx = zctx_new ();
    self->kvmap = zhash_new ();
    self->loop = zloop_new ();
    zloop_set_verbose (self->loop, FALSE);

    //  Set up our clone server sockets
    self->snapshot  = zsocket_new (self->ctx, ZMQ_ROUTER);
    self->publisher = zsocket_new (self->ctx, ZMQ_PUB);
    self->collector = zsocket_new (self->ctx, ZMQ_PULL);
    zsocket_bind (self->snapshot,  "tcp://*:%d", self->port);
    zsocket_bind (self->publisher, "tcp://*:%d", self->port + 1);
    zsocket_bind (self->collector, "tcp://*:%d", self->port + 2);

    //  Register our handlers with reactor
    zmq_pollitem_t poller = { self->snapshot, 0, ZMQ_POLLIN };
    zloop_poller (self->loop, &poller, s_snapshots, self);
    poller.socket = self->collector;
    zloop_poller (self->loop, &poller, s_collector, self);
    zloop_timer  (self->loop, 1000, 0, s_flush_ttl, self);

    //  Run reactor until process interrupted
    zloop_start (self->loop);

    zloop_destroy (&self->loop);
    zhash_destroy (&self->kvmap);
    zctx_destroy (&self->ctx);
    free (self);
    return 0;
}
Ejemplo n.º 3
0
zloop_t *
zloop_new (void)
{
    zloop_t
    *self;

    self = (zloop_t *) zmalloc (sizeof (zloop_t));
    if (!self)
        return NULL;

    self->readers = zlistx_new ();
    if (self->readers)
        self->pollers = zlistx_new ();
    if (self->pollers)
        self->timers = zlistx_new ();
    if (self->timers)
        self->zombies = zlistx_new ();
    if (self->zombies)
        self->tickets = zlistx_new ();
    if (self->tickets) {
        self->last_timer_id = 0;
        zlistx_set_destructor (self->readers, (czmq_destructor *) s_reader_destroy);
        zlistx_set_destructor (self->pollers, (czmq_destructor *) s_poller_destroy);
        zlistx_set_destructor (self->timers, (czmq_destructor *) s_timer_destroy);
        zlistx_set_comparator (self->timers, (czmq_comparator *) s_timer_comparator);
        zlistx_set_destructor (self->tickets, (czmq_destructor *) s_ticket_destroy);
        zlistx_set_comparator (self->tickets, (czmq_comparator *) s_ticket_comparator);
    }
    else
        zloop_destroy (&self);
    return self;
}
Ejemplo n.º 4
0
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;
}
Ejemplo n.º 5
0
Archivo: evsub.c Proyecto: bartuer/bew
/* url subtitle arguments */
int main (int argc, char *argv []) {
  void *context = zmq_init (1);

  void *subscriber = zmq_socket (context, ZMQ_SUB);
  size_t title_len = 0;
  assert(zmq_connect (subscriber, argv[1]) == 0);
  assert(title_len = strlen(argv[2]) > 1);
  assert(zmq_setsockopt (subscriber, ZMQ_SUBSCRIBE, argv[2], title_len) == 0);

  Bool verbose = 0;
  zloop_t *loop = zloop_new ();
  assert (loop);
  zloop_set_verbose (loop, verbose);

  zmq_pollitem_t sub_event = { subscriber, 0, ZMQ_POLLIN };
  if ( argc > 3 ) {
    assert(zloop_poller (loop, &sub_event, subscriber_cb, (void*)(argv + 3)) == 0);    
  } else {
    assert(zloop_poller (loop, &sub_event, subscriber_cb, NULL) == 0);    
  }

  zloop_start (loop);

  zloop_destroy (&loop);
  assert (loop == NULL);
  zmq_close (subscriber);
  zmq_term (context);

  return 0;
}
Ejemplo n.º 6
0
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");
}
Ejemplo n.º 7
0
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;
}
Ejemplo n.º 8
0
void 
bstar_destroy(bstar_t **self_p)
{
	assert(self_p);
	if (*self_p){
		bstar_t *self = *self_p;
		zloop_destroy(&self->loop);
		zctx_destroy(&self->ctx);
		free(self);
		*self_p = NULL;
	}
}
int main (int argc, char** argv)
{
    int port = 5556;

#ifdef FF_USE_LIBGC
    GC_INIT();
    set_program_name (argv[0]);
#endif

    if( argc >= 2 )
	port = atoi( argv[1] );
	
    clonesrv_t *self = (clonesrv_t *) zmalloc (sizeof (clonesrv_t));
    self->port = port;
    self->ctx = zctx_new ();
    self->kvmap = zhash_new ();
    self->loop = zloop_new ();
    zloop_set_verbose (self->loop, false);

    //  Set up our clone server sockets
    self->snapshot  = zsocket_new (self->ctx, ZMQ_ROUTER);
    zsocket_bind (self->snapshot,  "tcp://*:%d", self->port + socket_srv_offset_snapshot );
    self->publisher = zsocket_new (self->ctx, ZMQ_PUB);
    zsocket_bind (self->publisher, "tcp://*:%d", self->port + socket_srv_offset_publisher );
    self->collector = zsocket_new (self->ctx, ZMQ_PULL);
    zsocket_bind (self->collector, "tcp://*:%d", self->port + socket_srv_offset_collector );
    self->ping = zsocket_new (self->ctx, ZMQ_REP);
    zsocket_bind (self->ping,      "tcp://*:%d", self->port + socket_srv_offset_ping );

    //  Register our handlers with reactor
    zmq_pollitem_t poller = { 0, 0, ZMQ_POLLIN };
    poller.socket = self->snapshot;
    zloop_poller (self->loop, &poller, s_snapshots, self);
    poller.socket = self->collector;
    zloop_poller (self->loop, &poller, s_collector, self);
    zloop_timer (self->loop, 1000, 0, s_flush_ttl, self);
    poller.socket = self->ping;
    zloop_poller (self->loop, &poller, s_ping, self);

    
    
    DEBUG ("I: server up and running on port:%d...", port );
    //  Run reactor until process interrupted
    zloop_start (self->loop);

    zloop_destroy (&self->loop);
    zhash_destroy (&self->kvmap);
    zctx_destroy (&self->ctx);
    free (self);
    return 0;
}
Ejemplo n.º 10
0
Archivo: zdir.c Proyecto: diorcety/czmq
static void
s_zdir_watch_destroy (zdir_watch_t **watch_p)
{
    assert (watch_p);
    if (*watch_p) {
        zdir_watch_t *watch = *watch_p;

        zloop_destroy (&watch->loop);
        zhash_destroy (&watch->subs);

        freen (watch);
        *watch_p = NULL;
    }
}
Ejemplo n.º 11
0
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");
}
int graylog_forwarder_run_controller_loop(zconfig_t* config, zlist_t* devices, zlist_t *subscriptions, int rcv_hwm, int send_hwm)
{
    set_thread_name("controller");

    zsys_init();

    controller_state_t state = {.config = config};
    bool start_up_complete = controller_create_actors(&state, devices, subscriptions, rcv_hwm, send_hwm);

    if (!start_up_complete)
        goto exit;

    // set up event loop
    zloop_t *loop = zloop_new();
    assert(loop);
    zloop_set_verbose(loop, 0);

    // send tick commands every second
    int rc = zloop_timer(loop, 1000, 1, send_tick_commands, &state);
    assert(rc != -1);

    // run the loop
    // when running under the google profiler, zmq_poll terminates with EINTR
    // so we keep the loop running in this case
    if (!zsys_interrupted) {
        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);
    }
    printf("[I] controller: shutting down\n");

    // shutdown
    zloop_destroy(&loop);
    assert(loop == NULL);

 exit:
    printf("[I] controller: destroying actor threads\n");
    controller_destroy_actors(&state);
    printf("[I] controller: calling zsys_shutdown\n");
    zsys_shutdown();

    printf("[I] controller: terminated\n");
    return 0;
}
Ejemplo n.º 13
0
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);
}
Ejemplo n.º 14
0
/* ================ 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 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");
}
Ejemplo n.º 16
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);
}
Ejemplo n.º 17
0
Archivo: vtx_tcp.c Proyecto: imatix/vtx
static void
driver_destroy (driver_t **self_p)
{
    assert (self_p);
    if (*self_p) {
        driver_t *self = *self_p;
        if (self->verbose)
            zclock_log ("I: (tcp) shutting down driver");
        while (zlist_size (self->vockets)) {
            vocket_t *vocket = (vocket_t *) zlist_pop (self->vockets);
            vocket_destroy (&vocket);
        }
        zlist_destroy (&self->vockets);
        zloop_destroy (&self->loop);
        free (self);
        *self_p = NULL;
    }
}
Ejemplo n.º 18
0
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");
}
Ejemplo n.º 19
0
int main(void)
{
	zctx_t *ctx = zctx_new();
	lbbroker_t *self = (lbbroker_t *)zmalloc(sizeof(lbbroker_t));
	self->frontend = zsocket_new(ctx, ZMQ_ROUTER);
	self->backend = zsocket_new(ctx, ZMQ_ROUTER);

#if (defined (WIN32))
	zsocket_bind(self->frontend, "tcp://*:5672"); // frontend
	zsocket_bind(self->backend, "tcp://*:5673"); // backend
#else
	zsocket_bind(self->frontend, "ipc://frontend.ipc");
	zsocket_bind(self->backend, "ipc://backend.ipc");
#endif

	int client_nbr;
	for (client_nbr = 0; client_nbr < NBR_CLIENTS; client_nbr++)
		zthread_new(client_task, NULL);
	int worker_nbr;
	for (worker_nbr = 0; worker_nbr < NBR_WORKERS; worker_nbr++)
		zthread_new(worker_task, NULL);

	// Queue of available workers
	self->workers = zlist_new();

	// Prepare reactor and fire it up
	zloop_t *reactor = zloop_new();
	zmq_pollitem_t poller = { self->backend, 0, ZMQ_POLLIN };
	zloop_poller(reactor, &poller, s_handle_backend, self);
	zloop_start(reactor);
	zloop_destroy(&reactor);

	// When we're done, clean up properly
	while (zlist_size(self->workers)) {
		zframe_t *frame = (zframe_t *)zlist_pop(self->workers);
		zframe_destroy(&frame);
	}
	zlist_destroy(&self->workers);
	zctx_destroy(&ctx);
	free(self);
	return 0;
}
Ejemplo n.º 20
0
int maltcp_ctx_destroy(void **self_p) {
  clog_debug(maltcp_logger, "maltcp_ctx_destroy...\n");
  if (self_p && *self_p) {
    maltcp_ctx_t *self = (maltcp_ctx_t *) *self_p;


    // TODO(AF): zmq_close versus zsocket_destroy
    zsocket_set_linger(self->endpoints_socket, 0);
    clog_debug(maltcp_logger, "maltcp_ctx_destroy: linger=%d\n", zsocket_linger(self->endpoints_socket));
    zsocket_destroy(self->zmq_ctx, self->endpoints_socket);
  //  zmq_close(mal_ctx->endpoints_socket);

    // Free all structures in hash-table, close socket and destroy mutex.
    if (self->cnx_table) {
      maltcp_ctx_connection_t *cnx_ptr =  (maltcp_ctx_connection_t*) zhash_first(self->cnx_table);
      while (cnx_ptr) {
        // Close registered TCP connections
        clog_debug(maltcp_logger, "maltcp_ctx_destroy: close socket %d.\n", cnx_ptr->socket);
        maltcp_ctx_socket_destroy(cnx_ptr);
        // destroy all registered sockets
        cnx_ptr = (maltcp_ctx_connection_t*) zhash_next(self->cnx_table);
      }
    }

    // TODO (AF): Close all pollers?
    clog_debug(maltcp_logger, "maltcp_ctx_destroy: stopped.\n");

    free(self->maltcp_header);
    // Free all structures in hash-table, close socket and destroy mutex.
    if (self->cnx_table)
      zhash_destroy(&self->cnx_table);
    zloop_destroy(&self->zloop);
//    zsocket_destroy(self->zmq_ctx, self->endpoints_socket);
//    zmq_close(self->endpoints_socket);
    zctx_destroy(&self->zmq_ctx);

    free(self);
    *self_p = NULL;
  }
  clog_debug(maltcp_logger, "maltcp_ctx_destroy: destroyed.\n");
  return 0;
}
Ejemplo n.º 21
0
static void s_destroy(ticket_printer_t **self_p) {
    assert(self_p);
    if (*self_p) {
        ticket_printer_t *self = *self_p;
        if (self->ticket_store_req != NULL)
            zsock_destroy(&self->ticket_store_req);
        if (self->printer_store_req != NULL)
            zsock_destroy(&self->printer_store_req);
        zloop_destroy(&self->loop);
        if (self->hid_printer != NULL)
            boca_hid_destroy(&self->hid_printer);
#ifdef  __WIN32__
        if (self->serial_printer != NULL)
            boca_serial_destroy(&self->serial_printer);
#endif
        free(self->last_token);
        free(self);
        *self_p = NULL;
    }
}
Ejemplo n.º 22
0
Archivo: zloop.c Proyecto: fars/czmq
void
zloop_test (bool verbose)
{
    printf (" * zloop: ");
    int rc = 0;
    //  @selftest
    zctx_t *ctx = zctx_new ();
    assert (ctx);

    void *output = zsocket_new (ctx, ZMQ_PAIR);
    assert (output);
    zsocket_bind (output, "inproc://zloop.test");
    void *input = zsocket_new (ctx, ZMQ_PAIR);
    assert (input);
    zsocket_connect (input, "inproc://zloop.test");

    zloop_t *loop = zloop_new ();
    assert (loop);
    zloop_set_verbose (loop, verbose);

    //  Create a timer that will be canceled
    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 output
    zloop_timer (loop, 20, 1, s_timer_event, output);
    
    //  When we get the ping message, end the reactor
    zmq_pollitem_t poll_input = { input, 0, ZMQ_POLLIN };
    rc = zloop_poller (loop, &poll_input, s_socket_event, NULL);    
    assert (rc == 0);
    zloop_set_tolerant (loop, &poll_input);
    zloop_start (loop);

    zloop_destroy (&loop);
    assert (loop == NULL);

    zctx_destroy (&ctx);
    //  @end
    printf ("OK\n");
}
Ejemplo n.º 23
0
int main (int argc, char *argv [])
{
  if (argc == 2) {
    count = atoi(argv[1]);
  }

  zctx_t *ctx = zctx_new();
  void *client = zsocket_new(ctx, ZMQ_ROUTER);
  zloop_t *loop = zloop_new();

  zsocket_bind(client, "ipc:///tmp/zmqtestbr");

  zmq_pollitem_t items [] = { { client, 0, ZMQ_POLLIN, 0 } };
  zloop_poller(loop, items, recvHandler, client);

  zloop_start(loop);

  zloop_destroy(&loop);
  zctx_destroy(&ctx);
  return 0;
}
Ejemplo n.º 24
0
static void
s_self_destroy(self_t** self_p)
{
    assert(self_p);

    if (*self_p) {
        self_t* self = *self_p;
        if (self->mrb) {
            mrb_close(self->mrb);
            self->mrb = NULL;
        }
        zloop_destroy(&self->reactor);
        actor_message_destroy(&self->actor_msg);
        zsock_destroy(&self->router);
        zsock_destroy(&self->pull);
        if (self->discovery) {
            zyre_destroy(&self->discovery);
        }
        actor_discovery_destroy(&self->actor_discovery_msg);
        free(self);
        *self_p = NULL;
    }
}
Ejemplo n.º 25
0
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");
	
}
Ejemplo n.º 26
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;
}
Ejemplo n.º 27
0
JNIEXPORT void JNICALL
Java_org_zeromq_czmq_Zloop__1_1destroy (JNIEnv *env, jclass c, jlong self)
{
    zloop_destroy ((zloop_t **) &self);
}
Ejemplo n.º 28
0
Archivo: zloop.c Proyecto: skaes/czmq
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");
}
Ejemplo n.º 29
0
///
//  Destroy a reactor
QZloop::~QZloop ()
{
    zloop_destroy (&self);
}
Ejemplo n.º 30
0
///
//  Destroy a reactor
void QmlZloopAttached::destruct (QmlZloop *qmlSelf) {
    zloop_destroy (&qmlSelf->self);
};