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; private transient KadConfiguration config;
/* Timer used to execute refresh operations */ /* Timer used to execute refresh operations */
private final transient Timer refreshOperationTimer; private transient Timer refreshOperationTimer;
private final transient TimerTask refreshOperationTTask; private transient TimerTask refreshOperationTTask;
/* Whether this node is up and running */ /* Whether this node is up and running */
private boolean isRunning = false; private boolean isRunning = false;
@ -94,9 +94,16 @@ public class KademliaNode
this.routingTable = routingTable; this.routingTable = routingTable;
this.messageFactory = new MessageFactory(this, this.dht, this.config); this.messageFactory = new MessageFactory(this, this.dht, this.config);
this.server = new KadServer(udpPort, this.messageFactory, this.localNode, 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() refreshOperationTTask = new TimerTask()
{ {
@Override @Override
@ -114,8 +121,14 @@ public class KademliaNode
} }
}; };
refreshOperationTimer.schedule(refreshOperationTTask, this.config.restoreInterval(), this.config.restoreInterval()); 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 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 */ /* Shut down the server */
this.server.shutdown(); this.server.shutdown();
/* Close off the timer tasks */ this.stopRefreshOperation();
this.refreshOperationTTask.cancel();
this.refreshOperationTimer.cancel();
this.refreshOperationTimer.purge();
this.isRunning = false; 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.Random;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import kademlia.Statistics;
import kademlia.exceptions.KadServerDownException; import kademlia.exceptions.KadServerDownException;
import kademlia.message.Message; import kademlia.message.Message;
import kademlia.message.MessageFactory; import kademlia.message.MessageFactory;
@ -195,6 +196,7 @@ public class KadServer
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet); socket.receive(packet);
Statistics.dataReceived += packet.getLength();
//System.out.println("Received packet of size: " + packet.getLength()); //System.out.println("Received packet of size: " + packet.getLength());
/* We've received a packet, now handle it */ /* We've received a packet, now handle it */