KademliaDHT/src/kademlia/operation/BucketRefreshOperation.java
Joshua Kissoon a259579f4a Created a KadConfiguration interface that allows passing in a configuration file as needed.
This allows other applications to create their own configuration files
2014-03-29 14:37:01 +05:30

67 lines
2.1 KiB
Java

package kademlia.operation;
import java.io.IOException;
import kademlia.core.KadConfiguration;
import kademlia.core.KadServer;
import kademlia.node.Node;
import kademlia.node.NodeId;
/**
* At each time interval t, nodes need to refresh their K-Buckets
* This operation takes care of refreshing this node's K-Buckets
*
* @author Joshua Kissoon
* @created 20140224
*/
public class BucketRefreshOperation implements Operation
{
private final KadServer server;
private final Node localNode;
private final KadConfiguration config;
public BucketRefreshOperation(KadServer server, Node localNode, KadConfiguration config)
{
this.server = server;
this.localNode = localNode;
this.config = config;
}
/**
* Each bucket need to be refreshed at every time interval t.
* Find an identifier in each bucket's range, use it to look for nodes closest to this identifier
* allowing the bucket to be refreshed.
*
* Then Do a NodeLookupOperation for each of the generated NodeIds,
* This will find the K-Closest nodes to that ID, and update the necessary K-Bucket
*
* @throws java.io.IOException
*/
@Override
public synchronized void execute() throws IOException
{
for (int i = 1; i < NodeId.ID_LENGTH; i++)
{
/* Construct a NodeId that is i bits away from the current node Id */
final NodeId current = this.localNode.getNodeId().generateNodeIdByDistance(i);
/* Run the Node Lookup Operation, each in a different thread to speed up things */
new Thread()
{
@Override
public void run()
{
try
{
new NodeLookupOperation(server, localNode, localNode.getNodeId(), BucketRefreshOperation.this.config).execute();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.start();
}
}
}