Search in sources :

Example 41 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class ManagedContext method close.

/*
     * This should only be called when SIGINT is received
     */
private void close() {
    lock.lock();
    try {
        for (SocketBase s : sockets) {
            try {
                s.setSocketOpt(ZMQ.ZMQ_LINGER, 0);
                s.close();
            } catch (Exception ignore) {
            }
        }
        sockets.clear();
    } finally {
        lock.unlock();
    }
}
Also used : SocketBase(zmq.SocketBase)

Example 42 with SocketBase

use of zmq.SocketBase 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 43 with SocketBase

use of zmq.SocketBase 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 44 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class TestReqCorrelateRelaxed method overallSetup.

/**
 * Prepares sockets and runs actual tests.
 *
 * Doing it this way so order is guaranteed.
 *
 * @throws Exception
 */
@Test
public void overallSetup() throws Exception {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    boolean brc = ZMQ.bind(dealer, "inproc://a");
    assertThat(brc, is(true));
    SocketBase reqClient = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(reqClient, notNullValue());
    reqClient.setSocketOpt(ZMQ.ZMQ_REQ_CORRELATE, 1);
    reqClient.setSocketOpt(ZMQ.ZMQ_REQ_RELAXED, 1);
    reqClient.setSocketOpt(ZMQ.ZMQ_RCVTIMEO, 100);
    brc = ZMQ.connect(reqClient, "inproc://a");
    assertThat(brc, is(true));
    // Test good path
    byte[] origRequestId = testReqSentFrames(dealer, reqClient);
    testReqRecvGoodRequestId(dealer, reqClient, origRequestId);
    // Test what happens when a bad request ID is sent back.
    origRequestId = testReqSentFrames(dealer, reqClient);
    testReqRecvBadRequestId(dealer, reqClient, origRequestId);
    ZMQ.close(reqClient);
    ZMQ.close(dealer);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 45 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class TestReqrepDevice method testReprepDevice.

// Create REQ/ROUTER wiring.
@Test
public void testReprepDevice() throws IOException {
    int routerPort = Utils.findOpenPort();
    int dealerPort = Utils.findOpenPort();
    boolean brc;
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    // Create a req/rep device.
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    brc = ZMQ.bind(dealer, "tcp://127.0.0.1:" + dealerPort);
    assertThat(brc, is(true));
    SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
    assertThat(router, notNullValue());
    brc = ZMQ.bind(router, "tcp://127.0.0.1:" + routerPort);
    assertThat(brc, is(true));
    // Create a worker.
    SocketBase rep = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(rep, notNullValue());
    brc = ZMQ.connect(rep, "tcp://127.0.0.1:" + dealerPort);
    assertThat(brc, is(true));
    SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(req, notNullValue());
    brc = ZMQ.connect(req, "tcp://127.0.0.1:" + routerPort);
    assertThat(brc, is(true));
    // Send a request.
    int rc;
    Msg msg;
    String buff;
    long rcvmore;
    rc = ZMQ.send(req, "ABC", ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(3));
    rc = ZMQ.send(req, "DEFG", 0);
    assertThat(rc, is(4));
    // Pass the request through the device.
    for (int i = 0; i != 4; i++) {
        msg = ZMQ.recvMsg(router, 0);
        assertThat(msg, notNullValue());
        rcvmore = ZMQ.getSocketOption(router, ZMQ.ZMQ_RCVMORE);
        rc = ZMQ.sendMsg(dealer, msg, rcvmore > 0 ? ZMQ.ZMQ_SNDMORE : 0);
        assertThat(rc >= 0, is(true));
    }
    // Receive the request.
    msg = ZMQ.recv(rep, 0);
    assertThat(msg.size(), is(3));
    buff = new String(msg.data(), ZMQ.CHARSET);
    assertThat(buff, is("ABC"));
    rcvmore = ZMQ.getSocketOption(rep, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore > 0, is(true));
    msg = ZMQ.recv(rep, 0);
    assertThat(msg.size(), is(4));
    buff = new String(msg.data(), ZMQ.CHARSET);
    assertThat(buff, is("DEFG"));
    rcvmore = ZMQ.getSocketOption(rep, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore, is(0L));
    // Send the reply.
    rc = ZMQ.send(rep, "GHIJKL", ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(6));
    rc = ZMQ.send(rep, "MN", 0);
    assertThat(rc, is(2));
    // Pass the reply through the device.
    for (int i = 0; i != 4; i++) {
        msg = ZMQ.recvMsg(dealer, 0);
        assertThat(msg, notNullValue());
        rcvmore = ZMQ.getSocketOption(dealer, ZMQ.ZMQ_RCVMORE);
        rc = ZMQ.sendMsg(router, msg, rcvmore > 0 ? ZMQ.ZMQ_SNDMORE : 0);
        assertThat(rc >= 0, is(true));
    }
    // Receive the reply.
    msg = ZMQ.recv(req, 0);
    assertThat(msg.size(), is(6));
    buff = new String(msg.data(), ZMQ.CHARSET);
    assertThat(buff, is("GHIJKL"));
    rcvmore = ZMQ.getSocketOption(req, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore > 0, is(true));
    msg = ZMQ.recv(req, 0);
    assertThat(msg.size(), is(2));
    buff = new String(msg.data(), ZMQ.CHARSET);
    assertThat(buff, is("MN"));
    rcvmore = ZMQ.getSocketOption(req, ZMQ.ZMQ_RCVMORE);
    assertThat(rcvmore, is(0L));
    // Clean up.
    ZMQ.close(req);
    ZMQ.close(rep);
    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

SocketBase (zmq.SocketBase)80 Ctx (zmq.Ctx)63 Test (org.junit.Test)55 Msg (zmq.Msg)37 ArrayList (java.util.ArrayList)10 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)3 ByteBuffer (java.nio.ByteBuffer)2 Curve (zmq.io.mechanism.curve.Curve)2 InputStream (java.io.InputStream)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 ZMQ (zmq.ZMQ)1 Event (zmq.ZMQ.Event)1