Search in sources :

Example 46 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamEngineTest method exchange.

private void exchange(SocketBase sender, SocketBase receiver, byte[]... msgs) {
    int repetition = 50;
    for (int idx = 0; idx < repetition; ++idx) {
        for (byte[] msg : msgs) {
            int num = ZMQ.send(sender, msg, 0);
            assertThat(num, is(msg.length));
        }
    }
    for (int idx = 0; idx < repetition; ++idx) {
        for (byte[] msg : msgs) {
            Msg received = ZMQ.recv(receiver, 0);
            assertThat(received.data(), is(msg));
        }
    }
}
Also used : Msg(zmq.Msg)

Example 47 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TestPairTcp method testPairConnectSecondClientIssue285.

@Test
public void testPairConnectSecondClientIssue285() throws IOException {
    String host = "tcp://127.0.0.1:*";
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(bind, notNullValue());
    boolean brc = ZMQ.bind(bind, host);
    assertThat(brc, is(true));
    host = (String) ZMQ.getSocketOptionExt(bind, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    SocketBase first = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(first, notNullValue());
    brc = ZMQ.connect(first, host);
    assertThat(brc, is(true));
    Helper.bounce(bind, first);
    SocketBase second = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(second, notNullValue());
    brc = ZMQ.connect(second, host);
    assertThat(brc, is(true));
    int ret = ZMQ.send(bind, "data", 0);
    assertThat(ret, is(4));
    ret = ZMQ.send(bind, "datb", 0);
    assertThat(ret, is(4));
    ret = ZMQ.send(bind, "datc", 0);
    assertThat(ret, is(4));
    ZMQ.msleep(100);
    // no receiving from second connected pair
    Msg msg = ZMQ.recv(second, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, nullValue());
    // receiving from first connected pair
    msg = ZMQ.recv(first, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is("data".getBytes(ZMQ.CHARSET)));
    msg = ZMQ.recv(first, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is("datb".getBytes(ZMQ.CHARSET)));
    msg = ZMQ.recv(first, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is("datc".getBytes(ZMQ.CHARSET)));
    // Tear down the wiring.
    ZMQ.close(bind);
    ZMQ.close(first);
    ZMQ.close(second);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 48 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class MTrieTest method testAddRemoveMultiNodesAboveLevel.

@Test
public void testAddRemoveMultiNodesAboveLevel() {
    Mtrie mtrie = new Mtrie();
    Pipe other = createPipe();
    Pipe third = createPipe();
    boolean rc = mtrie.add(prefix, pipe);
    assertThat(rc, is(true));
    byte[] abv = Arrays.copyOf(prefix.data(), prefix.size());
    abv[1] = 0;
    Msg above = new Msg(abv);
    rc = mtrie.add(above, other);
    assertThat(rc, is(true));
    abv[1] = 33;
    above = new Msg(abv);
    rc = mtrie.add(above, third);
    assertThat(rc, is(true));
    rc = mtrie.rm(prefix, pipe);
    assertThat(rc, is(true));
    abv[1] = 0;
    above = new Msg(abv);
    rc = mtrie.rm(above, other);
    assertThat(rc, is(true));
    abv[1] = 33;
    above = new Msg(abv);
    rc = mtrie.rm(above, third);
    assertThat(rc, is(true));
}
Also used : Msg(zmq.Msg) Pipe(zmq.pipe.Pipe) Test(org.junit.Test)

Example 49 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class MTrieTest method testAddRemoveMultiNodesAboveLevelWithFunctionCall.

@Test
public void testAddRemoveMultiNodesAboveLevelWithFunctionCall() {
    Mtrie mtrie = new Mtrie();
    Pipe other = createPipe();
    Pipe third = createPipe();
    boolean rc = mtrie.add(prefix, pipe);
    assertThat(rc, is(true));
    byte[] abv = Arrays.copyOf(prefix.data(), prefix.size());
    abv[1] = 3;
    Msg above = new Msg(abv);
    rc = mtrie.add(above, other);
    assertThat(rc, is(true));
    abv[1] = 33;
    above = new Msg(abv);
    rc = mtrie.add(above, third);
    assertThat(rc, is(true));
    rc = mtrie.rm(pipe, handler, null);
    assertThat(rc, is(true));
    assertThat(handler.counter.get(), is(1));
    abv[1] = 3;
    above = new Msg(abv);
    rc = mtrie.rm(other, handler, null);
    assertThat(rc, is(true));
    assertThat(handler.counter.get(), is(2));
    abv[1] = 33;
    above = new Msg(abv);
    rc = mtrie.rm(third, handler, null);
    assertThat(rc, is(true));
    assertThat(handler.counter.get(), is(3));
}
Also used : Msg(zmq.Msg) Pipe(zmq.pipe.Pipe) Test(org.junit.Test)

Example 50 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class RouterMandatoryTest method testRouterMandatoryHwm.

@Test
public void testRouterMandatoryHwm() throws Exception {
    boolean rc;
    System.out.print("Starting router mandatory HWM test");
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(router, notNullValue());
    ZMQ.setSocketOption(router, ZMQ.ZMQ_ROUTER_MANDATORY, true);
    ZMQ.setSocketOption(router, ZMQ.ZMQ_SNDHWM, 1);
    ZMQ.setSocketOption(router, ZMQ.ZMQ_LINGER, 1);
    rc = ZMQ.bind(router, "tcp://127.0.0.1:*");
    assertThat(rc, is(true));
    // Create dealer called "X" and connect it to our router, configure HWM
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    ZMQ.setSocketOption(dealer, ZMQ.ZMQ_RCVHWM, 1);
    ZMQ.setSocketOption(dealer, ZMQ.ZMQ_IDENTITY, "X");
    String host = (String) ZMQ.getSocketOptionExt(router, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    rc = ZMQ.connect(dealer, host);
    assertThat(rc, is(true));
    System.out.print(".");
    // Get message from dealer to know when connection is ready
    int ret = ZMQ.send(dealer, "Hello", 0);
    assertThat(ret, is(5));
    System.out.print(".");
    Msg msg = ZMQ.recv(router, 0);
    System.out.print(".");
    assertThat(msg, notNullValue());
    assertThat(msg.data()[0], is((byte) 'X'));
    int i = 0;
    for (; i < 100000; ++i) {
        ret = ZMQ.send(router, "X", ZMQ.ZMQ_DONTWAIT | ZMQ.ZMQ_SNDMORE);
        if (ret == -1 && router.errno() == ZError.EAGAIN) {
            break;
        }
        assertThat(ret, is(1));
        ret = ZMQ.send(router, new byte[BUF_SIZE], BUF_SIZE, ZMQ.ZMQ_DONTWAIT);
        assertThat(ret, is(BUF_SIZE));
    }
    System.out.print(".");
    // This should fail after one message but kernel buffering could
    // skew results
    assertThat(i < 10, is(true));
    ZMQ.sleep(1);
    // Send second batch of messages
    for (; i < 100000; ++i) {
        ret = ZMQ.send(router, "X", ZMQ.ZMQ_DONTWAIT | ZMQ.ZMQ_SNDMORE);
        if (ret == -1 && router.errno() == ZError.EAGAIN) {
            break;
        }
        assertThat(ret, is(1));
        ret = ZMQ.send(router, new byte[BUF_SIZE], BUF_SIZE, ZMQ.ZMQ_DONTWAIT);
        assertThat(ret, is(BUF_SIZE));
    }
    System.out.print(".");
    // This should fail after two messages but kernel buffering could
    // skew results
    assertThat(i < 20, is(true));
    System.out.println("Done sending messages.");
    // Clean up.
    ZMQ.close(router);
    ZMQ.close(dealer);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Aggregations

Msg (zmq.Msg)124 Test (org.junit.Test)45 SocketBase (zmq.SocketBase)37 Ctx (zmq.Ctx)32 ByteBuffer (java.nio.ByteBuffer)16 ValueReference (zmq.util.ValueReference)16 Pipe (zmq.pipe.Pipe)13 Blob (zmq.util.Blob)7 ArrayList (java.util.ArrayList)5 OutputStream (java.io.OutputStream)4 Socket (java.net.Socket)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)2 Metadata (zmq.io.Metadata)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Event (zmq.ZMQ.Event)1 ZObject (zmq.ZObject)1 Step (zmq.io.coder.IDecoder.Step)1 RawDecoder (zmq.io.coder.raw.RawDecoder)1