Search in sources :

Example 61 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class RepSpecTest method testSpecEnvelope.

@Test
public void testSpecEnvelope() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // For an incoming message:
        // SHALL remove and store the address envelope, including the delimiter.
        // SHALL pass the remaining data frames to its calling application.
        // SHALL wait for a single reply message from its calling application.
        // SHALL prepend the address envelope and delimiter.
        // SHALL deliver this message back to the originating peer.
        envelope(ctx, bindAddress, ZMQ.ZMQ_REP, ZMQ.ZMQ_DEALER);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 62 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class RepSpecTest method testSpecFairQueueIn.

@Test
public void testSpecFairQueueIn() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // SHALL receive incoming messages from its peers using a fair-queuing
        // strategy.
        fairQueueIn(ctx, bindAddress, ZMQ.ZMQ_REP, ZMQ.ZMQ_REQ);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 63 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class RouterHandoverTest method testRouterHandover.

@Test
public void testRouterHandover() throws Exception {
    int rc;
    boolean brc;
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    brc = ZMQ.bind(router, "tcp://127.0.0.1:*");
    assertThat(brc, is(true));
    // Enable the handover flag
    ZMQ.setSocketOption(router, ZMQ.ZMQ_ROUTER_HANDOVER, 1);
    assertThat(router, notNullValue());
    // Create dealer called "X" and connect it to our router
    SocketBase dealerOne = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealerOne, notNullValue());
    ZMQ.setSocketOption(dealerOne, ZMQ.ZMQ_IDENTITY, "X");
    String host = (String) ZMQ.getSocketOptionExt(router, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    brc = ZMQ.connect(dealerOne, host);
    assertThat(brc, is(true));
    // Get message from dealer to know when connection is ready
    rc = ZMQ.send(dealerOne, "Hello", 0);
    assertThat(rc, is(5));
    Msg msg = ZMQ.recv(router, 0);
    assertThat(msg.size(), is(1));
    assertThat(new String(msg.data()), is("X"));
    msg = ZMQ.recv(router, 0);
    assertThat(msg.size(), is(5));
    // Now create a second dealer that uses the same identity
    SocketBase dealerTwo = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealerTwo, notNullValue());
    ZMQ.setSocketOption(dealerTwo, ZMQ.ZMQ_IDENTITY, "X");
    brc = ZMQ.connect(dealerTwo, host);
    assertThat(brc, is(true));
    // Get message from dealer to know when connection is ready
    rc = ZMQ.send(dealerTwo, "Hello", 0);
    assertThat(rc, is(5));
    msg = ZMQ.recv(router, 0);
    assertThat(msg.size(), is(1));
    assertThat(new String(msg.data()), is("X"));
    msg = ZMQ.recv(router, 0);
    assertThat(msg.size(), is(5));
    // Send a message to 'X' identity. This should be delivered
    // to the second dealer, instead of the first because of the handover.
    rc = ZMQ.send(router, "X", ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(1));
    rc = ZMQ.send(router, "Hello", 0);
    assertThat(rc, is(5));
    // Ensure that the first dealer doesn't receive the message
    // but the second one does
    msg = ZMQ.recv(dealerOne, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, nullValue());
    msg = ZMQ.recv(dealerTwo, 0);
    assertThat(msg.size(), is(5));
    // Clean up.
    ZMQ.close(router);
    ZMQ.close(dealerOne);
    ZMQ.close(dealerTwo);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 64 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestReqrepTcp method testReqrepTcp.

@Test
public void testReqrepTcp() throws Exception {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase repBind = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(repBind, notNullValue());
    boolean rc = ZMQ.bind(repBind, "tcp://127.0.0.1:*");
    assertThat(rc, is(true));
    String host = (String) ZMQ.getSocketOptionExt(repBind, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    SocketBase reqConnect = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(reqConnect, notNullValue());
    rc = ZMQ.connect(reqConnect, host);
    assertThat(rc, is(true));
    Helper.bounce(repBind, reqConnect);
    ZMQ.close(reqConnect);
    ZMQ.close(repBind);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 65 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class StreamEmptyTest method testStreamEmpty.

@Test
public void testStreamEmpty() throws IOException, InterruptedException {
    String host = "tcp://localhost:*";
    Ctx ctx = ZMQ.init(1);
    assert (ctx != null);
    // Set up listener STREAM.
    SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
    assert (bind != null);
    boolean rc = ZMQ.bind(bind, host);
    assert (rc);
    host = (String) ZMQ.getSocketOptionExt(bind, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    // Set up connection stream.
    SocketBase connect = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assert (connect != null);
    // Do the connection.
    rc = ZMQ.connect(connect, host);
    assert (rc);
    ZMQ.sleep(1);
    int ret = ZMQ.send(connect, "", 0);
    assertThat(ret, is(0));
    Msg msg = ZMQ.recv(bind, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(5));
    ret = ZMQ.send(bind, msg, ZMQ.ZMQ_SNDMORE);
    assertThat(ret, is(5));
    ret = ZMQ.send(bind, new Msg(), 0);
    assertThat(ret, is(0));
    ZMQ.setSocketOption(bind, ZMQ.ZMQ_LINGER, 0);
    ZMQ.setSocketOption(connect, ZMQ.ZMQ_LINGER, 0);
    ZMQ.close(bind);
    ZMQ.close(connect);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Aggregations

Ctx (zmq.Ctx)81 Test (org.junit.Test)73 SocketBase (zmq.SocketBase)63 Msg (zmq.Msg)31 AbstractSpecTest (zmq.socket.AbstractSpecTest)16 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 Ignore (org.junit.Ignore)4 ExecutorService (java.util.concurrent.ExecutorService)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Curve (zmq.io.mechanism.curve.Curve)2 InputStream (java.io.InputStream)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 Selector (java.nio.channels.Selector)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1