Search in sources :

Example 56 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TrieTest method testAddRemoveMultiNodesBelowLevel.

@Test
public void testAddRemoveMultiNodesBelowLevel() {
    Trie trie = new Trie();
    boolean rc = trie.add(prefix, 0, prefix.size());
    assertThat(rc, is(true));
    byte[] abv = Arrays.copyOf(prefix.data(), prefix.size());
    abv[1] = 0;
    Msg above = new Msg(abv);
    rc = trie.add(above, 0, above.size());
    assertThat(rc, is(true));
    abv[1] = -1;
    above = new Msg(abv);
    rc = trie.add(above, 0, above.size());
    assertThat(rc, is(true));
    rc = trie.rm(prefix, 0, prefix.size());
    assertThat(rc, is(true));
    abv[1] = 0;
    above = new Msg(abv);
    rc = trie.rm(above, 0, above.size());
    assertThat(rc, is(true));
    abv[1] = -1;
    above = new Msg(abv);
    rc = trie.rm(above, 0, above.size());
    assertThat(rc, is(true));
}
Also used : Msg(zmq.Msg) Test(org.junit.Test)

Example 57 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class TrieTest method testAddRemoveMultiNodesAboveLevel.

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

Example 58 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class XPubNodropTest method testXpubNoDrop.

// Create REQ/ROUTER wiring.
@Test
public void testXpubNoDrop() throws IOException {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    // Create a publisher
    SocketBase pubBind = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
    assertThat(pubBind, notNullValue());
    boolean rc = ZMQ.bind(pubBind, "inproc://soname");
    assertThat(rc, is(true));
    // set pub socket options
    ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_XPUB_NODROP, 1);
    int hwm = 2000;
    ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_SNDHWM, hwm);
    // Create a subscriber
    SocketBase subConnect = ZMQ.socket(ctx, ZMQ.ZMQ_SUB);
    assertThat(subConnect, notNullValue());
    rc = ZMQ.connect(subConnect, "inproc://soname");
    assertThat(rc, is(true));
    // Subscribe for all messages.
    ZMQ.setSocketOption(subConnect, ZMQ.ZMQ_SUBSCRIBE, "");
    int hwmlimit = hwm - 1;
    int sendCount = 0;
    // Send an empty message
    for (int i = 0; i < hwmlimit; i++) {
        int ret = ZMQ.send(pubBind, "", 0);
        assert (ret == 0);
        sendCount++;
    }
    int recvCount = 0;
    Msg msg = null;
    do {
        // Receive the message in the subscriber
        msg = ZMQ.recv(subConnect, ZMQ.ZMQ_DONTWAIT);
        if (msg != null) {
            recvCount++;
        }
    } while (msg != null);
    assertThat(sendCount, is(recvCount));
    // Now test real blocking behavior
    // Set a timeout, default is infinite
    int timeout = 0;
    ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_SNDTIMEO, timeout);
    recvCount = 0;
    sendCount = 0;
    hwmlimit = hwm;
    while (ZMQ.send(pubBind, "", 0) == 0) {
        sendCount++;
    }
    assertThat(pubBind.errno(), is(ZError.EAGAIN));
    while (ZMQ.recv(subConnect, ZMQ.ZMQ_DONTWAIT) != null) {
        recvCount++;
    }
    assertThat(sendCount, is(recvCount));
    // Tear down the wiring.
    ZMQ.close(pubBind);
    ZMQ.close(subConnect);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 59 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class XpubXsubTest method testXPubXSub.

@Test(timeout = 5000)
public void testXPubXSub() {
    System.out.println("XPub - XSub");
    final Ctx ctx = zmq.ZMQ.createContext();
    assertThat(ctx, notNullValue());
    boolean rc;
    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());
    SocketBase sub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XSUB);
    rc = zmq.ZMQ.connect(sub, endpoint);
    assertThat(rc, is(true));
    System.out.print("Send.");
    rc = sub.send(new Msg("\1topic".getBytes(ZMQ.CHARSET)), 0);
    assertThat(rc, is(true));
    rc = pub.send(new Msg("topic".getBytes(ZMQ.CHARSET)), 0);
    assertThat(rc, is(true));
    System.out.print("Recv.");
    // msg = sub.recv(0);
    // assertThat(msg.size(), is(5));
    // 
    // msg = sub.recv(0);
    // assertThat(msg.size(), is(3));
    // 
    rc = sub.send(new Msg("\0topic".getBytes(ZMQ.CHARSET)), 0);
    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());
    zmq.ZMQ.close(sub);
    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 60 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class ProxySingleSocketTest method testProxySingleSocket.

@Test
public void testProxySingleSocket() throws IOException, InterruptedException {
    int port = Utils.findOpenPort();
    String host = "tcp://127.0.0.1:" + port;
    // The main thread simply starts several clients and a server, and then
    // waits for the server to finish.
    Ctx ctx = ZMQ.createContext();
    SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(req, notNullValue());
    boolean rc = ZMQ.connect(req, host);
    assertThat(rc, is(true));
    // Control socket receives terminate command from main over inproc
    SocketBase control = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
    rc = ZMQ.bind(control, "inproc://control");
    assertThat(rc, is(true));
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.submit(new ServerTask(ctx, host));
    int ret = ZMQ.send(req, "msg1", 0);
    assertThat(ret, is(4));
    System.out.print(".");
    Msg msg = ZMQ.recv(req, 0);
    System.out.print(".");
    assertThat(msg, notNullValue());
    assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg1"));
    ret = ZMQ.send(req, "msg22", 0);
    assertThat(ret, is(5));
    System.out.print(".");
    msg = ZMQ.recv(req, 0);
    System.out.print(".");
    assertThat(msg, notNullValue());
    assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg22"));
    ret = ZMQ.send(control, ZMQ.PROXY_TERMINATE, 0);
    assertThat(ret, is(9));
    System.out.println(".");
    ZMQ.close(control);
    ZMQ.close(req);
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) ExecutorService(java.util.concurrent.ExecutorService) 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