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
This commit is contained in:
Joshua Kissoon 2014-03-22 13:03:10 +05:30
parent 6fdff97429
commit 42be8498c0
3 changed files with 40 additions and 25 deletions

View File

@ -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<Node> getNodes();
}

View File

@ -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<Node> getNodes()
{
return new ArrayList<>(this.nodes.values());

View File

@ -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<Node> 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)
{