use of zmq.Ctx in project jeromq by zeromq.
the class RepSpecTest method testSpecEnvelope.
@Test
public void testSpecEnvelope() throws IOException, InterruptedException {
Ctx ctx = ZMQ.createContext();
List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
for (String bindAddress : binds) {
// For an incoming message:
// SHALL remove and store the address envelope, including the delimiter.
// SHALL pass the remaining data frames to its calling application.
// SHALL wait for a single reply message from its calling application.
// SHALL prepend the address envelope and delimiter.
// SHALL deliver this message back to the originating peer.
envelope(ctx, bindAddress, ZMQ.ZMQ_REP, ZMQ.ZMQ_DEALER);
}
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class RepSpecTest method testSpecFairQueueIn.
@Test
public void testSpecFairQueueIn() throws IOException, InterruptedException {
Ctx ctx = ZMQ.createContext();
List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
for (String bindAddress : binds) {
// SHALL receive incoming messages from its peers using a fair-queuing
// strategy.
fairQueueIn(ctx, bindAddress, ZMQ.ZMQ_REP, ZMQ.ZMQ_REQ);
}
ZMQ.term(ctx);
}
use of zmq.Ctx 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.Ctx 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.Ctx 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);
}
Aggregations