use of zmq.Ctx in project jeromq by zeromq.
the class AbstractProtocolVersion method assertProtocolVersion.
protected byte[] assertProtocolVersion(int version, List<ByteBuffer> raws, String payload) throws IOException, InterruptedException {
String host = "tcp://localhost:*";
Ctx ctx = ZMQ.init(1);
assertThat(ctx, notNullValue());
SocketBase receiver = ZMQ.socket(ctx, ZMQ.ZMQ_PULL);
assertThat(receiver, notNullValue());
boolean rc = ZMQ.setSocketOption(receiver, ZMQ.ZMQ_LINGER, 0);
assertThat(rc, is(true));
rc = ZMQ.monitorSocket(receiver, "inproc://monitor", ZMQ.ZMQ_EVENT_HANDSHAKE_PROTOCOL);
assertThat(rc, is(true));
SocketMonitor monitor = new SocketMonitor(ctx, "inproc://monitor");
monitor.start();
rc = ZMQ.bind(receiver, host);
assertThat(rc, is(true));
String ep = (String) ZMQ.getSocketOptionExt(receiver, ZMQ.ZMQ_LAST_ENDPOINT);
int port = TestUtils.port(ep);
Socket sender = new Socket("127.0.0.1", port);
OutputStream out = sender.getOutputStream();
for (ByteBuffer raw : raws) {
out.write(raw.array());
}
Msg msg = ZMQ.recv(receiver, 0);
assertThat(msg, notNullValue());
assertThat(new String(msg.data(), ZMQ.CHARSET), is(payload));
monitor.join();
final Event event = monitor.events[0];
assertThat(event, notNullValue());
assertThat(event.event, is(ZMQ.ZMQ_EVENT_HANDSHAKE_PROTOCOL));
assertThat((Integer) event.arg, is(version));
InputStream in = sender.getInputStream();
byte[] data = new byte[255];
int read = in.read(data);
sender.close();
ZMQ.close(receiver);
ZMQ.term(ctx);
return Arrays.copyOf(data, read);
}
use of zmq.Ctx in project jeromq by zeromq.
the class LocalThr method main.
public static void main(String[] argv) {
String bindTo;
long messageCount;
int messageSize;
Ctx ctx;
SocketBase s;
boolean rc;
long i;
Msg msg;
long watch;
long elapsed;
long throughput;
double megabits;
if (argv.length != 3) {
printf("usage: local_thr <bind-to> <message-size> <message-count>\n");
return;
}
bindTo = 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_PULL);
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.bind(s, bindTo);
if (!rc) {
printf("error in bind: %s\n");
return;
}
msg = ZMQ.recvMsg(s, 0);
if (msg == null) {
printf("error in recvmsg: %s\n");
return;
}
watch = ZMQ.startStopwatch();
for (i = 0; i != messageCount - 1; i++) {
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 " + ZMQ.msgSize(msg));
return;
}
}
elapsed = ZMQ.stopStopwatch(watch);
if (elapsed == 0) {
elapsed = 1;
}
throughput = (long) ((double) messageCount / (double) elapsed * 1000000L);
megabits = (double) (throughput * messageSize * 8) / 1000000;
printf("message elapsed: %.3f \n", (double) elapsed / 1000000L);
printf("message size: %d [B]\n", (int) messageSize);
printf("message count: %d\n", (int) messageCount);
printf("mean throughput: %d [msg/s]\n", (int) throughput);
printf("mean throughput: %.3f [Mb/s]\n", (double) megabits);
ZMQ.close(s);
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class DealerSpecTest method testSpecDestroyQueueOnDisconnect.
@Test
@Ignore
public void testSpecDestroyQueueOnDisconnect() throws IOException, InterruptedException {
Ctx ctx = ZMQ.createContext();
List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
for (String bindAddress : binds) {
// SHALL create a double queue when a peer connects to it. If this peer
// disconnects, the DEALER socket SHALL destroy its double queue and SHALL
// discard any messages it contains.
// *** Test disabled until libzmq does this properly ***
// test_destroy_queue_on_disconnect (ctx);
}
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class DealerSpecTest method testSpecRoundRobinOut.
@Test
public void testSpecRoundRobinOut() throws IOException, InterruptedException {
Ctx ctx = ZMQ.createContext();
List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
for (String bindAddress : binds) {
// SHALL route outgoing messages to available peers using a round-robin
// strategy.
roundRobinOut(ctx, bindAddress, ZMQ.ZMQ_DEALER, ZMQ.ZMQ_REP);
}
ZMQ.term(ctx);
}
use of zmq.Ctx in project jeromq by zeromq.
the class DealerSpecTest method testSpecBlockOnSendNoPeers.
@Test
public void testSpecBlockOnSendNoPeers() throws IOException, InterruptedException {
Ctx ctx = ZMQ.createContext();
List<String> binds = Arrays.asList("inproc://a", "tcp://127.0.0.1:*");
for (String bindAddress : binds) {
// SHALL block on sending, or return a suitable error, when it has no connected peers.
blockOnSendNoPeers(ctx, bindAddress, ZMQ.ZMQ_DEALER);
}
ZMQ.term(ctx);
}
Aggregations