Fixed the bug in NodeConnection operation where the messages were still being re-sent even after an acknowledgement was received.

Fixed by canceling the TimerTask
This commit is contained in:
Joshua Kissoon 2014-02-19 10:30:42 +05:30
parent b3e1403872
commit ed1ac876f3
4 changed files with 18 additions and 4 deletions

View File

@ -34,6 +34,7 @@ public class KadServer
private boolean isRunning;
private final HashMap<Integer, Receiver> receivers;
private final Timer timer; // Schedule future tasks
private final HashMap<Integer, TimerTask> tasks; // Keep track of scheduled tasks
/* Factories */
private final MessageFactory messageFactory;
@ -41,14 +42,15 @@ public class KadServer
{
isRunning = true;
this.tasks = new HashMap<>();
this.receivers = new HashMap<>();
this.timer = new Timer(true);
}
public KadServer(int udpPort, MessageFactory mFactory) throws SocketException
{
this.udpPort = udpPort;
this.socket = new DatagramSocket(udpPort);
this.receivers = new HashMap<>();
this.timer = new Timer(true);
this.messageFactory = mFactory;
@ -92,7 +94,9 @@ public class KadServer
/* Setup the receiver to handle message response */
receivers.put(comm, recv);
timer.schedule(new TimeoutTask(comm, recv), Configuration.RESPONSE_TIMEOUT);
TimerTask task = new TimeoutTask(comm, recv);
timer.schedule(task, Configuration.RESPONSE_TIMEOUT);
tasks.put(comm, task);
/* Send the message */
sendMessage(to, msg, comm);
@ -167,7 +171,12 @@ public class KadServer
if (this.receivers.containsKey(comm))
{
/* If there is a reciever in the receivers to handle this */
synchronized (this)
{
receiver = this.receivers.remove(comm);
TimerTask task = (TimerTask) tasks.remove(comm);
task.cancel();
}
}
else
{
@ -207,6 +216,7 @@ public class KadServer
{
Integer key = new Integer(comm);
receivers.remove(key);
this.tasks.remove(key);
}
/**

View File

@ -32,6 +32,7 @@ public class ConnectReceiver implements Receiver
@Override
public void receive(Message incoming, int comm) throws IOException
{
System.out.println("Received incoming connect message, sending acknowledgement message.");
ConnectMessage mess = (ConnectMessage) incoming;
/* Update the local space by inserting the origin node. */

View File

@ -46,6 +46,7 @@ public class ConnectOperation implements Operation, Receiver
{
try
{
System.out.println("Connect Operation executing.");
/* Contact the bootstrap node */
this.error = true;
this.attempts = 0;
@ -109,6 +110,7 @@ public class ConnectOperation implements Operation, Receiver
@Override
public synchronized void timeout(int comm) throws IOException
{
System.out.println("Timeout function called");
if (++this.attempts < MAX_CONNECT_ATTEMPTS)
{
this.server.sendMessage(this.bootstrapNode, new ConnectMessage(this.localNode), this);

View File

@ -27,6 +27,7 @@ public class NodeConnectionTest
/* Connecting 2 to 1 */
kad1.connect(kad2.getNode());
System.out.println("Kad 1 After: ");
System.out.println(kad1.getNode().getRoutingTable());
System.out.println("Kad 2 After: ");