/** * 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(); }
private FutureBootstrap bootstrap() { final FutureWrappedBootstrap<FutureDone<Pair<FutureRouting, FutureRouting>>> result = new FutureWrappedBootstrap<FutureDone<Pair<FutureRouting, FutureRouting>>>(); result.setBootstrapTo(bootstrapTo); int conn = Math.max(routingConfiguration.getParallel(), requestP2PConfiguration.getParallel()); FutureChannelCreator fcc = peer.getConnectionBean().reservation().create(conn, 0); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator futureChannelCreator) throws Exception { if (futureChannelCreator.isSuccess()) { RoutingBuilder routingBuilder = createBuilder(requestP2PConfiguration, routingConfiguration); FutureDone<Pair<FutureRouting, FutureRouting>> futureBootstrap = peer.getDistributedRouting() .bootstrap( bootstrapTo, routingBuilder, futureChannelCreator.getChannelCreator()); Utils.addReleaseListener(futureChannelCreator.getChannelCreator(), futureBootstrap); result.waitFor(futureBootstrap); } else { result.setFailed(futureChannelCreator); } } }); return result; }
@Override public void run() { if (logger.isDebugEnabled()) { logger.debug("started task " + taskId + " which came from " + senderAddress); } synchronized (lock) { status.put(taskId, Status.STARTED); } Map<Number160, Data> outputData = null; try { outputData = mapper.execute(inputData, peerBean.getStorage()); } catch (Exception e) { outputData = null; registerException(taskId, e.toString()); } synchronized (lock) { status.put(taskId, Status.SUCCESS_RESULT_NOT_SENT); } final Map<Number160, Data> outputData2 = outputData; FutureChannelCreator futureChannelCreator = connectionBean.getConnectionReservation().reserve(1); futureChannelCreator.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator futureChannelCreator) throws Exception { if (futureChannelCreator.isSuccess()) { FutureResponse futureResponse = getTaskRPC() .sendResult( senderAddress, futureChannelCreator.getChannelCreator(), taskId, outputData2, peerBean.getKeyPair(), false, sign); futureResponse.addListener( new BaseFutureAdapter<FutureResponse>() { @Override public void operationComplete(FutureResponse future) throws Exception { if (future.isSuccess()) { synchronized (lock) { status.put(taskId, Status.SUCCESS_RESULT_SENT); } } else { registerException(taskId, "could not send result back"); } connectionBean .getConnectionReservation() .release(futureChannelCreator.getChannelCreator()); } }); } else { registerException(taskId, "could not reserve connection"); } } }); }
private static void sendDirectRequest( final RequestHandler<FutureResponse> request, final PeerConnection peerConnection) { FutureChannelCreator futureChannelCreator2 = peerConnection.acquire(request.futureResponse()); futureChannelCreator2.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(FutureChannelCreator future) throws Exception { if (future.isSuccess()) { request.futureResponse().request().keepAlive(true); request.sendTCP(peerConnection.channelCreator(), peerConnection); } else { request.futureResponse().failed("Could not acquire channel (2)", future); } } }); }
public FutureResponse pingPeerConnection(final PeerConnection peerConnection) { final RequestHandler<FutureResponse> request = peer.pingRPC().ping(peerConnection.remotePeer(), connectionConfiguration); FutureChannelCreator futureChannelCreator = peerConnection.acquire(request.futureResponse()); futureChannelCreator.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(FutureChannelCreator future) throws Exception { if (future.isSuccess()) { request.futureResponse().request().keepAlive(true); request.sendTCP(peerConnection); } 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; }
/** * If an other peer is responsible, we send this peer our data, so that the other peer can take * care of this. * * @param other The other peer * @param locationKey The location key * @param domainKey The domain key * @param dataMapConvert The data to store */ public FutureDone<Void> sendDirect( final PeerAddress other, final Number160 locationKey, final NavigableMap<Number640, Data> dataMap) { final FutureDone<Void> futureDone = new FutureDone<Void>(); FutureChannelCreator futureChannelCreator = peer.peer().connectionBean().reservation().create(0, 1); Utils.addReleaseListener(futureChannelCreator, futureDone); futureChannelCreator.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(final FutureChannelCreator future) throws Exception { if (future.isSuccess()) { PutBuilder putBuilder = new PutBuilder(peer, locationKey); putBuilder.dataMap(dataMap); FutureResponse futureResponse = storageRPC.putReplica(other, putBuilder, future.channelCreator()); futureResponse.addListener( new BaseFutureAdapter<FutureResponse>() { @Override public void operationComplete(FutureResponse future) throws Exception { if (future.isSuccess()) { futureDone.done(); } else { futureDone.failed(future); } } }); peer.peer().notifyAutomaticFutures(futureResponse); } else { futureDone.failed(future); LOG.error("otherResponsible failed {}", future.failedReason()); } } }); return futureDone; }
/** * Send a Message from one Peer to another Peer internally. This avoids the overhead of * sendDirect. */ private static void send( final PeerConnection peerConnection, PeerBean peerBean, ConnectionBean connectionBean, final FutureResponse futureResponse) { final RequestHandler<FutureResponse> requestHandler = new RequestHandler<FutureResponse>( futureResponse, peerBean, connectionBean, connectionBean.channelServer().channelServerConfiguration()); final FutureChannelCreator fcc = peerConnection.acquire(futureResponse); fcc.addListener( new BaseFutureAdapter<FutureChannelCreator>() { @Override public void operationComplete(FutureChannelCreator future) throws Exception { if (future.isSuccess()) { requestHandler.sendTCP(peerConnection.channelCreator(), peerConnection); } else { futureResponse.failed(future); } } }); }
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()); }