use of zmq.SocketBase 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.SocketBase 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.SocketBase 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.SocketBase 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);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class InprocLat method main.
public static void main(String[] argv) throws Exception {
if (argv.length != 2) {
printf("usage: inproc_lat <message-size> <roundtrip-count>\n");
return;
}
int messageSize = atoi(argv[0]);
int roundtripCount = atoi(argv[1]);
Ctx ctx = ZMQ.init(1);
if (ctx == null) {
printf("error in init:");
return;
}
SocketBase s = ZMQ.socket(ctx, ZMQ.ZMQ_REQ);
if (s == null) {
printf("error in socket: ");
return;
}
boolean rc = ZMQ.bind(s, "inproc://lat_test");
if (!rc) {
printf("error in bind: ");
return;
}
Thread localThread = new Thread(new Worker(ctx, roundtripCount));
localThread.start();
Msg smsg = ZMQ.msgInitWithSize(messageSize);
printf("message size: %d [B]\n", (int) messageSize);
printf("roundtrip count: %d\n", (int) roundtripCount);
long watch = ZMQ.startStopwatch();
for (int i = 0; i != roundtripCount; i++) {
int r = ZMQ.sendMsg(s, smsg, 0);
if (r < 0) {
printf("error in sendmsg: %s\n");
return;
}
Msg msg = ZMQ.recvMsg(s, 0);
if (msg == null) {
printf("error in recvmsg: %s\n");
return;
}
if (ZMQ.msgSize(msg) != messageSize) {
printf("message of incorrect size received\n");
return;
}
}
long elapsed = ZMQ.stopStopwatch(watch);
double latency = (double) elapsed / (roundtripCount * 2);
localThread.join();
printf("average latency: %.3f [us]\n", (double) latency);
ZMQ.close(s);
ZMQ.term(ctx);
}
Aggregations