Search in sources :

Example 16 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class MetadataTest method testMetadata.

@Test
public void testMetadata() throws IOException, InterruptedException {
    int port = Utils.findOpenPort();
    String host = "tcp://127.0.0.1:" + port;
    Ctx ctx = ZMQ.createContext();
    // Spawn ZAP handler
    // We create and bind ZAP socket in main thread to avoid case
    // where child thread does not start up fast enough.
    SocketBase handler = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(handler, notNullValue());
    boolean rc = ZMQ.bind(handler, "inproc://zeromq.zap.01");
    assertThat(rc, is(true));
    Thread thread = new Thread(new ZapHandler(handler));
    thread.start();
    // Server socket will accept connections
    SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(server, notNullValue());
    SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
    assertThat(client, notNullValue());
    ZMQ.setSocketOption(server, ZMQ.ZMQ_ZAP_DOMAIN, "DOMAIN");
    rc = ZMQ.bind(server, host);
    assertThat(rc, is(true));
    rc = ZMQ.connect(client, host);
    assertThat(rc, is(true));
    int ret = ZMQ.send(client, "This is a message", 0);
    assertThat(ret, is(17));
    Msg msg = ZMQ.recv(server, 0);
    assertThat(msg, notNullValue());
    String prop = ZMQ.getMessageMetadata(msg, "Socket-Type");
    assertThat(prop, is("DEALER"));
    prop = ZMQ.getMessageMetadata(msg, "User-Id");
    assertThat(prop, is("anonymous"));
    prop = ZMQ.getMessageMetadata(msg, "Peer-Address");
    assertThat(prop.startsWith("127.0.0.1:"), is(true));
    prop = ZMQ.getMessageMetadata(msg, "no such");
    assertThat(prop, nullValue());
    prop = ZMQ.getMessageMetadata(msg, "Hello");
    assertThat(prop, is("World"));
    ZMQ.closeZeroLinger(server);
    ZMQ.closeZeroLinger(client);
    // Shutdown
    ZMQ.term(ctx);
    // Wait until ZAP handler terminates
    thread.join();
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 17 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class SecurityCurveTest method testPlainCurveKeys.

@Test
public void testPlainCurveKeys() throws Exception {
    byte[][] serverKeyPair = new Curve().keypair();
    byte[] serverPublicKey = serverKeyPair[0];
    byte[] serverSecretKey = serverKeyPair[1];
    byte[][] clientKeyPair = new Curve().keypair();
    byte[] clientPublicKey = clientKeyPair[0];
    byte[] clientSecretKey = clientKeyPair[1];
    Ctx context = ZMQ.createContext();
    SocketBase server = context.createSocket(ZMQ.ZMQ_DEALER);
    ZMQ.setSocketOption(server, ZMQ.ZMQ_CURVE_SERVER, true);
    ZMQ.setSocketOption(server, ZMQ.ZMQ_CURVE_SECRETKEY, serverSecretKey);
    server.bind(connectionString);
    SocketBase client = context.createSocket(ZMQ.ZMQ_DEALER);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SERVERKEY, serverPublicKey);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_PUBLICKEY, clientPublicKey);
    ZMQ.setSocketOption(client, ZMQ.ZMQ_CURVE_SECRETKEY, clientSecretKey);
    client.connect(connectionString);
    byte[] testBytes = "hello-world".getBytes();
    ZMQ.send(client, testBytes, 0);
    byte[] recv = ZMQ.recv(server, 0).data();
    assertThat(recv, is(equalTo(testBytes)));
    server.close();
    client.close();
    context.terminate();
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Curve(zmq.io.mechanism.curve.Curve) Test(org.junit.Test)

Example 18 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class ProxyTest method testProxy.

