Beispiel #1
0
//  Worker using REQ socket to do load-balancing
//
static void *
worker_task(void *args)
{
	zctx_t *ctx = zctx_new();
	void *worker = zsocket_new(ctx, ZMQ_REQ);

#if (defined (WIN32))
	zsocket_connect(worker, "tcp://localhost:5673"); // backend
#else
	zsocket_connect(worker, "ipc://backend.ipc");
#endif

	//  Tell broker we're ready for work
	zframe_t *frame = zframe_new(WORKER_READY, strlen(WORKER_READY));
	zframe_send(&frame, worker, 0);

	//  Process messages as they arrive
	while (1) {
		zmsg_t *msg = zmsg_recv(worker);
		if (!msg)
			break;              //  Interrupted
		zframe_print(zmsg_last(msg), "Worker: ");
		zframe_reset(zmsg_last(msg), "OK", 2);
		zmsg_send(&msg, worker);
	}
	zctx_destroy(&ctx);
	return NULL;
}
int
main (int argc, char *argv[])
{

    if (argc != 3) {
        exit (-1);
    }

    int numb_msgs = atoi (argv[2]);

    zctx_t *ctx = zctx_new ();

    void *dealer = zsocket_new (ctx, ZMQ_DEALER);
    zsocket_set_linger (dealer, -1);
    zsocket_connect (dealer, "%s:9000", argv[1]);

    void *sub = zsocket_new (ctx, ZMQ_SUB);
    zsocket_connect (sub, "%s:9002", argv[1]);
    zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "all", 4);

    int64_t time[2];

    zmq_pollitem_t pollitem[1] = { {sub, 0, ZMQ_POLLIN}
    };

    zmq_poll (pollitem, 1, -1);
    zmsg_t *signal = zmsg_recv (sub);
    zmsg_destroy (&signal);

    char blob[SIZE] = { 0 };
    zmsg_t *msg = zmsg_new ();
    zframe_t *frame = zframe_new (blob, SIZE);
    zmsg_add (msg, frame);

    time[0] = zclock_time ();

    int i;
    for (i = 0; i < numb_msgs; i++) {
        zmsg_t *nmsg = zmsg_dup (msg);
        zmsg_send (&nmsg, dealer);


    }
    time[1] = zclock_time ();

    zmsg_destroy (&msg);

    zmq_poll (pollitem, 1, -1);
    msg = zmsg_recv (sub);
    zmsg_destroy (&msg);


    msg = zmsg_new ();
    frame = zframe_new (time, sizeof (int64_t) * 2);
    zmsg_add (msg, frame);
    zmsg_send (&msg, dealer);


    zctx_destroy (&ctx);
}
Beispiel #3
0
// Basic request-reply client using REQ socket
//
static void *
client_task(void *args)
{
	zctx_t *ctx = zctx_new();
	void *client = zsocket_new(ctx, ZMQ_REQ);

#if (defined (WIN32))
	zsocket_connect(client, "tcp://localhost:5672"); // frontend
#else
	zsocket_connect(client, "ipc://frontend.ipc");
#endif

	// Send request, get reply
	while (1) {
		zstr_send(client, "HELLO");
		char *reply = zstr_recv(client);
		if (!reply) {
			break;
		}
		printf("Client: %s\n", reply);
		free(reply);
		zclock_sleep(1);
	}

	zctx_destroy(&ctx);
	return NULL;
}
Beispiel #4
0
void
zproxy_test (bool verbose)
{
    printf (" * zproxy: ");

    //  @selftest
    zctx_t *ctx = zctx_new ();
    void *frontend = zsocket_new (ctx, ZMQ_PULL);
    int rc = zsocket_bind (frontend, "inproc://frontend");
    assert (rc == 0);
    void *backend = zsocket_new (ctx, ZMQ_PUSH);
    rc = zsocket_bind (backend, "inproc://backend");
    assert (rc == 0);
    zproxy_t *proxy = zproxy_new (ctx, frontend, backend);

    //  Connect application sockets to proxy
    void *faucet = zsocket_new (ctx, ZMQ_PUSH);
    rc = zsocket_connect (faucet, "inproc://frontend");
    assert (rc == 0);
    void *sink = zsocket_new (ctx, ZMQ_PULL);
    rc = zsocket_connect (sink, "inproc://backend");
    assert (rc == 0);

    //  Send some messages and check they arrived
    zstr_send (faucet, "Hello");
    char *string = zstr_recv (sink);
    assert (streq (string, "Hello"));
    zstr_free (&string);
    
    //  Check pause/resume functionality
    zproxy_pause (proxy);
    zstr_send (faucet, "World");

    zproxy_resume (proxy);
    string = zstr_recv (sink);
    assert (streq (string, "World"));
    zstr_free (&string);
    
    //  Create capture socket, must be a PULL socket
    void *capture = zsocket_new (ctx, ZMQ_PULL);
    rc = zsocket_bind (capture, "inproc://capture");
    assert (rc == 0);

    //  Switch on capturing, check that it works
    zproxy_capture (proxy, "inproc://capture");
    zstr_send (faucet, "Hello");
    
    string = zstr_recv (sink);
    assert (streq (string, "Hello"));
    zstr_free (&string);
    
    string = zstr_recv (capture);
    assert (streq (string, "Hello"));
    zstr_free (&string);
    zproxy_destroy (&proxy);
    zctx_destroy (&ctx);

    //  @end
    printf ("OK\n");
}
Beispiel #5
0
static void
collabclient_remakeSockets( cloneclient_t *cc )
{
    cc->snapshot = zsocket_new (cc->ctx, ZMQ_DEALER);
    zsocket_connect (cc->snapshot,
	collabclient_makeAddressString( cc->address,
	cc->port + socket_offset_snapshot));
    
    cc->subscriber = zsocket_new (cc->ctx, ZMQ_SUB);
    zsocket_set_subscribe (cc->subscriber, "");
    zsocket_connect (cc->subscriber,
	collabclient_makeAddressString( cc->address,
	cc->port + socket_offset_subscriber));
    zsocket_set_subscribe (cc->subscriber, SUBTREE);

    cc->publisher = zsocket_new (cc->ctx, ZMQ_PUSH);
    zsocket_connect (cc->publisher,
	collabclient_makeAddressString( cc->address,
	cc->port + socket_offset_publisher));

    int fd = 0;
    size_t fdsz = sizeof(fd);
    int rc = zmq_getsockopt( cc->subscriber, ZMQ_FD, &fd, &fdsz );
    printf("subscriber rc:%d fd:%d\n", rc, fd );
    GDrawAddReadFD( 0, fd, cc, zeromq_subscriber_fd_callback );
  
}
Beispiel #6
0
int game_thread( void * _parms ) {
  GameThreadParms * parms = (GameThreadParms*)_parms;

  GameState gs;
  SharedRenderState rs;

  game_init( gs, rs );

  gs.zmq_control_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR );
  {
    int ret = zsocket_connect( gs.zmq_control_socket, "inproc://control_game" );
    assert( ret == 0 );
  }
  
  gs.zmq_render_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR );
  zsocket_bind( gs.zmq_render_socket, "inproc://game_render" );

  gs.zmq_input_req = zsocket_new( parms->zmq_context, ZMQ_REQ );
  {
    int ret = zsocket_connect( gs.zmq_input_req, "inproc://input" );
    assert( ret == 0 );
  }

  unsigned int baseline = SDL_GetTicks();
  int framenum = 0;
  while ( true ) {
    unsigned int now = SDL_GetTicks();
    unsigned int target_frame = ( now - baseline ) / GAME_DELAY;
    if ( framenum <= target_frame ) {
      framenum++;
      // NOTE: build the state of the world at t = framenum * GAME_DELAY,
      // under normal conditions that's a time in the future
      // (the exception to that is if we are catching up on ticking game frames)
      game_tick( now, gs, rs );
      // notify the render thread that a new game state is ready.
      // on the next render frame, it will start interpolating between the previous state and this new one
      zstr_sendf( gs.zmq_render_socket, "%d %f %f %f %f %f %f %f %f %f", baseline + framenum * GAME_DELAY, rs.position.x, rs.position.y, rs.orientation.w, rs.orientation.x, rs.orientation.y, rs.orientation.z, rs.smoothed_angular.x, rs.smoothed_angular.y, rs.smoothed_angular.z );
    } else {
      int ahead = framenum * GAME_DELAY - ( now - baseline );
      assert( ahead > 0 );
      printf( "game sleep %d ms\n", ahead );
      SDL_Delay( ahead );
    }
    char * cmd = zstr_recv_nowait( gs.zmq_control_socket );
    if ( cmd != NULL ) {
      assert( strcmp( cmd, "stop" ) == 0 );
      free( cmd );
      break;
    }
  }
  return 0;
}
Beispiel #7
0
int socket_connect(void *socket, const char *format, ...)
{
    va_list args;
    int err, n;
    char buf[255], id[255];

    //zsocket_set_router_mandatory(socket, 1);

    va_start(args, format);

    //** Set the ID
    snprintf(buf, 255, format, args);
//  snprintf(id, 255, "%s:%ld", buf, random());

    snprintf(id, 255, "%ld", random_int(1, 1000));
    zsocket_set_identity(socket, strdup(id));

    err = zsocket_connect(socket, format, args);
    n = errno;
    va_end(args);

    log_printf(0, "id=!%s! err=%d errno=%d\n", id, err, n);

    assert(err == 0);
    return(err);
}
Beispiel #8
0
int
main(int argc, const char **argv){
  zctx_t *ctx = zctx_new();
  void *sub = zsocket_new(ctx, ZMQ_SUB);
  zsocket_set_subscribe(sub, "");
  zsocket_connect(sub, "tcp://127.0.0.1:3000");
  printf("connected to :3000\n");

  struct timeval start;
  gettimeofday(&start, NULL);

  while (1) {
    char *msg = zstr_recv(sub);
    if (0 == n--) {
      struct timeval end;
      gettimeofday(&end, NULL);
      int duration = end.tv_sec - start.tv_sec;
      printf("\n");
      printf("  pub/sub:\n");
      printf("    %d msgs\n", ops);
      printf("    %d ops/s\n", ops / duration);
      printf("    %d s\n", duration);
      printf("\n");
      exit(0);
    }
  }

  return 0;
}
Beispiel #9
0
static void* 
worker_routine(void* arg)
{
  zmsg_t* msg;
  zframe_t* frame;
  zctx_t* ctx = zctx_new();
  void* worker = zsocket_new(ctx, ZMQ_REQ);
  zsocket_connect(worker, "ipc://%s-localbe.ipc", self);

  frame = zframe_new(WORKER_READY, 1);
  zframe_send(&frame, worker, 0);

  while (1) {
    msg = zmsg_recv(worker);
    if (!msg)
      break;

    zframe_print(zmsg_last(msg), "Worker: ");
    zframe_reset(zmsg_last(msg), "OK", 2);
    zmsg_send(&msg, worker);
  }

  zctx_destroy(&ctx);
  return NULL;
}
Beispiel #10
0
void *
zthread_fork (zctx_t *ctx, zthread_attached_fn *thread_fn, void *args)
{
    shim_t *shim = NULL;
    //  Create our end of the pipe
    void *pipe = zctx__socket_pipe (ctx);
    assert (pipe);
    zsocket_bind (pipe, "inproc://zctx-pipe-%p", pipe);

    //  Prepare argument shim for child thread
    shim = (shim_t *) zmalloc (sizeof (shim_t));
    assert (shim);
    shim->attached = thread_fn;
    shim->args = args;
    shim->ctx = zctx_shadow (ctx);
    assert (shim->ctx);
    shim->pipe = zctx__socket_pipe (shim->ctx);
    assert (shim->pipe);

    //  Connect child pipe to our pipe
    zsocket_connect (shim->pipe, "inproc://zctx-pipe-%p", pipe);

    s_thread_start (shim);
    return pipe;
}
Beispiel #11
0
static agent_t *
s_agent_new (zctx_t *ctx, void *control)
{
    agent_t *self = (agent_t *) zmalloc (sizeof (agent_t));
    self->ctx = ctx;
    self->control = control;
    self->router = zsocket_new (ctx, ZMQ_ROUTER);

    //  Connect our data socket to caller's endpoint
    self->data = zsocket_new (ctx, ZMQ_PAIR);
    char *endpoint = zstr_recv (self->control);
    int rc = zsocket_connect (self->data, "%s", endpoint);
    assert (rc != -1);
    free (endpoint);

    //  Create new client codec using cert from API
    byte public_key [32];
    byte secret_key [32];
    rc = zmq_recv (self->control, public_key, 32, 0);
    assert (rc == 32);
    rc = zmq_recv (self->control, secret_key, 32, 0);
    assert (rc == 32);
    self->cert = zcert_new_from (public_key, secret_key);
        
    self->metadata = zhash_new ();
    zhash_autofree (self->metadata);
    self->clients = zhash_new ();
    self->max_clients = 100;
    self->max_pending = 10;
    self->client_ttl = 3600;    //  60 minutes
    self->pending_ttl = 60;     //  60 seconds
    return self;
}
Beispiel #12
0
static void *
client_task (void *args)
{
    zctx_t *ctx = zctx_new ();
    void *client = zsocket_new (ctx, ZMQ_DEALER);

    //  Set random identity to make tracing easier
    char identity [10];
    sprintf (identity, "%04X-%04X", randof (0x10000), randof (0x10000));
    zsocket_set_identity (client, identity);
    zsocket_connect (client, "tcp://localhost:5570");

    zmq_pollitem_t items [] = { { client, 0, ZMQ_POLLIN, 0 } };
    int request_nbr = 0;
    while (true) {
        //  Tick once per second, pulling in arriving messages
        int centitick;
        for (centitick = 0; centitick < 100; centitick++) {
            zmq_poll (items, 1, 10 * ZMQ_POLL_MSEC);
            if (items [0].revents & ZMQ_POLLIN) {
                zmsg_t *msg = zmsg_recv (client);
                zframe_print (zmsg_last (msg), identity);
                zmsg_destroy (&msg);
            }
        }
        zstr_send (client, "request #%d");
    }
    zctx_destroy (&ctx);
    return NULL;
}
Beispiel #13
0
static void test_zmq (flux_reactor_t *reactor)
{
    zctx_t *zctx;
    void *zs[2];
    flux_watcher_t *r, *w;

    ok ((zctx = zctx_new ()) != NULL,
        "zmq: created zmq context");
    zs[0] = zsocket_new (zctx, ZMQ_PAIR);
    zs[1] = zsocket_new (zctx, ZMQ_PAIR);
    ok (zs[0] && zs[1]
        && zsocket_bind (zs[0], "inproc://test_zmq") == 0
        && zsocket_connect (zs[1], "inproc://test_zmq") == 0,
        "zmq: connected ZMQ_PAIR sockets over inproc");
    r = flux_zmq_watcher_create (reactor, zs[0], FLUX_POLLIN, zmqreader, NULL);
    w = flux_zmq_watcher_create (reactor, zs[1], FLUX_POLLOUT, zmqwriter, NULL);
    ok (r != NULL && w != NULL,
        "zmq: nonblocking reader and writer created");
    flux_watcher_start (r);
    flux_watcher_start (w);
    ok (flux_reactor_run  (reactor, 0) == 0,
        "zmq: reactor ran to completion after %d messages", zmqwriter_msgcount);
    flux_watcher_stop (r);
    flux_watcher_stop (w);
    flux_watcher_destroy (r);
    flux_watcher_destroy (w);

    zsocket_destroy (zctx, zs[0]);
    zsocket_destroy (zctx, zs[1]);
    zctx_destroy (&zctx);
}
Beispiel #14
0
int main (void)
{
    zctx_t *ctx = zctx_new ();
    zctx_set_linger (ctx, 1000);
    
    void *pub = zsocket_new (ctx, ZMQ_XPUB);
    zsocket_set_hwm (pub, 0);
    zsocket_connect (pub, "tcp://127.0.0.1:9000");

    //  Wait for subscriber to subscribe
    zframe_t *frame = zframe_recv (pub);
    zframe_destroy (&frame);
    
    //  Send HELLOs for five seconds
    size_t total = 0;
    int64_t finish_at = zclock_time () + 5000;
    
    while (zclock_time () < finish_at) {
        //  Send 100K and then check time again
        int count = 0;
        for (count = 0; count < 100000; count++)
            zstr_send (pub, "HELLO");
        total++;
    }
    printf ("%zd00000 messages sent\n", total);
    
    zstr_send (pub, "WORLD");
    zctx_destroy (&ctx);
    return 0;
}
Beispiel #15
0
void
collabclient_closeLocalServer( int port )
{
#ifdef BUILD_COLLAB

    collabclient_sniffForLocalServer_t* cc = &collabclient_sniffForLocalServer_singleton;
    zctx_t* ctx = obtainMainZMQContext();
    int beacon_port = port;
    if( !port )
	port = collabclient_getDefaultBasePort();

    printf("collabclient_closeLocalServer() port:%d\n", port);
    void* socket = zsocket_new ( ctx, ZMQ_REQ );
    zsocket_connect ( socket,
        "tcp://localhost:%d", port+socket_offset_ping);
    zstr_send( socket, "quit" );
    cc->haveServer = 0;

    if( beacon_port )
    {
	g_hash_table_remove_all( peers );
    }
    collabclient_sniffForLocalServer();
    
#endif
}
Beispiel #16
0
int main (int argc, char *argv [])
{
    //  Socket to talk to server
	printf ("Collecting updates from weather server...\n");
        zctx_t *context = zctx_new ();

	void *subscriber = zsocket_new (context, ZMQ_PULL);
	zsocket_set_hwm(subscriber, 100000); 	

	int rc = zsocket_connect (subscriber, RECEIVE_SOCKET);
	assert (rc == 0);



	void *worker = zsocket_new(context, ZMQ_PUSH);
	rc = zsocket_bind(worker, WORKER_SOCKET);	
	assert(rc == 0);
	int nthreads = 0;
	for (nthreads=0; nthreads < 50; nthreads++)
	{
		zthread_fork(context, parser_thread, NULL);
	}

	//  Subscribe to zipcode, default is NYC, 10001
	while(1)
	{
		int size;
		char *string = safe_recv_from_server (subscriber, &size);
		parse_notifications(string, size,  worker);
		free (string);
	}
printf("Ending \n");
	zctx_destroy (&context);
	return 0;
}
Beispiel #17
0
void send_czmq (char *buf, int len)
{
    zctx_t *zctx;
    void *zs;
    zmsg_t *zmsg;

    if (!(zctx = zctx_new ()))
        log_err_exit ("C: zctx_new");
    if (lopt) /* zctx linger default = 0 (flush none) */
        zctx_set_linger (zctx, linger); 
    if (!(zs = zsocket_new (zctx, ZMQ_DEALER)))
        log_err_exit ("C: zsocket_new");
    //if (lopt) // doesn't work here 
    //    zsocket_set_linger (zs, linger); 
    if (iopt)
        zsocket_set_immediate (zs, imm);
    //zsocket_set_sndhwm (zs, 0); /* unlimited */
    if (zsocket_connect (zs, "%s", uri) < 0)
        log_err_exit ("C: zsocket_connect");
    if (!(zmsg = zmsg_new ()))
        oom ();
    if (zmsg_pushmem (zmsg, buf, bufsize) < 0)
        oom ();
    if (zmsg_send (&zmsg, zs) < 0)
        log_err_exit ("C: zmsg_send");
    if (sleep_usec > 0)
        usleep (sleep_usec);
    zctx_destroy (&zctx);
}
Beispiel #18
0
static agent_t *
s_agent_new (zctx_t *ctx, void *control)
{
    agent_t *self = (agent_t *) zmalloc (sizeof (agent_t));
    self->ctx = ctx;
    self->control = control;
    self->state = waiting;
    self->dealer = zsocket_new (ctx, ZMQ_DEALER);

    //  Connect our data socket to caller's endpoint
    self->data = zsocket_new (ctx, ZMQ_PAIR);
    char *endpoint = zstr_recv (self->control);
    int rc = zsocket_connect (self->data, "%s", endpoint);
    assert (rc != -1);
    free (endpoint);

    //  Create new client codec using cert from API
    byte public_key [32];
    byte secret_key [32];
    rc = zmq_recv (self->control, public_key, 32, 0);
    assert (rc == 32);
    rc = zmq_recv (self->control, secret_key, 32, 0);
    assert (rc == 32);
    
    zcert_t *cert = zcert_new_from (public_key, secret_key);
    self->codec = curve_codec_new_client (cert);
    zcert_destroy (&cert);

    return self;
}
Beispiel #19
0
void *
zthread_fork (zctx_t *ctx, zthread_attached_fn *thread_fn, void *args)
{
    shim_t *shim = NULL;
    //  Create our end of the pipe
    void *pipe = zsocket_new (ctx, ZMQ_PAIR);
    if (pipe) {
        zsocket_set_hwm (pipe, zctx_hwm (ctx));
        zsocket_bind (pipe, "inproc://zctx-pipe-%p", pipe);
    }
    else
        return NULL;
    
    //  Prepare argument shim for child thread
    shim = (shim_t *) zmalloc (sizeof (shim_t));
    if (shim) {
        shim->attached = thread_fn;
        shim->args = args;
        shim->ctx = zctx_shadow (ctx);
        if (!shim->ctx)
            return NULL;
    }
    else
        return NULL;
    
    //  Connect child pipe to our pipe
    shim->pipe = zsocket_new (shim->ctx, ZMQ_PAIR);
    if (!shim->pipe)
        return NULL;
    zsocket_set_hwm (shim->pipe, 1);
    zsocket_connect (shim->pipe, "inproc://zctx-pipe-%p", pipe);
    
    s_thread_start (shim);
    return pipe;
}
Beispiel #20
0
int main (int argc, char *argv [])
{
    zctx_t *context = zctx_new ();
    void *publisher = zsocket_new (context, ZMQ_PUB);
    if (argc == 2)
        zsocket_connect (publisher, argv [1]);
    else
        zsocket_bind (publisher, "tcp://*:5556");

    //  Ensure subscriber connection has time to complete
    sleep (1);

    //  Send out all 1,000 topic messages
    int topic_nbr;
    for (topic_nbr = 0; topic_nbr < 1000; topic_nbr++) {
        zstr_sendm (publisher, "%03d", topic_nbr, ZMQ_SNDMORE);
        zstr_send (publisher, "Save Roger");
    }
    //  Send one random update per second
    srandom ((unsigned) time (NULL));
    while (!zctx_interrupted) {
        sleep (1);
        zstr_sendm (publisher, "%03d", randof (1000), ZMQ_SNDMORE);
        zstr_send (publisher, "Off with his head!");
    }
    zctx_destroy (&context);
    return 0;
}
Beispiel #21
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;
}
Beispiel #22
0
static void
server_worker (void *args, zctx_t *ctx, void *pipe)
{
    void *worker = zsocket_new (ctx, ZMQ_DEALER);
    zsocket_connect (worker, "inproc://backend");

    while (true) {
        //  The DEALER socket gives us the reply envelope and message
        zmsg_t *msg = zmsg_recv (worker);
        zframe_t *identity = zmsg_pop (msg);
        zframe_t *content = zmsg_pop (msg);
        assert (content);
        zmsg_destroy (&msg);

        //  Send 0..4 replies back
        int reply, replies = randof (5);
        for (reply = 0; reply < replies; reply++) {
            //  Sleep for some fraction of a second
            zclock_sleep (randof (1000) + 1);
            zframe_send (&identity, worker, ZFRAME_REUSE + ZFRAME_MORE);
            zframe_send (&content, worker, ZFRAME_REUSE);
        }
        zframe_destroy (&identity);
        zframe_destroy (&content);
    }
}
Beispiel #23
0
//  Checks whether client can connect to server
static bool
s_can_connect (zctx_t *ctx, void **server, void **client)
{
    int port_nbr = zsocket_bind (*server, "tcp://127.0.0.1:*");
    assert (port_nbr > 0);
    int rc = zsocket_connect (*client, "tcp://127.0.0.1:%d", port_nbr);
    assert (rc == 0);
    //  Give the connection time to fail if that's the plan
    zclock_sleep (200);

    //  By default PUSH sockets block if there's no peer
    zsock_set_sndtimeo (*server, 200);
    zstr_send (*server, "Hello, World");

    zpoller_t *poller = zpoller_new (*client, NULL);
    bool success = (zpoller_wait (poller, 400) == *client);
    zpoller_destroy (&poller);
    zsocket_destroy (ctx, *client);
    zsocket_destroy (ctx, *server);
    *server = zsocket_new (ctx, ZMQ_PUSH);
    assert (*server);
    *client = zsocket_new (ctx, ZMQ_PULL);
    assert (*client);
    return success;
}
Beispiel #24
0
void *
zthread_fork (zctx_t *ctx, zthread_attached_fn *thread_fn, void *args)
{
    //  Create our end of the pipe
    //  Pipe has HWM of 1 at both sides to block runaway writers
    void *pipe = zsocket_new (ctx, ZMQ_PAIR);
    assert (pipe);
    zsockopt_set_hwm (pipe, 1);
    zsocket_bind (pipe, "inproc://zctx-pipe-%p", pipe);

    //  Prepare argument shim for child thread
    shim_t *shim = (shim_t *) zmalloc (sizeof (shim_t));
    shim->attached = thread_fn;
    shim->args = args;
    shim->ctx = zctx_shadow (ctx);

    //  Connect child pipe to our pipe
    shim->pipe = zsocket_new (shim->ctx, ZMQ_PAIR);
    assert (shim->pipe);
    zsockopt_set_hwm (shim->pipe, 1);
    zsocket_connect (shim->pipe, "inproc://zctx-pipe-%p", pipe);

    s_thread_start (shim);
    return pipe;
}
Beispiel #25
0
static void
client_task (void *args, zctx_t *ctx, void *pipe)
{
    void *client = zsocket_new (ctx, ZMQ_DEALER);
    zsocket_connect (client, "tcp://localhost:5555");
    printf ("Setting up test...\n");
    zclock_sleep (100);

    int requests;
    int64_t start;

    printf ("Synchronous round-trip test...\n");
    start = zclock_time ();
    for (requests = 0; requests < 10000; requests++) {
        zstr_send (client, "hello");
        char *reply = zstr_recv (client);
        free (reply);
    }
    printf (" %d calls/second\n",
        (1000 * 10000) / (int) (zclock_time () - start));

    printf ("Asynchronous round-trip test...\n");
    start = zclock_time ();
    for (requests = 0; requests < 100000; requests++)
        zstr_send (client, "hello");
    for (requests = 0; requests < 100000; requests++) {
        char *reply = zstr_recv (client);
        free (reply);
    }
    printf (" %d calls/second\n",
        (1000 * 100000) / (int) (zclock_time () - start));
    zstr_send (pipe, "done");
}
Beispiel #26
0
static void* 
client_routine(void* arg)
{
  zctx_t* ctx = zctx_new();

  void* client = zsocket_new(ctx, ZMQ_REQ);
  zsocket_connect(client, "ipc://frontend.ipc");

  while (1) {
    char* reply;
    zstr_send(client, "Hello");
    reply = zstr_recv(client);

    if (NULL == reply)
      break;

    fprintf(stdout, "client: %s\n", reply);
    free(reply);
    sleep(1);
  }

  zctx_destroy(&ctx);

  return NULL;
}
Beispiel #27
0
static bool
s_can_connect (void *server, void *client)
{
    //  We'll do each test on a new port number since otherwise we have to
    //  destroy and recreate the sockets each time.
    static int port_nbr = 9000;
    int rc = zsocket_bind (server, "tcp://*:%d", port_nbr);
    assert (rc == port_nbr);
    rc = zsocket_connect (client, "tcp://localhost:%d", port_nbr);
    assert (rc == 0);
    
    zpoller_t *poller = zpoller_new (client, NULL);
    zstr_send (server, "Hello, World");
    //  Need up to half a second if running under Valgrind
    bool success = zpoller_wait (poller, 500) == client;
    if (success)
        free (zstr_recv (client));
    zpoller_destroy (&poller);
    rc = zsocket_unbind (server, "tcp://*:%d", port_nbr);
    assert (rc != -1);
    rc = zsocket_disconnect (client, "tcp://localhost:%d", port_nbr);
    assert (rc != -1);
    port_nbr++;
    return success;
}
Beispiel #28
0
void
zre_peer_connect (zre_peer_t *self, char *reply_to, char *endpoint)
{
    assert (self);
    assert (!self->connected);

    //  Create new outgoing socket (drop any messages in transit)
    self->mailbox = zsocket_new (self->ctx, ZMQ_DEALER);
    
    //  Set our caller 'From' identity so that receiving node knows
    //  who each message came from.
    zsocket_set_identity (self->mailbox, reply_to);

    //  Set a high-water mark that allows for reasonable activity
    zsocket_set_sndhwm (self->mailbox, PEER_EXPIRED * 100);
    
    //  Send messages immediately or return EAGAIN
    zsocket_set_sndtimeo (self->mailbox, 0);
    
    //  Connect through to peer node
    zsocket_connect (self->mailbox, "tcp://%s", endpoint);
    self->endpoint = strdup (endpoint);
    self->connected = true;
    self->ready = false;
}
Beispiel #29
0
    explicit Streamer(int i)
        : socket_(nullptr)
        , backendSocket_(nullptr)
        , id_("Streamer" + std::to_string(i))
    {
        socket_ = zsocket_new(ctx_, ZMQ_DEALER);
        assert(socket_);
        //zsock_set_identity((zsock_t*)socket_, id_.c_str());
        int rc = zsocket_connect(socket_, "tcp://127.0.0.1:5562");
        assert(rc);

        backendSocket_ = zsocket_new(ctx_, ZMQ_DEALER);
        assert(backendSocket_);
        //zsock_set_identity((zsock_t*)backendSocket_, id_.c_str());
        rc = zsocket_connect(backendSocket_, "tcp://127.0.0.1:5563");
        assert(rc);
    }
