From ed1ac876f3ce49830f65017bd40ca3a0c6f258d5 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Wed, 19 Feb 2014 10:30:42 +0530 Subject: [PATCH] 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 --- src/kademlia/core/KadServer.java | 18 ++++++++++++++---- src/kademlia/message/ConnectReceiver.java | 1 + src/kademlia/operation/ConnectOperation.java | 2 ++ src/kademlia/tests/NodeConnectionTest.java | 1 + 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index 75be02c..58d0a93 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -34,6 +34,7 @@ public class KadServer private boolean isRunning; private final HashMap receivers; private final Timer timer; // Schedule future tasks + private final HashMap 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 */ - receiver = this.receivers.remove(comm); + 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); } /** diff --git a/src/kademlia/message/ConnectReceiver.java b/src/kademlia/message/ConnectReceiver.java index dbef95a..642afb8 100644 --- a/src/kademlia/message/ConnectReceiver.java +++ b/src/kademlia/message/ConnectReceiver.java @@ -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. */ diff --git a/src/kademlia/operation/ConnectOperation.java b/src/kademlia/operation/ConnectOperation.java index ae24b84..71c0ef5 100644 --- a/src/kademlia/operation/ConnectOperation.java +++ b/src/kademlia/operation/ConnectOperation.java @@ -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); diff --git a/src/kademlia/tests/NodeConnectionTest.java b/src/kademlia/tests/NodeConnectionTest.java index 2bba791..7623530 100644 --- a/src/kademlia/tests/NodeConnectionTest.java +++ b/src/kademlia/tests/NodeConnectionTest.java @@ -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: ");