Search in sources :

Example 66 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class RouterMandatoryTest method testRouterMandatory.

@Test
public void testRouterMandatory() throws Exception {
    int sent;
    boolean rc;
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(router, notNullValue());
    rc = ZMQ.bind(router, "tcp://127.0.0.1:*");
    assertThat(rc, is(true));
    // Sending a message to an unknown peer with the default setting
    // This will not report any error
    sent = ZMQ.send(router, "UNKNOWN", ZMQ.ZMQ_SNDMORE);
    assertThat(sent, is(7));
    sent = ZMQ.send(router, "DATA", 0);
    assertThat(sent, is(4));
    // Send a message to an unknown peer with mandatory routing
    // This will fail
    int mandatory = 1;
    ZMQ.setSocketOption(router, ZMQ.ZMQ_ROUTER_MANDATORY, mandatory);
    // Send a message and check that it fails
    sent = ZMQ.send(router, "UNKNOWN", ZMQ.ZMQ_SNDMORE | ZMQ.ZMQ_DONTWAIT);
    assertThat(sent, is(-1));
    assertThat(router.errno(), is(ZError.EHOSTUNREACH));
    // Create dealer called "X" and connect it to our router
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    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));
    // Get message from dealer to know when connection is ready
    int ret = ZMQ.send(dealer, "Hello", 0);
    assertThat(ret, is(5));
    Msg msg = ZMQ.recv(router, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.data()[0], is((byte) 'X'));
    // Send a message to connected dealer now
    // It should work
    sent = ZMQ.send(router, "X", ZMQ.ZMQ_SNDMORE);
    assertThat(sent, is(1));
    sent = ZMQ.send(router, "Hello", 0);
    assertThat(sent, is(5));
    // 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)

Example 67 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class RouterSpecTest method fairQueueIn.

