Routing Table

- Added a routing table interface to code to Interface not implementation
This commit is contained in:
Joshua Kissoon 2014-05-01 15:03:20 +05:30
parent fa242fad6a
commit 66b6a14ebc
2 changed files with 94 additions and 1 deletions

View File

@ -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<Node> 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<Node> contacts);
/**
* Method used by operations to notify the routing table of any contacts that have been unresponsive.
*
* @param n
*/
public void setUnresponsiveContact(Node n);
}

View File

@ -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<Node> findClosest(NodeId target, int numNodesRequired)
{
List<Node> 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<Node> 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<Contact> 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<Node> 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());