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;
  }
Beispiel #2
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();
 }
Beispiel #3
0
 @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 FutureChannelCreator acquire(
     final FutureChannelCreator futureChannelCreator, final FutureResponse futureResponse) {
   if (oneConnection.tryAcquire()) {
     futureResponse.addListener(
         new BaseFutureAdapter<FutureResponse>() {
           @Override
           public void operationComplete(FutureResponse future) throws Exception {
             oneConnection.release();
             synchronized (map) {
               Iterator<Map.Entry<FutureChannelCreator, FutureResponse>> iterator =
                   map.entrySet().iterator();
               if (iterator.hasNext()) {
                 Map.Entry<FutureChannelCreator, FutureResponse> entry = iterator.next();
                 iterator.remove();
                 acquire(entry.getKey(), entry.getValue());
               }
             }
           }
         });
     futureChannelCreator.reserved(cc);
     return futureChannelCreator;
   } else {
     synchronized (map) {
       map.put(futureChannelCreator, futureResponse);
     }
   }
   return futureChannelCreator;
 }
 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);
           }
         }
       });
 }
Beispiel #6
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();
  }
Beispiel #7
0
  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;
 }
Beispiel #9
0
 /**
  * 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 static void startServer() throws Exception {
    Random rnd = new Random(43L);
    Bindings b = new Bindings(Protocol.IPv4, Inet4Address.getByName("127.0.0.1"), 4000, 4000);
    // b.addInterface("eth0");
    Peer master = new PeerMaker(new Number160(rnd)).setPorts(4000).setBindings(b).makeAndListen();
    System.out.println("Server started Listening to: " + DiscoverNetworks.discoverInterfaces(b));
    System.out.println("address visible to outside is " + master.getPeerAddress());
    while (true) {
      for (PeerAddress pa : master.getPeerBean().getPeerMap().getAll()) {
        System.out.println("PeerAddress: " + pa);
        FutureChannelCreator fcc = master.getConnectionBean().getConnectionReservation().reserve(1);
        fcc.awaitUninterruptibly();

        ChannelCreator cc = fcc.getChannelCreator();

        // FutureResponse fr1 = master.getHandshakeRPC().pingTCP(pa, cc);
        // fr1.awaitUninterruptibly();

        // if (fr1.isSuccess())
        // System.out.println("peer online T:" + pa);
        // else
        // System.out.println("offline " + pa);

        // FutureResponse fr2 = master.getHandshakeRPC().pingUDP(pa, cc);
        // fr2.awaitUninterruptibly();

        master.getConnectionBean().getConnectionReservation().release(cc);

        // if (fr2.isSuccess())
        // System.out.println("peer online U:" + pa);
        // else
        // System.out.println("offline " + pa);
      }
      Thread.sleep(1500);
    }
  }
  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());
  }