mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
Added methods to start and stop the refresh operation
This commit is contained in:
parent
8d3ceb3ae1
commit
8aecda34c7
@ -58,9 +58,9 @@ 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,11 +363,8 @@ 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;
|
||||
|
||||
/* Save this Kademlia instance's state if required */
|
||||
|
17
src/kademlia/Statistics.java
Normal file
17
src/kademlia/Statistics.java
Normal 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;
|
||||
}
|
@ -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 */
|
||||
|
Loading…
Reference in New Issue
Block a user