private void fairQueueIn(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
    // Server socket will accept connections
    SocketBase receiver = ZMQ.socket(ctx, bindType);
    assertThat(receiver, notNullValue());
    int timeout = 250;
    boolean rc = ZMQ.setSocketOption(receiver, ZMQ.ZMQ_RCVTIMEO, timeout);
    assertThat(rc, is(true));
    rc = ZMQ.bind(receiver, address);
    assertThat(rc, is(true));
    address = (String) ZMQ.getSocketOptionExt(receiver, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(address, notNullValue());
    int services = 5;
    List<SocketBase> senders = new ArrayList<>();
    for (int peer = 0; peer < services; ++peer) {
        SocketBase sender = ZMQ.socket(ctx, connectType);
        assertThat(sender, notNullValue());
        senders.add(sender);
        rc = ZMQ.setSocketOption(sender, ZMQ.ZMQ_RCVTIMEO, timeout);
        assertThat(rc, is(true));
        rc = ZMQ.setSocketOption(sender, ZMQ.ZMQ_IDENTITY, "A" + peer);
        assertThat(rc, is(true));
        rc = ZMQ.connect(sender, address);
        assertThat(rc, is(true));
    }
    rc = sendSeq(senders.get(0), "M");
    assertThat(rc, is(true));
    recvSeq(receiver, "A0", "M");
    rc = sendSeq(senders.get(0), "M");
    assertThat(rc, is(true));
    recvSeq(receiver, "A0", "M");
    Set<String> sum = new HashSet<>();
    // send N requests
    for (int peer = 0; peer < services; ++peer) {
        sendSeq(senders.get(peer), "M");
        sum.add("A" + peer);
    }
    // handle N requests
    for (int peer = 0; peer < services; ++peer) {
        Msg msg = ZMQ.recv(receiver, 0);
        assertThat(msg, notNullValue());
        assertThat(msg.size(), is(2));
        sum.remove(new String(msg.data(), ZMQ.CHARSET));
        recvSeq(receiver, "M");
    }
    assertThat(sum.size(), is(0));
    ZMQ.closeZeroLinger(receiver);
    for (SocketBase sender : senders) {
        ZMQ.closeZeroLinger(sender);
    }
    // Wait for disconnects.
    ZMQ.msleep(100);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 68 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TestReqCorrelateRelaxed method testReqRecvBadRequestId.

/**
 * Asserts that a response with a non-current request ID is ignored.
 *
 * @param dealer
 * @param reqClient
 * @param origRequestId
 * @throws Exception
 */
public void testReqRecvBadRequestId(SocketBase dealer, SocketBase reqClient, byte[] origRequestId) throws Exception {
    Msg badRequestId = new Msg("gobbledygook".getBytes());
    Msg goodRequestId = new Msg(origRequestId);
    Msg empty = new Msg();
    Msg badResponsePayload = new Msg("Bad response".getBytes());
    Msg goodResponsePayload = new Msg(PAYLOAD.getBytes());
    // Send response with bad request ID
    assertThat(ZMQ.send(dealer, badRequestId, ZMQ.ZMQ_SNDMORE), is(12));
    assertThat(ZMQ.send(dealer, empty, ZMQ.ZMQ_SNDMORE), is(0));
    assertThat(ZMQ.send(dealer, badResponsePayload, 0), is(badResponsePayload.size()));
    // Send response with good request ID
    assertThat(ZMQ.send(dealer, goodRequestId, ZMQ.ZMQ_SNDMORE), is(REQUEST_ID_LENGTH));
    assertThat(ZMQ.send(dealer, empty, ZMQ.ZMQ_SNDMORE), is(0));
    assertThat(ZMQ.send(dealer, goodResponsePayload, 0), is(goodResponsePayload.size()));
    // Receive response (payload only)
    Msg receivedResponsePayload = ZMQ.recv(reqClient, 0);
    assertThat(receivedResponsePayload, notNullValue());
    // Expecting PAYLOAD, not "Bad payload"
    byte[] buf = new byte[128];
    int payloadLen = receivedResponsePayload.getBytes(0, buf, 0, 128);
    assertThat(payloadLen, is(PAYLOAD.getBytes().length));
    byte[] receivedPayload = Arrays.copyOf(buf, payloadLen);
    assertThat(receivedPayload, is(PAYLOAD.getBytes()));
}
Also used : Msg(zmq.Msg)

Example 69 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TestReqCorrelateRelaxed method testReqSentFrames.

/**
 * Tests that the correct frames are sent by the REQ socket.
 *
 * @param dealer
 * @param reqClient
 * @throws Exception
 * @return the request ID that was received
 */
public byte[] testReqSentFrames(SocketBase dealer, SocketBase reqClient) throws Exception {
    // Send simple payload over REQ socket
    Msg request = new Msg(PAYLOAD.getBytes());
    assertThat(ZMQ.sendMsg(reqClient, request, 0), is(PAYLOAD.getBytes().length));
    // Verify that the right frames were sent: [request ID, empty frame, payload]
    // 1. request ID
    Msg receivedReqId = ZMQ.recv(dealer, 0);
    assertThat(receivedReqId, notNullValue());
    assertThat(receivedReqId.size(), is(REQUEST_ID_LENGTH));
    assertThat(receivedReqId.flags() & ZMQ.ZMQ_MORE, not(0));
    byte[] buf = new byte[128];
    int requestIdLen = receivedReqId.getBytes(0, buf, 0, 128);
    assertThat(requestIdLen, is(REQUEST_ID_LENGTH));
    byte[] requestId = Arrays.copyOf(buf, REQUEST_ID_LENGTH);
    // 2. empty frame
    Msg receivedEmpty = ZMQ.recv(dealer, 0);
    assertThat(receivedEmpty, notNullValue());
    assertThat(receivedEmpty.size(), is(0));
    assertThat(receivedEmpty.flags() & ZMQ.ZMQ_MORE, not(0));
    // 3. Payload
    Msg receivedPayload = ZMQ.recv(dealer, 0);
    assertThat(receivedPayload, notNullValue());
    assertThat(receivedPayload.size(), is(PAYLOAD.getBytes().length));
    assertThat(receivedPayload.flags() & ZMQ.ZMQ_MORE, is(0));
    int receivedPayloadLen = receivedPayload.getBytes(0, buf, 0, 128);
    assertThat(receivedPayloadLen, is(PAYLOAD.getBytes().length));
    assertThat(Arrays.equals(receivedPayload.data(), PAYLOAD.getBytes()), is(true));
    return requestId;
}
Also used : Msg(zmq.Msg)

Example 70 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TestReqCorrelateRelaxed method testReqRecvGoodRequestId.

/**
 * Test that REQ sockets with CORRELATE/RELAXED receive a response with
 * correct request ID correctly.
 *
 * @param dealer
 * @param reqClient
 * @param origRequestId the request ID that was sent by the REQ socket
 * earlier
 * @throws Exception
 */
public void testReqRecvGoodRequestId(SocketBase dealer, SocketBase reqClient, byte[] origRequestId) throws Exception {
    Msg requestId = new Msg(origRequestId);
    Msg empty = new Msg();
    Msg responsePayload = new Msg(PAYLOAD.getBytes());
    // Send response
    assertThat(ZMQ.send(dealer, requestId, ZMQ.ZMQ_SNDMORE), is(REQUEST_ID_LENGTH));
    assertThat(ZMQ.send(dealer, empty, ZMQ.ZMQ_SNDMORE), is(0));
    assertThat(ZMQ.send(dealer, responsePayload, 0), is(responsePayload.size()));
    // Receive response (payload only)
    Msg receivedResponsePayload = ZMQ.recv(reqClient, 0);
    assertThat(receivedResponsePayload, notNullValue());
    byte[] buf = new byte[128];
    int payloadLen = receivedResponsePayload.getBytes(0, buf, 0, 128);
    assertThat(payloadLen, is(PAYLOAD.getBytes().length));
}
Also used : Msg(zmq.Msg)

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