Ejemplo n.º 1
0
  public synchronized TrackerResponse.PeerInfo[] getNodesCloseTo(final PeerId id, int count) {
    int distance = ourId.distance(id);
    int bucket = getBucketIndexForValue(distance);
    List<TrackerResponse.PeerInfo> result = new ArrayList<TrackerResponse.PeerInfo>();
    int delta = 0;
    while (result.size() < count) {
      if (bucket + delta < buckets.length) {
        result.addAll(buckets[bucket + delta].getEntries());
      }
      if (bucket - delta >= 0 && delta != 0) {
        result.addAll(buckets[bucket - delta].getEntries());
      }
      delta++;
      if (bucket + delta >= buckets.length && bucket - delta < 0) {
        break;
      }
    }

    Collections.sort(
        result,
        new Comparator<TrackerResponse.PeerInfo>() {
          public int compare(TrackerResponse.PeerInfo o1, TrackerResponse.PeerInfo o2) {
            int a = id.distance(o1.getPeerId());
            int b = id.distance(o2.getPeerId());
            return b - a;
          }
        });

    result = result.subList(0, Math.min(count, result.size()));

    return result.toArray(new TrackerResponse.PeerInfo[result.size()]);
  }
Ejemplo 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();
 }
Ejemplo n.º 3
0
 private int determineValue(TrackerResponse.PeerInfo node) {
   return ourId.distance(node.getPeerId());
 }