@Test(timeout = 10000)
public void testProxy() throws Throwable {
    // The main thread simply starts several clients and a server, and then
    // waits for the server to finish.
    Ctx ctx = ZMQ.createContext();
    String controlEndpoint = "tcp://localhost:" + Utils.findOpenPort();
    // Control socket receives terminate command from main over inproc
    SocketBase control = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
    assertThat(control, notNullValue());
    boolean rc = ZMQ.bind(control, controlEndpoint);
    assertThat(rc, is(true));
    String host = "tcp://127.0.0.1:" + Utils.findOpenPort();
    int count = 5;
    ExecutorService executor = Executors.newFixedThreadPool(count + 1);
    Server server = new Server(host, controlEndpoint, false);
    Future<Boolean> fserver = executor.submit(server);
    server.started.await();
    List<Future<Boolean>> clientsf = new ArrayList<>(count);
    for (int idx = 0; idx < count; ++idx) {
        Client client = new Client(host, controlEndpoint, false);
        clientsf.add(executor.submit(client));
        client.started.await();
    }
    while (true) {
        Thread.sleep(100);
        int sent = ZMQ.send(control, ZMQ.PROXY_TERMINATE, 0);
        assertThat(sent, is(9));
        if (clientsf.get(4).isDone()) {
            break;
        }
    }
    for (Future<Boolean> client : clientsf) {
        try {
            assertThat(client.get(), is(true));
        } catch (ExecutionException e) {
            e.getCause().printStackTrace();
            throw e.getCause();
        }
    }
    ZMQ.close(control);
    executor.shutdown();
    if (!executor.awaitTermination(4, TimeUnit.SECONDS)) {
        executor.shutdownNow();
        fail("Hanged tasks");
    }
    ZMQ.term(ctx);
    try {
        assertThat(fserver.get(), is(true));
    } catch (ExecutionException e) {
        throw e.getCause();
    }
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) ArrayList(java.util.ArrayList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 19 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestPairTcp method testPairMonitorBindConnect.

@Test
public void testPairMonitorBindConnect() throws InterruptedException, IOException {
    int port = Utils.findOpenPort();
    String host = "tcp://127.0.0.1:" + port;
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(bind, notNullValue());
    boolean rc = ZMQ.bind(bind, host);
    assertThat(rc, is(true));
    SocketBase connect = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(connect, notNullValue());
    rc = ZMQ.connect(connect, host);
    assertThat(rc, is(true));
    Helper.bounce(bind, connect);
    SocketBase monitor = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(monitor, notNullValue());
    rc = ZMQ.monitorSocket(connect, "inproc://events", ZMQ.ZMQ_EVENT_ALL);
    assertThat(rc, is(true));
    rc = ZMQ.bind(monitor, "inproc://events");
    assertThat(rc, is(false));
    rc = ZMQ.connect(monitor, "inproc://events");
    assertThat(rc, is(true));
    // Tear down the wiring.
    ZMQ.close(bind);
    ZMQ.close(connect);
    ZMQ.close(monitor);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 20 with Ctx

use of zmq.Ctx in project jeromq by zeromq.

the class TestPairTcp method testPairConnectSecondClientIssue285.

@Test
public void testPairConnectSecondClientIssue285() throws IOException {
    String host = "tcp://127.0.0.1:*";
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(bind, notNullValue());
    boolean brc = ZMQ.bind(bind, host);
    assertThat(brc, is(true));
    host = (String) ZMQ.getSocketOptionExt(bind, ZMQ.ZMQ_LAST_ENDPOINT);
    assertThat(host, notNullValue());
    SocketBase first = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(first, notNullValue());
    brc = ZMQ.connect(first, host);
    assertThat(brc, is(true));
    Helper.bounce(bind, first);
    SocketBase second = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(second, notNullValue());
    brc = ZMQ.connect(second, host);
    assertThat(brc, is(true));
    int ret = ZMQ.send(bind, "data", 0);
    assertThat(ret, is(4));
    ret = ZMQ.send(bind, "datb", 0);
    assertThat(ret, is(4));
    ret = ZMQ.send(bind, "datc", 0);
    assertThat(ret, is(4));
    ZMQ.msleep(100);
    // no receiving from second connected pair
    Msg msg = ZMQ.recv(second, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, nullValue());
    // receiving from first connected pair
    msg = ZMQ.recv(first, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is("data".getBytes(ZMQ.CHARSET)));
    msg = ZMQ.recv(first, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is("datb".getBytes(ZMQ.CHARSET)));
    msg = ZMQ.recv(first, ZMQ.ZMQ_DONTWAIT);
    assertThat(msg, notNullValue());
    assertThat(msg.data(), is("datc".getBytes(ZMQ.CHARSET)));
    // Tear down the wiring.
    ZMQ.close(bind);
    ZMQ.close(first);
    ZMQ.close(second);
    ZMQ.term(ctx);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

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