Exemplo n.º 1
0
 public void update(TrackerResponse.PeerInfo info) {
   if (!this.info.getSocketAddress().equals(info.getSocketAddress())) {
     throw new IllegalArgumentException("socket address differs");
   }
   if (!this.info.getPeerId().equals(info.getPeerId())) {
     this.info = info;
   }
   lastUpdate = timeSource.getTime();
   lastPinged = 0;
   pingAttempts = 0;
 }
Exemplo n.º 2
0
 public String averageDistancesPerBucket() {
   StringBuilder builder = new StringBuilder();
   for (Bucket bucket : buckets) {
     builder.append(bucket.toString()).append(": ");
     int distanceSum = 0;
     Collection<TrackerResponse.PeerInfo> entries = bucket.getEntries();
     for (TrackerResponse.PeerInfo node : entries) {
       distanceSum += ourId.distance(node.getPeerId());
     }
     double average = (double) distanceSum / entries.size();
     builder.append(average).append("\n");
   }
   return builder.toString();
 }
Exemplo n.º 3
0
 public synchronized void add(TrackerResponse.PeerInfo e) {
   InetSocketAddress address = e.getSocketAddress();
   if (entries.containsKey(address)) {
     RoutingTable.Bucket.Entry entry = entries.get(address);
     entry.update(e);
   } else if (canAddMore()) {
     entries.put(address, new Entry(e));
   } else {
     addEntryToWaitingList(e);
   }
 }
Exemplo n.º 4
0
 private void addEntryToWaitingList(TrackerResponse.PeerInfo peerInfo) {
   InetSocketAddress address = peerInfo.getSocketAddress();
   Entry entry = waitingEntries.get(address);
   if (entry == null) {
     if (waitingEntries.size() < waitingCapacity) {
       entry = new Entry(peerInfo);
       waitingEntries.put(address, entry);
     }
   } else {
     entry.update(peerInfo);
   }
 }
Exemplo n.º 5
0
 public void maintain() {
   long now = timeSource.getTime();
   if (lastUpdate + TIMEOUT > now) {
     return;
   }
   if (lastPinged + PING_TIMEOUT > now) {
     return;
   }
   if (pingAttempts < MAX_PING_ATTEMPTS) {
     pingAttempts++;
     totalPings++;
     lastPinged = now;
     pinger.ping(info.getSocketAddress());
   }
 }
Exemplo n.º 6
0
 private int determineValue(TrackerResponse.PeerInfo node) {
   return ourId.distance(node.getPeerId());
 }
Exemplo n.º 7
0
 public void addNode(TrackerResponse.PeerInfo node) {
   if (ourId.equals(node.getPeerId())) {
     return;
   }
   internalAdd(node);
 }