Statistician

- Returns some times in milliseconds instead of microseconds

KadServer
- Made a few operations synchronized
- Handled the exception when the timer is already canceled
This commit is contained in:
Joshua Kissoon 2014-05-07 11:22:02 +05:30
parent 1bee79789d
commit 718fa78b9d
3 changed files with 19 additions and 12 deletions

View File

@ -111,11 +111,11 @@ public class Statistician
/**
* Compute the average time a content lookup took
*
* @return The average time
* @return The average time in milliseconds
*/
public double averageContentLookupTime()
{
double avg = (double) this.totalContentLookupTime / (double) this.numContentLookups;
double avg = (double) ((double) this.totalContentLookupTime / (double) this.numContentLookups) / 1000000D;
DecimalFormat df = new DecimalFormat("#.00");
return new Double(df.format(avg));
}

View File

@ -14,7 +14,7 @@ public class DefaultConfiguration implements KadConfiguration
private final static long RESPONSE_TIMEOUT = 1500;
private final static long OPERATION_TIMEOUT = 3000;
private final static int CONCURRENCY = 10;
private final static int K = 10;
private final static int K = 5;
private final static int RCSIZE = 3;
private final static int STALE = 1;
private final static String LOCAL_FOLDER = "kademlia";

View File

@ -111,7 +111,7 @@ public class KadServer
{
if (!isRunning)
{
throw new KadServerDownException("Kad Server is not running on node " + this.localNode);
throw new KadServerDownException(this.localNode + " - Kad Server is not running.");
}
/* Generate a random communication ID */
@ -120,11 +120,18 @@ public class KadServer
/* If we have a receiver */
if (recv != null)
{
/* Setup the receiver to handle message response */
receivers.put(comm, recv);
TimerTask task = new TimeoutTask(comm, recv);
timer.schedule(task, this.config.responseTimeout());
tasks.put(comm, task);
try
{
/* Setup the receiver to handle message response */
receivers.put(comm, recv);
TimerTask task = new TimeoutTask(comm, recv);
timer.schedule(task, this.config.responseTimeout());
tasks.put(comm, task);
}
catch (IllegalStateException ex)
{
/* The timer is already cancelled so we cannot do anything here really */
}
}
/* Send the message */
@ -154,7 +161,7 @@ public class KadServer
/**
* Internal sendMessage method called by the public sendMessage method after a communicationId is generated
*/
private void sendMessage(Node to, Message msg, int comm) throws IOException
private synchronized void sendMessage(Node to, Message msg, int comm) throws IOException
{
/* Use a try-with resource to auto-close streams after usage */
try (ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout);)
@ -185,7 +192,7 @@ public class KadServer
/**
* Listen for incoming messages in a separate thread
*/
private void listen()
private synchronized void listen()
{
try
{
@ -272,7 +279,7 @@ public class KadServer
/**
* Stops listening and shuts down the server
*/
public void shutdown()
public synchronized void shutdown()
{
this.isRunning = false;
this.socket.close();