Search in sources :

Example 76 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class AbstractSpecTest method recvSeq.

public void recvSeq(SocketBase socket, String... data) {
    for (int idx = 0; idx < data.length; ++idx) {
        Msg msg = ZMQ.recv(socket, 0);
        String payload = data[idx];
        if (payload == null) {
            assertThat(msg, notNullValue());
            assertThat(msg.size(), is(0));
        } else {
            assertThat(msg, notNullValue());
            assertThat(msg.data(), is(payload.getBytes(ZMQ.CHARSET)));
        }
        int rc = ZMQ.getSocketOption(socket, ZMQ.ZMQ_RCVMORE);
        if (idx == data.length - 1) {
            assertThat(rc, is(0));
        } else {
            assertThat(rc, is(1));
        }
    }
}
Also used : Msg(zmq.Msg)

Example 77 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class RouterProbeTest method testProbeRouter.

@Test
public void testProbeRouter() throws IOException, InterruptedException {
    int port = Utils.findOpenPort();
    String host = "tcp://127.0.0.1:" + port;
    Ctx ctx = ZMQ.createContext();
    // Server socket will accept connections
    SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(server, notNullValue());
    boolean rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    // Create client and connect to server, doing a probe
    SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(client, notNullValue());
    rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_IDENTITY, "X");
    assertThat(rc, is(true));
    rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_PROBE_ROUTER, true);
    assertThat(rc, is(true));
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    // We expect an identity=X + empty message from client
    Msg msg = ZMQ.recv(server, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.get(0), is((byte) 'X'));
    msg = ZMQ.recv(server, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(0));
    // Send a message to client now
    int ret = ZMQ.send(server, "X", ZMQ.ZMQ_SNDMORE);
    assertThat(ret, is(1));
    ret = ZMQ.send(server, "Hello", 0);
    assertThat(ret, is(5));
    msg = ZMQ.recv(client, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(5));
    // TODO DIFF V4 test should stop here, check the logic if we should receive payload in the previous message.
    msg = ZMQ.recv(client, 0);
    assertThat(msg, notNullValue());
    assertThat(new String(msg.data(), ZMQ.CHARSET), is("Hello"));
    ZMQ.closeZeroLinger(server);
    ZMQ.closeZeroLinger(client);
    // Shutdown
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 78 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TestInvalidRep method testInvalidRep.

// Create REQ/ROUTER wiring.
@Test
public void testInvalidRep() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase routerSocket = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(routerSocket, notNullValue());
    SocketBase reqSocket = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(reqSocket, notNullValue());
    int linger = 0;
    int rc;
    ZMQ.setSocketOption(routerSocket, ZMQ.ZMQ_LINGER, linger);
    ZMQ.setSocketOption(reqSocket, ZMQ.ZMQ_LINGER, linger);
    boolean brc = ZMQ.bind(routerSocket, "inproc://hi");
    assertThat(brc, is(true));
    brc = ZMQ.connect(reqSocket, "inproc://hi");
    assertThat(brc, is(true));
    // Initial request.
    rc = ZMQ.send(reqSocket, "r", 0);
    assertThat(rc, is(1));
    // Receive the request.
    Msg addr;
    Msg bottom;
    Msg body;
    addr = ZMQ.recv(routerSocket, 0);
    int addrSize = addr.size();
    System.out.println("addrSize: " + addr.size());
    assertThat(addr.size() > 0, is(true));
    bottom = ZMQ.recv(routerSocket, 0);
    assertThat(bottom.size(), is(0));
    body = ZMQ.recv(routerSocket, 0);
    assertThat(body.size(), is(1));
    assertThat(body.data()[0], is((byte) 'r'));
    // Send invalid reply.
    rc = ZMQ.send(routerSocket, addr, 0);
    assertThat(rc, is(addrSize));
    // Send valid reply.
    rc = ZMQ.send(routerSocket, addr, ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(addrSize));
    rc = ZMQ.send(routerSocket, bottom, ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(0));
    rc = ZMQ.send(routerSocket, "b", 0);
    assertThat(rc, is(1));
    // Check whether we've got the valid reply.
    body = ZMQ.recv(reqSocket, 0);
    assertThat(body.size(), is(1));
    assertThat(body.data()[0], is((byte) 'b'));
    // Tear down the wiring.
    ZMQ.close(routerSocket);
    ZMQ.close(reqSocket);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 79 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TestReqRelaxed method bounce.

private void bounce(SocketBase socket) {
    boolean more = false;
    do {
        Msg msg = socket.recv(0);
        assertThat(msg, notNullValue());
        more = ZMQ.getSocketOption(socket, ZMQ.ZMQ_RCVMORE) > 0;
        socket.send(msg, more ? ZMQ.ZMQ_SNDMORE : 0);
    } while (more);
}
Also used : Msg(zmq.Msg)

Example 80 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class DistTest method testAttachedWhenSendingNoMoreMessage.

@Test
public void testAttachedWhenSendingNoMoreMessage() {
    Msg msg = new Msg();
    dist.sendToAll(msg);
    // no change in eligible/active states
    assertThat(dist.eligible(), is(1));
    assertThat(dist.active(), is(1));
    dist.attach(second);
    assertThat(dist.eligible(), is(2));
    // active should have changed, as we are NOT in the middle a sending a multi-part message
    assertThat(dist.active(), is(2));
    dist.sendToAll(msg);
    assertThat(dist.eligible(), is(2));
    assertThat(dist.active(), is(2));
    // no change in eligible/active states
    dist.activated(second);
    assertThat(dist.eligible(), is(2));
    assertThat(dist.active(), is(2));
}
Also used : Msg(zmq.Msg) 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