use of zmq.Msg in project jeromq by zeromq.
the class StreamTest method testStream2dealer.
@Test
public void testStream2dealer() throws IOException, InterruptedException {
final byte[] standardGreeting = new byte[64];
standardGreeting[0] = (byte) 0xff;
standardGreeting[8] = 1;
standardGreeting[9] = 0x7f;
standardGreeting[10] = 3;
standardGreeting[12] = 'N';
standardGreeting[13] = 'U';
standardGreeting[14] = 'L';
standardGreeting[15] = 'L';
String host = "tcp://localhost:*";
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
// We'll be using this socket in raw mode
SocketBase stream = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
assertThat(stream, notNullValue());
boolean rc = ZMQ.setSocketOption(stream, ZMQ.ZMQ_LINGER, 0);
assertThat(rc, is(true));
rc = ZMQ.bind(stream, host);
assertThat(rc, is(true));
host = (String) ZMQ.getSocketOptionExt(stream, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
// We'll be using this socket as the other peer
SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(dealer, notNullValue());
rc = ZMQ.setSocketOption(dealer, ZMQ.ZMQ_LINGER, 0);
assertThat(rc, is(true));
rc = ZMQ.connect(dealer, host);
assertThat(rc, is(true));
// Send a message on the dealer socket
int ret = ZMQ.send(dealer, "Hello", 0);
assertThat(ret, is(5));
// Connecting sends a zero message
// First frame is identity
Msg id = ZMQ.recv(stream, 0);
assertThat(id, notNullValue());
assertThat(id.hasMore(), is(true));
// Verify the existence of Peer-Address metadata
assertThat(id.getMetadata().get("Peer-Address").startsWith("127.0.0.1:"), is(true));
// Second frame is zero
Msg zero = ZMQ.recv(stream, 0);
assertThat(zero, notNullValue());
assertThat(zero.size(), is(0));
// Real data follows
// First frame is identity
id = ZMQ.recv(stream, 0);
assertThat(id, notNullValue());
assertThat(id.hasMore(), is(true));
// Verify the existence of Peer-Address metadata
assertThat(id.getMetadata().get("Peer-Address").startsWith("127.0.0.1:"), is(true));
// Second frame is greeting signature
Msg greeting = ZMQ.recv(stream, 0);
assertThat(greeting, notNullValue());
assertThat(greeting.size(), is(10));
assertThat(greeting.data(), is(new byte[] { (byte) 0xff, 0, 0, 0, 0, 0, 0, 0, 1, 0x7f }));
// Send our own protocol greeting
ret = ZMQ.send(stream, id, ZMQ.ZMQ_SNDMORE);
ret = ZMQ.send(stream, standardGreeting, standardGreeting.length, 0);
assertThat(ret, is(standardGreeting.length));
// Now we expect the data from the DEALER socket
// We want the rest of greeting along with the Ready command
int bytesRead = 0;
ByteBuffer read = ByteBuffer.allocate(100);
while (bytesRead < 97) {
// First frame is the identity of the connection (each time)
Msg msg = ZMQ.recv(stream, 0);
assertThat(msg, notNullValue());
assertThat(msg.hasMore(), is(true));
// Second frame contains the next chunk of data
msg = ZMQ.recv(stream, 0);
assertThat(msg, notNullValue());
bytesRead += msg.size();
read.put(msg.buf());
}
assertThat(read.get(0), is((byte) 3));
assertThat(read.get(1), is((byte) 0));
assertThat(read.get(2), is((byte) 'N'));
assertThat(read.get(3), is((byte) 'U'));
assertThat(read.get(4), is((byte) 'L'));
assertThat(read.get(5), is((byte) 'L'));
for (int idx = 0; idx < 16; ++idx) {
assertThat(read.get(2 + 4 + idx), is((byte) 0));
}
assertThat(read.get(54), is((byte) 4));
// octal notation, yes
assertThat(read.get(55), is((byte) 051));
assertThat(read.get(56), is((byte) 5));
assertThat(read.get(57), is((byte) 'R'));
assertThat(read.get(58), is((byte) 'E'));
assertThat(read.get(59), is((byte) 'A'));
assertThat(read.get(60), is((byte) 'D'));
assertThat(read.get(61), is((byte) 'Y'));
assertThat(read.get(62), is((byte) 013));
assertThat(read.get(63), is((byte) 'S'));
assertThat(read.get(64), is((byte) 'o'));
assertThat(read.get(65), is((byte) 'c'));
assertThat(read.get(66), is((byte) 'k'));
assertThat(read.get(67), is((byte) 'e'));
assertThat(read.get(68), is((byte) 't'));
assertThat(read.get(69), is((byte) '-'));
assertThat(read.get(70), is((byte) 'T'));
assertThat(read.get(71), is((byte) 'y'));
assertThat(read.get(72), is((byte) 'p'));
assertThat(read.get(73), is((byte) 'e'));
assertThat(read.get(74), is((byte) 0));
assertThat(read.get(75), is((byte) 0));
assertThat(read.get(76), is((byte) 0));
assertThat(read.get(77), is((byte) 6));
assertThat(read.get(78), is((byte) 'D'));
assertThat(read.get(79), is((byte) 'E'));
assertThat(read.get(80), is((byte) 'A'));
assertThat(read.get(81), is((byte) 'L'));
assertThat(read.get(82), is((byte) 'E'));
assertThat(read.get(83), is((byte) 'R'));
assertThat(read.get(84), is((byte) 010));
assertThat(read.get(85), is((byte) 'I'));
assertThat(read.get(86), is((byte) 'd'));
assertThat(read.get(87), is((byte) 'e'));
assertThat(read.get(88), is((byte) 'n'));
assertThat(read.get(89), is((byte) 't'));
assertThat(read.get(90), is((byte) 'i'));
assertThat(read.get(91), is((byte) 't'));
assertThat(read.get(92), is((byte) 'y'));
assertThat(read.get(93), is((byte) 0));
assertThat(read.get(94), is((byte) 0));
assertThat(read.get(95), is((byte) 0));
assertThat(read.get(96), is((byte) 0));
// Send Ready command
ZMQ.send(stream, id, ZMQ.ZMQ_SNDMORE);
ZMQ.send(stream, new byte[] { 4, 41, 5, 'R', 'E', 'A', 'D', 'Y', 11, 'S', 'o', 'c', 'k', 'e', 't', '-', 'T', 'y', 'p', 'e', 0, 0, 0, 6, 'R', 'O', 'U', 'T', 'E', 'R', 8, 'I', 'd', 'e', 'n', 't', 'i', 't', 'y', 0, 0, 0, 0 }, 0);
// Now we expect the data from the DEALER socket
// First frame is, again, the identity of the connection
id = ZMQ.recv(stream, 0);
assertThat(id, notNullValue());
assertThat(id.hasMore(), is(true));
// Third frame contains Hello message from DEALER
Msg un = ZMQ.recv(stream, 0);
assertThat(un, notNullValue());
assertThat(un.size(), is(7));
// Then we have a 5-byte message "Hello"
assertThat(un.get(0), is((byte) 0));
assertThat(un.get(1), is((byte) 5));
assertThat(un.get(2), is((byte) 'H'));
assertThat(un.get(3), is((byte) 'e'));
assertThat(un.get(4), is((byte) 'l'));
assertThat(un.get(5), is((byte) 'l'));
assertThat(un.get(6), is((byte) 'o'));
// Send "World" back to DEALER
ZMQ.send(stream, id, ZMQ.ZMQ_SNDMORE);
ret = ZMQ.send(stream, new byte[] { 0, 5, 'W', 'o', 'r', 'l', 'd' }, 0);
assertThat(ret, is(7));
// Expect response on DEALER socket
Msg recv = ZMQ.recv(dealer, 0);
assertThat(recv.size(), is(5));
assertThat(recv.data(), is("World".getBytes(ZMQ.CHARSET)));
ZMQ.close(stream);
ZMQ.close(dealer);
ZMQ.term(ctx);
}
use of zmq.Msg in project jeromq by zeromq.
the class CustomEncoderTest method testCustomEncoder.
@Test
public void testCustomEncoder() {
CustomEncoder cencoder = new CustomEncoder(32, Integer.MAX_VALUE / 2);
Msg msg = new Msg("12345678901234567890".getBytes(ZMQ.CHARSET));
cencoder.loadMsg(msg);
ValueReference<ByteBuffer> ref = new ValueReference<>();
int outsize = cencoder.encode(ref, 0);
assertThat(outsize, is(30));
ByteBuffer out = ref.get();
out.flip();
write(out);
byte[] data = sock.data();
assertThat(new String(data, 0, 6, ZMQ.CHARSET), is("HEADER"));
assertThat((int) data[9], is(20));
assertThat(new String(data, 10, 20, ZMQ.CHARSET), is("12345678901234567890"));
}
use of zmq.Msg in project jeromq by zeromq.
the class V1EncoderTest method testReader.
@Test
public void testReader() {
Msg msg = readShortMessage();
encoder.loadMsg(msg);
ValueReference<ByteBuffer> ref = new ValueReference<>();
int outsize = encoder.encode(ref, 0);
ByteBuffer out = ref.get();
assertThat(out, notNullValue());
assertThat(outsize, is(7));
assertThat(out.position(), is(7));
}
use of zmq.Msg in project jeromq by zeromq.
the class V2EncoderTest method testReader.
@Test
public void testReader() {
Msg msg = readShortMessage();
encoder.loadMsg(msg);
ValueReference<ByteBuffer> ref = new ValueReference<>();
int outsize = encoder.encode(ref, 0);
ByteBuffer out = ref.get();
assertThat(out, notNullValue());
assertThat(outsize, is(7));
assertThat(out.position(), is(7));
}
use of zmq.Msg in project jeromq by zeromq.
the class YQueueTest method testReuse.
@Test
public void testReuse() {
// yqueue has a first empty entry
YQueue<Msg> p = new YQueue<Msg>(3);
Msg m1 = new Msg(1);
Msg m2 = new Msg(2);
Msg m3 = new Msg(3);
Msg m4 = new Msg(4);
Msg m5 = new Msg(5);
Msg m6 = new Msg(6);
Msg m7 = new Msg(7);
m7.put("1234567".getBytes(ZMQ.CHARSET));
p.push(m1);
assertThat(p.backPos(), is(1));
// might allocated new chunk
p.push(m2);
p.push(m3);
assertThat(p.backPos(), is(3));
assertThat(p.frontPos(), is(0));
p.pop();
p.pop();
// offer the old chunk
p.pop();
assertThat(p.frontPos(), is(3));
p.push(m4);
// might reuse the old chunk
p.push(m5);
p.push(m6);
assertThat(p.backPos(), is(0));
}
Aggregations