예제 #1
0
 @Test
 public void pingPong() throws Exception {
   connect();
   Utils.rollMockClock(0);
   // No ping pong happened yet.
   assertEquals(Long.MAX_VALUE, peer.getLastPingTime());
   assertEquals(Long.MAX_VALUE, peer.getPingTime());
   ListenableFuture<Long> future = peer.ping();
   assertEquals(Long.MAX_VALUE, peer.getLastPingTime());
   assertEquals(Long.MAX_VALUE, peer.getPingTime());
   assertFalse(future.isDone());
   Ping pingMsg = (Ping) outbound(writeTarget);
   Utils.rollMockClock(5);
   // The pong is returned.
   inbound(writeTarget, new Pong(pingMsg.getNonce()));
   pingAndWait(writeTarget);
   assertTrue(future.isDone());
   long elapsed = future.get();
   assertTrue("" + elapsed, elapsed > 1000);
   assertEquals(elapsed, peer.getLastPingTime());
   assertEquals(elapsed, peer.getPingTime());
   // Do it again and make sure it affects the average.
   future = peer.ping();
   pingMsg = (Ping) outbound(writeTarget);
   Utils.rollMockClock(50);
   inbound(writeTarget, new Pong(pingMsg.getNonce()));
   elapsed = future.get();
   assertEquals(elapsed, peer.getLastPingTime());
   assertEquals(7250, peer.getPingTime());
 }
예제 #2
0
 @Override
 public void run() {
   synchronized (lock) {
     // make sure we only have 5 ping in parallel
     if (shutdown || COUNTER.get() > MAX_PING) {
       return;
     }
     for (Maintainable maintainable : maintainables) {
       PeerStatistic peerStatatistic = maintainable.nextForMaintenance(runningFutures.values());
       if (peerStatatistic == null) {
         continue;
       }
       BaseFuture future;
       if (peerStatatistic.isLocal()) {
         future = peer.localAnnounce().ping().peerAddress(peerStatatistic.peerAddress()).start();
         LOG.debug(
             "maintenance local ping from {} to {}",
             peer.peerAddress(),
             peerStatatistic.peerAddress());
       } else {
         future = peer.ping().peerAddress(peerStatatistic.peerAddress()).start();
         LOG.debug(
             "maintenance ping from {} to {}", peer.peerAddress(), peerStatatistic.peerAddress());
       }
       peer.notifyAutomaticFutures(future);
       runningFutures.put(future, peerStatatistic.peerAddress());
       COUNTER.incrementAndGet();
       future.addListener(
           new BaseFutureAdapter<BaseFuture>() {
             @Override
             public void operationComplete(BaseFuture future) throws Exception {
               synchronized (lock) {
                 runningFutures.remove(future);
                 COUNTER.decrementAndGet();
               }
             }
           });
     }
   }
 }