Search in sources :

Example 1 with SocketBase

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

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

use of zmq.SocketBase in project jeromq by zeromq.

the class ZFrame method recvFrame.

/**
 * Receive a new frame off the socket, Returns newly-allocated frame, or
 * null if there was no input waiting, or if the read was interrupted.
 * @param   socket
 *              Socket to read from
 * @param   flags
 *              Pass flags to 0MQ socket.recv call
 * @return
 *              received frame, else null
 */
public static ZFrame recvFrame(Socket socket, int flags) {
    final SocketBase base = socket.base();
    zmq.Msg msg = base.recv(flags);
    if (msg == null) {
        // Check to see if there was an error in recv
        socket.mayRaise();
        return null;
    }
    ZFrame frame = new ZFrame(msg);
    frame.setGroup(msg.getGroup());
    return frame;
}
Also used : SocketBase(zmq.SocketBase) Msg(zmq.Msg)

Example 4 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class ManagedContext method createSocket.

SocketBase createSocket(int type) {
    final SocketBase base = ctx.createSocket(type);
    lock.lock();
    try {
        sockets.add(base);
    } finally {
        lock.unlock();
    }
    return base;
}
Also used : SocketBase(zmq.SocketBase)

Example 5 with SocketBase

use of zmq.SocketBase in project jeromq by zeromq.

the class DealerSpecTest method roundRobinOut.

private void roundRobinOut(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
    SocketBase dealer = ZMQ.socket(ctx, bindType);
    boolean rc = ZMQ.bind(dealer, address);
    assertThat(rc, is(true));
    int timeout = 250;
    int services = 5;
    List<SocketBase> senders = new ArrayList<>();
    for (int peer = 0; peer < services; ++peer) {
        SocketBase reps = ZMQ.socket(ctx, connectType);
        assertThat(reps, notNullValue());
        senders.add(reps);
        rc = ZMQ.setSocketOption(reps, ZMQ.ZMQ_RCVTIMEO, timeout);
        assertThat(rc, is(true));
        String host = (String) ZMQ.getSocketOptionExt(dealer, ZMQ.ZMQ_LAST_ENDPOINT);
        assertThat(host, notNullValue());
        rc = ZMQ.connect(reps, host);
        assertThat(rc, is(true));
    }
    // Wait for connections.
    ZMQ.msleep(100);
    // Send all requests
    for (int peer = 0; peer < services; ++peer) {
        rc = sendSeq(dealer, null, "ABC");
        assertThat(rc, is(true));
    }
    // Expect every REP got one message
    for (int peer = 0; peer < services; ++peer) {
        recvSeq(senders.get(peer), "ABC");
    }
    ZMQ.closeZeroLinger(dealer);
    for (SocketBase sender : senders) {
        ZMQ.closeZeroLinger(sender);
    }
    // Wait for disconnects.
    ZMQ.msleep(100);
}
Also used : SocketBase(zmq.SocketBase) ArrayList(java.util.ArrayList)

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