2014-02-24 15:56:49 +00:00
|
|
|
package kademlia.operation;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import kademlia.core.KadServer;
|
|
|
|
import kademlia.node.Node;
|
2014-03-06 05:51:08 +00:00
|
|
|
import kademlia.node.NodeId;
|
2014-02-24 15:56:49 +00:00
|
|
|
|
|
|
|
/**
|
2014-03-06 05:51:08 +00:00
|
|
|
* At each time interval t, nodes need to refresh their K-Buckets
|
|
|
|
* This operation takes care of refreshing this node's K-Buckets
|
2014-02-24 15:56:49 +00:00
|
|
|
*
|
|
|
|
* @author Joshua Kissoon
|
|
|
|
* @created 20140224
|
|
|
|
*/
|
2014-03-06 05:51:08 +00:00
|
|
|
public class BucketRefreshOperation implements Operation
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
private final KadServer server;
|
|
|
|
private final Node localNode;
|
|
|
|
|
2014-03-06 05:51:08 +00:00
|
|
|
public BucketRefreshOperation(KadServer server, Node localNode)
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
|
|
|
this.server = server;
|
|
|
|
this.localNode = localNode;
|
|
|
|
}
|
|
|
|
|
2014-03-06 10:54:50 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2014-02-24 15:56:49 +00:00
|
|
|
@Override
|
2014-02-26 13:28:55 +00:00
|
|
|
public synchronized void execute() throws IOException
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
2014-03-06 10:54:50 +00:00
|
|
|
for (int i = 1; i < NodeId.ID_LENGTH; i++)
|
2014-03-06 05:51:08 +00:00
|
|
|
{
|
2014-03-06 10:54:50 +00:00
|
|
|
/* Construct a NodeId that is i bits away from the current node Id */
|
2014-03-06 11:08:46 +00:00
|
|
|
final NodeId current = this.localNode.getNodeId().generateNodeIdByDistance(i);
|
2014-03-06 05:51:08 +00:00
|
|
|
|
2014-03-06 11:08:46 +00:00
|
|
|
/* 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()).execute();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start();
|
2014-03-06 10:54:50 +00:00
|
|
|
}
|
2014-02-24 15:56:49 +00:00
|
|
|
}
|
|
|
|
}
|