From baeb6560505fb5dfb132d7729bd25c0cb428f401 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Tue, 6 May 2014 12:59:43 +0530 Subject: [PATCH] Cleaned up the code a bit - Removed some unnecessary exception throwing --- src/kademlia/core/KadServer.java | 10 +++++++- src/kademlia/message/MessageFactory.java | 4 ++-- .../operation/ContentLookupOperation.java | 2 ++ .../operation/NodeLookupOperation.java | 23 +++++++++++-------- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index 5a41376..a931b71 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -222,7 +222,10 @@ public class KadServer { receiver = this.receivers.remove(comm); TimerTask task = (TimerTask) tasks.remove(comm); - task.cancel(); + if (task != null) + { + task.cancel(); + } } } else @@ -297,6 +300,11 @@ public class KadServer @Override public void run() { + if (!KadServer.this.isRunning) + { + return; + } + try { unregister(comm); diff --git a/src/kademlia/message/MessageFactory.java b/src/kademlia/message/MessageFactory.java index a4ee16b..c06621e 100644 --- a/src/kademlia/message/MessageFactory.java +++ b/src/kademlia/message/MessageFactory.java @@ -48,7 +48,7 @@ public class MessageFactory case StoreContentMessage.CODE: return new StoreContentMessage(in); default: - System.out.println(this.localNode + " - No Message handler found for message. Code: " + code); + //System.out.println(this.localNode + " - No Message handler found for message. Code: " + code); return new SimpleMessage(in); } @@ -67,7 +67,7 @@ public class MessageFactory case StoreContentMessage.CODE: return new StoreContentReceiver(server, this.localNode, this.dht); default: - System.out.println("No receiver found for message. Code: " + code); + //System.out.println("No receiver found for message. Code: " + code); return new SimpleReceiver(); } } diff --git a/src/kademlia/operation/ContentLookupOperation.java b/src/kademlia/operation/ContentLookupOperation.java index 93cf872..2fd14b5 100644 --- a/src/kademlia/operation/ContentLookupOperation.java +++ b/src/kademlia/operation/ContentLookupOperation.java @@ -297,6 +297,8 @@ public class ContentLookupOperation implements Operation, Receiver /** * @return The list of all content found during the lookup operation + * + * @throws kademlia.exceptions.ContentNotFoundException */ public StorageEntry getContentFound() throws ContentNotFoundException { diff --git a/src/kademlia/operation/NodeLookupOperation.java b/src/kademlia/operation/NodeLookupOperation.java index f47d054..cf4f26c 100644 --- a/src/kademlia/operation/NodeLookupOperation.java +++ b/src/kademlia/operation/NodeLookupOperation.java @@ -39,7 +39,6 @@ public class NodeLookupOperation implements Operation, Receiver private final KadServer server; private final KademliaNode localNode; - private final NodeId lookupId; private final KadConfiguration config; private boolean error; @@ -68,7 +67,6 @@ public class NodeLookupOperation implements Operation, Receiver { this.server = server; this.localNode = localNode; - this.lookupId = lookupId; this.config = config; this.lookupMessage = new NodeLookupMessage(localNode.getNode(), lookupId); @@ -79,7 +77,6 @@ public class NodeLookupOperation implements Operation, Receiver */ this.comparator = new KeyComparator(lookupId); this.nodes = new TreeMap(this.comparator); - //this.nodes = new HashMap<>(); } /** @@ -117,11 +114,17 @@ public class NodeLookupOperation implements Operation, Receiver break; } } - if (error) - { - /* If we still haven't received any responses by then, do a routing timeout */ - throw new RoutingException("Lookup Timeout."); - } + + /** + * There is no need to throw an exception here! + * If the operation times out means we didn't get replies from all nodes, + * so lets just simply return the K-Closest nodes we knoe + */ +// if (error) +// { +// /* If we still haven't received any responses by then, do a routing timeout */ +// throw new RoutingException("Node Lookup Timeout."); +// } /* Now after we've finished, we would have an idea of offline nodes, lets update our routing table */ this.localNode.getRoutingTable().setUnresponsiveContacts(this.getFailedNodes()); @@ -300,11 +303,11 @@ public class NodeLookupOperation implements Operation, Receiver public synchronized void timeout(int comm) throws IOException { /* Get the node associated with this communication */ - Node n = this.messagesTransiting.get(new Integer(comm)); + Node n = this.messagesTransiting.get(comm); if (n == null) { - throw new UnknownMessageException("Unknown comm: " + comm); + return; } /* Mark this node as failed and inform the routing table that it is unresponsive */