Exemple #1
0
 /**
  * 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();
 }
Exemple #2
0
  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();
  }
 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 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());
  }