use of zmq.SocketBase in project jeromq by zeromq.
the class SubTest method testSend.
@Test
public void testSend() {
Ctx ctx = ZMQ.createContext();
SocketBase pub = null;
try {
pub = ctx.createSocket(ZMQ.ZMQ_SUB);
pub.send(new Msg(), ZMQ.ZMQ_DONTWAIT);
Assert.fail("Sub cannot send message");
} catch (UnsupportedOperationException e) {
assertThat(ctx.errno().get(), is(ZError.ENOTSUP));
} finally {
ZMQ.close(pub);
ZMQ.term(ctx);
}
}
use of zmq.SocketBase in project jeromq by zeromq.
the class SubTest method testHasOut.
@Test
public void testHasOut() {
Ctx ctx = ZMQ.createContext();
SocketBase pub = null;
try {
pub = ctx.createSocket(ZMQ.ZMQ_SUB);
int events = pub.getSocketOpt(ZMQ.ZMQ_EVENTS);
assertThat(events, is(0));
} finally {
ZMQ.close(pub);
ZMQ.term(ctx);
}
}
use of zmq.SocketBase in project jeromq by zeromq.
the class TestSubForward method testSubForward.
// Create REQ/ROUTER wiring.
@Test
public void testSubForward() throws IOException {
int port1 = Utils.findOpenPort();
int port2 = Utils.findOpenPort();
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
// First, create an intermediate device
SocketBase xpubBind = ZMQ.socket(ctx, ZMQ.ZMQ_XPUB);
assertThat(xpubBind, notNullValue());
boolean rc = ZMQ.bind(xpubBind, "tcp://127.0.0.1:" + port1);
SocketBase xsubBind = ZMQ.socket(ctx, ZMQ.ZMQ_XSUB);
assertThat(xsubBind, notNullValue());
rc = ZMQ.bind(xsubBind, "tcp://127.0.0.1:" + port2);
assertThat(rc, is(true));
// Create a publisher
SocketBase pubConnect = ZMQ.socket(ctx, ZMQ.ZMQ_PUB);
assertThat(pubConnect, notNullValue());
rc = ZMQ.connect(pubConnect, "tcp://127.0.0.1:" + port2);
assertThat(rc, is(true));
// Create a subscriber
SocketBase subConnect = ZMQ.socket(ctx, ZMQ.ZMQ_SUB);
assertThat(subConnect, notNullValue());
rc = ZMQ.connect(subConnect, "tcp://127.0.0.1:" + port1);
assertThat(rc, is(true));
// Subscribe for all messages.
ZMQ.setSocketOption(subConnect, ZMQ.ZMQ_SUBSCRIBE, "");
ZMQ.sleep(1);
// Pass the subscription upstream through the device
Msg msg = ZMQ.recv(xpubBind, 0);
assertThat(msg, notNullValue());
int n = ZMQ.send(xsubBind, msg, 0);
assertThat(n, not(0));
// Wait a bit till the subscription gets to the publisher
ZMQ.sleep(1);
// Send an empty message
n = ZMQ.send(pubConnect, null, 0, 0);
assertThat(n, is(0));
// Pass the message downstream through the device
msg = ZMQ.recv(xsubBind, 0);
assertThat(msg, notNullValue());
n = ZMQ.send(xpubBind, msg, 0);
assertThat(n, is(0));
// Receive the message in the subscriber
msg = ZMQ.recv(subConnect, 0);
assertThat(msg, notNullValue());
// Tear down the wiring.
ZMQ.close(xpubBind);
ZMQ.close(xsubBind);
ZMQ.close(pubConnect);
ZMQ.close(subConnect);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class XPubManualTest method testXpubManual.
@Test
public void testXpubManual() throws IOException {
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
// Create a publisher
SocketBase pubBind = ZMQ.socket(ctx, ZMQ.ZMQ_XPUB);
assertThat(pubBind, notNullValue());
boolean rc = ZMQ.bind(pubBind, "inproc://soname");
assertThat(rc, is(true));
// set pub socket options
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_XPUB_MANUAL, 1);
int hwm = 2000;
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_SNDHWM, hwm);
// Create a subscriber
SocketBase subConnect = ZMQ.socket(ctx, ZMQ.ZMQ_SUB);
assertThat(subConnect, notNullValue());
// Subscribe for "A" messages.
ZMQ.setSocketOption(subConnect, ZMQ.ZMQ_SUBSCRIBE, "A");
rc = ZMQ.connect(subConnect, "inproc://soname");
assertThat(rc, is(true));
// Create a subscriber
SocketBase subConnect2 = ZMQ.socket(ctx, ZMQ.ZMQ_SUB);
assertThat(subConnect2, notNullValue());
// Subscribe for "B" messages.
ZMQ.setSocketOption(subConnect2, ZMQ.ZMQ_SUBSCRIBE, "B");
rc = ZMQ.connect(subConnect2, "inproc://soname");
assertThat(rc, is(true));
Msg subMsg = ZMQ.recv(pubBind, ZMQ.ZMQ_DONTWAIT);
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_SUBSCRIBE, subMsg.data());
subMsg = ZMQ.recv(pubBind, ZMQ.ZMQ_DONTWAIT);
ZMQ.setSocketOption(pubBind, ZMQ.ZMQ_SUBSCRIBE, subMsg.data());
int hwmlimit = hwm - 1;
int sendCount = 0;
// Send message
for (int i = 0; i < hwmlimit; i++) {
int ret = ZMQ.send(pubBind, "A", 0);
assert (ret == 1);
ret = ZMQ.send(pubBind, "B", 0);
assert (ret == 1);
sendCount++;
}
int recvCount = 0;
Msg msg = null;
do {
// Receive the message in the subscriber
msg = ZMQ.recv(subConnect, ZMQ.ZMQ_DONTWAIT);
if (msg != null) {
recvCount++;
}
} while (msg != null);
assertThat(sendCount, is(recvCount));
recvCount = 0;
do {
// Receive the message in the subscriber
msg = ZMQ.recv(subConnect2, ZMQ.ZMQ_DONTWAIT);
if (msg != null) {
recvCount++;
}
} while (msg != null);
assertThat(sendCount, is(recvCount));
// Tear down the wiring.
ZMQ.close(pubBind);
ZMQ.close(subConnect);
ZMQ.close(subConnect2);
ZMQ.term(ctx);
}
use of zmq.SocketBase in project jeromq by zeromq.
the class XPubTest method testSetVerbose.
@Test
public void testSetVerbose() {
Ctx ctx = ZMQ.createContext();
SocketBase pub = null;
try {
pub = ctx.createSocket(ZMQ.ZMQ_XPUB);
boolean rc = pub.setSocketOpt(ZMQ.ZMQ_XPUB_VERBOSE, 0);
assertThat(rc, is(true));
} finally {
ZMQ.close(pub);
ZMQ.term(ctx);
}
}
Aggregations