Exemplo n.º 1
0
int testterm()
{
    int rc;
    int s;
    struct nn_thread thread;
    printf("test term\n");

    /*  Close the socket with no associated endpoints. */
    s = test_socket (AF_SP, NN_PAIR);
    test_close (s);

    /*  Test nn_term() before nn_close(). */
    nn_thread_init (&thread, worker, NULL);
    nn_sleep (100);
    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);
    errno_assert (nn_errno () == ETERM);

    /*  Wait till worker thread terminates. */
    nn_thread_term(&thread);
    printf("nn_thread_term finished\n");
    return 0;
}
Exemplo n.º 2
0
int main (int argc, const char *argv[])
{
    int end0;
    int end1;
    struct nn_thread thread5;
    struct nn_thread thread6;

    int port = get_test_port(argc, argv);

    test_addr_from(socket_address_h, "tcp", "127.0.0.1", port);
    test_addr_from(socket_address_i, "tcp", "127.0.0.1", port + 1);
    test_addr_from(socket_address_j, "tcp", "127.0.0.1", port + 2);

    /*  Test the bi-directional device with REQ/REP (headers). */

    /*  Start the devices. */
    nn_thread_init (&thread5, device5, NULL);
    nn_thread_init (&thread6, device6, NULL);

    /*  Create two sockets to connect to the device. */
    end0 = test_socket (AF_SP, NN_REQ);
    test_connect (end0, socket_address_h);
    end1 = test_socket (AF_SP, NN_REP);
    test_connect (end1, socket_address_j);

    /*  Wait for TCP to establish. */
    nn_sleep (100);

    /*  Pass a message between endpoints. */
    test_send (end0, "XYZ");
    test_recv (end1, "XYZ");

    /*  Now send a reply. */
    test_send (end1, "REPLYXYZ");
    test_recv (end0, "REPLYXYZ");

    /*  Clean up. */
    test_close (end0);
    test_close (end1);

    /*  Shut down the devices. */
    nn_term ();
    nn_thread_term (&thread5);
    nn_thread_term (&thread6);

    return 0;
}
Exemplo n.º 3
0
int main ()
{
    int end0;
    int end1;
    struct nn_thread thread5;
    struct nn_thread thread6;

    /*  Test the bi-directional device with REQ/REP (headers). */

    /*  Start the devices. */
    nn_thread_init (&thread5, device5, NULL);
    nn_thread_init (&thread6, device6, NULL);

    /*  Create two sockets to connect to the device. */
    end0 = test_socket (AF_SP, NN_REQ);
    test_connect (end0, SOCKET_ADDRESS_H);
    end1 = test_socket (AF_SP, NN_REP);
    test_connect (end1, SOCKET_ADDRESS_J);

    /*  Wait for TCP to establish. */
    nn_sleep (1000);

    /*  Pass a message between endpoints. */
    test_send (end0, "XYZ");
    test_recv (end1, "XYZ");

    /*  Now send a reply. */
    test_send (end1, "REPLYXYZ");
    test_recv (end0, "REPLYXYZ");

    /*  Clean up. */
    test_close (end0);
    test_close (end1);

    /*  Shut down the devices. */
    nn_term ();
    nn_thread_term (&thread5);
    nn_thread_term (&thread6);

    return 0;
}
Exemplo n.º 4
0
Arquivo: device.c Projeto: 0xe/nanomsg
int main ()
{
    int enda;
    int endb;
    int endc;
    int endd;
    int ende1;
    int ende2;
    struct nn_thread thread1;
    struct nn_thread thread2;
    struct nn_thread thread3;
    int timeo;

    /*  Test the bi-directional device. */

    /*  Start the device. */
    nn_thread_init (&thread1, device1, NULL);

    /*  Create two sockets to connect to the device. */
    enda = test_socket (AF_SP, NN_PAIR);
    test_connect (enda, SOCKET_ADDRESS_A);
    endb = test_socket (AF_SP, NN_PAIR);
    test_connect (endb, SOCKET_ADDRESS_B);

    /*  Pass a pair of messages between endpoints. */
    test_send (enda, "ABC");
    test_recv (endb, "ABC");
    test_send (endb, "ABC");
    test_recv (enda, "ABC");

    /*  Clean up. */
    test_close (endb);
    test_close (enda);

    /*  Test the uni-directional device. */

    /*  Start the device. */
    nn_thread_init (&thread2, device2, NULL);

    /*  Create two sockets to connect to the device. */
    endc = test_socket (AF_SP, NN_PUSH);
    test_connect (endc, SOCKET_ADDRESS_C);
    endd = test_socket (AF_SP, NN_PULL);
    test_connect (endd, SOCKET_ADDRESS_D);

    /*  Pass a message between endpoints. */
    test_send (endc, "XYZ");
    test_recv (endd, "XYZ");

    /*  Clean up. */
    test_close (endd);
    test_close (endc);

    /*  Test the loopback device. */

    /*  Start the device. */
    nn_thread_init (&thread3, device3, NULL);

    /*  Create two sockets to connect to the device. */
    ende1 = test_socket (AF_SP, NN_BUS);
    test_connect (ende1, SOCKET_ADDRESS_E);
    ende2 = test_socket (AF_SP, NN_BUS);
    test_connect (ende2, SOCKET_ADDRESS_E);

    /*  BUS is unreliable so wait a bit for connections to be established. */
    nn_sleep (100);

    /*  Pass a message to the bus. */
    test_send (ende1, "KLM");
    test_recv (ende2, "KLM");

    /*  Make sure that the message doesn't arrive at the socket it was
        originally sent to. */
    timeo = 100;
    test_setsockopt (ende1, NN_SOL_SOCKET, NN_RCVTIMEO,
       &timeo, sizeof (timeo));
    test_drop (ende1, ETIMEDOUT);

    /*  Clean up. */
    test_close (ende2);
    test_close (ende1);

    /*  Shut down the devices. */
    nn_term ();
    nn_thread_term (&thread1);
    nn_thread_term (&thread2);
    nn_thread_term (&thread3);

    return 0;
}
Exemplo n.º 5
0
 inline void term ()
 {
     nn_term ();
 }
