示例#1
0
bstar_t *
bstar_new (int primary, char *local, char *remote)
{
    bstar_t
        *self;

    self = (bstar_t *) zmalloc (sizeof (bstar_t));

    //  Initialize the Binary Star
    self->ctx = zctx_new ();
    self->loop = zloop_new ();
    self->state = primary? STATE_PRIMARY: STATE_BACKUP;

    //  Create publisher for state going to peer
    self->statepub = zsocket_new (self->ctx, ZMQ_PUB);
    zsocket_bind (self->statepub, local);

    //  Create subscriber for state coming from peer
    self->statesub = zsocket_new (self->ctx, ZMQ_SUB);
    zsocket_set_subscribe (self->statesub, "");
    zsocket_connect (self->statesub, remote);

    //  Set-up basic reactor events
    zloop_timer (self->loop, BSTAR_HEARTBEAT, 0, s_send_state, self);
    zmq_pollitem_t poller = { self->statesub, 0, ZMQ_POLLIN };
    zloop_poller (self->loop, &poller, s_recv_state, self);
    return self;
}
示例#2
0
文件: zdir.c 项目: diorcety/czmq
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);
}
示例#3
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;
}
示例#4
0
文件: evsub.c 项目: 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;
}
示例#5
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;
}
示例#6
0
JNIEXPORT jlong JNICALL
Java_org_zeromq_czmq_Zloop__1_1new (JNIEnv *env, jclass c)
{
    //  Disable CZMQ signal handling; allow Java to deal with it
    zsys_handler_set (NULL);
    jlong new_ = (jlong) (intptr_t) zloop_new ();
    return new_;
}
示例#7
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");
}
示例#8
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;
}
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;
}
示例#10
0
文件: zloop.c 项目: Cargo-Labs/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);

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

    zsock_destroy (&input);
    zsock_destroy (&output);
    //  @end
    printf ("OK\n");
}
示例#11
0
static client_proxy_t *s_new(zsock_t *pipe) {
    assert(pipe);
    client_proxy_t *self = zmalloc(sizeof(client_proxy_t));
    if (!self)
        return NULL;
    self->pipe = pipe;
    self->rep = NULL;
    self->pub = NULL;
    self->printer_store_sub = NULL;
    self->ticket_store_sub = NULL;
    self->ticket_store_req = NULL;
    self->printer_store_req = NULL;
    self->loop = zloop_new();
    return self;
}
示例#12
0
文件: vtx_tcp.c 项目: imatix/vtx
static driver_t *
driver_new (zctx_t *ctx, void *pipe)
{
    driver_t *self = (driver_t *) zmalloc (sizeof (driver_t));
    self->ctx = ctx;
    self->pipe = pipe;
    self->vockets = zlist_new ();
    self->loop = zloop_new ();
    self->scheme = VTX_TCP_SCHEME;

    //  Reactor starts by monitoring the driver control pipe
    zmq_pollitem_t item = { self->pipe, 0, ZMQ_POLLIN };
    zloop_poller (self->loop, &item, s_driver_control, self);
    return self;
}
示例#13
0
static ticket_printer_t *s_new_serial(zsock_t *pipe, const char *port) {
    ticket_printer_t *self = calloc(1, sizeof(ticket_printer_t));
    if (!self) {
        return NULL;
    }
    self->pipe = pipe;
    self->ticket_store_req = NULL;
    self->printer_store_req = NULL;
    self->loop = zloop_new();
    self->last_token = (char *) calloc(100, sizeof(char));
    self->boca_serial_port = strdup(port);
    self->serial_printer = boca_serial_new(port);
    self->state = STATE_PAUSE;
    return self;
}
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;
}
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");
}
示例#16
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);
}
示例#17
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;
}
示例#18
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);
}
示例#19
0
void start_loop(int count)
{
	zloop_t *loop = zloop_new();
	assert(loop);
	
	zmq_pollitem_t poll_socket = { dealer,  0, ZMQ_POLLIN, 0 };
	zloop_poller ( loop, &poll_socket, event, NULL);
	
	zloop_timer(loop, 1000, 0, sleep_loop, &count);
	zloop_timer(loop, 1000, 0, info_loop, &count);
	
	struct timeval tv;
	gettimeofday(&tv,NULL);
	start = (uint64_t)tv.tv_sec;
	
	zloop_start (loop);
	return;
}
示例#20
0
static ticket_printer_t *s_new(zsock_t *pipe, const char *device_path) {
    ticket_printer_t *self = calloc(1, sizeof(ticket_printer_t));
    if (!self) {
        return NULL;
    }
    self->pipe = pipe;
    self->ticket_store_req = NULL;
    self->printer_store_req = NULL;
    self->loop = zloop_new();
    self->last_token = (char *) calloc(100, sizeof(char));
    self->tix_counter = 0;
    self->boca_hid_device_path = strdup(device_path);
    self->hid_printer = boca_hid_new(device_path);
    self->serial_status_at = zclock_time();
    self->hid_status_at = zclock_time();
    self->state = STATE_PAUSE;
    return self;
}
示例#21
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");
}
示例#22
0
/*
PUB tcp://*:9003
<<  [ "(?stock:[A-Z][A-Z0-9]+)" ]
    [ timestamp :f64            ]
    [ price     :f64            ]
*/
int main(int argc, const char* argv[])
{
  SetConsoleTitle(L"tickz.server");

  // initialize random number generator
  srand( (unsigned int)time(NULL) );

  // initialize stock data
  tick_t msft = tick_new("MSFT", 41.78);
  tick_t aapl = tick_new("AAPL", 95.35);
  tick_t goog = tick_new("GOOG",571.09);
  tick_t yhoo = tick_new("YHOO", 34.53);
  tick_t bbry = tick_new("BBRY", 10.90);
  
  zlist_t *stocks = zlist_new();
  zlist_append(stocks,&msft);
  zlist_append(stocks,&aapl);
  zlist_append(stocks,&goog);
  zlist_append(stocks,&yhoo);
  zlist_append(stocks,&bbry);
  
  // set up publisher
  zctx_t *ctx = zctx_new();
  void *pub = zsocket_new(ctx,ZMQ_PUB);
  zsocket_bind(pub,"tcp://*:9003");

  // set up main loop
  zloop_t *loop = zloop_new();
  zloop_data_t loopdata;
  loopdata.stocks = stocks;
  loopdata.socket = pub;

  // every 500 ms, update the stocks and publish the new data
  int timer = zloop_timer(loop,500,0,onloop,&loopdata); //TOOD: take delay as input
  zloop_start(loop); //NOTE: CTRL+C will cleanly interrupt the infinite loop
  
  // clean up
  zctx_destroy(&ctx);
  zlist_destroy(&stocks);

  return 0;
}
示例#23
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;
}
示例#24
0
文件: zloop.c 项目: 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");
}
示例#25
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;
}
示例#26
0
文件: bstar.c 项目: rryqszq4/MadzMQ
bstar_t *
bstar_new(int primary, char *local, char *remote)
{
	bstar_t *self;

	self = (bstar_t *)zmalloc(sizeof(bstar_t));

	self->ctx = zctx_new();
	self->loop = zloop_new();
	self->state = primary ? STATE_PRIMARY : STATE_BACKUP;

	self->statepub = zsocket_new(self->ctx, ZMQ_PUB);
	zsocket_bind(self->statepub, local);

	self->statesub = zsocket_new(self->ctx, ZMQ_SUB);
	zsocket_set_subscribe(self->statesub, "");
	zsocket_connect(self->statesub, remote);

	zloop_timer(self->loop, BSTAR_HEARTBEAT, 0, s_send_state, self);
	zmq_pollitem_t poller = {self->statesub, 0, ZMQ_POLLIN};
	zloop_poller(self->loop, &poller, s_recv_state, self);
	return self;
}
示例#27
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");
	
}
示例#28
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;
}
示例#29
0
///
//  Create a new zloop reactor
QmlZloop *QmlZloopAttached::construct () {
    QmlZloop *qmlSelf = new QmlZloop ();
    qmlSelf->self = zloop_new ();
    return qmlSelf;
};
示例#30
0
static void forkzio_pipe_thd (void *args, zctx_t *zctx, void *zs)
{
    forkzio_t ctx = args;
    zmq_pollitem_t zp = { .fd = -1, .socket = zs, .events = ZMQ_POLLIN };
    zloop_t *zloop;
    pid_t pid;

    if (!(zloop = zloop_new ()))
        oom ();

    /* child stdin <= zs
     */
    zloop_poller (zloop, &zp, (zloop_fn *)forkzio_zsock_cb, ctx);
    ctx->zio[0] = zio_pipe_writer_create ("stdin", NULL);
    if (zio_zloop_attach (ctx->zio[0], zloop) < 0)
        err_exit ("zio_zloop_attach %s", zio_name (ctx->zio[0]));

    /* child stdout => zs
     */
    ctx->zio[1] = zio_pipe_reader_create ("stdout", zs, ctx);
    zio_set_close_cb (ctx->zio[1], forkzio_close_cb);
    if (zio_zloop_attach (ctx->zio[1], zloop) < 0)
        err_exit ("zio_zloop_attach %s", zio_name (ctx->zio[1]));
    ctx->readers++;

    /* child stderr => zs
     */
    ctx->zio[2] = zio_pipe_reader_create ("stderr", zs, ctx);
    zio_set_close_cb (ctx->zio[2], forkzio_close_cb);
    if (zio_zloop_attach (ctx->zio[2], zloop) < 0)
        err_exit ("zio_zloop_attach %s", zio_name (ctx->zio[2]));
    ctx->readers++;

    pid = forkzio_fork (ctx);
    (void)zloop_start (zloop);
    forkzio_wait (pid);

    zio_destroy (ctx->zio[0]);
    zio_destroy (ctx->zio[1]);
    zio_destroy (ctx->zio[2]);

    zstr_send (zs, ""); /* signify EOF by sending an empty message */
}

