示例#1
0
 @Override
 public void run() {
   ZMQ.zmq_sleep(1);
   SocketBase sc = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PUSH);
   assertThat(sc, notNullValue());
   boolean rc = ZMQ.zmq_connect(sc, "inproc://timeout_test");
   assertThat(rc, is(true));
   ZMQ.zmq_sleep(1);
   ZMQ.zmq_close(sc);
 }
示例#2
0
  public static boolean device(SocketBase insocket_, SocketBase outsocket_) {

    //  The algorithm below assumes ratio of requests and replies processed
    //  under full load to be 1:1.

    //  TODO: The current implementation drops messages when
    //  any of the pipes becomes full.

    boolean success;
    int rc;
    int more;
    Msg msg;
    PollItem items[] = new PollItem[2];

    items[0] = new PollItem(insocket_, ZMQ.ZMQ_POLLIN);
    items[1] = new PollItem(outsocket_, ZMQ.ZMQ_POLLIN);

    while (!Thread.currentThread().isInterrupted()) {
      //  Wait while there are either requests or replies to process.
      rc = ZMQ.zmq_poll(items, -1);
      if (rc < 0) return false;

      //  Process a request.
      if (items[0].isReadable()) {
        while (true) {
          msg = insocket_.recv(0);
          if (msg == null) break;

          more = insocket_.getsockopt(ZMQ.ZMQ_RCVMORE);

          success = outsocket_.send(msg, more > 0 ? ZMQ.ZMQ_SNDMORE : 0);
          if (!success) return false;
          if (more == 0) break;
        }
      }
      //  Process a reply.
      if (items[1].isReadable()) {
        while (true) {
          msg = outsocket_.recv(0);
          if (msg == null) break;

          more = outsocket_.getsockopt(ZMQ.ZMQ_RCVMORE);

          success = insocket_.send(msg, more > 0 ? ZMQ.ZMQ_SNDMORE : 0);
          if (!success) return false;
          if (more == 0) break;
        }
      }
    }
    return true;
  }
示例#3
0
    @Override
    public void run() {

      SocketBase s = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PAIR);
      boolean rc = s.connect(monitor_addr);
      assert (rc);
      // Only some of the exceptional events could fire
      while (true) {
        ZMQ.Event event = ZMQ.Event.read(s);
        if (event == null && s.errno() == ZError.ETERM) break;
        assert (event != null);

        switch (event.event) {
            // listener specific
          case ZMQ.ZMQ_EVENT_LISTENING:
            // assert (strcmp(addr, data_->listening.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_LISTENING;
            break;
          case ZMQ.ZMQ_EVENT_ACCEPTED:
            // assert (strcmp(addr, data_->accepted.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_ACCEPTED;
            break;
            // connecter specific
          case ZMQ.ZMQ_EVENT_CONNECTED:
            // assert (strcmp(addr, data_->connected.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CONNECTED;
            break;
          case ZMQ.ZMQ_EVENT_CONNECT_DELAYED:
            // assert (strcmp(addr, data_->connect_delayed.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CONNECT_DELAYED;
            break;
            // generic - either end of the socket
          case ZMQ.ZMQ_EVENT_CLOSE_FAILED:
            // assert (strcmp(addr, data_->close_failed.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CLOSE_FAILED;
            break;
          case ZMQ.ZMQ_EVENT_CLOSED:
            // assert (strcmp(addr, data_->closed.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_CLOSED;
            break;
          case ZMQ.ZMQ_EVENT_DISCONNECTED:
            // assert (strcmp(addr, data_->disconnected.addr) == 0);
            events |= ZMQ.ZMQ_EVENT_DISCONNECTED;
            break;
          default:
            // out of band / unexpected event
            assertTrue("Unkown Event " + event.event, true);
        }
      }
      s.close();
    }
示例#4
0
  @Test
  public void testReqrepInproc() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(sb, notNullValue());
    boolean brc = ZMQ.bind(sb, "inproc://a");
    assertThat(brc, is(true));

    SocketBase sc = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(sc, notNullValue());
    brc = ZMQ.connect(sc, "inproc://a");
    assertThat(brc, is(true));

    Helper.bounce(sb, sc);

    //  Tear down the wiring.
    ZMQ.close(sb);
    ZMQ.close(sc);
    ZMQ.term(ctx);
  }
