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; }
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(); }
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); } }
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); } }
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()); } }
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); }