Exemplo n.º 6
0
int main(int argc, char *argv[]) {
  int opt, i, n, rc=-1;
  struct epoll_event ev;
  CF.prog = argv[0];
  CF.now = time(NULL);
  utarray_new(CF.rdkafka_options,&ut_str_icd);
  utarray_new(CF.rdkafka_topic_options,&ut_str_icd);
  void *res;

  while ( (opt = getopt(argc, argv, "hv+N:n:d:b:t:c:C:")) != -1) {
    switch(opt) {
      case 'v': CF.verbose++; break;
      case 'n': CF.nthread=atoi(optarg); break;
      case 'd': CF.dir=strdup(optarg); break;
      case 't': CF.topic=strdup(optarg); break;
      case 'b': CF.broker=strdup(optarg); break;
      case 'c': utarray_push_back(CF.rdkafka_options,&optarg); break;
      case 'C': utarray_push_back(CF.rdkafka_topic_options,&optarg); break;
      case 'h': default: usage();
    }
  }
  if (CF.dir == NULL) usage();
  if (CF.broker == NULL) usage();
  if (CF.topic == NULL) CF.topic = CF.dir;

  /* stats (time series) for input/output tracking */
  CF.spr_msgs_ts = ts_new(6,  STATS_INTERVAL ,&ts_int_mm);
  CF.kaf_msgs_ts = ts_new(6,  STATS_INTERVAL, &ts_int_mm);
  CF.kaf_bytes_ts = ts_new(6, STATS_INTERVAL, &ts_int_mm);

  /* block all signals. we take signals synchronously via signalfd */
  sigset_t all;
  sigfillset(&all);
  sigprocmask(SIG_SETMASK,&all,NULL);

  /* a few signals we'll accept via our signalfd */
  sigset_t sw;
  sigemptyset(&sw);
  for(n=0; n < sizeof(sigs)/sizeof(*sigs); n++) sigaddset(&sw, sigs[n]);

  /* create the signalfd for receiving signals */
  CF.signal_fd = signalfd(-1, &sw, 0);
  if (CF.signal_fd == -1) {
    fprintf(stderr,"signalfd: %s\n", strerror(errno));
    goto done;
  }

  /* set up the epoll instance */
  CF.epoll_fd = epoll_create(1); 
  if (CF.epoll_fd == -1) {
    fprintf(stderr,"epoll: %s\n", strerror(errno));
    goto done;
  }

  if (setup_nano() < 0) goto done;

  /* add descriptors of interest */
  if (new_epoll(EPOLLIN, CF.signal_fd)) goto done; // signal socket

  /* fire up threads */
  rc = pthread_create(&CF.spr_thread, NULL, spr_worker, NULL); if (rc) goto done;
  CF.enc_thread = malloc(sizeof(pthread_t)*CF.nthread);
  if (CF.enc_thread == NULL) goto done;
  long id;
  for(i=0; i < CF.nthread; i++) {
    id = i;
    rc = pthread_create(&CF.enc_thread[i],NULL,enc_worker,(void*)id);
    if (rc) goto done;
  }

  CF.kaf_thread = malloc(sizeof(pthread_t)*CF.nthread);
  if (CF.kaf_thread == NULL) goto done;
  for(i=0; i < CF.nthread; i++) {
    id = i;
    rc = pthread_create(&CF.kaf_thread[i],NULL,kaf_worker,(void*)id);
    if (rc) goto done;
  }

  alarm(1);
  while (epoll_wait(CF.epoll_fd, &ev, 1, -1) > 0) {
    if (ev.data.fd == CF.signal_fd) { 
      if (handle_signal() < 0) goto done; 
    }
  }

  rc = 0;

done:
  CF.shutdown=1;
  nn_term();
  fprintf(stderr,"shutting down threads:\n");

  fprintf(stderr,"spoolreader...\n");
  if (CF.spr_thread) {
    pthread_cancel(CF.spr_thread);
    pthread_join(CF.spr_thread,NULL);
  }

  fprintf(stderr,"encoders...\n");
  if (CF.enc_thread) {
    for(i=0; i < CF.nthread; i++) {
      pthread_cancel(CF.enc_thread[i]);
      pthread_join(CF.enc_thread[i],NULL);
    }
  }

  fprintf(stderr,"transmitters...\n");
  if (CF.kaf_thread) {
    for(i=0; i < CF.nthread; i++) {
      pthread_cancel(CF.kaf_thread[i]);
      pthread_join(CF.kaf_thread[i],NULL);
    }
  }

  fprintf(stderr,"terminating...\n");
  if (CF.ingress_socket_push >= 0) nn_close(CF.ingress_socket_push);
  if (CF.ingress_socket_pull >= 0) nn_close(CF.ingress_socket_pull);
  if (CF.egress_socket_push >= 0) nn_close(CF.egress_socket_push);
  if (CF.egress_socket_pull >= 0) nn_close(CF.egress_socket_pull);
  if (CF.egress_socket_filepath) unlink(CF.egress_socket_filepath);
  if (CF.ingress_socket_filepath) unlink(CF.ingress_socket_filepath);
  if (CF.epoll_fd != -1) close(CF.epoll_fd);
  if (CF.signal_fd != -1) close(CF.signal_fd);
  ts_free(CF.spr_msgs_ts);
  ts_free(CF.kaf_msgs_ts);
  ts_free(CF.kaf_bytes_ts);
  if (CF.enc_thread) free(CF.enc_thread);
  if (CF.kaf_thread) free(CF.kaf_thread);
  utarray_free(CF.rdkafka_options);
  utarray_free(CF.rdkafka_topic_options);
  return rc;
}
Exemplo n.º 7
0
JNIEXPORT void JNICALL Java_org_nanomsg_NanoLibrary_nn_1term(JNIEnv* env,
                                                             jobject obj)
{
    nn_term();
}
Exemplo n.º 8
0
 void term()
 { nn_term(); }
Exemplo n.º 9
0
void routine2 (NN_UNUSED void *arg)
{
   nn_sleep (10);
   nn_term ();
}