use of org.zeromq.ZMQ.Context in project jeromq by zeromq.
the class TestZMQ method testByteBufferLarge.
@Test
public void testByteBufferLarge() throws InterruptedException, IOException, CharacterCodingException {
int port = Utils.findOpenPort();
ZMQ.Context context = ZMQ.context(1);
int[] array = new int[2048 * 2000];
for (int i = 0; i < array.length; ++i) {
array[i] = i;
}
ByteBuffer bSend = ByteBuffer.allocate(Integer.SIZE / 8 * array.length).order(ByteOrder.nativeOrder());
bSend.asIntBuffer().put(array);
ByteBuffer bRec = ByteBuffer.allocate(bSend.capacity()).order(ByteOrder.nativeOrder());
int[] recArray = new int[array.length];
ZMQ.Socket push = null;
ZMQ.Socket pull = null;
try {
push = context.socket(ZMQ.PUSH);
pull = context.socket(ZMQ.PULL);
pull.bind("tcp://*:" + port);
push.connect("tcp://localhost:" + port);
push.sendByteBuffer(bSend, 0);
pull.recvByteBuffer(bRec, 0);
bRec.flip();
bRec.asIntBuffer().get(recArray);
assertArrayEquals(array, recArray);
} finally {
try {
push.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
try {
pull.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
try {
context.term();
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
}
use of org.zeromq.ZMQ.Context in project jeromq by zeromq.
the class TestZMQ method testEventConnectDelayed.
@Test
public void testEventConnectDelayed() throws IOException {
Context context = ZMQ.context(1);
ZMQ.Event event;
Socket socket = context.socket(ZMQ.REP);
Socket monitor = context.socket(ZMQ.PAIR);
monitor.setReceiveTimeOut(100);
assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_CONNECT_DELAYED));
monitor.connect("inproc://monitor.socket");
int randomPort = Utils.findOpenPort();
socket.connect("tcp://127.0.0.1:" + randomPort);
event = ZMQ.Event.recv(monitor);
assertNotNull("No event was received", event);
assertEquals(ZMQ.EVENT_CONNECT_DELAYED, event.getEvent());
socket.close();
monitor.close();
context.term();
}
use of org.zeromq.ZMQ.Context in project jeromq by zeromq.
the class TestZMQ method testEventClosed.
@Test
public void testEventClosed() {
Context context = ZMQ.context(1);
Socket monitor = context.socket(ZMQ.PAIR);
try {
ZMQ.Event event;
Socket socket = context.socket(ZMQ.REP);
monitor.setReceiveTimeOut(100);
socket.bindToRandomPort("tcp://127.0.0.1");
assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_CLOSED));
monitor.connect("inproc://monitor.socket");
socket.close();
event = ZMQ.Event.recv(monitor);
assertNotNull("No event was received", event);
assertEquals(ZMQ.EVENT_CLOSED, event.getEvent());
} finally {
monitor.close();
context.term();
}
}
use of org.zeromq.ZMQ.Context in project jeromq by zeromq.
the class TestZMQ method testEventDisconnected.
@Test
public void testEventDisconnected() {
Context context = ZMQ.context(1);
ZMQ.Event event;
Socket socket = context.socket(ZMQ.REP);
Socket monitor = context.socket(ZMQ.PAIR);
Socket helper = context.socket(ZMQ.REQ);
monitor.setReceiveTimeOut(100);
int port = socket.bindToRandomPort("tcp://127.0.0.1");
helper.connect("tcp://127.0.0.1:" + port);
assertTrue(socket.monitor("inproc://monitor.socket", ZMQ.EVENT_DISCONNECTED));
monitor.connect("inproc://monitor.socket");
helper.close();
event = ZMQ.Event.recv(monitor);
assertNotNull("No event was received", event);
assertEquals(ZMQ.EVENT_DISCONNECTED, event.getEvent());
socket.close();
monitor.close();
context.term();
}
use of org.zeromq.ZMQ.Context in project aion by aionnetwork.
the class ProtocolProcessor method run.
@Override
public void run() {
LOG.info("Starting Aion Api Server <port={}>", cfgApi.getPort());
String bindAddr = "tcp://" + cfgApi.getIp() + ":" + cfgApi.getPort();
int msgTh = 5;
try {
// create context.
Context ctx = ZMQ.context(1);
// create router sock.
Socket feSock = ctx.socket(ROUTER);
if (cfgApi.isSecureConnectEnabledEnabled()) {
// Currently the system will only load the first pair of the key files.
loadCurveKeyPair();
if (curveSecKey != null && curvePubKey != null) {
feSock.setZAPDomain("global".getBytes());
feSock.setCurveServer(true);
feSock.setCurvePublicKey(curvePubKey);
feSock.setCurveSecretKey(curveSecKey);
LOG.info("Secure connection enabled!");
} else {
LOG.info("Can't find the keyfile for setup the connection. Secure connection disabled!");
}
} else {
LOG.info("Secure connection disabled!");
}
feSock.setSndHWM(zmqHWM);
feSock.bind(bindAddr);
Socket wkSocks = ctx.socket(DEALER);
wkSocks.bind(AION_ZMQ_WK_TH);
Socket cbSock = ctx.socket(DEALER);
cbSock.bind(AION_ZMQ_CB_TH);
Socket evSock = ctx.socket(DEALER);
evSock.bind(AION_ZMQ_EV_TH);
Socket hbSock = ctx.socket(DEALER);
hbSock.bind(AION_ZMQ_HB_TH);
ExecutorService es = Executors.newFixedThreadPool(msgTh);
es.execute(() -> callbackRun(ctx));
es.execute(this::txWaitRun);
es.execute(() -> eventRun(ctx));
es.execute(() -> workerRun(ctx));
es.execute(() -> hbRun(ctx));
Proxy.proxy(feSock, wkSocks, cbSock, evSock, hbSock);
if (LOG.isInfoEnabled()) {
LOG.info("ProtocolProcessor.run thread finish.");
}
if (LOG.isInfoEnabled()) {
LOG.info("Shutting down Zmq sockets...");
}
// Shutdown HdlrZmq
((HdlrZmq) handler).shutdown();
// Shutdown ZmqSocket
feSock.close();
wkSocks.close();
cbSock.close();
evSock.close();
hbSock.close();
// Shutdown ExecutorService
es.shutdown();
ctx.close();
if (LOG.isInfoEnabled()) {
LOG.info("Shutdown Zmq sockets... Done!");
}
} catch (Exception e) {
if (LOG.isErrorEnabled()) {
LOG.error("ProtocolProcessor.run exception: " + e.getMessage());
}
}
}
Aggregations