use of zmq.Ctx in project jeromq by zeromq.
the class TestPairTcp method testPairTcp.
@Test
public void testPairTcp() throws IOException {
int port = Utils.findOpenPort();
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase sb = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
assertThat(sb, notNullValue());
boolean brc = ZMQ.bind(sb, "tcp://127.0.0.1:" + port);
assertThat(brc, is(true));
SocketBase sc = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
assertThat(sc, notNullValue());
brc = ZMQ.connect(sc, "tcp://127.0.0.1:" + port);
assertThat(brc, is(true));
Helper.bounce(sb, sc);
// Tear down the wiring.
ZMQ.close(sb);
ZMQ.close(sc);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class TestPairTcp method testPairMonitorIssue291.
@Test
public void testPairMonitorIssue291() throws InterruptedException, IOException {
int port = Utils.findOpenPort();
String host = "tcp://127.0.0.1:" + port;
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
// bind first to use the address
SocketBase bind = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
assertThat(bind, notNullValue());
boolean rc = ZMQ.bind(bind, host);
assertThat(rc, is(true));
// monitor new socket and connect another pair to send events to
SocketBase monitored = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
assertThat(monitored, notNullValue());
rc = ZMQ.monitorSocket(monitored, "inproc://events", ZMQ.ZMQ_EVENT_ALL);
assertThat(rc, is(true));
SocketBase monitor = ZMQ.socket(ctx, ZMQ.ZMQ_PAIR);
assertThat(monitor, notNullValue());
rc = ZMQ.connect(monitor, "inproc://events");
assertThat(rc, is(true));
// bind monitored socket with already used address
rc = ZMQ.bind(monitored, host);
assertThat(rc, is(false));
// Tear down the wiring.
ZMQ.close(bind);
ZMQ.close(monitored);
ZMQ.close(monitor);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class TestPushPullTcp method testPushPullTcp.
@Test
public void testPushPullTcp() throws IOException {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase push = ZMQ.socket(ctx, ZMQ.ZMQ_PUSH);
assertThat(push, notNullValue());
boolean brc = ZMQ.bind(push, "tcp://127.0.0.1:*");
assertThat(brc, is(true));
String host = (String) ZMQ.getSocketOptionExt(push, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
SocketBase pull = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
assertThat(pull, notNullValue());
brc = ZMQ.connect(pull, host);
assertThat(brc, is(true));
byte[] content = "12345678ABCDEFGH12345678abcdefgh".getBytes(ZMQ.CHARSET);
// Send the message.
int rc = ZMQ.send(push, content, 32, ZMQ.ZMQ_SNDMORE);
assert (rc == 32);
rc = ZMQ.send(push, content, 32, 0);
assertThat(rc, is(32));
// Bounce the message back.
Msg msg;
msg = ZMQ.recv(pull, 0);
assert (msg.size() == 32);
int rcvmore = ZMQ.getSocketOption(pull, ZMQ.ZMQ_RCVMORE);
assertThat(rcvmore, is(1));
msg = ZMQ.recv(pull, 0);
assert (rc == 32);
rcvmore = ZMQ.getSocketOption(pull, ZMQ.ZMQ_RCVMORE);
assertThat(rcvmore, is(0));
// Tear down the wiring.
ZMQ.close(push);
ZMQ.close(pull);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class TestPubsubTcp method testPubsubTcp.
@Test
public void testPubsubTcp() throws Exception {
Ctx ctx = ZMQ.createContext();
assertThat(ctx, notNullValue());
SocketBase pubBind = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
assertThat(pubBind, notNullValue());
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_XPUB_NODROP, true);
boolean rc = ZMQ.bind(pubBind, "tcp://127.0.0.1:*");
assertThat(rc, is(true));
String host = (String) ZMQ.getSocketOptionExt(pubBind, ZMQ.ZMQ_LAST_ENDPOINT);
assertThat(host, notNullValue());
SocketBase subConnect = ZMQ.socket(ctx, ZMQ.ZMQ_SUB);
assertThat(subConnect, notNullValue());
rc = subConnect.setSocketOpt(ZMQ.ZMQ_SUBSCRIBE, "topic");
assertThat(rc, is(true));
rc = ZMQ.connect(subConnect, host);
assertThat(rc, is(true));
ZMQ.sleep(1);
System.out.print("Send");
rc = pubBind.send(new Msg("topic abc".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
rc = pubBind.send(new Msg("topix defg".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
rc = pubBind.send(new Msg("topic defgh".getBytes(ZMQ.CHARSET)), 0);
assertThat(rc, is(true));
System.out.print(".Recv.");
Msg msg = subConnect.recv(0);
System.out.print("1.");
assertThat(msg.size(), is(9));
msg = subConnect.recv(0);
System.out.print("2.");
assertThat(msg.size(), is(11));
System.out.print(".End.");
ZMQ.close(subConnect);
ZMQ.close(pubBind);
ZMQ.term(ctx);
System.out.println("Done.");
}
use of zmq.Ctx in project jeromq by zeromq.
the class RemoteThr method main.
public static void main(String[] argv) {
String connectTo;
long messageCount;
int messageSize;
Ctx ctx;
SocketBase s;
boolean rc;
long i;
Msg msg;
if (argv.length != 3) {
printf("usage: remote_thr <connect-to> <message-size> <message-count>\n");
return;
}
connectTo = argv[0];
messageSize = atoi(argv[1]);
messageCount = atol(argv[2]);
ctx = ZMQ.init(1);
if (ctx == null) {
printf("error in init");
return;
}
s = ZMQ.socket(ctx, ZMQ.ZMQ_PUSH);
if (s == null) {
printf("error in socket");
}
// Add your socket options here.
// For example ZMQ_RATE, ZMQ_RECOVERY_IVL and ZMQ_MCAST_LOOP for PGM.
rc = ZMQ.connect(s, connectTo);
if (!rc) {
printf("error in connect: %s\n");
return;
}
for (i = 0; i != messageCount; i++) {
msg = ZMQ.msgInitWithSize(messageSize);
if (msg == null) {
printf("error in msg_init: %s\n");
return;
}
int n = ZMQ.sendMsg(s, msg, 0);
if (n < 0) {
printf("error in sendmsg: %s\n");
return;
}
}
ZMQ.close(s);
ZMQ.term(ctx);
}
Aggregations