Search in sources :

Example 11 with PollItem

use of org.zeromq.ZMQ.PollItem in project aion by aionnetwork.

the class ZLoop method addPoller.

// --------------------------------------------------------------------------
// Register pollitem with the reactor. When the pollitem is ready, will call
// the handler, passing the arg. Returns 0 if OK, -1 if there was an error.
// If you register the pollitem more than once, each instance will invoke its
// corresponding handler.
public int addPoller(PollItem item_, IZLoopHandler handler, Object arg) {
    PollItem item = item_;
    if (item.getRawSocket() == null && item.getSocket() == null)
        return -1;
    SPoller poller = new SPoller(item_, handler, arg);
    pollers.add(poller);
    dirty = true;
    if (verbose)
        System.out.printf("I: zloop: register %s poller (%s, %s)\n", item.getSocket() != null ? item.getSocket().getType() : "RAW", item.getSocket(), item.getRawSocket());
    return 0;
}
Also used : PollItem(org.zeromq.ZMQ.PollItem)

Example 12 with PollItem

use of org.zeromq.ZMQ.PollItem in project aion by aionnetwork.

the class ZLoop method removePoller.

// --------------------------------------------------------------------------
// Cancel a pollitem from the reactor, specified by socket or FD. If both
// are specified, uses only socket. If multiple poll items exist for same
// socket/FD, cancels ALL of them.
public void removePoller(PollItem item_) {
    PollItem item = item_;
    Iterator<SPoller> it = pollers.iterator();
    while (it.hasNext()) {
        SPoller p = it.next();
        if (item.equals(p.item)) {
            it.remove();
            dirty = true;
        }
    }
    if (verbose)
        System.out.printf("I: zloop: cancel %s poller (%s, %s)", item.getSocket() != null ? item.getSocket().getType() : "RAW", item.getSocket(), item.getRawSocket());
}
Also used : PollItem(org.zeromq.ZMQ.PollItem)

Example 13 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) {
    // Prepare our context and sockets
    try (ZContext context = new ZContext()) {
        LBBroker arg = new LBBroker();
        arg.frontend = context.createSocket(SocketType.ROUTER);
        arg.backend = context.createSocket(SocketType.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();
    }
}
Also used : PollItem(org.zeromq.ZMQ.PollItem)

Example 14 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)

Example 15 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, SocketType 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)

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