From 66b6a14ebcf465ba9c3e4ba47de09d3a9649295c Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Thu, 1 May 2014 15:03:20 +0530 Subject: [PATCH] Routing Table - Added a routing table interface to code to Interface not implementation --- src/kademlia/routing/KadRoutingTable.java | 83 +++++++++++++++++++++++ src/kademlia/routing/RoutingTable.java | 12 +++- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/kademlia/routing/KadRoutingTable.java diff --git a/src/kademlia/routing/KadRoutingTable.java b/src/kademlia/routing/KadRoutingTable.java new file mode 100644 index 0000000..f50139a --- /dev/null +++ b/src/kademlia/routing/KadRoutingTable.java @@ -0,0 +1,83 @@ +package kademlia.routing; + +import java.util.List; +import kademlia.node.Node; +import kademlia.node.NodeId; + +/** + * Specification for Kademlia's Routing Table + * + * @author Joshua Kissoon + * @since 20140501 + */ +public interface KadRoutingTable +{ + + /** + * Initialize the RoutingTable to it's default state + */ + public void initialize(); + + /** + * Adds a contact to the routing table based on how far it is from the LocalNode. + * + * @param c The contact to add + */ + public void insert(Contact c); + + /** + * Adds a node to the routing table based on how far it is from the LocalNode. + * + * @param n The node to add + */ + public void insert(Node n); + + /** + * 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 int getBucketId(NodeId nid); + + /** + * Find the closest set of contacts to a given NodeId + * + * @param target The NodeId to find contacts close to + * @param numNodesRequired The number of contacts to find + * + * @return List A List of contacts closest to target + */ + public List findClosest(NodeId target, int numNodesRequired); + + /** + * @return List A List of all Nodes in this RoutingTable + */ + public List getAllNodes(); + + /** + * @return List A List of all Nodes in this RoutingTable + */ + public List getAllContacts(); + + /** + * @return Bucket[] The buckets in this Kad Instance + */ + public KadBucket[] getBuckets(); + + /** + * Method used by operations to notify the routing table of any contacts that have been unresponsive. + * + * @param contacts The set of unresponsive contacts + */ + public void setUnresponsiveContacts(List contacts); + + /** + * Method used by operations to notify the routing table of any contacts that have been unresponsive. + * + * @param n + */ + public void setUnresponsiveContact(Node n); + +} diff --git a/src/kademlia/routing/RoutingTable.java b/src/kademlia/routing/RoutingTable.java index 4bb3553..133d12d 100644 --- a/src/kademlia/routing/RoutingTable.java +++ b/src/kademlia/routing/RoutingTable.java @@ -12,7 +12,7 @@ import kademlia.node.NodeId; * @author Joshua Kissoon * @created 20140215 */ -public class RoutingTable +public class RoutingTable implements KadRoutingTable { private final Node localNode; // The current node @@ -35,6 +35,7 @@ public class RoutingTable /** * Initialize the RoutingTable to it's default state */ + @Override public final void initialize() { this.buckets = new KadBucket[NodeId.ID_LENGTH]; @@ -49,6 +50,7 @@ public class RoutingTable * * @param c The contact to add */ + @Override public synchronized final void insert(Contact c) { this.buckets[this.getBucketId(c.getNode().getNodeId())].insert(c); @@ -59,6 +61,7 @@ public class RoutingTable * * @param n The node to add */ + @Override public synchronized final void insert(Node n) { this.buckets[this.getBucketId(n.getNodeId())].insert(n); @@ -71,6 +74,7 @@ public class RoutingTable * * @return Integer The bucket ID in which the given node should be placed. */ + @Override public final int getBucketId(NodeId nid) { int bId = this.localNode.getNodeId().getDistance(nid) - 1; @@ -87,6 +91,7 @@ public class RoutingTable * * @return List A List of contacts closest to target */ + @Override public synchronized final List findClosest(NodeId target, int numNodesRequired) { List closest = new ArrayList<>(numNodesRequired); @@ -172,6 +177,7 @@ public class RoutingTable /** * @return List A List of all Nodes in this RoutingTable */ + @Override public final List getAllNodes() { List nodes = new ArrayList<>(); @@ -190,6 +196,7 @@ public class RoutingTable /** * @return List A List of all Nodes in this RoutingTable */ + @Override public final List getAllContacts() { List contacts = new ArrayList<>(); @@ -205,6 +212,7 @@ public class RoutingTable /** * @return Bucket[] The buckets in this Kad Instance */ + @Override public final KadBucket[] getBuckets() { return this.buckets; @@ -225,6 +233,7 @@ public class RoutingTable * * @param contacts The set of unresponsive contacts */ + @Override public void setUnresponsiveContacts(List contacts) { if (contacts.isEmpty()) @@ -242,6 +251,7 @@ public class RoutingTable * * @param n */ + @Override public synchronized void setUnresponsiveContact(Node n) { int bucketId = this.getBucketId(n.getNodeId());