use of zmq.SocketBase 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.SocketBase 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.SocketBase 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.SocketBase 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);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class RouterProbeTest method testProbeRouter.
@Test
public void testProbeRouter() throws IOException, InterruptedException {
int port = Utils.findOpenPort();
String host = "tcp://127.0.0.1:" + port;
Ctx ctx = ZMQ.createContext();
// Server socket will accept connections
SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(server, notNullValue());
boolean rc = ZMQ.bind(server, host);
assertThat(rc, is(true));
// Create client and connect to server, doing a probe
SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
assertThat(client, notNullValue());
rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_IDENTITY, "X");
assertThat(rc, is(true));
rc = ZMQ.setSocketOption(client, ZMQ.ZMQ_PROBE_ROUTER, true);
assertThat(rc, is(true));
rc = ZMQ.connect(client, host);
assertThat(rc, is(true));
// We expect an identity=X + empty message from client
Msg msg = ZMQ.recv(server, 0);
assertThat(msg, notNullValue());
assertThat(msg.get(0), is((byte) 'X'));
msg = ZMQ.recv(server, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(0));
// Send a message to client now
int ret = ZMQ.send(server, "X", ZMQ.ZMQ_SNDMORE);
assertThat(ret, is(1));
ret = ZMQ.send(server, "Hello", 0);
assertThat(ret, is(5));
msg = ZMQ.recv(client, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(5));
// TODO DIFF V4 test should stop here, check the logic if we should receive payload in the previous message.
msg = ZMQ.recv(client, 0);
assertThat(msg, notNullValue());
assertThat(new String(msg.data(), ZMQ.CHARSET), is("Hello"));
ZMQ.closeZeroLinger(server);
ZMQ.closeZeroLinger(client);
// Shutdown
ZMQ.term(ctx);
}
Aggregations