use of zmq.Msg in project jeromq by zeromq.
the class TrieTest method testAddRemoveMultiNodesBelowLevel.
@Test
public void testAddRemoveMultiNodesBelowLevel() {
Trie trie = new Trie();
boolean rc = trie.add(prefix, 0, prefix.size());
assertThat(rc, is(true));
byte[] abv = Arrays.copyOf(prefix.data(), prefix.size());
abv[1] = 0;
Msg above = new Msg(abv);
rc = trie.add(above, 0, above.size());
assertThat(rc, is(true));
abv[1] = -1;
above = new Msg(abv);
rc = trie.add(above, 0, above.size());
assertThat(rc, is(true));
rc = trie.rm(prefix, 0, prefix.size());
assertThat(rc, is(true));
abv[1] = 0;
above = new Msg(abv);
rc = trie.rm(above, 0, above.size());
assertThat(rc, is(true));
abv[1] = -1;
above = new Msg(abv);
rc = trie.rm(above, 0, above.size());
assertThat(rc, is(true));
}
use of zmq.Msg in project jeromq by zeromq.
the class TrieTest method testAddRemoveMultiNodesAboveLevel.
@Test
public void testAddRemoveMultiNodesAboveLevel() {
Trie trie = new Trie();
boolean rc = trie.add(prefix, 0, prefix.size());
assertThat(rc, is(true));
byte[] abv = Arrays.copyOf(prefix.data(), prefix.size());
abv[1] = 3;
Msg above = new Msg(abv);
rc = trie.add(above, 0, above.size());
assertThat(rc, is(true));
abv[1] = 33;
above = new Msg(abv);
rc = trie.add(above, 0, above.size());
assertThat(rc, is(true));
rc = trie.rm(prefix, 0, prefix.size());
assertThat(rc, is(true));
abv[1] = 3;
above = new Msg(abv);
rc = trie.rm(above, 0, above.size());
assertThat(rc, is(true));
abv[1] = 33;
above = new Msg(abv);
rc = trie.rm(above, 0, above.size());
assertThat(rc, is(true));
}
use of zmq.Msg in project jeromq by zeromq.
the class XPubNodropTest method testXpubNoDrop.
// Create REQ/ROUTER wiring.
@Test
public void testXpubNoDrop() throws IOException {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
// Create a publisher
SocketBase pubBind = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
assertThat(pubBind, notNullValue());
boolean rc = ZMQ.bind(pubBind, "inproc://soname");
assertThat(rc, is(true));
// set pub socket options
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_XPUB_NODROP, 1);
int hwm = 2000;
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_SNDHWM, hwm);
// Create a subscriber
SocketBase subConnect = ZMQ.socket(ctx, ZMQ.ZMQ_SUB);
assertThat(subConnect, notNullValue());
rc = ZMQ.connect(subConnect, "inproc://soname");
assertThat(rc, is(true));
// Subscribe for all messages.
ZMQ.setSocketOption(subConnect, ZMQ.ZMQ_SUBSCRIBE, "");
int hwmlimit = hwm - 1;
int sendCount = 0;
// Send an empty message
for (int i = 0; i < hwmlimit; i++) {
int ret = ZMQ.send(pubBind, "", 0);
assert (ret == 0);
sendCount++;
}
int recvCount = 0;
Msg msg = null;
do {
// Receive the message in the subscriber
msg = ZMQ.recv(subConnect, ZMQ.ZMQ_DONTWAIT);
if (msg != null) {
recvCount++;
}
} while (msg != null);
assertThat(sendCount, is(recvCount));
// Now test real blocking behavior
// Set a timeout, default is infinite
int timeout = 0;
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_SNDTIMEO, timeout);
recvCount = 0;
sendCount = 0;
hwmlimit = hwm;
while (ZMQ.send(pubBind, "", 0) == 0) {
sendCount++;
}
assertThat(pubBind.errno(), is(ZError.EAGAIN));
while (ZMQ.recv(subConnect, ZMQ.ZMQ_DONTWAIT) != null) {
recvCount++;
}
assertThat(sendCount, is(recvCount));
// Tear down the wiring.
ZMQ.close(pubBind);
ZMQ.close(subConnect);
ZMQ.term(ctx);
}
use of zmq.Msg in project jeromq by zeromq.
the class XpubXsubTest method testXPubXSub.
@Test(timeout = 5000)
public void testXPubXSub() {
System.out.println("XPub - XSub");
final Ctx ctx = zmq.ZMQ.createContext();
assertThat(ctx, notNullValue());
boolean rc;
SocketBase pub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XPUB);
rc = zmq.ZMQ.bind(pub, "inproc://1");
assertThat(rc, is(true));
String endpoint = (String) ZMQ.getSocketOptionExt(pub, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(endpoint, notNullValue());
SocketBase sub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XSUB);
rc = zmq.ZMQ.connect(sub, endpoint);
assertThat(rc, is(true));
System.out.print("Send.");
rc = sub.send(new Msg("\1topic".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
rc = pub.send(new Msg("topic".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
System.out.print("Recv.");
// msg = sub.recv(0);
// assertThat(msg.size(), is(5));
//
// msg = sub.recv(0);
// assertThat(msg.size(), is(3));
//
rc = sub.send(new Msg("\0topic".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
// rc = pub.send(new Msg("topix".getBytes(ZMQ.CHARSET)), ZMQ.ZMQ_SNDMORE);
// assertThat(rc, is(true));
//
// rc = pub.send(new Msg("hop".getBytes(ZMQ.CHARSET)), 0);
// assertThat(rc, is(true));
//
// rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_RCVTIMEO, 500);
// assertThat(rc, is(true));
//
// msg = sub.recv(0);
// assertThat(msg, nullValue());
zmq.ZMQ.close(sub);
zmq.ZMQ.close(pub);
zmq.ZMQ.term(ctx);
System.out.println("Done.");
}
use of zmq.Msg in project jeromq by zeromq.
the class ProxySingleSocketTest method testProxySingleSocket.
@Test
public void testProxySingleSocket() throws IOException, InterruptedException {
int port = Utils.findOpenPort();
String host = "tcp://127.0.0.1:" + port;
// The main thread simply starts several clients and a server, and then
// waits for the server to finish.
Ctx ctx = ZMQ.createContext();
SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(req, notNullValue());
boolean rc = ZMQ.connect(req, host);
assertThat(rc, is(true));
// Control socket receives terminate command from main over inproc
SocketBase control = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
rc = ZMQ.bind(control, "inproc://control");
assertThat(rc, is(true));
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(new ServerTask(ctx, host));
int ret = ZMQ.send(req, "msg1", 0);
assertThat(ret, is(4));
System.out.print(".");
Msg msg = ZMQ.recv(req, 0);
System.out.print(".");
assertThat(msg, notNullValue());
assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg1"));
ret = ZMQ.send(req, "msg22", 0);
assertThat(ret, is(5));
System.out.print(".");
msg = ZMQ.recv(req, 0);
System.out.print(".");
assertThat(msg, notNullValue());
assertThat(new String(msg.data(), ZMQ.CHARSET), is("msg22"));
ret = ZMQ.send(control, ZMQ.PROXY_TERMINATE, 0);
assertThat(ret, is(9));
System.out.println(".");
ZMQ.close(control);
ZMQ.close(req);
executor.shutdown();
executor.awaitTermination(30, TimeUnit.SECONDS);
ZMQ.term(ctx);
}
Aggregations