/** * Pings a peer. * * @param peerAddress The peer address of the remote peer. * @param isUDP Set to true if UDP should be used, false for TCP. * @return The future response */ public FutureResponse ping(PeerAddress peerAddress, final boolean isUDP) { final RequestHandler<FutureResponse> request = peer.pingRPC().ping(peerAddress, connectionConfiguration); if (isUDP) { FutureChannelCreator fcc = peer.connectionBean().reservation().create(1, 0); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isSuccess()) { FutureResponse futureResponse = request.sendUDP(future.channelCreator()); Utils.addReleaseListener(future.channelCreator(), futureResponse); } else { request.futureResponse().failed(future); } } }); } else { FutureChannelCreator fcc = peer.connectionBean().reservation().create(0, 1); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isSuccess()) { FutureResponse futureResponse = request.sendTCP(future.channelCreator()); Utils.addReleaseListener(future.channelCreator(), futureResponse); } else { request.futureResponse().failed(future); } } }); } return request.futureResponse(); }
FutureLateJoin<FutureResponse> pingBroadcast(final int port) { final Bindings bindings = peer.connectionBean().sender().channelClientConfiguration().externalBindings(); final int size = bindings.broadcastAddresses().size(); final FutureLateJoin<FutureResponse> futureLateJoin = new FutureLateJoin<FutureResponse>(size, 1); if (size > 0) { FutureChannelCreator fcc = peer.connectionBean().reservation().create(size, 0); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(FutureChannelCreator future) throws Exception { if (future.isSuccess()) { Utils.addReleaseListener(future.channelCreator(), futureLateJoin); for (int i = 0; i < size; i++) { final InetAddress broadcastAddress = bindings.broadcastAddresses().get(i); final PeerAddress peerAddress = new PeerAddress(Number160.ZERO, broadcastAddress, port, port); FutureResponse validBroadcast = peer.pingRPC() .pingBroadcastUDP( peerAddress, future.channelCreator(), connectionConfiguration); if (!futureLateJoin.add(validBroadcast)) { // the latejoin future is fininshed if the add returns false break; } } } else { futureLateJoin.failed(future); } } }); } else { futureLateJoin.failed("No broadcast address found. Cannot ping nothing"); } return futureLateJoin; }
public FutureDirect start() { if (peer.isShutdown()) { return FUTURE_REQUEST_SHUTDOWN; } final boolean keepAlive; final PeerAddress remotePeer; if (recipientAddress != null && recipientConnection == null) { keepAlive = false; remotePeer = recipientAddress; } else if (recipientAddress == null && recipientConnection != null) { keepAlive = true; remotePeer = recipientConnection.remotePeer(); } else if (peerConnection != null) { keepAlive = true; remotePeer = peerConnection.remotePeer(); } else { throw new IllegalArgumentException("either remotePeer or connection has to be set"); } if (futureChannelCreator == null) { futureChannelCreator = peer.connectionBean().reservation().create(isForceUDP() ? 1 : 0, isForceUDP() ? 0 : 1); } final RequestHandler<FutureResponse> request = peer.directDataRPC().sendInternal(remotePeer, this); if (keepAlive) { if (peerConnection != null) { sendDirectRequest(request, peerConnection); } else { recipientConnection.addListener( new BaseFutureAdapter<FuturePeerConnection>() { @Override public void operationComplete(final FuturePeerConnection future) throws Exception { if (future.isSuccess()) { sendDirectRequest(request, future.peerConnection()); } else { request.futureResponse().failed("Could not acquire channel (1)", future); } } }); } } else { futureChannelCreator.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isSuccess()) { final FutureResponse futureResponse = request.sendTCP(future.channelCreator()); Utils.addReleaseListener(future.channelCreator(), futureResponse); } else { request.futureResponse().failed("could not create channel", future); } } }); } return new FutureDirect(request.futureResponse()); }