/** * Return the total number of known Kademlia peers (locked + unlocked peers). * @return */ int getPeerCount() { int count = 0; for (KBucket bucket: kBuckets) count += bucket.size(); count += sBucket.size(); return count; }
/** * Adds a peer to the tail of the bucket if it is locked, or to the * head of the bucket if it isn't locked. * The bucket cannot be full when calling this method. * @param peer */ private void add(KademliaPeer peer) { if (isFull()) log.error("Error: adding a node to a full k-bucket. Bucket needs to be split first. Size=" + size() + ", capacity=" + capacity); if (peer.isLocked()) peers.add(peer); else peers.add(0, peer); }
@Test public void testRemove() { for (KBucket bucket: buckets) { Destination[] destinations = destinationMap.get(bucket); assertEquals(0, bucket.size()); bucket.addOrUpdate(new KademliaPeer(destinations[0])); assertEquals(1, bucket.size()); bucket.remove(destinations[0]); assertEquals(0, bucket.size()); bucket.addOrUpdate(new KademliaPeer(destinations[0])); bucket.addOrUpdate(new KademliaPeer(destinations[1])); bucket.addOrUpdate(new KademliaPeer(destinations[2])); assertEquals(3, bucket.size()); bucket.remove(destinations[0]); assertEquals(2, bucket.size()); bucket.remove(destinations[1]); assertEquals(1, bucket.size()); bucket.remove(destinations[2]); assertEquals(0, bucket.size()); } }
@Test public void testAddOrUpdate() { for (KBucket bucket: buckets) { KademliaPeer[] peers = destinationMap.get(bucket); // add a peer bucket.addOrUpdate(peers[0]); assertEquals(1, bucket.size()); assertEquals(peers[0], bucket.iterator().next()); // add the same peer again, verify there is no duplicate bucket.addOrUpdate(peers[0]); assertEquals(1, bucket.size()); assertEquals(peers[0], bucket.iterator().next()); // add a different peer bucket.addOrUpdate(peers[1]); assertEquals(2, bucket.size()); assertEquals(peers[0], bucket.getPeer(peers[0])); assertEquals(peers[1], bucket.getPeer(peers[1])); } }
List<KBucket> kBuckets = getKBuckets(); for (KBucket bucket: kBuckets) bucketSizes.add(bucket.size()); int peersRemaining = totalPeersAdded; while (peersRemaining > 0) { if (getKBuckets().get(i).size() == bucketSizes.get(i)-1) bucketSizes.set(i, bucketSizes.get(i)-1); if (bucket.size() == bucketSizes.get(i)-1) { bucketSizes.set(i, bucketSizes.get(i)-1); BigInteger startId = bucket.getStartId(); assertEquals(bucket.size(), bucketSizes.get(i).intValue());