From 42be8498c0826f38ffc847d9fda588b696e99708 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Sat, 22 Mar 2014 13:03:10 +0530 Subject: [PATCH] Finished the todos: * @todo Make the KadBucket represent the Bucket interface * @todo Change the code to reflect the bucket interface and not the specific KadBucket implementation * @todo Update this interface and use this as parameter type, etc... instead of the KadBucket implementation used throughout the application Now we're coding to the Bucket interface rather than the KadBucket implementation --- src/kademlia/routing/Bucket.java | 36 +++++++++++++++++++++----- src/kademlia/routing/KadBucket.java | 9 +++---- src/kademlia/routing/RoutingTable.java | 20 +++++--------- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/kademlia/routing/Bucket.java b/src/kademlia/routing/Bucket.java index 8a72820..537a90b 100644 --- a/src/kademlia/routing/Bucket.java +++ b/src/kademlia/routing/Bucket.java @@ -1,12 +1,11 @@ package kademlia.routing; +import java.util.List; import kademlia.node.Node; /** * A bucket used to store Nodes in the routing table. * - * @todo Update this interface and use this as parameter type, etc... instead of the KadBucket implementation used throughout the application - * * @author Joshua Kissoon * @created 20140215 */ @@ -21,10 +20,35 @@ public interface Bucket public void insert(Node n); /** - * Marks a node as dead: the dead node will be replace if - * insert was invoked + * Checks if this bucket contain a node * - * @param n the dead node + * @param n The node to check for + * + * @return boolean */ - public void markDead(Node n); + public boolean containNode(Node n); + + /** + * Remove a node from this bucket + * + * @param n The node to remove + */ + public void removeNode(Node n); + + /** + * Counts the number of nodes in this bucket. + * + * @return Integer The number of nodes in this bucket + */ + public int numNodes(); + + /** + * @return Integer The depth of this bucket in the RoutingTable + */ + public int getDepth(); + + /** + * @return An Iterable structure with all nodes in this bucket + */ + public List getNodes(); } diff --git a/src/kademlia/routing/KadBucket.java b/src/kademlia/routing/KadBucket.java index 42bb52a..fd6612a 100644 --- a/src/kademlia/routing/KadBucket.java +++ b/src/kademlia/routing/KadBucket.java @@ -55,6 +55,7 @@ public class KadBucket implements Bucket * * @return boolean */ + @Override public boolean containNode(Node n) { return this.nodes.containsKey(n.getNodeId()); @@ -65,27 +66,25 @@ public class KadBucket implements Bucket * * @param n The node to remove */ + @Override public void removeNode(Node n) { this.nodes.remove(n.getNodeId()); } + @Override public int numNodes() { return this.nodes.size(); } + @Override public int getDepth() { return this.depth; } @Override - public void markDead(Node n) - { - this.nodes.remove(n.getNodeId()); - } - public List getNodes() { return new ArrayList<>(this.nodes.values()); diff --git a/src/kademlia/routing/RoutingTable.java b/src/kademlia/routing/RoutingTable.java index 6602c3a..f6c127b 100644 --- a/src/kademlia/routing/RoutingTable.java +++ b/src/kademlia/routing/RoutingTable.java @@ -10,20 +10,12 @@ import kademlia.node.NodeId; * * @author Joshua Kissoon * @created 20140215 - * - * @todo Make the KadBucket represent the Bucket interface - * @todo Change the code to reflect the bucket interface and not the specific KadBucket implementation */ public class RoutingTable { private final Node localNode; // The current node - private transient KadBucket[] buckets; - - - { - buckets = new KadBucket[NodeId.ID_LENGTH]; // 160 buckets; 1 for each level in the tree - } + private transient Bucket[] buckets; public RoutingTable(Node localNode) { @@ -41,7 +33,7 @@ public class RoutingTable */ public final void initialize() { - this.buckets = new KadBucket[NodeId.ID_LENGTH]; + this.buckets = new Bucket[NodeId.ID_LENGTH]; for (int i = 0; i < NodeId.ID_LENGTH; i++) { buckets[i] = new KadBucket(i); @@ -174,7 +166,7 @@ public class RoutingTable { List nodes = new ArrayList<>(); - for (KadBucket b : this.buckets) + for (Bucket b : this.buckets) { nodes.addAll(b.getNodes()); } @@ -185,7 +177,7 @@ public class RoutingTable /** * @return Bucket[] The buckets in this Kad Instance */ - public final KadBucket[] getBuckets() + public final Bucket[] getBuckets() { return this.buckets; } @@ -195,7 +187,7 @@ public class RoutingTable * * @param buckets */ - public final void setBuckets(KadBucket[] buckets) + public final void setBuckets(Bucket[] buckets) { this.buckets = buckets; } @@ -204,7 +196,7 @@ public class RoutingTable public final String toString() { StringBuilder sb = new StringBuilder("\nPrinting Routing Table Started ***************** \n"); - for (KadBucket b : this.buckets) + for (Bucket b : this.buckets) { if (b.numNodes() > 0) {