Search in sources :

Example 31 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class StreamTest method testStream2dealer.

@Test
public void testStream2dealer() throws IOException, InterruptedException {
    final byte[] standardGreeting = new byte[64];
    standardGreeting[0] = (byte) 0xff;
    standardGreeting[8] = 1;
    standardGreeting[9] = 0x7f;
    standardGreeting[10] = 3;
    standardGreeting[12] = 'N';
    standardGreeting[13] = 'U';
    standardGreeting[14] = 'L';
    standardGreeting[15] = 'L';
    String host = "tcp://localhost:*";
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    // We'll be using this socket in raw mode
    SocketBase stream = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
    assertThat(stream, notNullValue());
    boolean rc = ZMQ.setSocketOption(stream, ZMQ.ZMQ_LINGER, 0);
    assertThat(rc, is(true));
    rc = ZMQ.bind(stream, host);
    assertThat(rc, is(true));
    host = (String) ZMQ.getSocketOptionExt(stream, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    // We'll be using this socket as the other peer
    SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(dealer, notNullValue());
    rc = ZMQ.setSocketOption(dealer, ZMQ.ZMQ_LINGER, 0);
    assertThat(rc, is(true));
    rc = ZMQ.connect(dealer, host);
    assertThat(rc, is(true));
    // Send a message on the dealer socket
    int ret = ZMQ.send(dealer, "Hello", 0);
    assertThat(ret, is(5));
    // Connecting sends a zero message
    // First frame is identity
    Msg id = ZMQ.recv(stream, 0);
    assertThat(id, notNullValue());
    assertThat(id.hasMore(), is(true));
    // Verify the existence of Peer-Address metadata
    assertThat(id.getMetadata().get("Peer-Address").startsWith("127.0.0.1:"), is(true));
    // Second frame is zero
    Msg zero = ZMQ.recv(stream, 0);
    assertThat(zero, notNullValue());
    assertThat(zero.size(), is(0));
    // Real data follows
    // First frame is identity
    id = ZMQ.recv(stream, 0);
    assertThat(id, notNullValue());
    assertThat(id.hasMore(), is(true));
    // Verify the existence of Peer-Address metadata
    assertThat(id.getMetadata().get("Peer-Address").startsWith("127.0.0.1:"), is(true));
    // Second frame is greeting signature
    Msg greeting = ZMQ.recv(stream, 0);
    assertThat(greeting, notNullValue());
    assertThat(greeting.size(), is(10));
    assertThat(greeting.data(), is(new byte[] { (byte) 0xff, 0, 0, 0, 0, 0, 0, 0, 1, 0x7f }));
    // Send our own protocol greeting
    ret = ZMQ.send(stream, id, ZMQ.ZMQ_SNDMORE);
    ret = ZMQ.send(stream, standardGreeting, standardGreeting.length, 0);
    assertThat(ret, is(standardGreeting.length));
    // Now we expect the data from the DEALER socket
    // We want the rest of greeting along with the Ready command
    int bytesRead = 0;
    ByteBuffer read = ByteBuffer.allocate(100);
    while (bytesRead < 97) {
        // First frame is the identity of the connection (each time)
        Msg msg = ZMQ.recv(stream, 0);
        assertThat(msg, notNullValue());
        assertThat(msg.hasMore(), is(true));
        // Second frame contains the next chunk of data
        msg = ZMQ.recv(stream, 0);
        assertThat(msg, notNullValue());
        bytesRead += msg.size();
        read.put(msg.buf());
    }
    assertThat(read.get(0), is((byte) 3));
    assertThat(read.get(1), is((byte) 0));
    assertThat(read.get(2), is((byte) 'N'));
    assertThat(read.get(3), is((byte) 'U'));
    assertThat(read.get(4), is((byte) 'L'));
    assertThat(read.get(5), is((byte) 'L'));
    for (int idx = 0; idx < 16; ++idx) {
        assertThat(read.get(2 + 4 + idx), is((byte) 0));
    }
    assertThat(read.get(54), is((byte) 4));
    // octal notation, yes
    assertThat(read.get(55), is((byte) 051));
    assertThat(read.get(56), is((byte) 5));
    assertThat(read.get(57), is((byte) 'R'));
    assertThat(read.get(58), is((byte) 'E'));
    assertThat(read.get(59), is((byte) 'A'));
    assertThat(read.get(60), is((byte) 'D'));
    assertThat(read.get(61), is((byte) 'Y'));
    assertThat(read.get(62), is((byte) 013));
    assertThat(read.get(63), is((byte) 'S'));
    assertThat(read.get(64), is((byte) 'o'));
    assertThat(read.get(65), is((byte) 'c'));
    assertThat(read.get(66), is((byte) 'k'));
    assertThat(read.get(67), is((byte) 'e'));
    assertThat(read.get(68), is((byte) 't'));
    assertThat(read.get(69), is((byte) '-'));
    assertThat(read.get(70), is((byte) 'T'));
    assertThat(read.get(71), is((byte) 'y'));
    assertThat(read.get(72), is((byte) 'p'));
    assertThat(read.get(73), is((byte) 'e'));
    assertThat(read.get(74), is((byte) 0));
    assertThat(read.get(75), is((byte) 0));
    assertThat(read.get(76), is((byte) 0));
    assertThat(read.get(77), is((byte) 6));
    assertThat(read.get(78), is((byte) 'D'));
    assertThat(read.get(79), is((byte) 'E'));
    assertThat(read.get(80), is((byte) 'A'));
    assertThat(read.get(81), is((byte) 'L'));
    assertThat(read.get(82), is((byte) 'E'));
    assertThat(read.get(83), is((byte) 'R'));
    assertThat(read.get(84), is((byte) 010));
    assertThat(read.get(85), is((byte) 'I'));
    assertThat(read.get(86), is((byte) 'd'));
    assertThat(read.get(87), is((byte) 'e'));
    assertThat(read.get(88), is((byte) 'n'));
    assertThat(read.get(89), is((byte) 't'));
    assertThat(read.get(90), is((byte) 'i'));
    assertThat(read.get(91), is((byte) 't'));
    assertThat(read.get(92), is((byte) 'y'));
    assertThat(read.get(93), is((byte) 0));
    assertThat(read.get(94), is((byte) 0));
    assertThat(read.get(95), is((byte) 0));
    assertThat(read.get(96), is((byte) 0));
    // Send Ready command
    ZMQ.send(stream, id, ZMQ.ZMQ_SNDMORE);
    ZMQ.send(stream, new byte[] { 4, 41, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e', 't', '-', 'T', 'y', 'p', 'e', 0, 0, 0, 6, 'R', 'O', 'U', 'T', 'E', 'R', 8, 'I', 'd', 'e', 'n', 't', 'i', 't', 'y', 0, 0, 0, 0 }, 0);
    // Now we expect the data from the DEALER socket
    // First frame is, again, the identity of the connection
    id = ZMQ.recv(stream, 0);
    assertThat(id, notNullValue());
    assertThat(id.hasMore(), is(true));
    // Third frame contains Hello message from DEALER
    Msg un = ZMQ.recv(stream, 0);
    assertThat(un, notNullValue());
    assertThat(un.size(), is(7));
    // Then we have a 5-byte message "Hello"
    assertThat(un.get(0), is((byte) 0));
    assertThat(un.get(1), is((byte) 5));
    assertThat(un.get(2), is((byte) 'H'));
    assertThat(un.get(3), is((byte) 'e'));
    assertThat(un.get(4), is((byte) 'l'));
    assertThat(un.get(5), is((byte) 'l'));
    assertThat(un.get(6), is((byte) 'o'));
    // Send "World" back to DEALER
    ZMQ.send(stream, id, ZMQ.ZMQ_SNDMORE);
    ret = ZMQ.send(stream, new byte[] { 0, 5, 'W', 'o', 'r', 'l', 'd' }, 0);
    assertThat(ret, is(7));
    // Expect response on DEALER socket
    Msg recv = ZMQ.recv(dealer, 0);
    assertThat(recv.size(), is(5));
    assertThat(recv.data(), is("World".getBytes(ZMQ.CHARSET)));
    ZMQ.close(stream);
    ZMQ.close(dealer);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 32 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class CustomEncoderTest method testAssignWrongCustomEncoder.

@SuppressWarnings("deprecation")
@Test(expected = ZError.InstantiationException.class)
public void testAssignWrongCustomEncoder() {
    Ctx ctx = ZMQ.createContext();
    SocketBase socket = ctx.createSocket(ZMQ.ZMQ_PAIR);
    try {
        socket.setSocketOpt(ZMQ.ZMQ_ENCODER, WrongEncoder.class);
    } finally {
        ZMQ.close(socket);
        ZMQ.term(ctx);
    }
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 33 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class CustomEncoderTest method testAssignCustomEncoder.

@SuppressWarnings("deprecation")
@Test
public void testAssignCustomEncoder() {
    Ctx ctx = ZMQ.createContext();
    SocketBase socket = ctx.createSocket(ZMQ.ZMQ_PAIR);
    boolean rc = socket.setSocketOpt(ZMQ.ZMQ_ENCODER, CustomEncoder.class);
    assertThat(rc, is(true));
    ZMQ.close(socket);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 34 with SocketBase

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

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

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