Search in sources :

Example 1 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class AbstractProtocolVersion method assertProtocolVersion.

protected byte[] assertProtocolVersion(int version, List<ByteBuffer> raws, String payload) throws IOException, InterruptedException {
    String host = "tcp://localhost:*";
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase receiver = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
    assertThat(receiver, notNullValue());
    boolean rc = ZMQ.setSocketOption(receiver, ZMQ.ZMQ_LINGER, 0);
    assertThat(rc, is(true));
    rc = ZMQ.monitorSocket(receiver, "inproc://monitor", ZMQ.ZMQ_EVENT_HANDSHAKE_PROTOCOL);
    assertThat(rc, is(true));
    SocketMonitor monitor = new SocketMonitor(ctx, "inproc://monitor");
    monitor.start();
    rc = ZMQ.bind(receiver, host);
    assertThat(rc, is(true));
    String ep = (String) ZMQ.getSocketOptionExt(receiver, ZMQ.ZMQ_LAST_ENDPOINT);
    int port = TestUtils.port(ep);
    Socket sender = new Socket("127.0.0.1", port);
    OutputStream out = sender.getOutputStream();
    for (ByteBuffer raw : raws) {
        out.write(raw.array());
    }
    Msg msg = ZMQ.recv(receiver, 0);
    assertThat(msg, notNullValue());
    assertThat(new String(msg.data(), ZMQ.CHARSET), is(payload));
    monitor.join();
    final Event event = monitor.events[0];
    assertThat(event, notNullValue());
    assertThat(event.event, is(ZMQ.ZMQ_EVENT_HANDSHAKE_PROTOCOL));
    assertThat((Integer) event.arg, is(version));
    InputStream in = sender.getInputStream();
    byte[] data = new byte[255];
    int read = in.read(data);
    sender.close();
    ZMQ.close(receiver);
    ZMQ.term(ctx);
    return Arrays.copyOf(data, read);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteBuffer(java.nio.ByteBuffer) Event(zmq.ZMQ.Event) Socket(java.net.Socket)

Example 2 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class LocalThr method main.

public static void main(String[] argv) {
    String bindTo;
    long messageCount;
    int messageSize;
    Ctx ctx;
    SocketBase s;
    boolean rc;
    long i;
    Msg msg;
    long watch;
    long elapsed;
    long throughput;
    double megabits;
    if (argv.length != 3) {
        printf("usage: local_thr <bind-to> <message-size> <message-count>\n");
        return;
    }
    bindTo = argv[0];
    messageSize = atoi(argv[1]);
    messageCount = atol(argv[2]);
    ctx = ZMQ.init(1);
    if (ctx == null) {
        printf("error in init");
        return;
    }
    s = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
    if (s == null) {
        printf("error in socket");
    }
    // Add your socket options here.
    // For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
    rc = ZMQ.bind(s, bindTo);
    if (!rc) {
        printf("error in bind: %s\n");
        return;
    }
    msg = ZMQ.recvMsg(s, 0);
    if (msg == null) {
        printf("error in recvmsg: %s\n");
        return;
    }
    watch = ZMQ.startStopwatch();
    for (i = 0; i != messageCount - 1; i++) {
        msg = ZMQ.recvMsg(s, 0);
        if (msg == null) {
            printf("error in recvmsg: %s\n");
            return;
        }
        if (ZMQ.msgSize(msg) != messageSize) {
            printf("message of incorrect size received " + ZMQ.msgSize(msg));
            return;
        }
    }
    elapsed = ZMQ.stopStopwatch(watch);
    if (elapsed == 0) {
        elapsed = 1;
    }
    throughput = (long) ((double) messageCount / (double) elapsed * 1000000L);
    megabits = (double) (throughput * messageSize * 8) / 1000000;
    printf("message elapsed: %.3f \n", (double) elapsed / 1000000L);
    printf("message size: %d [B]\n", (int) messageSize);
    printf("message count: %d\n", (int) messageCount);
    printf("mean throughput: %d [msg/s]\n", (int) throughput);
    printf("mean throughput: %.3f [Mb/s]\n", (double) megabits);
    ZMQ.close(s);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx)

Example 3 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class DealerSpecTest method testSpecDestroyQueueOnDisconnect.

@Test
@Ignore
public void testSpecDestroyQueueOnDisconnect() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
    // SHALL create a double queue when a peer connects to it. If this peer
    // disconnects, the DEALER socket SHALL destroy its double queue and SHALL
    // discard any messages it contains.
    // *** Test disabled until libzmq does this properly ***
    // test_destroy_queue_on_disconnect (ctx);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 4 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class DealerSpecTest method testSpecRoundRobinOut.

@Test
public void testSpecRoundRobinOut() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // SHALL route outgoing messages to available peers using a round-robin
        // strategy.
        roundRobinOut(ctx, bindAddress, ZMQ.ZMQ_DEALER, ZMQ.ZMQ_REP);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Example 5 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class DealerSpecTest method testSpecBlockOnSendNoPeers.

@Test
public void testSpecBlockOnSendNoPeers() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.createContext();
    List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
    for (String bindAddress : binds) {
        // SHALL block on sending, or return a suitable error, when it has no connected peers.
        blockOnSendNoPeers(ctx, bindAddress, ZMQ.ZMQ_DEALER);
    }
    ZMQ.term(ctx);
}
Also used : Ctx(zmq.Ctx) Test(org.junit.Test) AbstractSpecTest(zmq.socket.AbstractSpecTest)

Aggregations

Ctx (zmq.Ctx)81 Test (org.junit.Test)73 SocketBase (zmq.SocketBase)63 Msg (zmq.Msg)31 AbstractSpecTest (zmq.socket.AbstractSpecTest)16 OutputStream (java.io.OutputStream)5 Socket (java.net.Socket)5 Ignore (org.junit.Ignore)4 ExecutorService (java.util.concurrent.ExecutorService)3 ByteBuffer (java.nio.ByteBuffer)2 ArrayList (java.util.ArrayList)2 Curve (zmq.io.mechanism.curve.Curve)2 InputStream (java.io.InputStream)1 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 Selector (java.nio.channels.Selector)1 UUID (java.util.UUID)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1