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()]); }
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(); }
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(); }
private int determineValue(TrackerResponse.PeerInfo node) { return ourId.distance(node.getPeerId()); }
public void addNode(TrackerResponse.PeerInfo node) { if (ourId.equals(node.getPeerId())) { return; } internalAdd(node); }