示例#5
0
  @Test
  public void testPairInproc() {

    Ctx ctx = ZMQ.zmq_init(1);
    assertThat(ctx, notNullValue());
    SocketBase sb = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(sb, notNullValue());
    boolean brc = ZMQ.zmq_bind(sb, "inproc://a");
    assertThat(brc, is(true));

    SocketBase sc = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(sc, notNullValue());
    brc = ZMQ.zmq_connect(sc, "inproc://a");
    assertThat(brc, is(true));

    TestHelper.bounce(sb, sc);

    //  Tear down the wiring.
    ZMQ.zmq_close(sb);
    ZMQ.zmq_close(sc);
    ZMQ.zmq_term(ctx);
  }
示例#6
0
  public static void bounce(SocketBase sb, SocketBase sc) {
    byte[] content = "12345678ABCDEFGH12345678abcdefgh".getBytes();

    //  Send the message.
    int rc = ZMQ.zmq_send(sc, content, 32, ZMQ.ZMQ_SNDMORE);
    assert (rc == 32);
    rc = ZMQ.zmq_send(sc, content, 32, 0);
    assertThat(rc, is(32));

    //  Bounce the message back.
    Msg msg;
    msg = ZMQ.zmq_recv(sb, 0);
    assert (msg.size() == 32);
    long rcvmore = ZMQ.zmq_getsockopt(sb, ZMQ.ZMQ_RCVMORE);
    assert (rcvmore == 1);
    msg = ZMQ.zmq_recv(sb, 0);
    assert (rc == 32);
    rcvmore = ZMQ.zmq_getsockopt(sb, ZMQ.ZMQ_RCVMORE);
    assert (rcvmore == 0);
    rc = ZMQ.zmq_send(sb, new Msg(msg), ZMQ.ZMQ_SNDMORE);
    assert (rc == 32);
    rc = ZMQ.zmq_send(sb, new Msg(msg), 0);
    assert (rc == 32);

    //  Receive the bounced message.
    msg = ZMQ.zmq_recv(sc, 0);
    assert (rc == 32);
    rcvmore = ZMQ.zmq_getsockopt(sc, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore, is(1L));
    msg = ZMQ.zmq_recv(sc, 0);
    assert (rc == 32);
    rcvmore = ZMQ.zmq_getsockopt(sc, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore, is(0L));
    //  Check whether the message is still the same.
    // assert (memcmp (buf2, content, 32) == 0);
  }
示例#7
0
  @Test
  public void testTimeo() throws Exception {
    Ctx ctx = ZMQ.zmqInit(1);
    assertThat(ctx, notNullValue());

    SocketBase sb = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PULL);
    assertThat(sb, notNullValue());
    boolean rc = ZMQ.zmq_bind(sb, "inproc://timeout_test");
    assertThat(rc, is(true));

    //  Check whether non-blocking recv returns immediately.
    Msg msg = ZMQ.zmq_recv(sb, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, nullValue());

    //  Check whether recv timeout is honoured.
    int timeout = 500;
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_RCVTIMEO, timeout);
    long watch = ZMQ.zmq_stopwatch_start();
    msg = ZMQ.zmq_recv(sb, 0);
    assertThat(msg, nullValue());
    long elapsed = ZMQ.zmq_stopwatch_stop(watch);
    assertThat(elapsed > 440000 && elapsed < 550000, is(true));

    //  Check whether connection during the wait doesn't distort the timeout.
    timeout = 2000;
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_RCVTIMEO, timeout);
    Thread thread = new Thread(new Worker(ctx));
    thread.start();

    watch = ZMQ.zmq_stopwatch_start();
    msg = ZMQ.zmq_recv(sb, 0);
    assertThat(msg, nullValue());
    elapsed = ZMQ.zmq_stopwatch_stop(watch);
    assertThat(elapsed > 1900000 && elapsed < 2100000, is(true));
    thread.join();

    //  Check that timeouts don't break normal message transfer.
    SocketBase sc = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_PUSH);
    assertThat(sc, notNullValue());
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_RCVTIMEO, timeout);
    ZMQ.zmq_setsockopt(sb, ZMQ.ZMQ_SNDTIMEO, timeout);
    rc = ZMQ.zmq_connect(sc, "inproc://timeout_test");
    assertThat(rc, is(true));
    Msg smsg = new Msg("12345678ABCDEFGH12345678abcdefgh".getBytes(ZMQ.CHARSET));
    int r = ZMQ.zmq_send(sc, smsg, 0);
    assertThat(r, is(32));
    msg = ZMQ.zmq_recv(sb, 0);
    assertThat(msg.size(), is(32));

    ZMQ.zmq_close(sc);
    ZMQ.zmq_close(sb);
    ZMQ.zmq_term(ctx);
  }
