示例#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()]);
  }
示例#2
0
 public String dump() {
   StringBuilder builder = new StringBuilder();
   builder.append(Hex.encodeHex(ourId.getBytes()));
   builder.append("\n");
   for (Bucket bucket : buckets) {
     builder.append(bucket.dump());
   }
   return builder.toString();
 }
示例#3
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();
 }
示例#4
0
 private int determineValue(TrackerResponse.PeerInfo node) {
   return ourId.distance(node.getPeerId());
 }
示例#5
0
 public void addNode(TrackerResponse.PeerInfo node) {
   if (ourId.equals(node.getPeerId())) {
     return;
   }
   internalAdd(node);
 }