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);
}
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));
}
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));
}
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));
}
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);
}
Aggregations