private void process(RequestMessage request) { if (unique.equals(request.getIdentifier().unique)) return; for (ReplyProtocol replyProtocol : replyProtocols) { try { ResponseMessage response = tryApplyProtocol(replyProtocol, request); if (response != null) { response.setIdentifier(request.getIdentifier()); forwardSingle(request.getResponseListenerAddress(), response, DispatchType.PLAIN); } return; } catch (ClassCastException ignored) { } } logger.trace( String.format("Message of type %s has been ignored", request.getClass().getSimpleName())); }
/** * Sends a broadcast. * * <p>Node will NOT receive its own request. (i.e. response protocols of this sender will ignore * any broadcast from itself (or from sender with same address?)). * * <p>Current thread is NOT blocked by this method call. But no two response-actions (onReceive or * onFail on any request) or response protocols will be executed at same time, so you can write * not thread-safe code inside them. * * @param message mail entry * @param timeout timeout in milliseconds * @param receiveListener is executed when get a response * @param <ReplyType> response type */ public <ReplyType extends ResponseMessage> void broadcast( RequestMessage<ReplyType> message, int timeout, ReceiveListener<ReplyType> receiveListener) { // TODO: thing through logic submit( null, message, DispatchType.PLAIN, response -> receiveListener.onReceive(message.getResponseListenerAddress(), response)); }
private <ReplyType extends ResponseMessage> void submit( InetSocketAddress address, RequestMessage<ReplyType> message, DispatchType type, Consumer<ReplyType> consumer) { MessageIdentifier identifier = new MessageIdentifier(unique); message.setIdentifier(identifier); message.setResponseListenerAddress(udpListener.getListeningAddress()); responsesWaiters.put( identifier, responseMessage -> { try { //noinspection unchecked ReplyType casted = (ReplyType) responseMessage; consumer.accept(casted); } catch (ClassCastException e) { logger.warn("Accepted message of wrong type", e); } }); forwardSingle(address, message, type); }