Search in sources :

Example 21 with Msg

use of zmq.Msg 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 22 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class AbstractDecoderTest method testReader.

@Test
public void testReader() {
    ByteBuffer in = decoder.getBuffer();
    int insize = readShortMessage(in);
    assertThat(insize, is(7));
    in.flip();
    ValueReference<Integer> process = new ValueReference<>(0);
    decoder.decode(in, insize, process);
    assertThat(process.get(), is(7));
    Msg msg = decoder.msg();
    assertThat(msg, notNullValue());
    assertThat(msg.flags(), is(1));
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer) ValueReference(zmq.util.ValueReference) Test(org.junit.Test)

Example 23 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class AbstractDecoderTest method testReaderLong.

@Test
public void testReaderLong() {
    ByteBuffer in = decoder.getBuffer();
    int insize = readLongMessage1(in);
    assertThat(insize, is(64));
    in.flip();
    ValueReference<Integer> process = new ValueReference<>(0);
    decoder.decode(in, insize, process);
    assertThat(process.get(), is(64));
    in = decoder.getBuffer();
    assertThat(in.capacity(), is(200));
    assertThat(in.position(), is(62));
    in.put("23456789".getBytes(ZMQ.CHARSET));
    insize = readLongMessage2(in);
    assertThat(insize, is(200));
    decoder.decode(in, 138, process);
    assertThat(process.get(), is(138));
    assertThat(in.array()[199], is((byte) 'x'));
    Msg msg = decoder.msg();
    assertThat(msg, notNullValue());
    byte last = msg.data()[199];
    assertThat(last, is((byte) 'x'));
    assertThat(msg.size(), is(200));
    assertThat(msg.flags(), is(1));
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer) ValueReference(zmq.util.ValueReference) Test(org.junit.Test)

Example 24 with Msg

use of zmq.Msg in project jeromq by zeromq.

the class AbstractDecoderTest method testReaderExtraLong.

@Test
public void testReaderExtraLong() {
    ByteBuffer in = decoder.getBuffer();
    int insize = readExtraLongMessage(in);
    // assertThat(insize, is(62));
    in.flip();
    ValueReference<Integer> process = new ValueReference<>(0);
    decoder.decode(in, insize, process);
    assertThat(process.get(), is(insize));
    in = decoder.getBuffer();
    assertThat(in.capacity(), is(330));
    assertThat(in.position(), is(52));
    in.put("23456789".getBytes(ZMQ.CHARSET));
    in.put("0123456789".getBytes(ZMQ.CHARSET));
    insize = readLongMessage2(in);
    assertThat(insize, is(200));
    insize = readLongMessage2(in);
    assertThat(insize, is(330));
    decoder.decode(in, 278, process);
    assertThat(process.get(), is(278));
    assertThat(in.array()[329], is((byte) 'x'));
    Msg msg = decoder.msg();
    assertThat(msg, notNullValue());
    byte last = msg.data()[329];
    assertThat(last, is((byte) 'x'));
    assertThat(msg.size(), is(330));
}
Also used : Msg(zmq.Msg) ByteBuffer(java.nio.ByteBuffer) ValueReference(zmq.util.ValueReference) Test(org.junit.Test)

Example 25 with Msg

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

Aggregations

Msg (zmq.Msg)124 Test (org.junit.Test)45 SocketBase (zmq.SocketBase)37 Ctx (zmq.Ctx)32 ByteBuffer (java.nio.ByteBuffer)16 ValueReference (zmq.util.ValueReference)16 Pipe (zmq.pipe.Pipe)13 Blob (zmq.util.Blob)7 ArrayList (java.util.ArrayList)5 OutputStream (java.io.OutputStream)4 Socket (java.net.Socket)4 HashSet (java.util.HashSet)3 ExecutorService (java.util.concurrent.ExecutorService)2 Metadata (zmq.io.Metadata)2 InputStream (java.io.InputStream)1 List (java.util.List)1 Event (zmq.ZMQ.Event)1 ZObject (zmq.ZObject)1 Step (zmq.io.coder.IDecoder.Step)1 RawDecoder (zmq.io.coder.raw.RawDecoder)1