org.zeromq.ZMQ.PollItem Java Examples

The following examples show how to use org.zeromq.ZMQ.PollItem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZLoop.java    From aion with MIT License 6 votes vote down vote up
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());

}
 
Example #2
Source File: ZAuth.java    From aion with MIT License 5 votes vote down vote up
@Override
public void run(Object[] args, ZContext ctx, Socket pipe) {
    this.pipe = pipe;

    //create ZAP handler and get ready for requests
    handler = ctx.createSocket(ZMQ.REP);
    try {
        handler.bind("inproc://zeromq.zap.01");
    } catch (ZMQException e) {
        pipe.send("ERROR");
        return;
    }

    pipe.send("OK");

    PollItem[] pollItems = {new PollItem(pipe, Poller.POLLIN), new PollItem(handler, Poller.POLLIN)};
    while (!terminated && !Thread.currentThread().isInterrupted()) {
        int rc = ZMQ.poll(pollItems, -1);
        if (rc == -1) {
            break; //interrupt

        }

        if (pollItems[0].isReadable()) {
            if (!controlMessage()) {
                break;
            }
        }

        if (pollItems[1].isReadable()) {
            if (!authenticate()) {
                break;
            }
        }
    }
}
 
Example #3
Source File: ZLoop.java    From aion with MIT License 5 votes vote down vote up
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;
    }
 
Example #4
Source File: ZmqLazyPirateGateway.java    From spring-integration-zmq with Apache License 2.0 5 votes vote down vote up
protected Object handleRequestMessage(final Message<?> requestMessage) {
	if (!running) {
		return null;
	}
	
	Future<Object> response = executorService.submit(new Callable<Object>() {
		public Object call() throws Exception {
			byte[] requestData = requestConverter.convert(requestMessage.getPayload());
			int retriesLeft = retryCount;
			while (!Thread.currentThread().isInterrupted()) {
				socket.send(requestData);
				PollItem items[] = { new PollItem(socket, Poller.POLLIN) };
				int rc = ZMQ.poll(items, socketReceiveTimeout);
				if (rc == -1) {
					break;
				}
				if (items[0].isReadable()) {
					byte[] reply = socket.recv();
					return replyConverter.convert(reply);
				} else if (--retriesLeft == 0) {
					break;
				} else {
					ZmqLazyPirateGateway.this.connect();
				}
			}
			ZmqLazyPirateGateway.this.connect();
			return null;
		}
	});
			
	try {
		return response.get();
	} catch (Throwable t) {
		throw new MessageHandlingException(requestMessage, t);
	}
}
 
Example #5
Source File: ZLoop.java    From aion with MIT License 4 votes vote down vote up
protected SPoller(PollItem item, IZLoopHandler handler, Object arg) {
    this.item = item;
    this.handler = handler;
    this.arg = arg;
    errors = 0;
}
 
Example #6
Source File: Proxy.java    From aion with MIT License 4 votes vote down vote up
static void proxy(Socket frontend, Socket backend, Socket callback, Socket event, Socket hb) {
    PollItem[] items = new PollItem[5];
    items[0] = new PollItem(frontend, Poller.POLLIN);
    items[1] = new PollItem(backend, Poller.POLLIN);
    items[2] = new PollItem(callback, Poller.POLLIN);
    items[3] = new PollItem(event, Poller.POLLIN);
    items[4] = new PollItem(hb, Poller.POLLIN);

    try {
        while (!shutDown.get()) {
            // Wait while there are either requests or replies to process.
            int rc = ZMQ.poll(items, 3000);
            if (rc < 0) {
                continue;
            }

            // Process a request.
            if (items[0].isReadable()) {
                while (true) {
                    if (msgProcessRecv(frontend, backend, hb)) {
                        return;
                    }
                    break;
                }
            }
            // Process a reply.
            if (items[1].isReadable()) {
                while (true) {
                    if (msgProcessSend(backend, frontend)) {
                        return;
                    }
                    break;
                }
            }

            // Process a callback
            if (items[2].isReadable()) {
                while (true) {
                    if (msgProcessSend(callback, frontend)) {
                        return;
                    }
                    break;
                }
            }

            if (items[3].isReadable()) {
                while (true) {
                    if (msgProcessSend(event, frontend)) {
                        return;
                    }
                    break;
                }
            }

            // heartBeat reply
            if (items[4].isReadable()) {
                while (true) {
                    if (msgProcessSend(hb, frontend)) {
                        return;
                    }
                    break;
                }
            }
        }

        LOG.debug("zmq-proxy thread was interrupted.");
    } catch (Exception e) {
        LOG.error("aion.api.server.zmq.Proxy exception" + e.getMessage());
    }
}
 
Example #7
Source File: MsgExecutor.java    From aion_api with MIT License 4 votes vote down vote up
private void proxy(
        Socket feSocket, Socket beSocket, Socket cbSocket, Socket nbDealer, Socket hbDealer) {
    PollItem[] items = new PollItem[4];
    items[0] = new PollItem(feSocket, ZMQ.Poller.POLLIN);
    items[1] = new PollItem(beSocket, ZMQ.Poller.POLLIN);
    items[2] = new PollItem(nbDealer, ZMQ.Poller.POLLIN);
    items[3] = new PollItem(hbDealer, ZMQ.Poller.POLLIN);

    try {
        this.isInitialized.set(true);

        while (this.running) {
            //  Wait while there are either requests or replies to process.
            int rc = ZMQ.poll(items, 3000);
            if (rc < 1) {
                if (this.running && LOGGER.isDebugEnabled()) {
                    LOGGER.debug("ZMQ.poll error rc:{}", rc);
                }
                continue;
            }

            //  Process a reply.
            if (items[0].isReadable()) {
                while (true) {
                    if (!msgHandle(feSocket, beSocket, cbSocket, nbDealer, hbDealer)) {
                        throw new Exception("ZMQ items[0] handle abnormal!");
                    }
                    break;
                }
            }
            //  Process a request.
            if (items[1].isReadable()) {
                while (true) {
                    if (invalidMsgHandle(beSocket, feSocket)) {
                        throw new Exception("ZMQ items[1] handle abnormal!");
                    }
                    break;
                }
            }

            //  Process a request.
            if (items[2].isReadable()) {
                while (true) {
                    if (invalidMsgHandle(nbDealer, feSocket)) {
                        throw new Exception("ZMQ items[2] handle abnormal!");
                    }
                    break;
                }
            }

            if (items[3].isReadable()) {
                while (true) {
                    if (invalidMsgHandle(hbDealer, feSocket)) {
                        throw new Exception("ZMQ items[3] handle abnormal!");
                    }
                    break;
                }
            }
        }
    } catch (Exception e) {
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("proxy exception: [{}]", e.getMessage());
        }
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("socket proxy exit!");
    }
}
 
Example #8
Source File: ZLoop.java    From aion with MIT License votes vote down vote up
public int handle(ZLoop loop, PollItem item, Object arg);