use of zmq.Ctx in project jeromq by zeromq.
the class TestReqrepDevice method testReprepDevice.
// Create REQ/ROUTER wiring.
@Test
public void testReprepDevice() throws IOException {
int routerPort = Utils.findOpenPort();
int dealerPort = Utils.findOpenPort();
boolean brc;
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
// Create a req/rep device.
SocketBase dealer = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(dealer, notNullValue());
brc = ZMQ.bind(dealer, "tcp://127.0.0.1:" + dealerPort);
assertThat(brc, is(true));
SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(router, notNullValue());
brc = ZMQ.bind(router, "tcp://127.0.0.1:" + routerPort);
assertThat(brc, is(true));
// Create a worker.
SocketBase rep = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
assertThat(rep, notNullValue());
brc = ZMQ.connect(rep, "tcp://127.0.0.1:" + dealerPort);
assertThat(brc, is(true));
SocketBase req = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(req, notNullValue());
brc = ZMQ.connect(req, "tcp://127.0.0.1:" + routerPort);
assertThat(brc, is(true));
// Send a request.
int rc;
Msg msg;
String buff;
long rcvmore;
rc = ZMQ.send(req, "ABC", ZMQ.ZMQ_SNDMORE);
assertThat(rc, is(3));
rc = ZMQ.send(req, "DEFG", 0);
assertThat(rc, is(4));
// Pass the request through the device.
for (int i = 0; i != 4; i++) {
msg = ZMQ.recvMsg(router, 0);
assertThat(msg, notNullValue());
rcvmore = ZMQ.getSocketOption(router, ZMQ.ZMQ_RCVMORE);
rc = ZMQ.sendMsg(dealer, msg, rcvmore > 0 ? ZMQ.ZMQ_SNDMORE : 0);
assertThat(rc >= 0, is(true));
}
// Receive the request.
msg = ZMQ.recv(rep, 0);
assertThat(msg.size(), is(3));
buff = new String(msg.data(), ZMQ.CHARSET);
assertThat(buff, is("ABC"));
rcvmore = ZMQ.getSocketOption(rep, ZMQ.ZMQ_RCVMORE);
assertThat(rcvmore > 0, is(true));
msg = ZMQ.recv(rep, 0);
assertThat(msg.size(), is(4));
buff = new String(msg.data(), ZMQ.CHARSET);
assertThat(buff, is("DEFG"));
rcvmore = ZMQ.getSocketOption(rep, ZMQ.ZMQ_RCVMORE);
assertThat(rcvmore, is(0L));
// Send the reply.
rc = ZMQ.send(rep, "GHIJKL", ZMQ.ZMQ_SNDMORE);
assertThat(rc, is(6));
rc = ZMQ.send(rep, "MN", 0);
assertThat(rc, is(2));
// Pass the reply through the device.
for (int i = 0; i != 4; i++) {
msg = ZMQ.recvMsg(dealer, 0);
assertThat(msg, notNullValue());
rcvmore = ZMQ.getSocketOption(dealer, ZMQ.ZMQ_RCVMORE);
rc = ZMQ.sendMsg(router, msg, rcvmore > 0 ? ZMQ.ZMQ_SNDMORE : 0);
assertThat(rc >= 0, is(true));
}
// Receive the reply.
msg = ZMQ.recv(req, 0);
assertThat(msg.size(), is(6));
buff = new String(msg.data(), ZMQ.CHARSET);
assertThat(buff, is("GHIJKL"));
rcvmore = ZMQ.getSocketOption(req, ZMQ.ZMQ_RCVMORE);
assertThat(rcvmore > 0, is(true));
msg = ZMQ.recv(req, 0);
assertThat(msg.size(), is(2));
buff = new String(msg.data(), ZMQ.CHARSET);
assertThat(buff, is("MN"));
rcvmore = ZMQ.getSocketOption(req, ZMQ.ZMQ_RCVMORE);
assertThat(rcvmore, is(0L));
// Clean up.
ZMQ.close(req);
ZMQ.close(rep);
ZMQ.close(router);
ZMQ.close(dealer);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class StreamTest method testStream2stream.
@Test
public void testStream2stream() throws IOException, InterruptedException {
String host = "tcp://localhost:*";
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
// Set up listener STREAM.
SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
assertThat(bind, notNullValue());
boolean rc = ZMQ.bind(bind, host);
assertThat(rc, is(true));
host = (String) ZMQ.getSocketOptionExt(bind, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
// Set up connection stream.
SocketBase connect = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
assertThat(connect, notNullValue());
// Do the connection.
rc = ZMQ.connect(connect, host);
assertThat(rc, is(true));
ZMQ.sleep(1);
// Connecting sends a zero message
// Server: First frame is identity, second frame is zero
Msg msg = ZMQ.recv(bind, 0);
assertThat(msg, notNullValue());
assertThat(msg.size() > 0, is(true));
msg = ZMQ.recv(bind, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(0));
ZMQ.close(bind);
ZMQ.close(connect);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class XpubXsubTest method testXPubSub.
@Test(timeout = 5000)
public void testXPubSub() {
System.out.println("XPub - Sub");
final Ctx ctx = zmq.ZMQ.createContext();
assertThat(ctx, notNullValue());
boolean rc;
SocketBase sub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_SUB);
rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_SUBSCRIBE, "topic");
assertThat(rc, is(true));
rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_SUBSCRIBE, "topix");
assertThat(rc, is(true));
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());
rc = zmq.ZMQ.connect(sub, endpoint);
assertThat(rc, is(true));
System.out.print("Send.");
rc = pub.send(new Msg("topic".getBytes(ZMQ.CHARSET)), ZMQ.ZMQ_SNDMORE);
assertThat(rc, is(true));
rc = pub.send(new Msg("hop".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
System.out.print("Recv.");
Msg msg = sub.recv(0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(5));
msg = sub.recv(0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(3));
rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_UNSUBSCRIBE, "topix");
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());
System.out.print("End.");
zmq.ZMQ.close(sub);
for (int idx = 0; idx < 2; ++idx) {
rc = pub.send(new Msg("topic abc".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
ZMQ.msleep(10);
}
zmq.ZMQ.close(pub);
zmq.ZMQ.term(ctx);
System.out.println("Done.");
}
use of zmq.Ctx in project jeromq by zeromq.
the class XpubXsubTest method testIssue476.
@Test(timeout = 5000)
public void testIssue476() throws InterruptedException, ExecutionException {
System.out.println("Issue 476");
final Ctx ctx = zmq.ZMQ.createContext();
assertThat(ctx, notNullValue());
boolean rc;
final SocketBase proxyPub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XPUB);
rc = proxyPub.bind("inproc://1");
assertThat(rc, is(true));
final SocketBase proxySub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XSUB);
rc = proxySub.bind("inproc://2");
assertThat(rc, is(true));
final SocketBase ctrl = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_PAIR);
rc = ctrl.bind("inproc://ctrl-proxy");
assertThat(rc, is(true));
ExecutorService service = Executors.newFixedThreadPool(1);
Future<?> proxy = service.submit(() -> {
ZMQ.proxy(proxySub, proxyPub, null, ctrl);
});
SocketBase sub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_SUB);
rc = zmq.ZMQ.setSocketOption(sub, zmq.ZMQ.ZMQ_SUBSCRIBE, "topic");
assertThat(rc, is(true));
rc = zmq.ZMQ.connect(sub, "inproc://1");
assertThat(rc, is(true));
SocketBase pub = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_XPUB);
rc = zmq.ZMQ.connect(pub, "inproc://2");
assertThat(rc, is(true));
rc = zmq.ZMQ.setSocketOption(sub, ZMQ.ZMQ_RCVTIMEO, 100);
assertThat(rc, is(true));
sub.recv(0);
System.out.print("Send.");
rc = pub.send(new Msg("topic".getBytes(ZMQ.CHARSET)), ZMQ.ZMQ_SNDMORE);
assertThat(rc, is(true));
rc = pub.send(new Msg("hop".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
System.out.print("Recv.");
Msg msg = sub.recv(0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(5));
msg = sub.recv(0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(3));
System.out.print("End.");
zmq.ZMQ.close(sub);
for (int idx = 0; idx < 2; ++idx) {
rc = pub.send(new Msg("topic abc".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
ZMQ.msleep(10);
}
zmq.ZMQ.close(pub);
final SocketBase command = zmq.ZMQ.socket(ctx, zmq.ZMQ.ZMQ_PAIR);
rc = command.connect("inproc://ctrl-proxy");
assertThat(rc, is(true));
command.send(new Msg(ZMQ.PROXY_TERMINATE), 0);
proxy.get();
zmq.ZMQ.close(command);
zmq.ZMQ.close(proxyPub);
zmq.ZMQ.close(proxySub);
zmq.ZMQ.close(ctrl);
zmq.ZMQ.term(ctx);
System.out.println("Done.");
}
use of zmq.Ctx in project jeromq by zeromq.
the class ProxyTerminateTest method testProxyTerminate.
@Test(timeout = 5000)
public void testProxyTerminate() throws IOException, InterruptedException, ExecutionException {
int port = Utils.findOpenPort();
String frontend = "tcp://127.0.0.1:" + port;
port = Utils.findOpenPort();
String backend = "tcp://127.0.0.1:" + port;
// The main thread simply starts a basic steerable proxy server, publishes some messages, and then
// waits for the server to terminate.
Ctx ctx = ZMQ.createContext();
// Control socket receives terminate command from main over inproc
SocketBase control = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
boolean rc = ZMQ.bind(control, "inproc://control");
assertThat(rc, is(true));
CompletableFuture<Boolean> resultHander = new CompletableFuture<>();
Thread thread = new Thread(new ServerTask(ctx, frontend, backend, resultHander));
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
resultHander.completeExceptionally(e);
}
});
thread.start();
Thread.sleep(500);
// Start a secondary publisher which writes data to the SUB-PUSH server socket
SocketBase publisher = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
assertThat(publisher, notNullValue());
rc = ZMQ.connect(publisher, frontend);
assertThat(rc, is(true));
Thread.sleep(50);
int ret = ZMQ.send(publisher, "This is a test", 0);
assertThat(ret, is(14));
Thread.sleep(50);
ret = ZMQ.send(publisher, "This is a test", 0);
assertThat(ret, is(14));
Thread.sleep(50);
ret = ZMQ.send(publisher, "This is a test", 0);
assertThat(ret, is(14));
ret = ZMQ.send(control, ZMQ.PROXY_TERMINATE, 0);
assertThat(ret, is(9));
ZMQ.close(publisher);
ZMQ.close(control);
resultHander.get();
ZMQ.term(ctx);
}
Aggregations