/** * Updates the time at which the k-bucket for a DHT key, and the s-bucket, * was last refreshed. * @param key */ void updateLastLookupTime(Hash key) { long time = System.currentTimeMillis(); getKBucket(key).setLastLookupTime(time); sBucket.setLastLookupTime(key, time); }
/** * Returns the (s or k) bucket that contains a given {@link Destination}. * The s-bucket is checked first, then the k-buckets. * If no bucket contains the peer, <code>null</code> is returned. * @param destination * @return */ private AbstractBucket getBucket(Destination destination) { if (sBucket.contains(destination)) return sBucket; else { KBucket kBucket = getKBucket(destination.calculateHash()); if (kBucket.contains(destination)) return kBucket; else return null; } }
bucket = getKBucket(peer.calculateHash());
/** * Adds a <code>{@link KademliaPeer}</code> to the s-bucket or a k-bucket, * depending on its distance to the local node and how full the buckets are. * @param destination */ public void addOrUpdate(KademliaPeer peer) { Hash destHash = peer.getDestinationHash(); if (localDestinationHash.equals(destHash)) { log.debug("Not adding local destination to bucket."); return; } KademliaPeer removedOrNotAdded = sBucket.addOrUpdate(peer); if (removedOrNotAdded == null) getKBucket(destHash).remove(peer); // if the peer was in a k-bucket, remove it because it is now in the s-bucket else addToKBucket(removedOrNotAdded); // if a peer was removed from the s-bucket or didn't qualify as a sibling, add it to a k-bucket // log int numBuckets = kBuckets.size(); int numPeers = getAllPeers().size(); int numSiblings = sBucket.size(); log.debug("Peer " + Util.toBase32(destHash) + " added/updated. Peers=" + numPeers + " sib=" + numSiblings + " buk=" + numBuckets + " (not counting the sibling bucket)"); }