Search in sources :

Example 1 with IZLoopHandler

use of org.zeromq.ZLoop.IZLoopHandler in project jeromq by zeromq.

the class TestZLoop method testZLoopWithUDP.

@Test
public void testZLoopWithUDP() throws IOException {
    final int port = Utils.findOpenPort();
    final InetSocketAddress addr = new InetSocketAddress("127.0.0.1", port);
    ZLoop loop = new ZLoop(ctx);
    DatagramChannel udpIn = DatagramChannel.open();
    assertThat(udpIn, notNullValue());
    udpIn.configureBlocking(false);
    udpIn.socket().bind(new InetSocketAddress(port));
    DatagramChannel udpOut = DatagramChannel.open();
    assertThat(udpOut, notNullValue());
    udpOut.configureBlocking(false);
    udpOut.socket().connect(addr);
    final AtomicInteger counter = new AtomicInteger();
    final AtomicBoolean done = new AtomicBoolean();
    loop.addPoller(new PollItem(udpIn, ZMQ.Poller.POLLIN), new IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            DatagramChannel udpIn = (DatagramChannel) arg;
            ByteBuffer bb = ByteBuffer.allocate(3);
            try {
                udpIn.receive(bb);
                String read = new String(bb.array(), 0, bb.limit(), ZMQ.CHARSET);
                assertThat(read, is("udp"));
                done.set(true);
                counter.incrementAndGet();
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            return -1;
        }
    }, udpIn);
    loop.addPoller(new PollItem(udpOut, ZMQ.Poller.POLLOUT), new IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            DatagramChannel udpOut = (DatagramChannel) arg;
            try {
                ByteBuffer bb = ByteBuffer.allocate(3);
                bb.put("udp".getBytes(ZMQ.CHARSET));
                bb.flip();
                int written = udpOut.send(bb, addr);
                assertThat(written, is(3));
                counter.incrementAndGet();
            } catch (IOException e) {
                e.printStackTrace();
                fail();
            }
            return 0;
        }
    }, udpOut);
    loop.start();
    assertThat(done.get(), is(true));
    assertThat(counter.get(), is(2));
    udpIn.close();
    udpOut.close();
}
Also used : PollItem(org.zeromq.ZMQ.PollItem) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(java.nio.channels.DatagramChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IZLoopHandler(org.zeromq.ZLoop.IZLoopHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 DatagramChannel (java.nio.channels.DatagramChannel)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1 IZLoopHandler (org.zeromq.ZLoop.IZLoopHandler)1 PollItem (org.zeromq.ZMQ.PollItem)1