Beispiel #30
0
int
zsocket_test (Bool verbose)
{
    printf (" * zsocket: ");

    //  @selftest
    zctx_t *ctx = zctx_new ();
    assert (ctx);

    //  Create a detached thread, let it run
    char *interf = "*";
    char *domain = "localhost";
    int service = 5560;

    void *writer = zsocket_new (ctx, ZMQ_PUSH);
    assert (writer);
    void *reader = zsocket_new (ctx, ZMQ_PULL);
    assert (reader);
    assert (streq (zsocket_type_str (writer), "PUSH"));
    assert (streq (zsocket_type_str (reader), "PULL"));
    int rc = zsocket_bind (writer, "tcp://%s:%d", interf, service);
    assert (rc == service);
    rc = zsocket_connect (reader, "tcp://%s:%d", domain, service);
    assert (rc == 0);
    zstr_send (writer, "HELLO");
    char *message = zstr_recv (reader);
    assert (message);
    assert (streq (message, "HELLO"));
    free (message);

    int port = zsocket_bind (writer, "tcp://%s:*", interf);
    assert (port >= ZSOCKET_DYNFROM && port <= ZSOCKET_DYNTO);

    assert (zsocket_poll (writer, 100) == FALSE);

    rc = zsocket_connect (reader, "txp://%s:%d", domain, service);
    assert (rc == -1);

    zsocket_destroy (ctx, writer);
    zctx_destroy (&ctx);
    //  @end

    printf ("OK\n");
    return 0;
}