Пример #1
0
int node0(void)
{
        int sock = nn_socket(AF_SP, NN_BUS);

        assert(sock >= 0);
        assert(nn_bind(sock, NODE0_SOCKET_ADDR) >= 0);
        sleep(1); /* wait for connections */

        assert(nn_connect(sock, NODE1_SOCKET_ADDR) >= 0);
        assert(nn_connect(sock, NODE2_SOCKET_ADDR) >= 0);
        sleep(1); /* wait for connections */

        return sock;
}
Пример #2
0
int node0 (const char *url)
{
  int sock = nn_socket (AF_SP, NN_PULL);
  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);
      printf ("NODE0: RECEIVED \"%s\"\n", buf);
      nn_freemsg (buf);
    }
}
Пример #3
0
static PyObject *sockBind(sock_t *self, PyObject *address)
{
    const char *addr;

    addr = PyString_AS_STRING(address);
    if (addr == NULL)
        return NULL;

    self->endpoint = nn_bind(self->sock, addr);
    if (self->endpoint == -1){
        ERR;
        return NULL;
    }
    Py_RETURN_NONE;
}
Пример #4
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;
}
Пример #5
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;
}
Пример #6
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);
}
Пример #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
int server (const char *url)
{
    int sock = nn_socket (AF_SP, NN_PUB);
    assert (sock >= 0);
    assert (nn_bind (sock, url) >= 0);
    while (1)
    {
        char *d = date();
        int sz_d = strlen(d) + 1; // '\0' too
        printf ("SERVER: PUBLISHING DATE %s\n", d);
        int bytes = nn_send (sock, d, sz_d, 0);
        assert (bytes == sz_d);
        sleep(1);
    }
    return nn_shutdown (sock, 0);
}
Пример #9
0
int main ()
{
    int rc;
    int sink;
    int source1;
    int source2;
    char buf [3];

    sink = nn_socket (AF_SP, NN_SINK);
    errno_assert (sink != -1);
    rc = nn_bind (sink, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    source1 = nn_socket (AF_SP, NN_SOURCE);
    errno_assert (source1 != -1);
    rc = nn_connect (source1, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    source2 = nn_socket (AF_SP, NN_SOURCE);
    errno_assert (source2 != -1);
    rc = nn_connect (source2, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    rc = nn_send (source1, "ABC", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_send (source2, "DEF", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

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

    rc = nn_close (sink);
    errno_assert (rc == 0);
    rc = nn_close (source1);
    errno_assert (rc == 0);    
    rc = nn_close (source2);
    errno_assert (rc == 0);

    return 0;
}
Пример #10
0
int main(int argc, char **argv)
{
	
	const char *url = argv[1]; 
	/* create socket */
	int sock = nn_socket(AF_SP, NN_PUB);  if (sock < 0) exit(-1);
	/* bind socket */
	if (nn_bind(sock, url) < 0) exit(-1); 

	while (1)
	{
		char *text = "LOL";  

		printf("PUB: %s \n", text); 
		sleep(1); 
	}

	return 0; 
}
Пример #11
0
static void apply_socket_config(int sock, struct cfg_a_nanomsg_addr *addr) {
    int rc;

    for(; addr; addr = addr->next) {
        if(addr->val.tag == CFG_NANOMSG_ADDR_BIND) {
            rc = nn_bind(sock, addr->val.val);
            if(rc < 0) {
                fprintf(stderr, "Error in nn_bind: %s", nn_strerror(errno));
                exit(2);
            }
        } else if(addr->val.tag == CFG_NANOMSG_ADDR_CONNECT) {
            rc = nn_connect(sock, addr->val.val);
            if(rc < 0) {
                fprintf(stderr, "Error in nn_connect: %s", nn_strerror(errno));
                exit(2);
            }
        }
    }
}
Пример #12
0
int main (int argc, char *argv [])
{
    const char *bind_to;
    size_t sz;
    int rts;
    char *buf;
    int nbytes;
    int s;
    int rc;
    int i;

    if (argc != 4) {
        printf ("usage: local_lat <bind-to> <msg-size> <roundtrips>\n");
        return 1;
    }
    bind_to = argv [1];
    sz = atoi (argv [2]);
    rts = atoi (argv [3]);

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

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

    for (i = 0; i != rts; i++) {
        nbytes = nn_recv (s, buf, sz, 0);
        assert (nbytes == sz);
        nbytes = nn_send (s, buf, sz, 0);
        assert (nbytes == sz);
    }

    free (buf);

    rc = nn_close (s);
    assert (rc == 0);

    return 0;
}
Пример #13
0
int main ()
{
  int rc;
  int sb;
  int sc;
  int i;
  char buf [4];
  int opt;
  size_t sz;

  // server

  sb = nn_socket (AF_SP, NN_PAIR);
  errno_assert (sb >= 0);

  // bind
  rc = nn_bind (sb, SOCKET_ADDRESS);
  errno_assert (rc > 0);

  // receive
  bzero(buf, 4);
  rc = nn_recv(sb, buf, sizeof(buf), 0);
  errno_assert(rc >= 0);
  nn_assert(rc == 3);

  printf("server: I received: '%s'\n", buf);

  // send
  memcpy(buf, "LUV\0", 4); // trailing null for later printf
  rc = nn_send (sb, buf, 3, 0);
  printf("server: I sent: '%s'\n", buf);

  errno_assert (rc >= 0);
  nn_assert (rc == 3);
   
  // close
  rc = nn_close (sb);
  errno_assert (rc == 0);

  return 0;
}
Пример #14
0
void act_server(size_t sze) {
	int sock = nn_socket(AF_SP, NN_REP);
	std::string host = "tcp://*:12345";
	if (nn_bind(sock, host.c_str()) < 0) {
            printf("Error: %s\n", nn_strerror(errno));
            return;
	}

	nn_recv(sock, NULL, 0, 0);
	nn_send(sock, NULL, 0, 0);

	std::vector<size_t> lens;
	make_package(sze, lens);

	MDL::Timer timer;
	timer.start();
	MDL::net::receive_all(sock, lens);
	timer.end();

	printf("receive %f\n", timer.second());
	nn_close(sock);
}
Пример #15
0
static int32_t init_pluginsocks(struct plugin_info *plugin,int32_t permanentflag,uint64_t instanceid,uint64_t daemonid,int32_t timeout)
{
//#ifdef _WIN32
//    sprintf(plugin->connectaddr,"tcp://127.0.0.1:7774");
//#endif
    if ( (plugin->pushsock= nn_socket(AF_SP,NN_PUSH)) < 0 )
    {
        printf("error creating plugin->pushsock %s\n",nn_strerror(nn_errno()));
        return(-1);
    }
    else if ( nn_settimeouts2(plugin->pushsock,10,1) < 0 )
    {
        printf("error setting plugin->pushsock timeouts %s\n",nn_strerror(nn_errno()));
        return(-1);
    }
    else if ( nn_connect(plugin->pushsock,plugin->connectaddr) < 0 )
    {
        printf("error connecting plugin->pushsock.%d to %s %s\n",plugin->pushsock,plugin->connectaddr,nn_strerror(nn_errno()));
        return(-1);
    }
    if ( (plugin->pullsock= nn_socket(AF_SP,NN_BUS)) < 0 )
    {
        printf("error creating plugin->pullsock %s\n",nn_strerror(nn_errno()));
        return(-1);
    }
    else if ( nn_settimeouts2(plugin->pullsock,10,1) < 0 )
    {
        printf("error setting plugin->pullsock timeouts %s\n",nn_strerror(nn_errno()));
        return(-1);
    }
    else if ( nn_bind(plugin->pullsock,plugin->bindaddr) < 0 )
    {
        printf("error connecting plugin->pullsock.%d to %s %s\n",plugin->pullsock,plugin->bindaddr,nn_strerror(nn_errno()));
        return(-1);
    }
    printf("%s bind.(%s) %d and connected.(%s) %d\n",plugin->name,plugin->bindaddr,plugin->pullsock,plugin->connectaddr,plugin->pushsock);
    return(0);
}
Пример #16
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);
  char verify[100];
  strcpy(verify, "");
  while (1)
    {
      // RECV
      char *buf = NULL;
      int recv = nn_recv (sock, &buf, NN_MSG, 0);
      if (recv >= 0 && strcmp(buf, verify))
      {
          strcpy(verify, buf);
          printf ("%s: RECEIVED '%s' FROM BUS\n", argv[1], buf);

          // SEND
          int sz_n = strlen(buf) + 1; // '\0' too
          int send = nn_send (sock, buf, sz_n, 0);
          assert (send == sz_n);
          nn_freemsg (buf);
      }
    }
  printf ("out \n");
  return nn_shutdown (sock, 0);
}
Пример #17
0
int node0 (const char *url)
{
  int sz_date = strlen (DATE) + 1; // '\0' too
  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 *d = date();
          int sz_d = strlen(d) + 1; // '\0' too
          printf ("NODE0: SENDING DATE %s\n", d);
          bytes = nn_send (sock, d, sz_d, 0);
          assert (bytes == sz_d);
        }
      nn_freemsg (buf);
    }
  return nn_shutdown (sock, 0);
}
Пример #18
0
int server(const char *url)
{
	int sock = nn_socket(AF_SP, NN_PUB);
	nn_bind(sock, url);

	char msg[] = "Mhello.world";
	int sz_d = strlen(&msg) + 1; // '\0' too

	while (1)
	{
		//char *d = date();
		//int sz_d = strlen(d) + 1; // '\0' too
		//printf("SERVER: PUBLISHING DATE %s\n", d);
		printf("PUBLISH: '%s'\n", msg);
		int bytes = nn_send(sock, &msg, sz_d, 0);
		assert(bytes == sz_d);

		// Receive and wait
		char *buf = NULL;
		nn_recv(sock, &buf, NN_MSG, NN_DONTWAIT);
		Sleep(1000);
	}
	return nn_shutdown(sock, 0);
}
Пример #19
0
void replyThread ()
{
	LOG_INFO (gLog, "reply thread init...");
	int rep = nn_socket (AF_SP, NN_REP);
	if (rep < 0)
		LOG_ERROR (gLog, "replyThread nn_socket fail!! err=%s", strerror (errno));
	int rc = nn_bind (rep, NANOMSG_REP_URL);
	LOG_INFO (gLog, "REP URL=%s", NANOMSG_REP_URL);
	if (rc < 0)
		LOG_ERROR (gLog, "replyThread nn_bind fail!! err=%s", strerror (errno));

	while(1)
	{
		char buf[128] = {0x00};
		int bufSize = sizeof (buf);

		int rc = nn_recv (rep, buf, bufSize, 0);
		if (rc < 0)
			LOG_ERROR (gLog, "reply nn_recv fail!! err=%s", strerror (errno));

		LOG_DEBUG (gLog, "recv request : %s", buf);
		//1. parse req
		char mid[20] = {0x00};
		char pid[20] = {0x00};
		char startSeq[20] = {0x00};
		char endSeq[20] = {0x00};
		char type[20] = {0x00};
		rc = splitRequest (buf, mid, pid, type, startSeq, endSeq);
		if (rc == -1)
		{
			nn_send (rep, "parameter error", 15, 0);
			continue;
		}
		LOG_INFO (gLog, "mid : %s, pid : %s, type : %s, start : %s, end : %s", mid, pid, type, startSeq, endSeq);

		if (strcmp (type, "MT") == 0)
		{
			rescueTick (rep, mid, pid, startSeq, endSeq);
			char retmsg[32] = {0x00};
			snprintf (retmsg, sizeof (retmsg), "[success]");
			rc = nn_send (rep, retmsg, strlen (retmsg), 0); 
			if (rc < 0)
			{
				LOG_ERROR (gLog, "replyThread[rescue] nn_send fail!! %s", strerror (errno));
				return;
			}
	
			continue;
		}
		if (strcmp (type, "MQ") == 0)
		{
			rescueQuote (rep, mid, pid, startSeq, endSeq);
			char retmsg[32] = {0x00};
			snprintf (retmsg, sizeof (retmsg), "[success]");
			rc = nn_send (rep, retmsg, strlen (retmsg), 0); 
			if (rc < 0)
			{
				LOG_ERROR (gLog, "replyThread[rescue] nn_send fail!! %s", strerror (errno));
				return;
			}
	
			continue;
		}

		//2. query db
		sqlite3 *db;
		char **result;
		int rows, cols, i;
		char msg[20480] = {0x00};
		char startCondition[30] = {0x00};
		char endCondition[30] = {0x00};
		char limitCondition[30] = {0x00};

		if (strlen (endSeq) > 0)
			snprintf (endCondition, 30, "and seq <= %s", endSeq);
		if ((strlen (mid) == 0) && (strlen (pid) == 0) && (strlen (startSeq) > 0))
			snprintf (limitCondition, 30, "%s,", startSeq);
		else if (strlen (startSeq) > 0)
			snprintf (startCondition, 30, "and seq >= %s", startSeq);

		char sqlStr[512] = {0x00};
		if (strlen (pid) == 0)
			sprintf (pid, "%%");
		if (strcmp (type, "Q") == 0)
		{
			if (strlen (startSeq) == 0)
				startSeq[0] = '0';
			snprintf (sqlStr, 512, "select * from quote where mid like '%%%s%%' and pid like '%s' "
					"order by mid, pid limit %s, 100;", mid, pid, startSeq);
		}
		else
		{
			//snprintf (sqlStr, 512, "select * from %c_msg where mid like '%%%s%%' and pid like '%s' "
			//		"and type like '%%%s%%' %s %s order by seq limit %s100;", 
			//		mid[0], mid, pid, type, startCondition, endCondition, limitCondition);
			snprintf (sqlStr, 512, "select * from msg where mid like '%%%s%%' and pid like '%s' "
					"and type like '%%%s%%' %s %s order by seq limit %s100;", 
					mid, pid, type, startCondition, endCondition, limitCondition);
		}
		LOG_INFO (gLog, "request sql : %s", sqlStr);

		pthread_mutex_lock (&mutex);
		rc = sqlite3_open_v2 ("rts.db", &db, SQLITE_OPEN_READONLY, NULL);
		if (rc != SQLITE_OK)
		{
			LOG_ERROR (gLog, "sqlite3_open_v2 fail!! msg=%s, sqlite msg=%s", 
					strerror (errno), sqlite3_errstr (rc));
			sqlite3_close (db);
			pthread_mutex_unlock (&mutex);
			continue;
		}
		rc = sqlite3_get_table (db, sqlStr, &result, &rows, &cols, 0);
		if (rc != SQLITE_OK)
		{
			LOG_ERROR (gLog, "replyThread rqlite3_get_table fail!! msg=%s, sqlite msg=%s, sql=%s", 
					strerror (errno), sqlite3_errstr (rc), sqlStr);
			sqlite3_close (db);
			rc = nn_send (rep, "parameter error", 15, 0); 
			pthread_mutex_unlock (&mutex);
			continue;
		}
		if (strcmp (type, "Q") == 0)
		{
			int j = 0;
			for (i = 1; i < rows+1; i++)
			{
				sprintf (&msg[strlen (msg)], "%s%c", "8=FIX.4.2", 0x01);
				sprintf (&msg[strlen (msg)], "%s%c", "35=Q", 0x01);
				for (j = 0; j < cols; j++)
				{
					switch (j)
					{
						case 0:
							sprintf (&msg[strlen (msg)], "%s", "3="); break;
						case 1:
							sprintf (&msg[strlen (msg)], "%s", "5="); break;
						case 2:
							sprintf (&msg[strlen (msg)], "%s", "407="); break;
						case 3:
							sprintf (&msg[strlen (msg)], "%s", "15007="); break;
						case 4:
							sprintf (&msg[strlen (msg)], "%s", "400="); break;
						case 5:
							sprintf (&msg[strlen (msg)], "%s", "388="); break;
						case 6:
							sprintf (&msg[strlen (msg)], "%s", "394="); break;
						case 7:
							sprintf (&msg[strlen (msg)], "%s", "385="); break;
						case 8:
							sprintf (&msg[strlen (msg)], "%s", "22="); break;
						case 9:
							sprintf (&msg[strlen (msg)], "%s", "15008="); break;
						case 10:
							sprintf (&msg[strlen (msg)], "%s", "15006="); break;
						default: break;
					}
					if (result[i*cols+j])
						sprintf (&msg[strlen (msg)], "%s", result[i*cols+j]);

					sprintf (&msg[strlen (msg)], "%c", 0x01);
				}
				sprintf (&msg[strlen (msg)], "%s%c", "10=000", 0x01);
				//LOG_TRACE (gLog, "%s", result[i*11+4]);
			}
		}
		else
		{
			for (i = 1; i < rows+1; i++)
			{
				//msg column in 4 position
				LOG_TRACE (gLog, "%s", result[i*5+4]);
				sprintf (&msg[strlen (msg)], "%s", result[i*5+4]);
			}
		}
		sprintf (&msg[strlen (msg)], "[end]");
		LOG_TRACE (gLog, "msg = %s", msg);
		sqlite3_free_table (result);
		sqlite3_close (db);
		pthread_mutex_unlock (&mutex);

		rc = nn_send (rep, msg, strlen (msg), 0); 
		if (rc < 0)
		{
			LOG_ERROR (gLog, "replyThread nn_send fail!! %s", strerror (errno));
			continue;
		}
	}
}
Пример #20
0
int main ()
{
    int rc;
    int sb;
    int sc;
    int i;
    char buf [256];
    int val;

    /*  Create a simple topology. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);

    /*  Try a duplicate bind. It should fail. */
    rc = nn_bind (sc, SOCKET_ADDRESS);
    nn_assert (rc < 0 && errno == EADDRINUSE);

    /*  Ping-pong test. */
    for (i = 0; i != 100; ++i) {

        test_send (sc, "ABC");
        test_recv (sb, "ABC");
        test_send (sb, "DEFG");
        test_recv (sc, "DEFG");
    }

    /*  Batch transfer test. */
    for (i = 0; i != 100; ++i) {
        test_send (sc, "XYZ");
    }
    for (i = 0; i != 100; ++i) {
        test_recv (sb, "XYZ");
    }

    test_close (sc);
    test_close (sb);

    /*  Test whether queue limits are observed. */
    sb = test_socket (AF_SP, NN_PAIR);
    val = 200;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVBUF, &val, sizeof (val));
    errno_assert (rc == 0);
    test_bind (sb, SOCKET_ADDRESS);
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);

    val = 200;
    rc = nn_setsockopt (sc, NN_SOL_SOCKET, NN_SNDTIMEO, &val, sizeof (val));
    errno_assert (rc == 0);
    i = 0;
    while (1) {
        rc = nn_send (sc, "0123456789", 10, 0);
        if (rc < 0 && nn_errno () == ETIMEDOUT)
            break;
        errno_assert (rc >= 0);
        nn_assert (rc == 10);
        ++i;
    }
    nn_assert (i == 20);
    test_recv (sb, "0123456789");
    test_send (sc, "0123456789");
    rc = nn_send (sc, "0123456789", 10, 0);
    nn_assert (rc < 0 && nn_errno () == ETIMEDOUT);
    for (i = 0; i != 20; ++i) {
        test_recv (sb, "0123456789");
    }

    /*  Make sure that even a message that doesn't fit into the buffers
        gets across. */
    for (i = 0; i != sizeof (buf); ++i)
        buf [i] = 'A';
    rc = nn_send (sc, buf, 256, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 256);
    rc = nn_recv (sb, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 256);

    test_close (sc);
    test_close (sb);

