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; package kademlia.routing;
import java.util.List;
import kademlia.node.Node; import kademlia.node.Node;
/** /**
* A bucket used to store Nodes in the routing table. * 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 * @author Joshua Kissoon
* @created 20140215 * @created 20140215
*/ */
@ -21,10 +20,35 @@ public interface Bucket
public void insert(Node n); public void insert(Node n);
/** /**
* Marks a node as dead: the dead node will be replace if * Checks if this bucket contain a node
* insert was invoked
* *
* @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 * @return boolean
*/ */
@Override
public boolean containNode(Node n) public boolean containNode(Node n)
{ {
return this.nodes.containsKey(n.getNodeId()); return this.nodes.containsKey(n.getNodeId());
@ -65,27 +66,25 @@ public class KadBucket implements Bucket
* *
* @param n The node to remove * @param n The node to remove
*/ */
@Override
public void removeNode(Node n) public void removeNode(Node n)
{ {
this.nodes.remove(n.getNodeId()); this.nodes.remove(n.getNodeId());
} }
@Override
public int numNodes() public int numNodes()
{ {
return this.nodes.size(); return this.nodes.size();
} }
@Override
public int getDepth() public int getDepth()
{ {
return this.depth; return this.depth;
} }
@Override @Override
public void markDead(Node n)
{
this.nodes.remove(n.getNodeId());
}
public List<Node> getNodes() public List<Node> getNodes()
{ {
return new ArrayList<>(this.nodes.values()); return new ArrayList<>(this.nodes.values());

View File

@ -10,20 +10,12 @@ import kademlia.node.NodeId;
* *
* @author Joshua Kissoon * @author Joshua Kissoon
* @created 20140215 * @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 public class RoutingTable
{ {
private final Node localNode; // The current node private final Node localNode; // The current node
private transient KadBucket[] buckets; private transient Bucket[] buckets;
{
buckets = new KadBucket[NodeId.ID_LENGTH]; // 160 buckets; 1 for each level in the tree
}
public RoutingTable(Node localNode) public RoutingTable(Node localNode)
{ {
@ -41,7 +33,7 @@ public class RoutingTable
*/ */
public final void initialize() 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++) for (int i = 0; i < NodeId.ID_LENGTH; i++)
{ {
buckets[i] = new KadBucket(i); buckets[i] = new KadBucket(i);
@ -174,7 +166,7 @@ public class RoutingTable
{ {
List<Node> nodes = new ArrayList<>(); List<Node> nodes = new ArrayList<>();
for (KadBucket b : this.buckets) for (Bucket b : this.buckets)
{ {
nodes.addAll(b.getNodes()); nodes.addAll(b.getNodes());
} }
@ -185,7 +177,7 @@ public class RoutingTable
/** /**
* @return Bucket[] The buckets in this Kad Instance * @return Bucket[] The buckets in this Kad Instance
*/ */
public final KadBucket[] getBuckets() public final Bucket[] getBuckets()
{ {
return this.buckets; return this.buckets;
} }
@ -195,7 +187,7 @@ public class RoutingTable
* *
* @param buckets * @param buckets
*/ */
public final void setBuckets(KadBucket[] buckets) public final void setBuckets(Bucket[] buckets)
{ {
this.buckets = buckets; this.buckets = buckets;
} }
@ -204,7 +196,7 @@ public class RoutingTable
public final String toString() public final String toString()
{ {
StringBuilder sb = new StringBuilder("\nPrinting Routing Table Started ***************** \n"); StringBuilder sb = new StringBuilder("\nPrinting Routing Table Started ***************** \n");
for (KadBucket b : this.buckets) for (Bucket b : this.buckets)
{ {
if (b.numNodes() > 0) if (b.numNodes() > 0)
{ {