From 6fdff97429634d4cad5c6ef9b529046a20c6e831 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Sat, 22 Mar 2014 12:55:19 +0530 Subject: [PATCH] Setup the get bucket ID computation in one method. Added Javadoc --- .gitignore | 1 + src/kademlia/core/KadServer.java | 1 - src/kademlia/routing/RoutingTable.java | 30 +++++++++++++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..838458f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/dist/ \ No newline at end of file diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index 69fa54a..288eb59 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -209,7 +209,6 @@ public class KadServer /* If there is a reciever in the receivers to handle this */ synchronized (this) { - this.printReceivers(); receiver = this.receivers.remove(comm); TimerTask task = (TimerTask) tasks.remove(comm); task.cancel(); diff --git a/src/kademlia/routing/RoutingTable.java b/src/kademlia/routing/RoutingTable.java index 1d99287..6602c3a 100644 --- a/src/kademlia/routing/RoutingTable.java +++ b/src/kademlia/routing/RoutingTable.java @@ -49,28 +49,23 @@ public class RoutingTable } /** - * Adds a new node to the routing table + * Adds a new node to the routing table based on how far it is from the LocalNode. * * @param n The contact to add */ public final void insert(Node n) { - /* bucketId is the distance between these nodes */ - int bucketId = this.localNode.getNodeId().getDistance(n.getNodeId()); - - /* Put this contact to the bucket that stores contacts prefixLength distance away */ - this.buckets[bucketId].insert(n); + this.buckets[this.getBucketId(n.getNodeId())].insert(n); } /** - * Remove a node from the routing table + * Remove a node from the routing table. * * @param n The node to remove */ public final void remove(Node n) { - /* Find the first set bit: how far this node is away from the contact node */ - int bucketId = this.localNode.getNodeId().getDistance(n.getNodeId()); + int bucketId = this.getBucketId(n.getNodeId()); /* If the bucket has the contact, remove it */ if (this.buckets[bucketId].containNode(n)) @@ -79,6 +74,21 @@ public class RoutingTable } } + /** + * Compute the bucket ID in which a given node should be placed; the bucketId is computed based on how far the node is away from the Local Node. + * + * @param nid The NodeId for which we want to find which bucket it belong to + * + * @return Integer The bucket ID in which the given node should be placed. + */ + public final int getBucketId(NodeId nid) + { + int bId = this.localNode.getNodeId().getDistance(nid) - 1; + + /* If we are trying to insert a node into it's own routing table, then the bucket ID will be -1, so let's just keep it in bucket 0 */ + return bId < 0 ? 0 : bId; + } + /** * Find the closest set of contacts to a given NodeId * @@ -92,7 +102,7 @@ public class RoutingTable List closest = new ArrayList<>(num); /* Get the bucket index to search for closest from */ - int bucketIndex = this.localNode.getNodeId().getDistance(target) - 1; + int bucketIndex = this.getBucketId(target); /* Add the contacts from this bucket to the return contacts */ for (Node c : this.buckets[bucketIndex].getNodes())