/** * 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)); }
/** * Sends a message. * * <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. * * <p> * * <p>This gets being very similar to automaton programming :) * * @param address receiver of message * @param message mail entry * @param type way of sending a message: TCP, single UPD... * @param timeout timeout in milliseconds * @param receiveListener an action to invoke when got an answer. * @param failListener an action to invoke when timeout exceeded. * @param <ReplyType> response message type */ public <ReplyType extends ResponseMessage> void send( InetSocketAddress address, RequestMessage<ReplyType> message, DispatchType type, int timeout, ReceiveListener<ReplyType> receiveListener, FailListener failListener) { BlockingQueue<ResponseMessage> responseContainer = submit(address, message, type); // TODO: make in single thread scheduledExecutor.schedule( () -> { //noinspection unchecked ReplyType response = (ReplyType) responseContainer.poll(); if (response != null) receiveListener.onReceive(address, response); else failListener.onFail(address); }, timeout, TimeUnit.MILLISECONDS); // TODO: clear responseWaiters map }