Added methods to start and stop the refresh operation

This commit is contained in:
Joshua Kissoon 2014-05-05 18:03:57 +05:30
parent 8d3ceb3ae1
commit 8aecda34c7
3 changed files with 54 additions and 25 deletions

View File

@ -58,8 +58,8 @@ public class KademliaNode
private transient KadConfiguration config;
/* Timer used to execute refresh operations */
private final transient Timer refreshOperationTimer;
private final transient TimerTask refreshOperationTTask;
private transient Timer refreshOperationTimer;
private transient TimerTask refreshOperationTTask;
/* Whether this node is up and running */
private boolean isRunning = false;
@ -94,28 +94,41 @@ public class KademliaNode
this.routingTable = routingTable;
this.messageFactory = new MessageFactory(this, this.dht, this.config);
this.server = new KadServer(udpPort, this.messageFactory, this.localNode, this.config);
this.refreshOperationTimer = new Timer(true);
this.startRefreshOperation();
this.isRunning = true;
}
/* Schedule Recurring RestoreOperation */
/**
* Schedule the recurring refresh operation
*/
public final void startRefreshOperation()
{
this.refreshOperationTimer = new Timer(true);
refreshOperationTTask = new TimerTask()
{
@Override
public void run()
{
try
{
@Override
public void run()
{
try
{
/* Runs a DHT RefreshOperation */
KademliaNode.this.refresh();
}
catch (IOException e)
{
System.err.println("Refresh Operation Failed; Message: " + e.getMessage());
}
}
/* Runs a DHT RefreshOperation */
KademliaNode.this.refresh();
}
catch (IOException e)
{
System.err.println("Refresh Operation Failed; Message: " + e.getMessage());
}
}
};
refreshOperationTimer.schedule(refreshOperationTTask, this.config.restoreInterval(), this.config.restoreInterval());
}
this.isRunning = true;
public final void stopRefreshOperation()
{
/* Close off the timer tasks */
this.refreshOperationTTask.cancel();
this.refreshOperationTimer.cancel();
this.refreshOperationTimer.purge();
}
public KademliaNode(String ownerId, Node node, int udpPort, RoutingTable routingTable, KadConfiguration config) throws IOException
@ -350,10 +363,7 @@ public class KademliaNode
/* Shut down the server */
this.server.shutdown();
/* Close off the timer tasks */
this.refreshOperationTTask.cancel();
this.refreshOperationTimer.cancel();
this.refreshOperationTimer.purge();
this.stopRefreshOperation();
this.isRunning = false;

View File

@ -0,0 +1,17 @@
package kademlia;
/**
* Class that keeps statistics for a single run
*
* @author Joshua Kissoon
* @since 20140505
*/
public class Statistics
{
/**
* How much data was sent and received by the server over the network
*/
public static long dataSent = 0;
public static long dataReceived = 0;
}

View File

@ -13,6 +13,7 @@ import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import kademlia.Statistics;
import kademlia.exceptions.KadServerDownException;
import kademlia.message.Message;
import kademlia.message.MessageFactory;
@ -195,6 +196,7 @@ public class KadServer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
Statistics.dataReceived += packet.getLength();
//System.out.println("Received packet of size: " + packet.getLength());
/* We've received a packet, now handle it */