Search in sources :

Example 16 with PollItem

use of org.zeromq.ZMQ.PollItem in project jeromq by zeromq.

the class TestZLoop method testZLoop.

@Test
public void testZLoop() {
    int rc = 0;
    // setUp() should create the context
    assert (ctx != null);
    ZLoop loop = new ZLoop(ctx);
    assert (loop != null);
    ZLoop.IZLoopHandler timerEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            ((Socket) arg).send("PING", 0);
            return 0;
        }
    };
    ZLoop.IZLoopHandler socketEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            received = ((Socket) arg).recvStr(0);
            // Just end the reactor
            return -1;
        }
    };
    // After 10 msecs, send a ping message to output
    loop.addTimer(10, 1, timerEvent, input);
    // When we get the ping message, end the reactor
    PollItem pollInput = new PollItem(output, Poller.POLLIN);
    rc = loop.addPoller(pollInput, socketEvent, output);
    Assert.assertEquals(0, rc);
    loop.start();
    loop.removePoller(pollInput);
    Assert.assertEquals("PING", received);
}
Also used : IZLoopHandler(org.zeromq.ZLoop.IZLoopHandler) PollItem(org.zeromq.ZMQ.PollItem) IZLoopHandler(org.zeromq.ZLoop.IZLoopHandler) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 17 with PollItem

use of org.zeromq.ZMQ.PollItem in project jeromq by zeromq.

the class TestZLoop method testZLoopAddTimerFromTimer.

@Test
public void testZLoopAddTimerFromTimer() {
    int rc = 0;
    ZLoop loop = new ZLoop(ctx);
    assert (loop != null);
    ZLoop.IZLoopHandler timerEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            final long now = System.currentTimeMillis();
            ZLoop.IZLoopHandler timerEvent2 = new ZLoop.IZLoopHandler() {

                @Override
                public int handle(ZLoop loop, PollItem item, Object arg) {
                    final long now2 = System.currentTimeMillis();
                    assert (now2 >= now + 10);
                    ((Socket) arg).send("PING", 0);
                    return 0;
                }
            };
            loop.addTimer(10, 1, timerEvent2, arg);
            return 0;
        }
    };
    ZLoop.IZLoopHandler socketEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            received = ((Socket) arg).recvStr(0);
            // Just end the reactor
            return -1;
        }
    };
    // After 10 msecs, fire a timer that registers
    // another timer that sends the ping message
    loop.addTimer(10, 1, timerEvent, input);
    // When we get the ping message, end the reactor
    PollItem pollInput = new PollItem(output, Poller.POLLIN);
    rc = loop.addPoller(pollInput, socketEvent, output);
    Assert.assertEquals(0, rc);
    loop.start();
    loop.removePoller(pollInput);
    Assert.assertEquals("PING", received);
}
Also used : IZLoopHandler(org.zeromq.ZLoop.IZLoopHandler) PollItem(org.zeromq.ZMQ.PollItem) IZLoopHandler(org.zeromq.ZLoop.IZLoopHandler) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 18 with PollItem

use of org.zeromq.ZMQ.PollItem in project jeromq by zeromq.

the class ZLoopTest method testZLoopAddTimerFromTimer.

@Test
public void testZLoopAddTimerFromTimer() {
    int rc = 0;
    ZLoop loop = new ZLoop(ctx);
    assertThat(loop, notNullValue());
    ZLoop.IZLoopHandler timerEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            final long now = System.currentTimeMillis();
            ZLoop.IZLoopHandler timerEvent2 = new ZLoop.IZLoopHandler() {

                @Override
                public int handle(ZLoop loop, PollItem item, Object arg) {
                    final long now2 = System.currentTimeMillis();
                    assertThat(now2 >= now + 10, is(true));
                    ((Socket) arg).send("PING", 0);
                    return 0;
                }
            };
            loop.addTimer(10, 1, timerEvent2, arg);
            return 0;
        }
    };
    ZLoop.IZLoopHandler socketEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            received = ((Socket) arg).recvStr(0);
            // Just end the reactor
            return -1;
        }
    };
    // After 10 msecs, fire a timer that registers
    // another timer that sends the ping message
    loop.addTimer(10, 1, timerEvent, input);
    // When we get the ping message, end the reactor
    PollItem pollInput = new PollItem(output, Poller.POLLIN);
    rc = loop.addPoller(pollInput, socketEvent, output);
    assertThat(rc, is(0));
    loop.start();
    loop.removePoller(pollInput);
    assertThat(received, is("PING"));
}
Also used : PollItem(org.zeromq.ZMQ.PollItem) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 19 with PollItem

use of org.zeromq.ZMQ.PollItem in project jeromq by zeromq.

the class ZLoopTest method testZLoopAddTimerFromSocketHandler.

@Test(timeout = 1000)
public void testZLoopAddTimerFromSocketHandler() {
    int rc = 0;
    ZLoop loop = new ZLoop(ctx);
    assertThat(loop, notNullValue());
    ZLoop.IZLoopHandler timerEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            ((Socket) arg).send("PING", 0);
            return 0;
        }
    };
    ZLoop.IZLoopHandler socketEvent = new ZLoop.IZLoopHandler() {

        @Override
        public int handle(ZLoop loop, PollItem item, Object arg) {
            final long now = System.currentTimeMillis();
            ZLoop.IZLoopHandler timerEvent2 = new ZLoop.IZLoopHandler() {

                @Override
                public int handle(ZLoop loop, PollItem item, Object arg) {
                    final long now2 = System.currentTimeMillis();
                    assertThat(now2 >= now + 10, is(true));
                    received = ((Socket) arg).recvStr(0);
                    // Just end the reactor
                    return -1;
                }
            };
            // After 10 msec fire a timer that ends the reactor
            loop.addTimer(10, 1, timerEvent2, arg);
            return 0;
        }
    };
    // Fire a timer that sends the ping message
    loop.addTimer(0, 1, timerEvent, input);
    // When we get the ping message, end the reactor
    PollItem pollInput = new PollItem(output, Poller.POLLIN);
    rc = loop.addPoller(pollInput, socketEvent, output);
    assertThat(rc, is(0));
    loop.start();
    loop.removePoller(pollInput);
    assertThat(received, is("PING"));
}
Also used : PollItem(org.zeromq.ZMQ.PollItem) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Aggregations

PollItem (org.zeromq.ZMQ.PollItem)19 Socket (org.zeromq.ZMQ.Socket)11 Test (org.junit.Test)9 IZLoopHandler (org.zeromq.ZLoop.IZLoopHandler)5 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