Search in sources :

Example 6 with PollItem

use of zmq.poll.PollItem in project jeromq by zeromq.

the class ZPoller method dispatch.

/**
 * Dispatches the polled events.
 *
 * @param all   the items used for dispatching
 * @param size  the number of items to dispatch
 * @return true if correctly dispatched, false in case of error
 */
protected boolean dispatch(final Collection<? extends ItemHolder> all, int size) {
    ItemHolder[] array = all.toArray(new ItemHolder[all.size()]);
    // protected against handlers unregistering during this loop
    for (ItemHolder holder : array) {
        EventsHandler handler = holder.handler();
        if (handler == null) {
            handler = globalHandler;
        }
        if (handler == null) {
            // no handler, short-circuit
            continue;
        }
        final PollItem item = holder.item();
        final int events = item.readyOps();
        if (events <= 0) {
            // no events, short-circuit
            continue;
        }
        final Socket socket = holder.socket();
        final SelectableChannel channel = holder.item().getRawSocket();
        if (socket != null) {
            assert (channel == null);
            // dispatch on socket
            if (!handler.events(socket, events)) {
                return false;
            }
        }
        if (channel != null) {
            // dispatch on channel
            assert (socket == null);
            if (!handler.events(channel, events)) {
                return false;
            }
        }
    }
    return true;
}
Also used : PollItem(zmq.poll.PollItem) SelectableChannel(java.nio.channels.SelectableChannel) Socket(org.zeromq.ZMQ.Socket)

Aggregations

PollItem (zmq.poll.PollItem)6 SelectableChannel (java.nio.channels.SelectableChannel)2 Selector (java.nio.channels.Selector)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 SelectionKey (java.nio.channels.SelectionKey)1 HashMap (java.util.HashMap)1 Socket (org.zeromq.ZMQ.Socket)1