示例#8
0
  @Test
  public void testMonitor() throws Exception {
    String addr = "tcp://127.0.0.1:5590";
    SocketMonitor[] threads = new SocketMonitor[3];
    //  Create the infrastructure
    Ctx ctx = ZMQ.zmq_init(1);
    assert (ctx != null);
    // set socket monitor
    SocketBase rep = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_REP);
    assert (rep != null);
    try {
      ZMQ.zmq_socket_monitor(rep, addr, 0);
      assertTrue(false);
    } catch (IllegalArgumentException e) {
    }

    // REP socket monitor, all events
    boolean rc = ZMQ.zmq_socket_monitor(rep, "inproc://monitor.rep", ZMQ.ZMQ_EVENT_ALL);
    assertEquals(true, rc);

    threads[0] = new SocketMonitor(ctx, "inproc://monitor.rep");
    threads[0].start();

    rc = ZMQ.zmq_bind(rep, addr);
    assert (rc);

    SocketBase req = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_REQ);
    assert (req != null);

    // REQ socket monitor, all events
    rc = ZMQ.zmq_socket_monitor(req, "inproc://monitor.req", ZMQ.ZMQ_EVENT_ALL);
    assertEquals(true, rc);

    threads[1] = new SocketMonitor(ctx, "inproc://monitor.req");
    threads[1].start();

    rc = ZMQ.zmq_connect(req, addr);
    assert (rc);

    // 2nd REQ socket
    SocketBase req2 = ZMQ.zmq_socket(ctx, ZMQ.ZMQ_REQ);
    assert (req2 != null);

    // 2nd REQ socket monitor, connected event only
    rc = ZMQ.zmq_socket_monitor(req2, "inproc://monitor.req2", ZMQ.ZMQ_EVENT_CONNECTED);
    assertEquals(true, rc);

    threads[2] = new SocketMonitor(ctx, "inproc://monitor.req2");
    threads[2].start();

    rc = ZMQ.zmq_connect(req2, addr);
    assert (rc);

    Helper.bounce(rep, req);

    // Allow a window for socket events as connect can be async
    ZMQ.zmq_sleep(1);

    // Close the REP socket
    ZMQ.zmq_close(rep);

    // Allow some time for detecting error states
    ZMQ.zmq_sleep(1);
    // Close the REQ socket
    ZMQ.zmq_close(req);
    // Close the 2nd REQ socket
    ZMQ.zmq_close(req2);

    // Allow for closed or disconnected events to bubble up
    ZMQ.zmq_sleep(1);

    ZMQ.zmq_term(ctx);

    // Expected REP socket events
    // We expect to at least observe these events
    assertTrue((threads[0].events & ZMQ.ZMQ_EVENT_LISTENING) > 0);
    assertTrue((threads[0].events & ZMQ.ZMQ_EVENT_ACCEPTED) > 0);
    assertTrue((threads[0].events & ZMQ.ZMQ_EVENT_CLOSED) > 0);

    // Expected REQ socket events
    assertTrue((threads[1].events & ZMQ.ZMQ_EVENT_CONNECTED) > 0);
    assertTrue((threads[1].events & ZMQ.ZMQ_EVENT_DISCONNECTED) > 0);
    assertTrue((threads[1].events & ZMQ.ZMQ_EVENT_CLOSED) > 0);

    // Expected 2nd REQ socket events
    assertTrue((threads[2].events & ZMQ.ZMQ_EVENT_CONNECTED) > 0);
    assertTrue((threads[2].events & ZMQ.ZMQ_EVENT_CLOSED) == 0);

    threads[0].join();
    threads[1].join();
    threads[2].join();
  }