#if 0
    /*  Test whether connection rejection is handled decently. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    s1 = test_socket (AF_SP, NN_PAIR);
    test_connect (s1, SOCKET_ADDRESS);
    s2 = test_socket (AF_SP, NN_PAIR);
    test_connect (s2, SOCKET_ADDRESS);
    nn_sleep (100);
    test_close (s2);
    test_close (s1);
    test_close (sb);
#endif

    return 0;
}
Пример #21
0
void test_reallocmsg_pubsub ()
{
    int rc;
    int pub;
    int sub1;
    int sub2;
    void *p;
    void *p1;
    void *p2;

    /*  Create sockets. */
    pub = nn_socket (AF_SP, NN_PUB);
    sub1 = nn_socket (AF_SP, NN_SUB);
    sub2 = nn_socket (AF_SP, NN_SUB);
    rc = nn_bind (pub, "inproc://test");
    errno_assert (rc >= 0);
    rc = nn_connect (sub1, "inproc://test");
    errno_assert (rc >= 0);
    rc = nn_connect (sub2, "inproc://test");
    errno_assert (rc >= 0);
    rc = nn_setsockopt (sub1, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);
    rc = nn_setsockopt (sub2, NN_SUB, NN_SUB_SUBSCRIBE, "", 0);
    errno_assert (rc == 0);

    /*  Publish message. */
    p = nn_allocmsg (12, 0);
    nn_assert (p);
    memcpy (p, "Hello World!", 12);
    rc = nn_send (pub, &p, NN_MSG, 0);
    errno_assert (rc == 12);

    /*  Receive messages, both messages are the same object with inproc. */
    rc = nn_recv (sub1, &p1, NN_MSG, 0);
    errno_assert (rc == 12);
    rc = nn_recv (sub2, &p2, NN_MSG, 0);
    errno_assert (rc == 12);
    nn_assert (p1 == p2);
    rc = memcmp (p1, "Hello World!", 12);
    nn_assert (rc == 0);
    rc = memcmp (p2, "Hello World!", 12);
    nn_assert (rc == 0);

    /*  Reallocate one message, both messages shouldn't be the same object
        anymore. */
    p1 = nn_reallocmsg (p1, 15);
    errno_assert (p1);
    nn_assert (p1 != p2);
    memcpy ((char*)p1 + 12, " 42", 3);
    rc = memcmp (p1, "Hello World! 42", 15);
    nn_assert (rc == 0);

    /*  Release messages. */
    rc = nn_freemsg (p1);
    errno_assert (rc == 0);
    rc = nn_freemsg (p2);
    errno_assert (rc == 0);

    /*  Clean up. */
    nn_close (sub2);
    nn_close (sub1);
    nn_close (pub);
}
Пример #22
0
int main ()
{
    int rc;
    int bus1;
    int bus2;
    int bus3;
    char buf [3];

    /*  Create a simple bus topology consisting of 3 nodes. */
    bus1 = nn_socket (AF_SP, NN_BUS);
    errno_assert (bus1 != -1);
    rc = nn_bind (bus1, SOCKET_ADDRESS_A);
    errno_assert (rc >= 0);
    bus2 = nn_socket (AF_SP, NN_BUS);
    errno_assert (bus2 != -1);
    rc = nn_bind (bus2, SOCKET_ADDRESS_B);
    errno_assert (rc >= 0);
    rc = nn_connect (bus2, SOCKET_ADDRESS_A);
    errno_assert (rc >= 0);
    bus3 = nn_socket (AF_SP, NN_BUS);
    errno_assert (bus3 != -1);
    rc = nn_connect (bus3, SOCKET_ADDRESS_A);
    errno_assert (rc >= 0);
    rc = nn_connect (bus3, SOCKET_ADDRESS_B);
    errno_assert (rc >= 0);

    /*  Send a message from each node. */
    rc = nn_send (bus1, "A", 1, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 1);
    rc = nn_send (bus2, "AB", 2, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 2);
    rc = nn_send (bus3, "ABC", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    /*  Check that two messages arrived at each node. */
    rc = nn_recv (bus1, buf, 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 2 || rc == 3);
    rc = nn_recv (bus1, buf, 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 2 || rc == 3);
    rc = nn_recv (bus2, buf, 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 1 || rc == 3);
    rc = nn_recv (bus2, buf, 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 1 || rc == 3);
    rc = nn_recv (bus3, buf, 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 1 || rc == 2);
    rc = nn_recv (bus3, buf, 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 1 || rc == 2);

    /*  Wait till both connections are established. */
    nn_sleep (10);

    rc = nn_close (bus3);
    errno_assert (rc == 0);
    rc = nn_close (bus2);
    errno_assert (rc == 0);    
    rc = nn_close (bus1);
    errno_assert (rc == 0);

    return 0;
}
Пример #23
0
BROKER_HANDLE Broker_Create(void)
{
    BROKER_HANDLE_DATA* result;

    /*Codes_SRS_BROKER_13_067: [Broker_Create shall malloc a new instance of BROKER_HANDLE_DATA and return NULL if it fails.]*/
    result = REFCOUNT_TYPE_CREATE(BROKER_HANDLE_DATA);
    if (result == NULL)
    {
        LogError("malloc returned NULL");
        /*return as is*/
    }
    else
    {
        /*Codes_SRS_BROKER_13_007: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules with a valid VECTOR_HANDLE.]*/
        result->modules = singlylinkedlist_create();
        if (result->modules == NULL)
        {
            /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/
            LogError("VECTOR_create failed");
            free(result);
            result = NULL;
        }
        else
        {
            /*Codes_SRS_BROKER_13_023: [Broker_Create shall initialize BROKER_HANDLE_DATA::modules_lock with a valid LOCK_HANDLE.]*/
            result->modules_lock = Lock_Init();
            if (result->modules_lock == NULL)
            {
                /*Codes_SRS_BROKER_13_003: [This function shall return NULL if an underlying API call to the platform causes an error.]*/
                LogError("Lock_Init failed");
                singlylinkedlist_destroy(result->modules);
                free(result);
                result = NULL;
            }
            else
            {
                /*Codes_SRS_BROKER_17_001: [ Broker_Create shall initialize a socket for publishing messages. ]*/
                result->publish_socket = nn_socket(AF_SP, NN_PUB);
                if (result->publish_socket < 0)
                {
                    /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                    LogError("nanomsg puclish socket create failedL %d", result->publish_socket);
                    singlylinkedlist_destroy(result->modules);
                    Lock_Deinit(result->modules_lock);
                    free(result);
                    result = NULL;
                }
                else
                {
                    result->url = construct_url();
                    if (result->url == NULL)
                    {
                        /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                        singlylinkedlist_destroy(result->modules);
                        Lock_Deinit(result->modules_lock);
                        nn_close(result->publish_socket);
                        free(result);
                        LogError("Unable to generate unique url.");
                        result = NULL;
                    }
                    else
                    {
                        /*Codes_SRS_BROKER_17_004: [ Broker_Create shall bind the socket to the BROKER_HANDLE_DATA::url. ]*/
                        if (nn_bind(result->publish_socket, STRING_c_str(result->url)) < 0)
                        {
                            /*Codes_SRS_BROKER_13_003: [ This function shall return NULL if an underlying API call to the platform causes an error. ]*/
                            LogError("nanomsg bind failed");
                            singlylinkedlist_destroy(result->modules);
                            Lock_Deinit(result->modules_lock);
                            nn_close(result->publish_socket);
                            STRING_delete(result->url);                
                            free(result);
                            result = NULL;
                        }
                    }
                }
            }
        }
    }

    /*Codes_SRS_BROKER_13_001: [This API shall yield a BROKER_HANDLE representing the newly created message broker. This handle value shall not be equal to NULL when the API call is successful.]*/
    return result;
}
Пример #24
0
REMOTE_MODULE_HANDLE
ProxyGateway_Attach (
    const MODULE_API * module_apis,
    const char * connection_id
) {
    REMOTE_MODULE_HANDLE remote_module;
    if (NULL == module_apis) {
        /* Codes_SRS_PROXY_GATEWAY_027_000: [Prerequisite Check - If the `module_apis` parameter is `NULL`, then `ProxyGateway_Attach` shall do nothing and return `NULL`] */
        LogError("%s: NULL parameter - module_apis", __FUNCTION__);
        remote_module = NULL;
    } else if (MODULE_API_VERSION_1 < (int)module_apis->version) {
        /* Codes_SRS_PROXY_GATEWAY_027_001: [Prerequisite Check - If the `module_apis` version is beyond `MODULE_API_VERSION_1`, then `ProxyGateway_Attach` shall do nothing and return `NULL`] */
        LogError("%s: Incompatible API version: %d!", __FUNCTION__, (1 + (int)module_apis->version));
        remote_module = NULL;
    } else if (NULL == ((MODULE_API_1 *)module_apis)->Module_Create) {
        /* Codes_SRS_PROXY_GATEWAY_027_002: [Prerequisite Check - If the `module_apis` interface fails to provide `Module_Create`, then `ProxyGateway_Attach` shall do nothing and return `NULL`] */
        LogError("%s: Required interface not met - module_apis->Module_Create", __FUNCTION__);
        remote_module = NULL;
    } else if (NULL == ((MODULE_API_1 *)module_apis)->Module_Destroy) {
        /* Codes_SRS_PROXY_GATEWAY_027_003: [Prerequisite Check - If the `module_apis` interface fails to provide `Module_Destroy`, then `ProxyGateway_Attach` shall do nothing and return `NULL`] */
        LogError("%s: Required interface not met - module_apis->Module_Destroy", __FUNCTION__);
        remote_module = NULL;
    } else if (NULL == ((MODULE_API_1 *)module_apis)->Module_Receive) {
        /* Codes_SRS_PROXY_GATEWAY_027_004: [Prerequisite Check - If the `module_apis` interface fails to provide `Module_Receive`, then `ProxyGateway_Attach` shall do nothing and return `NULL`] */
        LogError("%s: Required interface not met - module_apis->Module_Receive", __FUNCTION__);
        remote_module = NULL;
    } else if (NULL == connection_id) {
        /* Codes_SRS_PROXY_GATEWAY_027_005: [Prerequisite Check - If the `connection_id` parameter is `NULL`, then `ProxyGateway_Attach` shall do nothing and return `NULL`] */
        LogError("%s: NULL parameter - connection_id", __FUNCTION__);
        remote_module = NULL;
    } else if (GATEWAY_CONNECTION_ID_MAX + 1 == strnlen_(connection_id, GATEWAY_CONNECTION_ID_MAX + 1)) {
        /* Codes_SRS_PROXY_GATEWAY_027_006: [Prerequisite Check - If the `connection_id` parameter is longer than `GATEWAY_CONNECTION_ID_MAX`, then `ProxyGateway_Attach` shall do nothing and return `NULL`] */
        LogError("%s: connection_id exceeds GATEWAY_CONNECTION_ID_MAX in length", __FUNCTION__);
        remote_module = NULL;
    /* Codes_SRS_PROXY_GATEWAY_027_007: [`ProxyGateway_Attach` shall allocate the memory required to support its instance data] */
    } else if (NULL == (remote_module = (REMOTE_MODULE_HANDLE)calloc(1, sizeof(REMOTE_MODULE)))) {
        /* Codes_SRS_PROXY_GATEWAY_027_008: [If memory allocation fails for the instance data, then `ProxyGateway_Attach` shall return `NULL`] */
        LogError("%s: Unable to allocate memory!", __FUNCTION__);
    } else {
        static const size_t ENDPOINT_DECORATION_SIZE = sizeof("ipc://") - 1;

        const size_t control_channel_uri_size = strlen(connection_id) + ENDPOINT_DECORATION_SIZE + 1;
        char * control_channel_uri;

        // Transform the connection id into a nanomsg URI
        /* Codes_SRS_PROXY_GATEWAY_027_009: [`ProxyGateway_Attach` shall allocate the memory required to formulate the connection string to the Azure IoT Gateway] */
        if (NULL == (control_channel_uri = (char *)malloc(control_channel_uri_size))) {
            /* Codes_SRS_PROXY_GATEWAY_027_010: [If memory allocation fails for the connection string, then `ProxyGateway_Attach` shall free any previously allocated memory and return `NULL`] */
            LogError("%s: Unable to allocate memory!", __FUNCTION__);
            free(remote_module);
            remote_module = NULL;
        } else if (NULL == strcpy(control_channel_uri, "ipc://")) {
            LogError("%s: Unable to compose channel uri prefix!", __FUNCTION__);
            free(remote_module);
            remote_module = NULL;
        } else if (NULL == strncat(control_channel_uri, connection_id, GATEWAY_CONNECTION_ID_MAX + 1)) {
            LogError("%s: Unable to compose channel uri body!", __FUNCTION__);
            free(remote_module);
            remote_module = NULL;
        } else {
            /* Codes_SRS_PROXY_GATEWAY_027_011: [`ProxyGateway_Attach` shall create a socket for the Azure IoT Gateway control channel by calling `int nn_socket(int domain, int protocol)` with `AF_SP` as the `domain` and `NN_PAIR` as the `protocol`] */
            if (-1 == (remote_module->control_socket = nn_socket(AF_SP, NN_PAIR))) {
                /* Codes_SRS_PROXY_GATEWAY_027_012: [If the call to `nn_socket` returns -1, then `ProxyGateway_Attach` shall free any previously allocated memory and return `NULL`] */
                LogError("%s: Unable to create the gateway socket!", __FUNCTION__);
                free(remote_module);
                remote_module = NULL;
            /* Codes_SRS_PROXY_GATEWAY_027_013: [`ProxyGateway_Attach` shall bind to the Azure IoT Gateway control channel by calling `int nn_bind(int s, const char * addr)` with the newly created socket as `s` and the newly formulated connection string as `addr`] */
            } else if (0 > (remote_module->control_endpoint = nn_bind(remote_module->control_socket, control_channel_uri))) {
                /* Codes_SRS_PROXY_GATEWAY_027_014: [If the call to `nn_bind` returns a negative value, then `ProxyGateway_Attach` shall close the socket, free any previously allocated memory and return `NULL`] */
                LogError("%s: Unable to connect to the gateway control channel!", __FUNCTION__);
				nn_really_close(remote_module->control_socket);
                free(remote_module);
                remote_module = NULL;
            } else {
                // Save the module API
                remote_module->module.module_apis = module_apis;

                // Initialize remaining fields
                remote_module->message_socket = -1;
                remote_module->message_endpoint = -1;
            }
        }
        /* Codes_SRS_PROXY_GATEWAY_027_015: [`ProxyGateway_Attach` shall release the memory required to formulate the connection string] */
        free(control_channel_uri);
    }

    /* Codes_SRS_PROXY_GATEWAY_027_016: [If no errors are encountered, then `ProxyGateway_Attach` return a handle to the OopModule instance] */
    return remote_module;
}
Пример #25
0
int sock_bind(const char *url) {
  int sock = nn_socket(AF_SP, NN_PAIR);
  assert(sock >= 0);
  assert(nn_bind(sock, url) >= 0);
  return sock;
}
Пример #26
0
int main (int argc, char *argv [])
{
    int rc;
    int s;
    int i;
    char *buf;
    struct nn_thread thread;
    struct nn_stopwatch stopwatch;
    uint64_t elapsed;
    unsigned long throughput;
    double megabits;

    if (argc != 3) {
        printf ("usage: thread_thr <message-size> <message-count>\n");
        return 1;
    }

    message_size = atoi (argv [1]);
    message_count = atoi (argv [2]);

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

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

    nn_thread_init (&thread, worker, NULL);

    /*  First message is used to start the stopwatch. */
    rc = nn_recv (s, buf, message_size, 0);
    assert (rc == 0);

    nn_stopwatch_init (&stopwatch);

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

    elapsed = nn_stopwatch_term (&stopwatch);

    nn_thread_term (&thread);
    free (buf);
    rc = nn_close (s);
    assert (rc == 0);

    if (elapsed == 0)
        elapsed = 1;
    throughput = (unsigned long)
        ((double) message_count / (double) elapsed * 1000000);
    megabits = (double) (throughput * message_size * 8) / 1000000;

    printf ("message size: %d [B]\n", (int) message_size);
    printf ("message count: %d\n", (int) message_count);
    printf ("mean throughput: %d [msg/s]\n", (int) throughput);
    printf ("mean throughput: %.3f [Mb/s]\n", (double) megabits);

    return 0;
}
Пример #27
0
int main()
{

    int back_router = nn_socket(AF_SP_RAW, NN_REP);
    nn_bind(back_router, BACKROUTER);

    chan jobs = chmake(char*, 128);
    chan workers = chmake(char*, 64);

    go(collect(workers, back_router));
    go(generate_work(jobs));
    go(route_work(workers, back_router, jobs)); 

    msleep(now() + 5435345);
    
    int front_router = nn_socket(AF_SP_RAW, NN_REP);
    nn_bind(front_router, FRONTROUTER);
    
    char ctrl [256];
    char body [256];
    char ctrl2 [256];
    char body2 [256];

    struct nn_msghdr hdr;
    struct nn_msghdr hdr2;

    struct nn_iovec iovec;
    iovec.iov_base = body;
    iovec.iov_len = sizeof(body);

    memset(&hdr, 0, sizeof (hdr));
    hdr.msg_iov = &iovec;
    hdr.msg_iovlen = 1;

    hdr.msg_control = ctrl;
    hdr.msg_controllen = sizeof (ctrl);

    int rc = nn_recvmsg(back_router, &hdr, 0);
    printf("got rc: %d, got ctrl: %d\n", rc, hdr.msg_controllen);

    struct nn_iovec iovec2;
    iovec2.iov_base = body2;
    iovec2.iov_len = sizeof(body2);

    memset(&hdr2, 0, sizeof (hdr));

    hdr2.msg_iov = &iovec2;
    hdr2.msg_iovlen = 1;
    hdr2.msg_control = ctrl2;
    hdr2.msg_controllen = sizeof (ctrl2);

    rc = nn_recvmsg(back_router, &hdr2, 0);
    /* 
    struct nn_cmsghdr *cmsg;

    cmsg = NN_CMSG_FIRSTHDR (&hdr);
    while (cmsg != NULL) {
        size_t len = cmsg->cmsg_len - sizeof (struct nn_cmsghdr);
        printf ("level: %d property: %d length: %dB data: ",
            (int) cmsg->cmsg_level,
            (int) cmsg->cmsg_type,
            (int) len);
        unsigned char *data = NN_CMSG_DATA(cmsg);
        while (len) {
            printf ("%02X", *data);
            ++data;
            --len;
        }
        printf ("\n");

        cmsg = NN_CMSG_NXTHDR (&hdr.msg_control, cmsg);
    }
*/
    hdr.msg_iov = &iovec2;
    hdr.msg_iovlen = 1;
    nn_sendmsg(back_router, &hdr, 0);

    /*
    while(1){
        sleep(10);
        buf = NULL;
        printf("waiting for worker\n");

        int n = nn_recv(response, &buf, NN_MSG, 0);

        printf("server got msg: %.*s\n", n, buf);
        sprintf(tevs, "you are number %d served", ++c);
        printf("responding to %d\n",response);
        
        nn_send(response, tevs, strlen(tevs), 0);
        nn_freemsg(buf);
    }
   
    nn_shutdown(response, 0); 
    */
    return 0;
}
Пример #28
0
int main ()
{
#ifndef NN_HAVE_WSL
    int sb;
    int sc;
    int i;
    int s1, s2;
    void * dummy_buf;
    int rc;
    int opt;
    size_t opt_sz = sizeof (opt);

    int size;
    char * buf;

    /*  Try closing a IPC socket while it not connected. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);
    test_close (sc);

    /*  Open the socket anew. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);

    /*  Leave enough time for at least one re-connect attempt. */
    nn_sleep (200);

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);

    /*  Ping-pong test. */
    for (i = 0; i != 1; ++i) {
        test_send (sc, "0123456789012345678901234567890123456789");
        test_recv (sb, "0123456789012345678901234567890123456789");
        test_send (sb, "0123456789012345678901234567890123456789");
        test_recv (sc, "0123456789012345678901234567890123456789");
    }

    /*  Batch transfer test. */
    for (i = 0; i != 100; ++i) {
        test_send (sc, "XYZ");
    }
    for (i = 0; i != 100; ++i) {
        test_recv (sb, "XYZ");
    }

    /*  Send something large enough to trigger overlapped I/O on Windows. */
    size = 10000;
    buf = malloc (size);
    for (i = 0; i < size; ++i) {
        buf[i] = 48 + i % 10;
    }
    buf[size-1] = '\0';
    test_send (sc, buf);
    test_recv (sb, buf);
    free (buf);

    test_close (sc);
    test_close (sb);

    /*  Test whether connection rejection is handled decently. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    s1 = test_socket (AF_SP, NN_PAIR);
    test_connect (s1, SOCKET_ADDRESS);
    s2 = test_socket (AF_SP, NN_PAIR);
    test_connect (s2, SOCKET_ADDRESS);
    nn_sleep (100);
    test_close (s2);
    test_close (s1);
    test_close (sb);

/*  On Windows, CreateNamedPipeA does not run exclusively.
    We should look at fixing this, but it will require
    changing the usock code for Windows.  In the meantime just
    disable this test on Windows. */
#if !defined(NN_HAVE_WINDOWS)
    /*  Test two sockets binding to the same address. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    s1 = test_socket (AF_SP, NN_PAIR);
    rc = nn_bind (s1, SOCKET_ADDRESS);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EADDRINUSE);
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);
    nn_sleep (100);
    test_send (sb, "ABC");
    test_recv (sc, "ABC");
    test_close (sb);
    test_close (sc);
    test_close (s1);
#endif

    /*  Test NN_RCVMAXSIZE limit */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, SOCKET_ADDRESS);
    s1 = test_socket (AF_SP, NN_PAIR);
    test_connect (s1, SOCKET_ADDRESS);
    opt = 4;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, opt_sz);
    nn_assert (rc == 0);
    nn_sleep (100);
    test_send (s1, "ABCD");
    test_recv (sb, "ABCD");
    test_send (s1, "ABCDE");
    /*  Without sleep nn_recv returns EAGAIN even for string
        of acceptable size, so false positives are possible. */
    nn_sleep (100);
    rc = nn_recv (sb, &dummy_buf, NN_MSG, NN_DONTWAIT);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EAGAIN);
    test_close (sb);
    test_close (s1);

    /*  Test that NN_RCVMAXSIZE can be -1, but not lower */
    sb = test_socket (AF_SP, NN_PAIR);
    opt = -1;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, opt_sz);
    nn_assert (rc >= 0);
    opt = -2;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, opt_sz);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    test_close (sb);

    /*  Test closing a socket that is waiting to connect. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, SOCKET_ADDRESS);
    nn_sleep (100);
    test_close (sc);
#endif /* NN_HAVE_WSL */

    return 0;
}
Пример #29
0
int main (int argc, const char *argv[])
{
    int rc;
    int sb;
    int i;
    int opt;
    size_t sz;
    int s1, s2;
    void * dummy_buf;
    char addr[128];
    char socket_address[128];

    int port = get_test_port(argc, argv);

    test_addr_from(socket_address, "tcp", "127.0.0.1", port);

    /*  Try closing bound but unconnected socket. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    test_close (sb);

    /*  Try closing a TCP socket while it not connected. At the same time
        test specifying the local address for the connection. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_addr_from(addr, "tcp", "127.0.0.1;127.0.0.1", port);
    test_connect (sc, addr);
    test_close (sc);

    /*  Open the socket anew. */
    sc = test_socket (AF_SP, NN_PAIR);

    /*  Check NODELAY socket option. */
    sz = sizeof (opt);
    rc = nn_getsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, &sz);
    errno_assert (rc == 0);
    nn_assert (sz == sizeof (opt));
    nn_assert (opt == 0);
    opt = 2;
    rc = nn_setsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, sizeof (opt));
    nn_assert (rc < 0 && nn_errno () == EINVAL);
    opt = 1;
    rc = nn_setsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, sizeof (opt));
    errno_assert (rc == 0);
    sz = sizeof (opt);
    rc = nn_getsockopt (sc, NN_TCP, NN_TCP_NODELAY, &opt, &sz);
    errno_assert (rc == 0);
    nn_assert (sz == sizeof (opt));
    nn_assert (opt == 1);

    /*  Try using invalid address strings. */
    rc = nn_connect (sc, "tcp://*:");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://*:1000000");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://*:some_port");
    nn_assert (rc < 0);
    rc = nn_connect (sc, "tcp://eth10000;127.0.0.1:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == ENODEV);
    rc = nn_connect (sc, "tcp://127.0.0.1");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_bind (sc, "tcp://127.0.0.1:");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_bind (sc, "tcp://127.0.0.1:1000000");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_bind (sc, "tcp://eth10000:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == ENODEV);
    rc = nn_connect (sc, "tcp://:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://-hostname:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://abc.123.---.#:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://[::1]:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://abc.123.:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://abc...123:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    rc = nn_connect (sc, "tcp://.123:5555");
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);

    /*  Connect correctly. Do so before binding the peer socket. */
    test_connect (sc, socket_address);

    /*  Leave enough time for at least on re-connect attempt. */
    nn_sleep (200);

    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);

    /*  Ping-pong test. */
    for (i = 0; i != 100; ++i) {

        test_send (sc, "ABC");
        test_recv (sb, "ABC");

        test_send (sb, "DEF");
        test_recv (sc, "DEF");
    }

    /*  Batch transfer test. */
    for (i = 0; i != 100; ++i) {
        test_send (sc, "0123456789012345678901234567890123456789");
    }
    for (i = 0; i != 100; ++i) {
        test_recv (sb, "0123456789012345678901234567890123456789");
    }

    test_close (sc);
    test_close (sb);

    /*  Test whether connection rejection is handled decently. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    s1 = test_socket (AF_SP, NN_PAIR);
    test_connect (s1, socket_address);
    s2 = test_socket (AF_SP, NN_PAIR);
    test_connect (s2, socket_address);
    nn_sleep (100);
    test_close (s2);
    test_close (s1);
    test_close (sb);

    /*  Test two sockets binding to the same address. */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    s1 = test_socket (AF_SP, NN_PAIR);

    rc = nn_bind (s1, socket_address);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EADDRINUSE);

    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, socket_address);
    nn_sleep (100);
    test_send (sb, "ABC");
    test_recv (sc, "ABC");
    test_close (sb);
    test_close (sc);
    test_close (s1);

    /*  Test NN_RCVMAXSIZE limit */
    sb = test_socket (AF_SP, NN_PAIR);
    test_bind (sb, socket_address);
    s1 = test_socket (AF_SP, NN_PAIR);
    test_connect (s1, socket_address);
    opt = 4;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt));
    nn_assert (rc == 0);
    nn_sleep (100);
    test_send (s1, "ABC");
    test_recv (sb, "ABC");
    test_send (s1, "0123456789012345678901234567890123456789");
    rc = nn_recv (sb, &dummy_buf, NN_MSG, NN_DONTWAIT);
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EAGAIN);
    test_close (sb);
    test_close (s1);

    /*  Test that NN_RCVMAXSIZE can be -1, but not lower */
    sb = test_socket (AF_SP, NN_PAIR);
    opt = -1;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt));
    nn_assert (rc >= 0);
    opt = -2;
    rc = nn_setsockopt (sb, NN_SOL_SOCKET, NN_RCVMAXSIZE, &opt, sizeof (opt));
    nn_assert (rc < 0);
    errno_assert (nn_errno () == EINVAL);
    test_close (sb);

    /*  Test closing a socket that is waiting to connect. */
    sc = test_socket (AF_SP, NN_PAIR);
    test_connect (sc, socket_address);
    nn_sleep (100);
    test_close (sc);

    return 0;
}
Пример #30
0
int main ()
{
    int rc;
    int surveyor;
    int respondent1;
    int respondent2;
    int respondent3;
    int deadline;
    char buf [7];

    /*  Test a simple survey with three respondents. */
    surveyor = nn_socket (AF_SP, NN_SURVEYOR);
    errno_assert (surveyor != -1);
    deadline = 500;
    rc = nn_setsockopt (surveyor, NN_SURVEYOR, NN_SURVEYOR_DEADLINE,
        &deadline, sizeof (deadline));
    errno_assert (rc == 0);
    rc = nn_bind (surveyor, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    respondent1 = nn_socket (AF_SP, NN_RESPONDENT);
    errno_assert (respondent1 != -1);
    rc = nn_connect (respondent1, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    respondent2 = nn_socket (AF_SP, NN_RESPONDENT);
    errno_assert (respondent2 != -1);
    rc = nn_connect (respondent2, SOCKET_ADDRESS);
    errno_assert (rc >= 0);
    respondent3 = nn_socket (AF_SP, NN_RESPONDENT);
    errno_assert (respondent3 != -1);
    rc = nn_connect (respondent3, SOCKET_ADDRESS);
    errno_assert (rc >= 0);

    /*  Send the survey. */
    rc = nn_send (surveyor, "ABC", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    /*  First respondent answers. */
    rc = nn_recv (respondent1, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_send (respondent1, "DEF", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    /*  Second respondent answers. */
    rc = nn_recv (respondent2, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_send (respondent2, "DEF", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    /*  Surveyor gets the responses. */
    rc = nn_recv (surveyor, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_recv (surveyor, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    /*  There are no more responses. Surveyor hits the deadline. */
    rc = nn_recv (surveyor, buf, sizeof (buf), 0);
    errno_assert (rc == -1 && nn_errno () == EFSM);

    /*  Third respondent answers (it have already missed the deadline). */
    rc = nn_recv (respondent3, buf, sizeof (buf), 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);
    rc = nn_send (respondent3, "GHI", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    /*  Surveyor initiates new survey. */
    rc = nn_send (surveyor, "ABC", 3, 0);
    errno_assert (rc >= 0);
    nn_assert (rc == 3);

    /*  Check that stale response from third respondent is not delivered. */
    rc = nn_recv (surveyor, buf, sizeof (buf), 0);
    errno_assert (rc == -1 && nn_errno () == EFSM);

    rc = nn_close (surveyor);
    errno_assert (rc == 0);
    rc = nn_close (respondent1);
    errno_assert (rc == 0);    
    rc = nn_close (respondent2);
    errno_assert (rc == 0);
    rc = nn_close (respondent3);
    errno_assert (rc == 0);

    return 0;
}