public ZPoller getZPoller() { return new ZPoller(context); }
/** * Issue a poll call, using the specified timeout value. * <p> * Since ZeroMQ 3.0, the timeout parameter is in <i>milliseconds<i>, * but prior to this the unit was <i>microseconds</i>. * * @param timeout * the timeout, as per zmq_poll (); * if -1, it will block indefinitely until an event * happens; if 0, it will return immediately; * otherwise, it will wait for at most that many * milliseconds/microseconds (see above). * @see "http://api.zeromq.org/3-0:zmq-poll" * @return how many objects where signaled by poll () */ public int poll(final long timeout) { return poll(timeout, true); }
public final boolean register(final SelectableChannel channel, final EventsHandler handler) { return register(channel, handler, IN | OUT | ERR); }
/** * Register a Socket for polling on specified events. * * @param socket the registering socket. * @param handler the events handler for this socket * @param events the events to listen to, as a mask composed by ORing POLLIN, POLLOUT and POLLERR. * @return true if registered, otherwise false */ public final boolean register(final Socket socket, final EventsHandler handler, final int events) { if (socket == null) { return false; } return add(socket, create(socket, handler, events)); }
private boolean pause(ZPoller poller, boolean pause) { state.paused = pause; if (pause) { poller.unregister(frontend); poller.unregister(backend); // TODO why not a mechanism for eventually flushing the sockets during the pause? } else { poller.register(frontend, ZPoller.POLLIN); poller.register(backend, ZPoller.POLLIN); // Now Wait also until there are either requests or replies to process. } return true; }
try (ZPoller zpoller = ctx.getZPoller()) { logger.trace("new poller: {}", zpoller); zpoller.register(listeningSocket, new ZMQHandler((socket, events) -> { while ((socket.getEvents() & ZPoller.IN) != 0) { int received = socket.recv(databuffer, 0, 65535, 0); zpoller.register(stopPair[1], new ZMQHandler((socket, events) -> { return false; }), ZPoller.ERR | ZPoller.IN); zpoller.setGlobalHandler(new ZMQHandler((socket, events) -> { return true; })); while (running && ! isInterrupted() && ctx.isRunning() && zpoller.poll(-1) > 0) {
public Double(final ZContext ctx, final Socket pipe, final Selector selector, final Actor actor, final Object... args) { this.context = ctx; this.pipe = pipe; this.actor = actor; final List<Socket> created = actor.createSockets(ctx, args); assert (created != null); sockets = new ArrayList<>(created); poller = new ZPoller(selector); poller.setGlobalHandler(this); }
/** * Issue a poll call, using the specified timeout value. * * @param timeout the timeout, as per zmq_poll (); * @param dispatchEvents true to dispatch events using items handler and the global one. * @see "http://api.zeromq.org/3-0:zmq-poll" * @return how many objects where signaled by poll () */ protected int poll(final long timeout, final boolean dispatchEvents) { // get all the raw items final Collection<ItemHolder> all = items(); final Set<zmq.poll.PollItem> pollItems = new HashSet<>(all.size()); for (ItemHolder holder : all) { pollItems.add(holder.item()); } // polling time final int rc = poll(selector, timeout, pollItems); if (!dispatchEvents) { // raw result return rc; } if (dispatch(all, pollItems.size())) { // returns event counts after dispatch if everything went fine return rc; } // error in dispatching return -1; }
/** * Tells if a socket is in error from this poller. * * @param socket the socket to ask for. * @return true if in error, otherwise false */ public boolean isError(final Socket socket) { return error((Object) socket); }
/** * Creates a new poller. * * @param creator the items creator * @param selector the selector to use for polling. */ public ZPoller(final ItemCreator creator, final Selector selector) { this.creator = creator; this.selector = selector; items = new HashMap<>(); all = createContainer(0); }
/** * Register an ItemHolder for polling on specified events. * * @param item the registering item. * @return true if registered, otherwise false */ public final boolean register(final ItemHolder item) { return add(null, item); }
/** * Destroys the poller. Does actually nothing. */ @Override public void close() throws IOException { destroy(); }
public boolean dispatch() { return dispatch(all, all.size()); }
/** * Register a SelectableChannel for polling on specified events. * * @param channel the registering channel. * @param handler the events handler for this socket * @param events the events to listen to, as a mask composed by XORing POLLIN, POLLOUT and POLLERR. * @return true if registered, otherwise false */ public final boolean register(final SelectableChannel channel, final EventsHandler handler, final int events) { if (channel == null) { return false; } return add(channel, create(channel, handler, events)); }
/** * Tells if a channel is in error from this poller. * * @param channel the channel to ask for. * @return true if in error, otherwise false */ public boolean isError(final SelectableChannel channel) { return error((Object) channel); }
protected boolean add(Object socketOrChannel, final ItemHolder holder) { if (socketOrChannel == null) { Socket socket = holder.socket(); SelectableChannel ch = holder.item().getRawSocket(); if (ch == null) { // not a channel assert (socket != null); socketOrChannel = socket; } else if (socket == null) { // not a socket assert (ch != null); socketOrChannel = ch; } } assert (socketOrChannel != null); Set<ItemHolder> holders = items.get(socketOrChannel); if (holders == null) { holders = createContainer(1); items.put(socketOrChannel, holders); } final boolean rc = holders.add(holder); if (rc) { all.add(holder); } return rc; }
public final boolean register(final Socket socket, final EventsHandler handler) { return register(socket, handler, POLLIN | POLLOUT | POLLERR); }
@Override public int breathe() { long timeout = actor.looping(pipe, poller); return poller.poll(timeout); // events have been dispatched, }
public boolean error(final Socket socket) { return error((Object) socket); }
public final boolean register(final SelectableChannel channel, final int events) { return register(channel, globalHandler, events); }