use of zmq.SocketBase in project jeromq by zeromq.
the class ReqSpecTest method roundRobinOut.
private void roundRobinOut(Ctx ctx, String address, int bindType, int connectType) throws IOException, InterruptedException {
SocketBase req = ZMQ.socket(ctx, bindType);
boolean rc = ZMQ.bind(req, address);
assertThat(rc, is(true));
int timeout = 250;
int services = 5;
List<SocketBase> senders = new ArrayList<>();
for (int peer = 0; peer < services; ++peer) {
SocketBase reps = ZMQ.socket(ctx, connectType);
assertThat(reps, notNullValue());
senders.add(reps);
rc = ZMQ.setSocketOption(reps, ZMQ.ZMQ_RCVTIMEO, timeout);
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(req, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
rc = ZMQ.connect(reps, host);
assertThat(rc, is(true));
}
// We have to give the connects time to finish otherwise the requests
// will not properly round-robin. We could alternatively connect the
// REQ sockets to the REP sockets.
ZMQ.msleep(200);
// Send our peer-replies, and expect every REP it used once in order
for (int peer = 0; peer < services; ++peer) {
rc = sendSeq(req, "ABC");
assertThat(rc, is(true));
recvSeq(senders.get(peer), "ABC");
rc = sendSeq(senders.get(peer), "DEF");
assertThat(rc, is(true));
recvSeq(req, "DEF");
}
ZMQ.closeZeroLinger(req);
for (SocketBase sender : senders) {
ZMQ.closeZeroLinger(sender);
}
// Wait for disconnects.
ZMQ.msleep(100);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class RouterHandoverTest method testRouterHandover.
@Test
public void testRouterHandover() throws Exception {
int rc;
boolean brc;
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase router = ZMQ.socket(ctx, ZMQ.ZMQ_ROUTER);
brc = ZMQ.bind(router, "tcp://127.0.0.1:*");
assertThat(brc, is(true));
// Enable the handover flag
ZMQ.setSocketOption(router, ZMQ.ZMQ_ROUTER_HANDOVER, 1);
assertThat(router, notNullValue());
// Create dealer called "X" and connect it to our router
SocketBase dealerOne = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(dealerOne, notNullValue());
ZMQ.setSocketOption(dealerOne, ZMQ.ZMQ_IDENTITY, "X");
String host = (String) ZMQ.getSocketOptionExt(router, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
brc = ZMQ.connect(dealerOne, host);
assertThat(brc, is(true));
// Get message from dealer to know when connection is ready
rc = ZMQ.send(dealerOne, "Hello", 0);
assertThat(rc, is(5));
Msg msg = ZMQ.recv(router, 0);
assertThat(msg.size(), is(1));
assertThat(new String(msg.data()), is("X"));
msg = ZMQ.recv(router, 0);
assertThat(msg.size(), is(5));
// Now create a second dealer that uses the same identity
SocketBase dealerTwo = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(dealerTwo, notNullValue());
ZMQ.setSocketOption(dealerTwo, ZMQ.ZMQ_IDENTITY, "X");
brc = ZMQ.connect(dealerTwo, host);
assertThat(brc, is(true));
// Get message from dealer to know when connection is ready
rc = ZMQ.send(dealerTwo, "Hello", 0);
assertThat(rc, is(5));
msg = ZMQ.recv(router, 0);
assertThat(msg.size(), is(1));
assertThat(new String(msg.data()), is("X"));
msg = ZMQ.recv(router, 0);
assertThat(msg.size(), is(5));
// Send a message to 'X' identity. This should be delivered
// to the second dealer, instead of the first because of the handover.
rc = ZMQ.send(router, "X", ZMQ.ZMQ_SNDMORE);
assertThat(rc, is(1));
rc = ZMQ.send(router, "Hello", 0);
assertThat(rc, is(5));
// Ensure that the first dealer doesn't receive the message
// but the second one does
msg = ZMQ.recv(dealerOne, ZMQ.ZMQ_DONTWAIT);
assertThat(msg, nullValue());
msg = ZMQ.recv(dealerTwo, 0);
assertThat(msg.size(), is(5));
// Clean up.
ZMQ.close(router);
ZMQ.close(dealerOne);
ZMQ.close(dealerTwo);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class TestReqrepTcp method testReqrepTcp.
@Test
public void testReqrepTcp() throws Exception {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase repBind = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
assertThat(repBind, notNullValue());
boolean rc = ZMQ.bind(repBind, "tcp://127.0.0.1:*");
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(repBind, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
SocketBase reqConnect = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
assertThat(reqConnect, notNullValue());
rc = ZMQ.connect(reqConnect, host);
assertThat(rc, is(true));
Helper.bounce(repBind, reqConnect);
ZMQ.close(reqConnect);
ZMQ.close(repBind);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class StreamEmptyTest method testStreamEmpty.
@Test
public void testStreamEmpty() throws IOException, InterruptedException {
String host = "tcp://localhost:*";
Ctx ctx = ZMQ.init(1);
assert (ctx != null);
// Set up listener STREAM.
SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_STREAM);
assert (bind != null);
boolean rc = ZMQ.bind(bind, host);
assert (rc);
host = (String) ZMQ.getSocketOptionExt(bind, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
// Set up connection stream.
SocketBase connect = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assert (connect != null);
// Do the connection.
rc = ZMQ.connect(connect, host);
assert (rc);
ZMQ.sleep(1);
int ret = ZMQ.send(connect, "", 0);
assertThat(ret, is(0));
Msg msg = ZMQ.recv(bind, 0);
assertThat(msg, notNullValue());
assertThat(msg.size(), is(5));
ret = ZMQ.send(bind, msg, ZMQ.ZMQ_SNDMORE);
assertThat(ret, is(5));
ret = ZMQ.send(bind, new Msg(), 0);
assertThat(ret, is(0));
ZMQ.setSocketOption(bind, ZMQ.ZMQ_LINGER, 0);
ZMQ.setSocketOption(connect, ZMQ.ZMQ_LINGER, 0);
ZMQ.close(bind);
ZMQ.close(connect);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class SecurityNullTest method testNullMechanismSecurity.
@Test
public void testNullMechanismSecurity() throws IOException, InterruptedException {
String host = "tcp://127.0.0.1:*";
Ctx ctx = ZMQ.createContext();
// Spawn ZAP handler
// We create and bind ZAP socket in main thread to avoid case
// where child thread does not start up fast enough.
SocketBase handler = ZMQ.socket(ctx, ZMQ.ZMQ_REP);
assertThat(handler, notNullValue());
boolean rc = ZMQ.bind(handler, "inproc://zeromq.zap.01");
assertThat(rc, is(true));
Thread thread = new Thread(new ZapHandler(handler));
thread.start();
// We bounce between a binding server and a connecting client
// We first test client/server with no ZAP domain
// Libzmq does not call our ZAP handler, the connect must succeed
System.out.println("Test NO ZAP domain");
SocketBase server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(server, notNullValue());
SocketBase client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(client, notNullValue());
rc = ZMQ.bind(server, host);
assertThat(rc, is(true));
String endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(endpoint, notNullValue());
rc = ZMQ.connect(client, endpoint);
assertThat(rc, is(true));
Helper.bounce(server, client);
ZMQ.closeZeroLinger(server);
ZMQ.closeZeroLinger(client);
// Now define a ZAP domain for the server; this enables
// authentication. We're using the wrong domain so this test
// must fail.
System.out.println("Test WRONG ZAP domain");
server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(server, notNullValue());
client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(client, notNullValue());
ZMQ.setSocketOption(server, ZMQ.ZMQ_ZAP_DOMAIN, "WRONG");
rc = ZMQ.bind(server, host);
assertThat(rc, is(true));
endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(endpoint, notNullValue());
rc = ZMQ.connect(client, endpoint);
assertThat(rc, is(true));
Helper.expectBounceFail(server, client);
ZMQ.closeZeroLinger(server);
ZMQ.closeZeroLinger(client);
// Now use the right domain, the test must pass
System.out.println("Test RIGHT ZAP domain");
server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(server, notNullValue());
client = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(client, notNullValue());
ZMQ.setSocketOption(server, ZMQ.ZMQ_ZAP_DOMAIN, "TEST");
rc = ZMQ.bind(server, host);
assertThat(rc, is(true));
endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(endpoint, notNullValue());
rc = ZMQ.connect(client, endpoint);
assertThat(rc, is(true));
Helper.bounce(server, client);
ZMQ.closeZeroLinger(server);
ZMQ.closeZeroLinger(client);
// Unauthenticated messages from a vanilla socket shouldn't be received
server = ZMQ.socket(ctx, ZMQ.ZMQ_DEALER);
assertThat(server, notNullValue());
ZMQ.setSocketOption(server, ZMQ.ZMQ_ZAP_DOMAIN, "WRONG");
rc = ZMQ.bind(server, host);
assertThat(rc, is(true));
endpoint = (String) ZMQ.getSocketOptionExt(server, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(endpoint, notNullValue());
Socket sock = new Socket("127.0.0.1", TestUtils.port(endpoint));
// send anonymous ZMTP/1.0 greeting
OutputStream out = sock.getOutputStream();
out.write(new StringBuilder().append(0x01).append(0x00).toString().getBytes(ZMQ.CHARSET));
// send sneaky message that shouldn't be received
out.write(new StringBuilder().append(0x08).append(0x00).append("sneaky").append(0x00).toString().getBytes(ZMQ.CHARSET));
int timeout = 250;
ZMQ.setSocketOption(server, ZMQ.ZMQ_RCVTIMEO, timeout);
Msg msg = ZMQ.recv(server, 0);
assertThat(msg, nullValue());
sock.close();
ZMQ.closeZeroLinger(server);
// Shutdown
ZMQ.term(ctx);
// Wait until ZAP handler terminates
thread.join();
}
Aggregations