Search in sources :

Example 1 with Event

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

Aggregations

InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Socket (java.net.Socket)1 ByteBuffer (java.nio.ByteBuffer)1 Ctx (zmq.Ctx)1 Msg (zmq.Msg)1 SocketBase (zmq.SocketBase)1 Event (zmq.ZMQ.Event)1