Search in sources :

Example 71 with Msg

use of zmq.Msg 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)

Example 72 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class StreamTest method testStream2stream.

@Test
public void testStream2stream() throws IOException, InterruptedException {
    String host = "tcp://localhost:*";
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    // Set up listener STREAM.
    SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
    assertThat(bind, notNullValue());
    boolean rc = ZMQ.bind(bind, host);
    assertThat(rc, is(true));
    host = (String) ZMQ.getSocketOptionExt(bind, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    // Set up connection stream.
    SocketBase connect = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
    assertThat(connect, notNullValue());
    // Do the connection.
    rc = ZMQ.connect(connect, host);
    assertThat(rc, is(true));
    ZMQ.sleep(1);
    // Connecting sends a zero message
    // Server: First frame is identity, second frame is zero
    Msg msg = ZMQ.recv(bind, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size() > 0, is(true));
    msg = ZMQ.recv(bind, 0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(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)

Example 73 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class XpubXsubTest method testXPubSub.

@Test(timeout = 5000)
public void testXPubSub() {
    System.out.println("XPub - Sub");
    final Ctx ctx = zmq.ZMQ.createContext();
    assertThat(ctx, notNullValue());
    boolean rc;
    SocketBase sub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_SUB);
    rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_SUBSCRIBE, "topic");
    assertThat(rc, is(true));
    rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_SUBSCRIBE, "topix");
    assertThat(rc, is(true));
    SocketBase pub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XPUB);
    rc = zmq.ZMQ.bind(pub, "inproc://1");
    assertThat(rc, is(true));
    String endpoint = (String) ZMQ.getSocketOptionExt(pub, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(endpoint, notNullValue());
    rc = zmq.ZMQ.connect(sub, endpoint);
    assertThat(rc, is(true));
    System.out.print("Send.");
    rc = pub.send(new Msg("topic".getBytes(ZMQ.CHARSET)), ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(true));
    rc = pub.send(new Msg("hop".getBytes(ZMQ.CHARSET)), 0);
    assertThat(rc, is(true));
    System.out.print("Recv.");
    Msg msg = sub.recv(0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(5));
    msg = sub.recv(0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(3));
    rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_UNSUBSCRIBE, "topix");
    assertThat(rc, is(true));
    rc = pub.send(new Msg("topix".getBytes(ZMQ.CHARSET)), ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(true));
    rc = pub.send(new Msg("hop".getBytes(ZMQ.CHARSET)), 0);
    assertThat(rc, is(true));
    rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_RCVTIMEO, 500);
    assertThat(rc, is(true));
    msg = sub.recv(0);
    assertThat(msg, nullValue());
    System.out.print("End.");
    zmq.ZMQ.close(sub);
    for (int idx = 0; idx < 2; ++idx) {
        rc = pub.send(new Msg("topic abc".getBytes(ZMQ.CHARSET)), 0);
        assertThat(rc, is(true));
        ZMQ.msleep(10);
    }
    zmq.ZMQ.close(pub);
    zmq.ZMQ.term(ctx);
    System.out.println("Done.");
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 74 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class XpubXsubTest method testIssue476.

@Test(timeout = 5000)
public void testIssue476() throws InterruptedException, ExecutionException {
    System.out.println("Issue 476");
    final Ctx ctx = zmq.ZMQ.createContext();
    assertThat(ctx, notNullValue());
    boolean rc;
    final SocketBase proxyPub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XPUB);
    rc = proxyPub.bind("inproc://1");
    assertThat(rc, is(true));
    final SocketBase proxySub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XSUB);
    rc = proxySub.bind("inproc://2");
    assertThat(rc, is(true));
    final SocketBase ctrl = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_PAIR);
    rc = ctrl.bind("inproc://ctrl-proxy");
    assertThat(rc, is(true));
    ExecutorService service = Executors.newFixedThreadPool(1);
    Future<?> proxy = service.submit(() -> {
        ZMQ.proxy(proxySub, proxyPub, null, ctrl);
    });
    SocketBase sub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_SUB);
    rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_SUBSCRIBE, "topic");
    assertThat(rc, is(true));
    rc = zmq.ZMQ.connect(sub, "inproc://1");
    assertThat(rc, is(true));
    SocketBase pub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XPUB);
    rc = zmq.ZMQ.connect(pub, "inproc://2");
    assertThat(rc, is(true));
    rc = zmq.ZMQ.setSocketOption(sub, ZMQ.ZMQ_RCVTIMEO, 100);
    assertThat(rc, is(true));
    sub.recv(0);
    System.out.print("Send.");
    rc = pub.send(new Msg("topic".getBytes(ZMQ.CHARSET)), ZMQ.ZMQ_SNDMORE);
    assertThat(rc, is(true));
    rc = pub.send(new Msg("hop".getBytes(ZMQ.CHARSET)), 0);
    assertThat(rc, is(true));
    System.out.print("Recv.");
    Msg msg = sub.recv(0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(5));
    msg = sub.recv(0);
    assertThat(msg, notNullValue());
    assertThat(msg.size(), is(3));
    System.out.print("End.");
    zmq.ZMQ.close(sub);
    for (int idx = 0; idx < 2; ++idx) {
        rc = pub.send(new Msg("topic abc".getBytes(ZMQ.CHARSET)), 0);
        assertThat(rc, is(true));
        ZMQ.msleep(10);
    }
    zmq.ZMQ.close(pub);
    final SocketBase command = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_PAIR);
    rc = command.connect("inproc://ctrl-proxy");
    assertThat(rc, is(true));
    command.send(new Msg(ZMQ.PROXY_TERMINATE), 0);
    proxy.get();
    zmq.ZMQ.close(command);
    zmq.ZMQ.close(proxyPub);
    zmq.ZMQ.close(proxySub);
    zmq.ZMQ.close(ctrl);
    zmq.ZMQ.term(ctx);
    System.out.println("Done.");
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 75 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class AbstractSpecTest method sendSeq.

public boolean sendSeq(SocketBase socket, String... data) {
    int rc = 0;
    if (data.length == 0) {
        return false;
    }
    for (int idx = 0; idx < data.length; ++idx) {
        String payload = data[idx];
        if (payload == null) {
            rc = ZMQ.send(socket, new Msg(), idx == data.length - 1 ? 0 : ZMQ.ZMQ_SNDMORE);
            assertThat(rc < 0, is(false));
        } else {
            Msg msg = new Msg(payload.getBytes(ZMQ.CHARSET));
            rc = ZMQ.send(socket, msg, idx == data.length - 1 ? 0 : ZMQ.ZMQ_SNDMORE);
            assertThat(rc < 0, is(false));
        }
    }
    return rc >= 0;
}
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