Search in sources :

Example 6 with PollItem

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

the class bstar method voter.

//  .split voter method
//  This method registers a client voter socket. Messages received
//  on this socket provide the CLIENT_REQUEST events for the Binary Star
//  FSM and are passed to the provided application handler. We require
//  exactly one voter per {{bstar}} instance:
public int voter(String endpoint, int type, IZLoopHandler handler, Object arg) {
    //  Hold actual handler+arg so we can call this later
    Socket socket = ctx.createSocket(type);
    socket.bind(endpoint);
    voterFn = handler;
    voterArg = arg;
    PollItem poller = new PollItem(socket, ZMQ.Poller.POLLIN);
    return loop.addPoller(poller, VoterReady, this);
}
Also used : PollItem(org.zeromq.ZMQ.PollItem) Socket(org.zeromq.ZMQ.Socket)

Example 7 with PollItem

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

the class lbbroker3 method main.

/**
     * And the main task now sets-up child tasks, then starts its reactor.
     * If you press Ctrl-C, the reactor exits and the main task shuts down.
     */
public static void main(String[] args) {
    ZContext context = new ZContext();
    LBBroker arg = new LBBroker();
    //  Prepare our context and sockets
    arg.frontend = context.createSocket(ZMQ.ROUTER);
    arg.backend = context.createSocket(ZMQ.ROUTER);
    arg.frontend.bind("ipc://frontend.ipc");
    arg.backend.bind("ipc://backend.ipc");
    int clientNbr;
    for (clientNbr = 0; clientNbr < NBR_CLIENTS; clientNbr++) ZThread.start(new ClientTask());
    for (int workerNbr = 0; workerNbr < NBR_WORKERS; workerNbr++) ZThread.start(new WorkerTask());
    //  Queue of available workers
    arg.workers = new LinkedList<ZFrame>();
    //  Prepare reactor and fire it up
    ZLoop reactor = new ZLoop(context);
    PollItem item = new PollItem(arg.backend, ZMQ.Poller.POLLIN);
    reactor.addPoller(item, backendHandler, arg);
    reactor.start();
    context.destroy();
}
Also used : ZFrame(org.zeromq.ZFrame) PollItem(org.zeromq.ZMQ.PollItem) ZLoop(org.zeromq.ZLoop) ZContext(org.zeromq.ZContext)

Example 8 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 : PollItem(org.zeromq.ZMQ.PollItem) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 9 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 : PollItem(org.zeromq.ZMQ.PollItem) Socket(org.zeromq.ZMQ.Socket) Test(org.junit.Test)

Example 10 with PollItem

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

the class clonesrv5 method run.

public void run() {
    //  Register our handlers with reactor
    PollItem poller = new PollItem(snapshot, ZMQ.Poller.POLLIN);
    loop.addPoller(poller, new Snapshots(), this);
    poller = new PollItem(collector, ZMQ.Poller.POLLIN);
    loop.addPoller(poller, new Collector(), this);
    loop.addTimer(1000, 0, new FlushTTL(), this);
    loop.start();
    loop.destroy();
    ctx.destroy();
}
Also used : PollItem(org.zeromq.ZMQ.PollItem)

Aggregations

PollItem (org.zeromq.ZMQ.PollItem)10 Socket (org.zeromq.ZMQ.Socket)6 Test (org.junit.Test)4 ZFrame (org.zeromq.ZFrame)3 ZContext (org.zeromq.ZContext)2 ZLoop (org.zeromq.ZLoop)2 ZMsg (org.zeromq.ZMsg)1