From 8aecda34c7990338e5232fe977099ad30174a1b4 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Mon, 5 May 2014 18:03:57 +0530 Subject: [PATCH] Added methods to start and stop the refresh operation --- src/kademlia/KademliaNode.java | 60 +++++++++++++++++++------------- src/kademlia/Statistics.java | 17 +++++++++ src/kademlia/core/KadServer.java | 2 ++ 3 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/kademlia/Statistics.java diff --git a/src/kademlia/KademliaNode.java b/src/kademlia/KademliaNode.java index b53c686..5d14145 100644 --- a/src/kademlia/KademliaNode.java +++ b/src/kademlia/KademliaNode.java @@ -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 */ diff --git a/src/kademlia/Statistics.java b/src/kademlia/Statistics.java new file mode 100644 index 0000000..dbdd6b9 --- /dev/null +++ b/src/kademlia/Statistics.java @@ -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; +} diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index 63d6aca..7fb8da2 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -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 */