static void forkzio_pty_thd (void *args, zctx_t *zctx, void *zs)
{
    forkzio_t ctx = args;
    zmq_pollitem_t zp = { .fd = -1, .socket = zs, .events = ZMQ_POLLIN };
    zloop_t *zloop;
    pid_t pid;
    int ptyfd;

    if (!(zloop = zloop_new ()))
        oom ();

    switch ((pid = forkpty (&ptyfd, NULL, NULL, NULL))) {
        case -1: /* error */
            err_exit ("forkpty");
        case 0: /* child */
            (void)execvp (ctx->av[0], ctx->av);
            err_exit ("%s", ctx->av[0]);
        default: /* parent */
            break;
    }

    /* Data read from zs is written to pty master
     */
    zloop_poller (zloop, &zp, (zloop_fn *)forkzio_zsock_cb, ctx);
    ctx->zio[0] = zio_writer_create ("stdin", ptyfd, NULL);
    zio_set_unbuffered (ctx->zio[0]);
    if (zio_zloop_attach (ctx->zio[0], zloop) < 0)
        err_exit ("zio_zloop_attach %s", zio_name (ctx->zio[0]));

    /* Data read from pty master is written to zs
     */
    ctx->zio[1] = zio_reader_create ("stdout", ptyfd, zs, ctx);
    zio_set_unbuffered (ctx->zio[1]);
    zio_set_close_cb (ctx->zio[1], forkzio_close_cb);
    if (zio_zloop_attach (ctx->zio[1], zloop) < 0)
        err_exit ("zio_zloop_attach %s", zio_name (ctx->zio[1]));
    ctx->readers++;

    (void)zloop_start (zloop);
    forkzio_wait (pid);

    zio_destroy (ctx->zio[0]);
    zio_destroy (ctx->zio[1]);

    zstr_send (zs, ""); /* signify EOF by sending an empty message */
}

forkzio_t forkzio_open (zctx_t *zctx, int ac, char **av, int flags)
{
    zthread_attached_fn *thd = forkzio_pipe_thd;
    forkzio_t ctx = xzmalloc (sizeof (*ctx));

    ctx->ac = ac;
    ctx->av = av;
    ctx->zctx = zctx;
    ctx->flags = flags;

    if ((ctx->flags & FORKZIO_FLAG_PTY))
        thd = forkzio_pty_thd;
    if (!(ctx->zs = zthread_fork (zctx, thd, ctx))) {
        free (ctx);
        ctx = NULL;
    }
    return ctx;
}