Search in sources :

Example 36 with SocketBase

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

Example 37 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class TestPairIpc method testPairIpc.

// Create REQ/ROUTER wiring.
@Test(timeout = 5000)
public void testPairIpc() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase pairBind = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(pairBind, notNullValue());
    UUID random;
    do {
        random = UUID.randomUUID();
    } while (!ZMQ.bind(pairBind, "ipc:///tmp/tester/" + random.toString()));
    SocketBase pairConnect = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
    assertThat(pairConnect, notNullValue());
    boolean brc = ZMQ.connect(pairConnect, "ipc:///tmp/tester/" + random.toString());
    assertThat(brc, is(true));
    Helper.bounce(pairBind, pairConnect);
    // Tear down the wiring.
    ZMQ.close(pairBind);
    ZMQ.close(pairConnect);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) UUID(java.util.UUID) Test(org.junit.Test)

Example 38 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class ConflateTest method test.

@Test
public void test() throws IOException, InterruptedException {
    Ctx ctx = ZMQ.init(1);
    assert (ctx != null);
    SocketBase in = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
    assert (in != null);
    String host = "tcp://localhost:*";
    int conflate = 1;
    ZMQ.setSocketOption(in, ZMQ.ZMQ_CONFLATE, conflate);
    boolean rc = ZMQ.bind(in, host);
    assert (rc);
    SocketBase out = ZMQ.socket(ctx, ZMQ.ZMQ_PUSH);
    assert (out != null);
    String ep = (String) ZMQ.getSocketOptionExt(in, ZMQ.ZMQ_LAST_ENDPOINT);
    rc = ZMQ.connect(out, ep);
    assert (rc);
    int messageCount = 20;
    for (int j = 0; j < messageCount; ++j) {
        int count = Helper.send(out, Integer.toString(j));
        assert (count > 0);
    }
    Thread.sleep(200);
    String recvd = Helper.recv(in);
    Assert.assertEquals("19", recvd);
    ZMQ.close(in);
    ZMQ.close(out);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 39 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class TestReqrepInproc method testReqrepInproc.

// Create REQ/ROUTER wiring.
@Test
public void testReqrepInproc() {
    Ctx ctx = ZMQ.init(1);
    assertThat(ctx, notNullValue());
    SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
    assertThat(sb, notNullValue());
    boolean brc = ZMQ.bind(sb, "inproc://a");
    assertThat(brc, is(true));
    SocketBase sc = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
    assertThat(sc, notNullValue());
    brc = ZMQ.connect(sc, "inproc://a");
    assertThat(brc, is(true));
    Helper.bounce(sb, sc);
    // Tear down the wiring.
    ZMQ.close(sb);
    ZMQ.close(sc);
    ZMQ.term(ctx);
}
Also used : SocketBase(zmq.SocketBase) Ctx(zmq.Ctx) Test(org.junit.Test)

Example 40 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class ZFrame method send.

/**
 * Internal method to call org.zeromq.Socket send() method.
 * @param socket
 *          0MQ socket to send on
 * @param flags
 *          Valid send() method flags, defined in org.zeromq.ZMQ class
 * @return
 *          True if success, else False
 */
public boolean send(Socket socket, int flags) {
    Utils.checkArgument(socket != null, "socket parameter must be set");
    final SocketBase base = socket.base();
    final zmq.Msg msg = new Msg(data);
    if (group != null) {
        msg.setGroup(group);
    }
    int sendFlags = (flags & ZFrame.MORE) == ZFrame.MORE ? zmq.ZMQ.ZMQ_SNDMORE : 0;
    sendFlags |= (flags & ZFrame.DONTWAIT) == ZFrame.DONTWAIT ? zmq.ZMQ.ZMQ_DONTWAIT : 0;
    // Only set the routerId if the socket is a ZMQ_Server
    if (base instanceof zmq.socket.clientserver.Server) {
        msg.setRoutingId(this.routingId);
    }
    return base.send(msg, sendFlags);
}
Also used : Msg(zmq.Msg) SocketBase(zmq.SocketBase) Msg(zmq.Msg)

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