Пример #1
0
/* we use nano sockets to set up a pipeline like:
 *
 *   spool-reader
 *       PUSH
 *         |   <--- "ingress" socket
 *       PULL
 *    json-encoder
 *       PUSH
 *         |   <--- "egress" socket
 *       PULL
 *     kaf-sender
 */
int setup_nano(void) {
  int rc = -1;

  /* set up the ingress and egress sockets */
  if ( (CF.ingress_socket_push = nn_socket(AF_SP, NN_PUSH)) < 0) goto done;
  if ( (CF.ingress_socket_pull = nn_socket(AF_SP, NN_PULL)) < 0) goto done;
  if ( (CF.egress_socket_push  = nn_socket(AF_SP, NN_PUSH)) < 0) goto done;
  if ( (CF.egress_socket_pull  = nn_socket(AF_SP, NN_PULL)) < 0) goto done;

  pid_t pid = getpid();
  snprintf(CF.ingress_socket_nanopath,sizeof(CF.ingress_socket_nanopath), 
     "ipc:///tmp/%d.ingress.ipc", pid); /* if you change this, change below too! */
  snprintf(CF.egress_socket_nanopath, sizeof(CF.egress_socket_nanopath), 
     "ipc:///tmp/%d.egress.ipc", pid); /* if you change this, change below too! */
  /* the path without the ipc:// prefix is kept for unlinking */
  CF.ingress_socket_filepath = &CF.ingress_socket_nanopath[6];
  CF.egress_socket_filepath =  &CF.egress_socket_nanopath[6];

  if (nn_bind(CF.ingress_socket_push,    CF.ingress_socket_nanopath) < 0) goto done;
  if (nn_connect(CF.ingress_socket_pull, CF.ingress_socket_nanopath) < 0) goto done;

  if (nn_bind(CF.egress_socket_push,     CF.egress_socket_nanopath) < 0) goto done;
  if (nn_connect(CF.egress_socket_pull,  CF.egress_socket_nanopath) < 0) goto done;

  rc = 0;

 done:
  if (rc < 0) fprintf(stderr,"nano: %s\n", nn_strerror(errno));
  return rc;
}
Пример #2
0
int main ()
{
    int rc;
    char buf [3];
    struct nn_thread thread;

    sb = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sb != -1);
    rc = nn_bind (sb, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sc = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sc != -1);
    rc = nn_connect (sc, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    nn_thread_init (&thread, worker, NULL);

    rc = nn_recv (sb, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_recv (sb, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    nn_thread_term (&thread);

    rc = nn_close (sc);
    errno_assert (rc == 0);
    rc = nn_close (sb);
    errno_assert (rc == 0);

    return 0;
}
Пример #3
0
int main ()
{
    int rc;
    int s;
    struct nn_thread thread;

    /*  Close the socket with no associated endpoints. */
    s = nn_socket (AF_SP, NN_PAIR);
    errno_assert (s != -1);
    rc = nn_close (s);
    errno_assert (rc == 0);

    /*  Test nn_term() before nn_close(). */
    nn_thread_init (&thread, worker, NULL);
    nn_sleep (10);
    nn_term ();

    /*  Check that it's not possible to create new sockets after nn_term(). */
    rc = nn_socket (AF_SP, NN_PAIR);
    nn_assert (rc == -1 && nn_errno () == ETERM);
    
    /*  Wait till worker thread terminates. */
    nn_thread_term (&thread);

    return 0;
}
Пример #4
0
int main(int argc, char *argv[]) {
#ifdef USE_NETWORK
    ArgMapping mapping;
    long role = 0;
    std::string host = "127.0.0.1";
    mapping.arg("m", gM, "m");
    mapping.arg("p", gP, "p");
    mapping.arg("r", gR, "r");
    mapping.arg("L", gL, "L");
    mapping.arg("R", role, "role, 0:server, 1:client");
    mapping.arg("H", host, "host");
    mapping.arg("C", gC, "cipher to send");
    mapping.parse(argc, argv);

    if (role == 0) {
        int sock = nn_socket(AF_SP, NN_REP);
        if (nn_bind(sock, "tcp://*:12345") < 0) {
            printf("%s\n", nn_strerror(errno));
            return -1;
        }
        printf("SID %d\n", sock);
        act_server(sock);
    } else if (role == 1){
        int sock = nn_socket(AF_SP, NN_REQ);
        std::string h = "tcp://" + host + ":12345";
        if (nn_connect(sock, h.c_str()) < 0) {
            printf("%s\nn", nn_strerror(errno));
            return -1;
        }
        printf("SID %d\n", sock);
        act_client(sock);
    }
#endif
    return 0;
}
Пример #5
0
int device (const char *url1, const char *url2)
{
    int s1, s2, rv;
    s1 = nn_socket (AF_SP_RAW, NN_REQ);
    if (s1 < 0) {
        fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));
        return (-1);
    }
    if (nn_bind (s1, url1) < 0) {
        fprintf (stderr, "nn_bind1(%s): %s\n", url1, nn_strerror (nn_errno ()));
        return (-1);
    }
    s2 = nn_socket (AF_SP_RAW, NN_REP);
    if (s2 < 0) {
        fprintf (stderr, "nn_socket: %s\n", nn_strerror(nn_errno ()));
        return (-1);
    }
    if (nn_bind (s2, url2) < 0) {
        fprintf (stderr, "nn_bind2(%s): %s\n", url2, nn_strerror (nn_errno ()));
        return (-1);
    }

    if (nn_device (s1, s2) != 0) {
        fprintf (stderr, "nn_device: %s\n", nn_strerror (nn_errno ()));
        return (-1);
    }
    return (0);
}
static int connection_setup(OUTPROCESS_HANDLE_DATA* handleData, OUTPROCESS_MODULE_CONFIG * config)
{
	int result;
	handleData->control_socket = -1;
	/*
	* Start with messaging socket.
	*/
	/*Codes_SRS_OUTPROCESS_MODULE_17_008: [ This function shall create a pair socket for sending gateway messages to the module host. ]*/
	handleData->message_socket = nn_socket(AF_SP, NN_PAIR);
	if (handleData->message_socket < 0)
	{
		result = handleData->message_socket;
		LogError("message socket failed to create, result = %d, errno = %d", result, nn_errno());
	}
	else
	{
		/*Codes_SRS_OUTPROCESS_MODULE_17_009: [ This function shall bind and connect the pair socket to the message_uri. ]*/
		int message_bind_id = nn_connect(handleData->message_socket, STRING_c_str(config->message_uri));
		if (message_bind_id < 0)
		{
			result = message_bind_id;
			LogError("remote socket failed to bind to message URL, result = %d, errno = %d", result, nn_errno());
		}
		else
		{
			/*
			* Now, the control socket.
			*/
			/*Codes_SRS_OUTPROCESS_MODULE_17_010: [ This function shall create a request/reply socket for sending control messages to the module host. ]*/
			handleData->control_socket = nn_socket(AF_SP, NN_PAIR);
			if (handleData->control_socket < 0)
			{
				result = handleData->control_socket;
				LogError("remote socket failed to connect to control URL, result = %d, errno = %d", result, nn_errno());
			}
			else
			{
				/*Codes_SRS_OUTPROCESS_MODULE_17_011: [ This function shall connect the request/reply socket to the control_id. ]*/
				int control_connect_id = nn_connect(handleData->control_socket, STRING_c_str(config->control_uri));
				if (control_connect_id < 0)
				{
					result = control_connect_id;
					LogError("remote socket failed to connect to control URL, result = %d, errno = %d", result, nn_errno());
				}
				else
				{
					result = 0;
				}
			}
		}
	}
	return result;
}
Пример #7
0
int main() {
  struct nn_msgctl ctl1;
  struct nn_msgctl ctl2;
  int s1 = nn_socket (AF_SP_RAW, NN_REP);
  int s2 = nn_socket (AF_SP, NN_REQ);
  int s3 = nn_socket (AF_SP, NN_REQ);
  void *buf1 = nullptr;
  void *buf2 = nullptr;

  nnxx_assert (s1 >= 0);
  nnxx_assert (s2 >= 0);

  /*  Connecting sockets. */
  nnxx_assert (nn_bind (s1, "inproc://test") >= 0);
  nnxx_assert (nn_connect (s2, "inproc://test") >= 0);
  nnxx_assert (nn_connect (s3, "inproc://test") >= 0);

  /*  Sending requests. */
  nnxx_assert (nn_send (s2, "Hello World! (1)", 16, 0) == 16);
  nnxx_assert (nn_send (s3, "Hello World! (2)", 16, 0) == 16);

  /*  Recieving requests. */
  nnxx_assert (nn_recvfrom (s1, &buf1, NN_MSG, 0, &ctl1) == 16);
  nnxx_assert (nn_recvfrom (s1, &buf2, NN_MSG, 0, &ctl2) == 16);

  /*  Making sure we have the correct data. */
  nnxx_assert (memcmp (buf1, "Hello World! (1)", 16) == 0);
  nnxx_assert (memcmp (buf2, "Hello World! (2)", 16) == 0);

  /*  Sending responses back in reverse order. */
  nnxx_assert (nn_sendto (s1, &buf2, NN_MSG, 0, &ctl2) == 16);
  nnxx_assert (nn_sendto (s1, &buf1, NN_MSG, 0, &ctl1) == 16);

  /*  Recieving responses. */
  nnxx_assert (nn_recv (s2, &buf1, NN_MSG, 0) == 16);
  nnxx_assert (nn_recv (s3, &buf2, NN_MSG, 0) == 16);

  /*  Making sure the clients got the right responses. */
  nnxx_assert (memcmp (buf1, "Hello World! (1)", 16) == 0);
  nnxx_assert (memcmp (buf2, "Hello World! (2)", 16) == 0);

  /*  Releasing resources. */
  nn_freemsg (buf2);
  nn_freemsg (buf1);
  nn_close (s2);
  nn_close (s1);
  return nnxx::unittest::result;
}
Пример #8
0
void test_reallocmsg_reqrep ()
{
    int rc;
    int req;
    int rep;
    void *p;
    void *p2;

    /*  Create sockets. */
    req = nn_socket (AF_SP, NN_REQ);
    rep = nn_socket (AF_SP, NN_REP);
    rc = nn_bind (rep, "inproc://test");
    errno_assert (rc >= 0);
    rc = nn_connect (req, "inproc://test");
    errno_assert (rc >= 0);

    /*  Create message, make sure we handle overflow. */
    p = nn_allocmsg (100, 0);
    nn_assert (p);
    p2 = nn_reallocmsg (p, -1000);
    errno_assert (nn_errno () == ENOMEM);
    nn_assert (p2 == NULL);

    /*  Realloc to fit data size. */
    memcpy (p, "Hello World!", 12);
    p = nn_reallocmsg (p, 12);
    nn_assert (p);
    rc = nn_send (req, &p, NN_MSG, 0);
    errno_assert (rc == 12);

    /*  Receive request and send response. */
    rc = nn_recv (rep, &p, NN_MSG, 0);
    errno_assert (rc == 12);
    rc = nn_send (rep, &p, NN_MSG, 0);
    errno_assert (rc == 12);

    /*  Receive response and free message. */
    rc = nn_recv (req, &p, NN_MSG, 0);
    errno_assert (rc == 12);
    rc = memcmp (p, "Hello World!", 12);
    nn_assert (rc == 0);
    rc = nn_freemsg (p);
    errno_assert (rc == 0);

    /*  Clean up. */
    nn_close (req);
    nn_close (rep);
}
Пример #9
0
int main ()
{
    int rc;
    int sb;
    int sc;
    struct nn_iovec iov [2];
    struct nn_msghdr hdr;
    char buf [6];

    sb = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sb != -1);
    rc = nn_bind (sb, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sc = nn_socket (AF_SP, NN_PAIR);
    errno_assert (sc != -1);
    rc = nn_connect (sc, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    iov [0].iov_base = "AB";
    iov [0].iov_len = 2;
    iov [1].iov_base = "CDEF";
    iov [1].iov_len = 4;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = 2;
    rc = nn_sendmsg (sc, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 6);

    iov [0].iov_base = buf;
    iov [0].iov_len = 4;
    iov [1].iov_base = buf + 4;
    iov [1].iov_len = 2;
    memset (&hdr, 0, sizeof (hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = 2;
    rc = nn_recvmsg (sb, &hdr, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 6);
    nn_assert (memcmp (buf, "ABCDEF", 6) == 0);

    rc = nn_close (sc);
    errno_assert (rc == 0);
    rc = nn_close (sb);
    errno_assert (rc == 0);

    return 0;
}
Пример #10
0
int main ()
{
    int rc;
    int pub;
    int sub1;
    int sub2;
    char buf [3];

    pub = nn_socket (AF_SP, NN_PUB);
    errno_assert (pub != -1);
    rc = nn_bind (pub, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sub1 = nn_socket (AF_SP, NN_SUB);
    errno_assert (sub1 != -1);
    rc = nn_setsockopt (sub1, NN_SUB, NN_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    rc = nn_connect (sub1, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    sub2 = nn_socket (AF_SP, NN_SUB);
    errno_assert (sub2 != -1);
    rc = nn_setsockopt (sub2, NN_SUB, NN_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    rc = nn_connect (sub2, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    /*  Wait till connections are established to prevent message loss. */
    nn_sleep (10);

    rc = nn_send (pub, "ABC", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    rc = nn_recv (sub1, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_recv (sub2, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    rc = nn_close (pub);
    errno_assert (rc == 0);
    rc = nn_close (sub1);
    errno_assert (rc == 0);    
    rc = nn_close (sub2);
    errno_assert (rc == 0);

    return 0;
}
Пример #11
0
void worker (void *arg)
{
    int rc;
    int s;
    int i;
    char *buf;

    s = nn_socket (AF_SP, NN_PAIR);
    assert (s != -1);
    rc = nn_connect (s, "inproc://inproc_thr");
    assert (rc >= 0);

    buf = malloc (message_size);
    assert (buf);
    memset (buf, 111, message_size);

    rc = nn_send (s, NULL, 0, 0);
    assert (rc == 0);

    for (i = 0; i != message_count; i++) {
        rc = nn_send (s, buf, message_size, 0);
        assert (rc == message_size);
    }

    free (buf);
    rc = nn_close (s);
    assert (rc == 0);
}
Пример #12
0
int ftw_nanomsg_router_start (const char *addr)
{
//	struct nn_thread worker_thread;
	int frontend;
//	int backend = NULL;
//	int rc = NULL;
//
//	ftw_nanomsg_router_worker_inputs *args = (ftw_nanomsg_router_worker_inputs (*)) nn_alloc_( sizeof(ftw_nanomsg_router_worker_inputs));
//	args->backend = backend;
//	args->frontend = frontend;
	frontend = nn_socket(AF_SP_RAW, NN_REP);
	ftw_nanomsg_bind(frontend, addr);
//	backend = nn_socket(AF_SP_RAW, NN_REP);
//
//
//	if (frontend >= 0 && backend >= 0) {
//		rc = ftw_nanomsg_bind(router, addr);
//		if (rc > 0) {
////			args->router_id = router;
////			nn_thread_init (&worker_thread, ftw_nanomsg_router_worker, (void *)args);
//			nn_sleep(200);
//			rc = router;
//		}
//	}
//
//	return rc;
	return frontend;
}
Пример #13
0
ccx_share_status ccx_share_start(const char *stream_name) //TODO add stream
{
	dbg_print(CCX_DMT_SHARE, "[share] ccx_share_start: starting service\n");
	//TODO for multiple files we have to move creation to ccx_share_init
	ccx_share_ctx.nn_sock = nn_socket(AF_SP, NN_PUB);
	if (ccx_share_ctx.nn_sock < 0) {
		perror("[share] ccx_share_start: can't nn_socket()\n");
		fatal(EXIT_NOT_CLASSIFIED, "ccx_share_start");
	}

	if (!ccx_options.sharing_url) {
		ccx_options.sharing_url = strdup("tcp://*:3269");
	}

	dbg_print(CCX_DMT_SHARE, "[share] ccx_share_start: url=%s\n", ccx_options.sharing_url);

	ccx_share_ctx.nn_binder = nn_bind(ccx_share_ctx.nn_sock, ccx_options.sharing_url);
	if (ccx_share_ctx.nn_binder < 0) {
		perror("[share] ccx_share_start: can't nn_bind()\n");
		fatal(EXIT_NOT_CLASSIFIED, "ccx_share_start");
	}

	int linger = -1;
	int rc = nn_setsockopt(ccx_share_ctx.nn_sock, NN_SOL_SOCKET, NN_LINGER, &linger, sizeof(int));
	if (rc < 0) {
		perror("[share] ccx_share_start: can't nn_setsockopt()\n");
		fatal(EXIT_NOT_CLASSIFIED, "ccx_share_start");
	}

	//TODO remove path from stream name to minimize traffic (/?)
	ccx_share_ctx.stream_name = strdup(stream_name ? stream_name : "unknown");

	sleep(1); //We have to sleep a while, because it takes some time for subscribers to subscribe
	return CCX_SHARE_OK;
}
Пример #14
0
static int nn_create_socket2 (nn_options_t *options)
{
    int sock;
	int sock_type;

	switch(options->protocol) {
	case PROTO_SURVEY:
        sock_type = NN_RESPONDENT;
		break;
	case PROTO_REQREP:
        sock_type = NN_REP;
		break;
	case PROTO_PUBSUB:
        sock_type = NN_SUB;
		break;
	case PROTO_PIPELINE:
        sock_type = NN_PULL;
		break;
	case PROTO_PAIR:
        sock_type = NN_PAIR;
		break;
	case PROTO_BUS:
        return -1;
	}

    sock = nn_socket (AF_SP_RAW, sock_type);
    nn_assert_errno (sock >= 0, "Can't create socket");

    return sock;
}
Пример #15
0
subscribe_socket::subscribe_socket(callback_type callback)
    :callback(callback) {
  // create a socket
  z_socket = nn_socket(AF_SP, NN_SUB);
  set_conservative_socket_parameters(z_socket);
  thr.launch([=](){this->thread_function();});
}
Пример #16
0
/* --- server --- */
int node0(const char *url) {

  int sz_date = strlen(DATE) + 1;
  int sock = nn_socket(AF_SP, NN_REP);

  assert(sock >= 0);
  assert(nn_bind(sock, url) >= 0);

  while(1) {
    char *buf = NULL;
    int bytes = nn_recv(sock, &buf, NN_MSG, 0);
    assert(bytes >= 0);
    if(strncmp(DATE, buf, sz_date) == 0) {
      printf("NODE0: RECEIVED DATE REQUEST\n");
      char *st_date = date();
      int sz_date = strlen(st_date) + 1;
      printf("NODE0: SENDING DATE %s\n", st_date);
      bytes = nn_send(sock, st_date, sz_date, 0);
      assert(bytes == sz_date);
    }
    nn_freemsg(buf);
  }

  return nn_shutdown(sock, 0);
}
Пример #17
0
void act_client(std::string &addr, size_t sze) {
	int sock = nn_socket(AF_SP, NN_REQ);
	std::string host = "tcp://" + addr + ":12345";
	printf("host: %s\n", host.c_str());
	if (nn_connect(sock, host.c_str()) < 0) {
            printf("%s\nn", nn_strerror(errno));
            return;
	}

	std::vector<size_t> lens;
	make_package(sze, lens);
	std::vector<void *> data;
	for (auto len : lens)
		data.push_back((void *)new char[len]);

	MDL::Timer timer;
	nn_send(sock, NULL, 0, 0);
	nn_recv(sock, NULL, 0, 0);

	timer.start();
	MDL::net::send_all(sock, data, lens);
	timer.end();
	printf("send %f\n", timer.second());
	nn_close(sock);

	for (auto p : data) {
		char *pp = (char *)p;
		delete []pp;
	}
}
Пример #18
0
static void server(NN_UNUSED void *arg)
{
    int bytes;
    int count;
    int sock = nn_socket(AF_SP, NN_PULL);
    int res[TEST_LOOPS];
    nn_assert(sock >= 0);
    nn_assert(nn_bind(sock, SOCKET_ADDRESS) >= 0);
    count = THREAD_COUNT * TEST_LOOPS;
    memset(res, 0, sizeof (res));
    while (count > 0)
    {
        char *buf = NULL;
        int tid;
        int num;
        bytes = nn_recv(sock, &buf, NN_MSG, 0);
        nn_assert(bytes >= 0);
        nn_assert(bytes >= 2);
        nn_assert(buf[0] >= 'A' && buf[0] <= 'Z');
        nn_assert(buf[1] >= 'a' && buf[0] <= 'z');
        tid = buf[0]-'A';
        num = buf[1]-'a';
        nn_assert(tid < THREAD_COUNT);
        nn_assert(res[tid] == num);
        res[tid]=num+1;
        nn_freemsg(buf);
        count--;
    }
    nn_close(sock);
}
Пример #19
0
int32_t _lb_socket(uint16_t port,uint16_t globalport,uint16_t relaysport,int32_t maxmillis,char servers[][MAX_SERVERNAME],int32_t num,char backups[][MAX_SERVERNAME],int32_t numbacks,char failsafes[][MAX_SERVERNAME],int32_t numfailsafes)
{
    int32_t lbsock,timeout,retrymillis,priority = 1;
    if ( (lbsock= nn_socket(AF_SP,NN_REQ)) >= 0 )
    {
        retrymillis = (maxmillis / 30) + 1;
printf("!!!!!!!!!!!! lbsock.%d !!!!!!!!!!!\n",lbsock);
        if ( nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_RECONNECT_IVL,&retrymillis,sizeof(retrymillis)) < 0 )
            printf("error setting NN_REQ NN_RECONNECT_IVL_MAX socket %s\n",nn_errstr());
        else if ( nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_RECONNECT_IVL_MAX,&maxmillis,sizeof(maxmillis)) < 0 )
            fprintf(stderr,"error setting NN_REQ NN_RECONNECT_IVL_MAX socket %s\n",nn_errstr());
        timeout = SUPERNET.PLUGINTIMEOUT;
        if ( nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)) < 0 )
            printf("error setting NN_SOL_SOCKET NN_RCVTIMEO socket %s\n",nn_errstr());
        timeout = 100;
        if ( nn_setsockopt(lbsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)) < 0 )
            printf("error setting NN_SOL_SOCKET NN_SNDTIMEO socket %s\n",nn_errstr());
        if ( num > 0 )
            priority = nn_add_lbservers(port,globalport,relaysport,priority,lbsock,servers,num);
        if ( numbacks > 0 )
            priority = nn_add_lbservers(port,globalport,relaysport,priority,lbsock,backups,numbacks);
        if ( numfailsafes > 0 )
            priority = nn_add_lbservers(port,globalport,relaysport,priority,lbsock,failsafes,numfailsafes);
    } else printf("error getting req socket %s\n",nn_errstr());
    //printf("RELAYS.lb.num %d\n",RELAYS.lb.num);
    return(lbsock);
}
Пример #20
0
int main ()
{
    int rc;
    int sb;
    int i;
    int j;
    struct nn_thread threads [THREAD_COUNT];

    /*  Stress the shutdown algorithm. */

    sb = nn_socket (AF_SP, NN_PUB);
    errno_assert (sb >= 0);
    rc = nn_bind (sb, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    for (j = 0; j != 10; ++j) {
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_init (&threads [i], routine, NULL);
        for (i = 0; i != THREAD_COUNT; ++i)
            nn_thread_term (&threads [i]);
    }

    rc = nn_close (sb);
    errno_assert (rc == 0);

    return 0;
}
Пример #21
0
static void client(void *arg)
{
    intptr_t id = (intptr_t)arg;
    int bytes;
    char msg[3];
    size_t sz_msg;
    int i;

    msg[0] = 'A' + id%26;
    msg[1] = 'a';
    msg[2] = '\0';
    /*  '\0' too */
    sz_msg = strlen (msg) + 1;

    for (i = 0; i < TEST_LOOPS; i++) {
        int cli_sock = nn_socket (AF_SP, NN_PUSH);
        msg[1] = 'a' + i%26;
        nn_assert (cli_sock >= 0);
        nn_assert (nn_connect (cli_sock, SOCKET_ADDRESS) >= 0);
        /*  Give time to allow for connect to establish. */
        nn_sleep (50);
        bytes = nn_send (cli_sock, msg, sz_msg, 0);
        /*  This would better be handled via semaphore or condvar. */
        nn_sleep (100);
        nn_assert (bytes == sz_msg);
        nn_close (cli_sock);
    }
}
Пример #22
0
JNIEXPORT jint JNICALL Java_org_nanomsg_NanoLibrary_nn_1socket(JNIEnv* env,
                                                               jobject obj,
                                                               jint domain,
                                                               jint protocol)
{
    return nn_socket(domain, protocol);
}
Пример #23
0
int
connect_to_message_channel (
    REMOTE_MODULE_HANDLE remote_module,
    const MESSAGE_URI * channel_uri
) {
    int result;

    /* SRS_PROXY_GATEWAY_027_0xx: [`connect_to_message_channel` shall create a socket for the Azure IoT Gateway message channel by calling `int nn_socket(int domain, int protocol)` with `AF_SP` as `domain` and `MESSAGE_URI::uri_type` as `protocol`] */
    if (-1 == (remote_module->message_socket = nn_socket(AF_SP, channel_uri->uri_type))) {
        /* SRS_PROXY_GATEWAY_027_0xx: [If a call to `nn_socket` returns -1, then `connect_to_message_channel` shall free any previously allocated memory, abandon the control message and prepare for the next create message] */
        LogError("%s: Unable to create the gateway socket!", __FUNCTION__);
        result = __LINE__;
    /* SRS_PROXY_GATEWAY_027_0xx: [`connect_to_message_channel` shall bind to the Azure IoT Gateway message channel by calling `int nn_bind(int s, const char * addr)` with the newly created socket as `s` and `MESSAGE_URI::uri` as `addr`] */
    } else if (0 > (remote_module->message_endpoint = nn_bind(remote_module->message_socket, channel_uri->uri))) {
        /* SRS_PROXY_GATEWAY_027_0xx: [If a call to `nn_connect` returns a negative value, then `connect_to_message_channel` shall free any previously allocated memory, abandon the control message and prepare for the next create message] */
        LogError("%s: Unable to connect to the gateway message channel!", __FUNCTION__);
        result = __LINE__;
        (void)nn_really_close(remote_module->message_socket);
        remote_module->message_socket = -1;
    } else {
        /* SRS_PROXY_GATEWAY_027_0xx: [If no errors are encountered, then `connect_to_message_channel` shall return zero] */
        result = 0;
    }

    return result;
}
Пример #24
0
static void *server_task(void *args)
{
    thr_info *info = (thr_info *) args;
    const char *bind_to = info->bind_to;
    size_t msg_size = info->msg_size;
    int msg_count = info->msg_count;
    int perf_type = info->perf_type;

    int srv_fd = nn_socket(AF_SP, NN_PAIR);
    assert(srv_fd != -1);
    int rc = nn_bind(srv_fd, bind_to);
    assert(rc >= 0);

    int req_count = 0;
    size_t nbytes;
    while (true) {
        char *buf = (char *) malloc(msg_size + 1);
        nbytes = nn_recv(srv_fd, buf, msg_size, 0);
        assert(nbytes == (int)msg_size);
        if (perf_type == LATENCY) {
            nbytes = nn_send(srv_fd, buf, msg_size, 0);
            assert(nbytes == (int)msg_size);
        }
        free(buf);

        req_count++;
        if (req_count == msg_count) {
            nbytes = nn_send(srv_fd, "well", 5, 0);
        }
    }

    return NULL;
}
Пример #25
0
static void proxy_init_feeders (proxy_t *p, char **feeds)
{
    int opt;

    if (0 > (p->nn_feed = nn_socket(AF_SP, NN_PULL))) {
        LOG("feeder.socket() failed");
        exit(2);
    }

    opt = 32768;
    nn_setsockopt(p->nn_feed, NN_SOL_SOCKET, NN_RCVBUF, &opt, sizeof(opt));
    opt = 1000;
    nn_setsockopt(p->nn_feed, NN_SOL_SOCKET, NN_RECONNECT_IVL, &opt, sizeof(opt));
    opt = 1000;
    nn_setsockopt(p->nn_feed, NN_SOL_SOCKET, NN_RECONNECT_IVL_MAX, &opt, sizeof(opt));

    for (char **purl = feeds; *purl ;++purl) {
        if (0 > nn_connect (p->nn_feed, *purl)) {
            LOG("feeder.connect(%s) failed", *purl);
            exit(2);
        } else {
            LOG("feeder.connect(%s)", *purl);
        }
    }
}
Пример #26
0
bool pnet_internal_create_socket(pnet_socket *socket, pint16 type)
{
    if (unlikely(!socket)) {
        plog_error("pnet_internal_create_socket() нет объекта socket");
        return false;
    }

    int sock = nn_socket(AF_SP_RAW, type);
    if (unlikely(sock < 0)) {
        plog_error("pnet_internal_create_socket() не удалось создать сокет | %s (%d)", nn_strerror(nn_errno()), nn_errno());
        return false;
    }

    socket->socket_fd = sock;

    int val = 1;
    if (unlikely(nn_setsockopt(socket->socket_fd, NN_TCP, NN_TCP_NODELAY, &val, sizeof(val)))) {
        plog_error("pnet_internal_create_socket() не удалось выставить NN_TCP_NODELAY | %s (%d)", nn_strerror(nn_errno()), nn_errno());
        return false;
    }

    val = 0;
    if (unlikely(nn_setsockopt(socket->socket_fd, NN_SOL_SOCKET, NN_LINGER, &val, sizeof(val)))) {
        plog_error("pnet_internal_create_socket() не удалось выставить NN_LINGER | %s (%d)", nn_strerror(nn_errno()), nn_errno());
        return false;
    }

    val = 10000;
    if (unlikely(nn_setsockopt(socket->socket_fd, NN_SOL_SOCKET, NN_RECONNECT_IVL_MAX, &val, sizeof(val)))) {
        plog_error("pnet_internal_create_socket() не удалось выставить NN_RECONNECT_IVL_MAX | %s (%d)", nn_strerror(nn_errno()), nn_errno());
        return false;
    }

    return true;
}
Пример #27
0
flux_cli_t * flux_cli_init(const char * broker_url, int timeout, int verbose) {
    flux_cli_t * client;
    if(verbose) printf("Client starting on %s...\n", broker_url);

    client = malloc(sizeof(flux_cli_t));
    if(!client) return NULL;
    memset(client, 0, sizeof(flux_cli_t));

    client->verbose = verbose;
    client->timeout = timeout;
    client->n_servers = 0;
    client->servers = NULL;

    // Bind to broker SURVEYOR socket
    client->broker_sock =  nn_socket(AF_SP, NN_REQ);
    assert(client->broker_sock >= 0);
    assert(nn_setsockopt(client->broker_sock, NN_SOL_SOCKET, NN_RCVTIMEO, &client->timeout, sizeof(int)) >= 0);
    assert(nn_setsockopt(client->broker_sock, NN_SOL_SOCKET, NN_SNDTIMEO, &client->timeout, sizeof(int)) >= 0);
    if(nn_connect(client->broker_sock, broker_url) < 0) {
        printf("Unable to init flux client: %s\n", nn_strerror(errno));
        return NULL;
    }

    return client;
}
Пример #28
0
int node (const int argc, const char **argv)
{
    int sock = nn_socket (AF_SP, NN_BUS);
    assert (sock >= 0);
    assert (nn_bind (sock, argv[2]) >= 0);
    sleep (1); // wait for connections
    if (argc >= 3)
    {
        int x=3;
        for(x; x<argc; x++)
            assert (nn_connect (sock, argv[x]) >= 0);
    }
    sleep (1); // wait for connections
    int to = 100;
    assert (nn_setsockopt (sock, NN_SOL_SOCKET, NN_RCVTIMEO, &to, sizeof (to)) >= 0);
    // SEND
    int sz_n = strlen(argv[1]) + 1; // '\0' too
    printf ("%s: SENDING '%s' ONTO BUS\n", argv[1], argv[1]);
    int send = nn_send (sock, argv[1], sz_n, 0);
    assert (send == sz_n);
    while (1)
    {
        // RECV
        char *buf = NULL;
        int recv = nn_recv (sock, &buf, NN_MSG, 0);
        if (recv >= 0)
        {
            printf ("%s: RECEIVED '%s' FROM BUS\n", argv[1], buf);
            nn_freemsg (buf);
        }
    }
    return nn_shutdown (sock, 0);
}
Пример #29
0
int32_t nn_createsocket(char *endpoint,int32_t bindflag,char *name,int32_t type,uint16_t port,int32_t sendtimeout,int32_t recvtimeout)
{
    int32_t sock;
    if ( (sock= nn_socket(AF_SP,type)) < 0 )
        fprintf(stderr,"error getting socket %s\n",nn_errstr());
    if ( bindflag != 0 )
    {
        if ( endpoint[0] == 0 )
            expand_epbits(endpoint,calc_epbits(SUPERNET.transport,0,port,type));
        if ( nn_bind(sock,endpoint) < 0 )
            fprintf(stderr,"error binding to relaypoint sock.%d type.%d to (%s) (%s) %s\n",sock,type,name,endpoint,nn_errstr());
        else fprintf(stderr,"BIND.(%s) <- %s\n",endpoint,name);
    }
    else if ( bindflag == 0 && endpoint[0] != 0 )
    {
        if ( nn_connect(sock,endpoint) < 0 )
            fprintf(stderr,"error connecting to relaypoint sock.%d type.%d to (%s) (%s) %s\n",sock,type,name,endpoint,nn_errstr());
        else fprintf(stderr,"%s -> CONNECT.(%s)\n",name,endpoint);
    }
    if ( nn_settimeouts(sock,sendtimeout,recvtimeout) < 0 )
    {
        fprintf(stderr,"nn_createsocket.(%s) %d\n",name,sock);
        return(-1);
    }
    return(sock);
}
Пример #30
0
void worker (NN_UNUSED void *arg)
{
    int rc;
    int s;
    int i;
    char *buf;

    s = nn_socket (AF_SP, NN_PAIR);
    assert (s != -1);
    rc = nn_connect (s, "inproc://inproc_lat");
    assert (rc >= 0);

    buf = malloc (message_size);
    assert (buf);

    for (i = 0; i != roundtrip_count; i++) {
        rc = nn_recv (s, buf, message_size, 0);
        assert (rc == (int)message_size);
        rc = nn_send (s, buf, message_size, 0);
        assert (rc == (int)message_size);
    }

    free (buf);
    rc = nn_close (s);
    assert (rc == 0);
}