/** * Queries the DHT for the <code>k</code> peers closest to a given key. * This method blocks. * @see ClosestNodesLookupTask */ private List<Destination> getClosestNodes(Hash key) throws InterruptedException { bucketManager.updateLastLookupTime(key); ClosestNodesLookupTask lookupTask = new ClosestNodesLookupTask(key, sendQueue, i2pReceiver, bucketManager); return lookupTask.call(); }
SBucket(Hash localDestinationHash) { super(KademliaConstants.S); distanceComparator = new PeerDistanceComparator(localDestinationHash); int numSections = (KademliaConstants.S - 1) / KademliaConstants.K + 1; if (numSections < 1) numSections = 1; sections = new BucketSection[numSections]; for (int i=0; i<numSections; i++) sections[i] = new BucketSection(BigInteger.ZERO, BigInteger.ZERO, 0); }
private void addPeerData(DhtPeerStatsRenderer renderer, AbstractBucket bucket, Hash localDestinationHash) { for (KademliaPeer peer: bucket) { BigInteger distance = KademliaUtil.getDistance(localDestinationHash, peer.calculateHash()); data.add(new KademliaPeerStatsRow( renderer, data.size() + 1, peer, getBucketPrefix(renderer, bucket), distance, peer.isLocked(), peer.getConsecTimeouts(), peer.getFirstSeen() )); } }
private void refresh(KBucket bucket) throws InterruptedException { Hash key = createRandomHash(bucket.getStartId(), bucket.getEndId()); getClosestNodes(key); }
@Override public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) { BanList banList = BanList.getInstance(); banList.update(sender, packet); if (!banList.isBanned(sender)) // any type of incoming packet updates the peer's record in the bucket/sibling list, or adds the peer to the bucket/sibling list addOrUpdate(new KademliaPeer(sender)); else remove(sender); }
private void refresh(BucketSection section) throws InterruptedException { Hash key = createRandomHash(section.getStart(), section.getEnd()); getClosestNodes(key); }
/** * Returns <code>true</code> if <code>dest1</code> is closer to <code>key</code> than <code>dest2</code>. * @param key * @param destination * @param peers */ private boolean isCloser(Destination dest1, Destination dest2, Hash key) { return new PeerDistanceComparator(key).compare(dest1, dest2) < 0; }
/** * 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); }
/** * Looks up a <code>KademliaPeer</code> by I2P destination. If no bucket * (k or s-bucket) contains the peer, <code>null</code> is returned. * @param destination * @return */ private KademliaPeer getPeer(Destination destination) { AbstractBucket bucket = getBucket(destination); if (bucket != null) return bucket.getPeer(destination); else return null; }
/** * Connects to the Kademlia network; blocks until done. */ private void bootstrap() { new BootstrapTask(i2pReceiver).run(); }
/** * Calls <code>addOrUpdate(KademliaPeer)</code> for one or more peers. * @param peers */ public void addAll(Collection<KademliaPeer> peers) { for (KademliaPeer node: peers) addOrUpdate(node); }
/** * Returns <code>true</code> if a peer exists in the bucket. * @param destination * @return */ boolean contains(Destination destination) { return getPeer(destination) != null; }
/** * @see KademliaDHT.getPeerStats() */ DhtPeerStats getPeerStats(DhtPeerStatsRenderer renderer) { return new KademliaPeerStats(renderer, sBucket, kBuckets, localDestinationHash); }
/** * Returns <code>true</code> if the bucket can be split in order to make room for a new peer. * @return */ private boolean canSplit(Destination destination) { return depth%KademliaConstants.B!=0 || rangeContains(destination); }
void setLastLookupTime(Hash key, long lastLookupTime) { BucketSection section = getSection(key); if (section != null) section.lastLookupTime = lastLookupTime; }
public DhtPeerStats getDhtStats(DhtPeerStatsRenderer renderer) { if (dht == null) return null; else return dht.getPeerStats(renderer); }
@Override public DhtResults findAll(Hash key, Class<? extends DhtStorablePacket> dataType) throws InterruptedException { return find(key, dataType, true); }
@Override public DhtPeerStats getPeerStats(DhtPeerStatsRenderer renderer) { return bucketManager.getPeerStats(renderer); }