From 3f82c4a0efa20e89287e630958737b46d553ff1a Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Sat, 22 Mar 2014 12:35:00 +0530 Subject: [PATCH] Did some code cleanups - Add Comments - Remove println statements - Deleted useless comments, etc --- src/kademlia/core/KadServer.java | 15 ++-- src/kademlia/core/Kademlia.java | 7 +- src/kademlia/exceptions/RoutingException.java | 11 +-- .../exceptions/UnknownMessageException.java | 11 +-- src/kademlia/message/AcknowledgeMessage.java | 12 +-- src/kademlia/message/ConnectMessage.java | 11 +-- src/kademlia/message/ConnectReceiver.java | 12 +-- .../message/ContentLookupMessage.java | 4 +- src/kademlia/message/ContentMessage.java | 2 +- src/kademlia/message/Message.java | 8 +- src/kademlia/message/NodeLookupMessage.java | 11 +-- src/kademlia/message/NodeLookupReceiver.java | 19 ++--- src/kademlia/message/NodeReplyMessage.java | 12 +-- src/kademlia/message/SimpleMessage.java | 12 +-- src/kademlia/message/SimpleReceiver.java | 14 ++-- .../message/StoreContentReceiver.java | 3 - src/kademlia/node/Node.java | 3 +- src/kademlia/node/NodeId.java | 2 +- .../operation/BucketRefreshOperation.java | 1 - src/kademlia/operation/ConnectOperation.java | 4 - .../operation/NodeLookupOperation.java | 46 +----------- src/kademlia/operation/Operation.java | 11 +-- src/kademlia/operation/Receiver.java | 13 ++-- src/kademlia/operation/StoreOperation.java | 4 - src/kademlia/routing/Bucket.java | 13 ++-- src/kademlia/routing/KadBucket.java | 5 +- src/kademlia/routing/RoutingTable.java | 3 - src/kademlia/tests/ContentSendingTest.java | 2 +- src/kademlia/tests/DHTContentImpl.java | 2 +- src/kademlia/tests/NodeConnectionTest.java | 2 +- src/kademlia/tests/SimpleMessageTest.java | 11 +-- src/kademlia/tests/TreeMapTest.java | 74 ------------------- 32 files changed, 114 insertions(+), 246 deletions(-) delete mode 100644 src/kademlia/tests/TreeMapTest.java diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index 8b30cbf..69fa54a 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -113,7 +113,7 @@ public class KadServer if (recv != null) { /* Setup the receiver to handle message response */ - System.out.println(this.localNode + " Putting Receiver for comm: " + comm + " Receiver: " + recv); + //System.out.println(this.localNode + " Putting Receiver for comm: " + comm + " Receiver: " + recv); receivers.put(comm, recv); TimerTask task = new TimeoutTask(comm, recv); timer.schedule(task, Configuration.RESPONSE_TIMEOUT); @@ -200,18 +200,15 @@ public class KadServer Message msg = messageFactory.createMessage(messCode, din); din.close(); - System.out.println(this.localNode.getNodeId() + " Message Received: [Comm: " + comm + "] " + msg); + //System.out.println(this.localNode.getNodeId() + " Message Received: [Comm: " + comm + "] " + msg); /* Get a receiver for this message */ Receiver receiver; if (this.receivers.containsKey(comm)) { - System.out.println("Receiver found: " + this.receivers.get(comm)); /* If there is a reciever in the receivers to handle this */ synchronized (this) { - System.out.println("Sync Block Receiver found: " + this.receivers.get(comm)); - System.out.println(this.localNode + " Receivers: "); this.printReceivers(); receiver = this.receivers.remove(comm); TimerTask task = (TimerTask) tasks.remove(comm); @@ -234,7 +231,7 @@ public class KadServer catch (IOException e) { this.isRunning = false; - System.out.println("Server ran into a problem in listener method. Message: " + e.getMessage()); + System.err.println("Server ran into a problem in listener method. Message: " + e.getMessage()); } } } @@ -297,14 +294,14 @@ public class KadServer } catch (IOException e) { - System.out.println("Cannot unregister a receiver. Message: " + e.getMessage()); + System.err.println("Cannot unregister a receiver. Message: " + e.getMessage()); } } } - + public void printReceivers() { - for(Integer r : this.receivers.keySet()) + for (Integer r : this.receivers.keySet()) { System.out.println("Receiver for comm: " + r + "; Receiver: " + this.receivers.get(r)); } diff --git a/src/kademlia/core/Kademlia.java b/src/kademlia/core/Kademlia.java index 79e2ce7..c9b6691 100644 --- a/src/kademlia/core/Kademlia.java +++ b/src/kademlia/core/Kademlia.java @@ -155,7 +155,7 @@ public class Kademlia */ din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId) + File.separator + "dht.kns")); DHT idht = new JsonDHTSerializer().read(din); - System.out.println("Finished reading data."); + return new Kademlia(ownerId, inode, ikad.getPort(), idht); } @@ -227,14 +227,12 @@ public class Kademlia if (this.dht.contains(param)) { /* If the content exist in our own DHT, then return it. */ - System.out.println("Found content locally"); contentFound = new ArrayList<>(); contentFound.add(this.dht.get(param)); } else { /* Seems like it doesn't exist in our DHT, get it from other Nodes */ - System.out.println("Looking for content on foreign nodes"); ContentLookupOperation clo = new ContentLookupOperation(server, localNode, param, numResultsReq); clo.execute(); contentFound = clo.getContentFound(); @@ -294,7 +292,6 @@ public class Kademlia */ private void saveKadState() throws FileNotFoundException, IOException { - System.out.println("Saving state"); DataOutputStream dout; /** @@ -323,8 +320,6 @@ public class Kademlia dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId) + File.separator + "dht.kns")); new JsonDHTSerializer().write(this.dht, dout); - System.out.println("FInished saving state"); - } /** diff --git a/src/kademlia/exceptions/RoutingException.java b/src/kademlia/exceptions/RoutingException.java index c28f671..aa0697c 100644 --- a/src/kademlia/exceptions/RoutingException.java +++ b/src/kademlia/exceptions/RoutingException.java @@ -1,12 +1,13 @@ -/** - * @author Joshua Kissoon - * @created 20140219 - * @desc An exception to be thrown whenever there is a routing problem - */ package kademlia.exceptions; import java.io.IOException; +/** + * An exception to be thrown whenever there is a routing problem + * + * @author Joshua Kissoon + * @created 20140219 + */ public class RoutingException extends IOException { diff --git a/src/kademlia/exceptions/UnknownMessageException.java b/src/kademlia/exceptions/UnknownMessageException.java index 8a70670..a7116ec 100644 --- a/src/kademlia/exceptions/UnknownMessageException.java +++ b/src/kademlia/exceptions/UnknownMessageException.java @@ -1,10 +1,11 @@ -/** - * @author Joshua Kissoon - * @created 20140219 - * @desc An exception used to indicate an unknown message type or communication identifier - */ package kademlia.exceptions; +/** + * An exception used to indicate an unknown message type or communication identifier + * + * @author Joshua Kissoon + * @created 20140219 + */ public class UnknownMessageException extends RuntimeException { diff --git a/src/kademlia/message/AcknowledgeMessage.java b/src/kademlia/message/AcknowledgeMessage.java index 28b51a4..0f4fef5 100644 --- a/src/kademlia/message/AcknowledgeMessage.java +++ b/src/kademlia/message/AcknowledgeMessage.java @@ -1,8 +1,3 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc A message used to acknowledge a request from a node - */ package kademlia.message; import java.io.DataInputStream; @@ -10,6 +5,13 @@ import java.io.DataOutputStream; import java.io.IOException; import kademlia.node.Node; +/** + * A message used to acknowledge a request from a node; can be used in many situations. + * - Mainly used to acknowledge a connect message + * + * @author Joshua Kissoon + * @created 20140218 + */ public class AcknowledgeMessage implements Message { diff --git a/src/kademlia/message/ConnectMessage.java b/src/kademlia/message/ConnectMessage.java index c8e3bb2..25629be 100644 --- a/src/kademlia/message/ConnectMessage.java +++ b/src/kademlia/message/ConnectMessage.java @@ -1,8 +1,3 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc A message used to connect nodes - */ package kademlia.message; import java.io.DataInputStream; @@ -10,6 +5,12 @@ import java.io.DataOutputStream; import java.io.IOException; import kademlia.node.Node; +/** + * A message sent to another node requesting to connect to them. + * + * @author Joshua Kissoon + * @created 20140218 + */ public class ConnectMessage implements Message { diff --git a/src/kademlia/message/ConnectReceiver.java b/src/kademlia/message/ConnectReceiver.java index 745b57a..c6e58d8 100644 --- a/src/kademlia/message/ConnectReceiver.java +++ b/src/kademlia/message/ConnectReceiver.java @@ -1,8 +1,3 @@ -/** - * @author Joshua Kissoon - * @created 20140219 - * @desc Receives a ConnectMessage and sends an AcknowledgeMessage as reply - */ package kademlia.message; import java.io.IOException; @@ -10,6 +5,12 @@ import kademlia.core.KadServer; import kademlia.node.Node; import kademlia.operation.Receiver; +/** + * Receives a ConnectMessage and sends an AcknowledgeMessage as reply. + * + * @author Joshua Kissoon + * @created 20140219 + */ public class ConnectReceiver implements Receiver { @@ -38,7 +39,6 @@ public class ConnectReceiver implements Receiver this.localNode.getRoutingTable().insert(mess.getOrigin()); /* Respond to the connect request */ - System.out.println(this.localNode + " Connect message received, sending an AcknowledgementMessage to " + mess.getOrigin()); AcknowledgeMessage msg = new AcknowledgeMessage(this.localNode); /* Reply to the connect message with an Acknowledgement */ diff --git a/src/kademlia/message/ContentLookupMessage.java b/src/kademlia/message/ContentLookupMessage.java index b62077c..9f91c79 100644 --- a/src/kademlia/message/ContentLookupMessage.java +++ b/src/kademlia/message/ContentLookupMessage.java @@ -1,16 +1,14 @@ package kademlia.message; -import com.google.gson.JsonSerializationContext; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import kademlia.core.GetParameter; -import kademlia.dht.KadContent; import kademlia.node.Node; import kademlia.serializer.JsonSerializer; /** - * Messages used to send to another node requesting content + * Messages used to send to another node requesting content. * * @author Joshua Kissoon * @since 20140226 diff --git a/src/kademlia/message/ContentMessage.java b/src/kademlia/message/ContentMessage.java index 6155a1e..8015751 100644 --- a/src/kademlia/message/ContentMessage.java +++ b/src/kademlia/message/ContentMessage.java @@ -50,7 +50,7 @@ public class ContentMessage implements Message public final void fromStream(DataInputStream in) throws IOException { this.origin = new Node(in); - + try { this.content = new JsonSerializer().read(in); diff --git a/src/kademlia/message/Message.java b/src/kademlia/message/Message.java index 890c4c7..c1fd105 100644 --- a/src/kademlia/message/Message.java +++ b/src/kademlia/message/Message.java @@ -1,10 +1,14 @@ package kademlia.message; -public interface Message extends Streamable { +public interface Message extends Streamable +{ + /** * The unique code for the message type, used to differentiate all messages * from each other. Since this is of byte type there can * be at most 256 different message types. - **/ + * + * @return byte A unique code representing the message type + * */ public byte code(); } diff --git a/src/kademlia/message/NodeLookupMessage.java b/src/kademlia/message/NodeLookupMessage.java index 9853563..865f161 100644 --- a/src/kademlia/message/NodeLookupMessage.java +++ b/src/kademlia/message/NodeLookupMessage.java @@ -1,8 +1,3 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc A message used to connect nodes - */ package kademlia.message; import java.io.DataInputStream; @@ -11,6 +6,12 @@ import java.io.IOException; import kademlia.node.Node; import kademlia.node.NodeId; +/** + * A message sent to other nodes requesting the K-Closest nodes to a key sent in this message. + * + * @author Joshua Kissoon + * @created 20140218 + */ public class NodeLookupMessage implements Message { diff --git a/src/kademlia/message/NodeLookupReceiver.java b/src/kademlia/message/NodeLookupReceiver.java index f8b52d9..6930364 100644 --- a/src/kademlia/message/NodeLookupReceiver.java +++ b/src/kademlia/message/NodeLookupReceiver.java @@ -1,8 +1,3 @@ -/** - * @author Joshua Kissoon - * @created 20140219 - * @desc Receives a ConnectMessage and sends an AcknowledgeMessage as reply - */ package kademlia.message; import java.io.IOException; @@ -12,6 +7,12 @@ import kademlia.core.KadServer; import kademlia.node.Node; import kademlia.operation.Receiver; +/** + * Receives a NodeLookupMessage and sends a NodeReplyMessage as reply with the K-Closest nodes to the ID sent. + * + * @author Joshua Kissoon + * @created 20140219 + */ public class NodeLookupReceiver implements Receiver { @@ -45,15 +46,7 @@ public class NodeLookupReceiver implements Receiver /* Find nodes closest to the LookupId */ List nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K); -// System.out.println("\n" + this.localNode + " Closest Nodes: "); -// for (Node n : nodes) -// { -// System.out.println(n.getNodeId()); -// } -// System.out.println(); - /* Respond to the NodeLookupMessage */ - System.out.println(this.localNode + " Sending NodeReplyMessage to " + origin); Message reply = new NodeReplyMessage(this.localNode, nodes); /* Let the Server send the reply */ diff --git a/src/kademlia/message/NodeReplyMessage.java b/src/kademlia/message/NodeReplyMessage.java index c90d08a..993cf13 100644 --- a/src/kademlia/message/NodeReplyMessage.java +++ b/src/kademlia/message/NodeReplyMessage.java @@ -1,8 +1,3 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc A message used to connect nodes - */ package kademlia.message; import java.io.DataInputStream; @@ -12,6 +7,13 @@ import java.util.ArrayList; import java.util.List; import kademlia.node.Node; +/** + * A message used to connect nodes. + * When a NodeLookup Request comes in, we respond with a NodeReplyMessage. + * + * @author Joshua Kissoon + * @created 20140218 + */ public class NodeReplyMessage implements Message { diff --git a/src/kademlia/message/SimpleMessage.java b/src/kademlia/message/SimpleMessage.java index 9d392c0..2b2b157 100644 --- a/src/kademlia/message/SimpleMessage.java +++ b/src/kademlia/message/SimpleMessage.java @@ -1,14 +1,15 @@ -/** - * @author Joshua Kissoon - * @created 20140217 - * @desc A simple message used for testing the system - */ package kademlia.message; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +/** + * A simple message used for testing the system; Default message constructed if the message type sent is not available + * + * @author Joshua Kissoon + * @created 20140217 + */ public class SimpleMessage implements Message { @@ -24,7 +25,6 @@ public class SimpleMessage implements Message public SimpleMessage(DataInputStream in) { - System.out.println("Creating message from input stream."); this.fromStream(in); } diff --git a/src/kademlia/message/SimpleReceiver.java b/src/kademlia/message/SimpleReceiver.java index 7a41496..e5f455c 100644 --- a/src/kademlia/message/SimpleReceiver.java +++ b/src/kademlia/message/SimpleReceiver.java @@ -1,14 +1,14 @@ -/** - * @author Joshua - * @created - * @desc - */ package kademlia.message; import java.io.IOException; -import kademlia.message.Message; import kademlia.operation.Receiver; +/** + * Default receiver if none other is called + * + * @author Joshua Kissoon + * @created 20140202 + */ public class SimpleReceiver implements Receiver { @@ -21,6 +21,6 @@ public class SimpleReceiver implements Receiver @Override public void timeout(int conversationId) throws IOException { - System.out.println(""); + System.out.println("SimpleReceiver message timeout."); } } diff --git a/src/kademlia/message/StoreContentReceiver.java b/src/kademlia/message/StoreContentReceiver.java index b6832df..10a0b49 100644 --- a/src/kademlia/message/StoreContentReceiver.java +++ b/src/kademlia/message/StoreContentReceiver.java @@ -35,9 +35,6 @@ public class StoreContentReceiver implements Receiver /* Insert the message sender into this node's routing table */ this.localNode.getRoutingTable().insert(msg.getOrigin()); - System.out.println(this.localNode + " - Received a store content message"); - System.out.println(msg); - try { /* Store this Content into the DHT */ diff --git a/src/kademlia/node/Node.java b/src/kademlia/node/Node.java index 33c3674..f6cc88f 100644 --- a/src/kademlia/node/Node.java +++ b/src/kademlia/node/Node.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; -import java.util.Comparator; import kademlia.message.Streamable; import kademlia.routing.RoutingTable; @@ -23,7 +22,7 @@ public class Node implements Streamable private NodeId nodeId; private InetAddress inetAddress; private int port; - private String strRep; + private final String strRep; private transient RoutingTable routingTable; diff --git a/src/kademlia/node/NodeId.java b/src/kademlia/node/NodeId.java index 4840628..0341617 100644 --- a/src/kademlia/node/NodeId.java +++ b/src/kademlia/node/NodeId.java @@ -239,7 +239,7 @@ public class NodeId implements Streamable } @Override - public void fromStream(DataInputStream in) throws IOException + public final void fromStream(DataInputStream in) throws IOException { byte[] input = new byte[ID_LENGTH / 8]; in.readFully(input); diff --git a/src/kademlia/operation/BucketRefreshOperation.java b/src/kademlia/operation/BucketRefreshOperation.java index 7fd514f..7f98ec7 100644 --- a/src/kademlia/operation/BucketRefreshOperation.java +++ b/src/kademlia/operation/BucketRefreshOperation.java @@ -50,7 +50,6 @@ public class BucketRefreshOperation implements Operation { try { - //System.out.println("Distance: " + localNode.getNodeId().getDistance(current) + " - ID: " + current); new NodeLookupOperation(server, localNode, localNode.getNodeId()).execute(); } catch (IOException e) diff --git a/src/kademlia/operation/ConnectOperation.java b/src/kademlia/operation/ConnectOperation.java index 4dccf5c..e52a47a 100644 --- a/src/kademlia/operation/ConnectOperation.java +++ b/src/kademlia/operation/ConnectOperation.java @@ -61,7 +61,6 @@ public class ConnectOperation implements Operation, Receiver } /* Perform lookup for our own ID to get nodes close to us */ - System.out.println("Looking up for nodes with our own ID"); Operation lookup = new NodeLookupOperation(this.server, this.localNode, this.localNode.getNodeId()); lookup.execute(); @@ -70,7 +69,6 @@ public class ConnectOperation implements Operation, Receiver * I think after the above lookup operation, K buckets will be filled * Not sure if this operation is needed here */ - } catch (IOException | InterruptedException e) { @@ -88,7 +86,6 @@ public class ConnectOperation implements Operation, Receiver { /* The incoming message will be an acknowledgement message */ AcknowledgeMessage msg = (AcknowledgeMessage) incoming; - System.out.println("ConnectOperation now handling Acknowledgement Message: " + msg); /* The bootstrap node has responded, insert it into our space */ this.localNode.getRoutingTable().insert(this.bootstrapNode); @@ -111,7 +108,6 @@ 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/operation/NodeLookupOperation.java b/src/kademlia/operation/NodeLookupOperation.java index cb0054e..5dbaab1 100644 --- a/src/kademlia/operation/NodeLookupOperation.java +++ b/src/kademlia/operation/NodeLookupOperation.java @@ -6,7 +6,6 @@ import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.SortedMap; import java.util.TreeMap; import kademlia.core.Configuration; import kademlia.core.KadServer; @@ -127,33 +126,11 @@ public class NodeLookupOperation implements Operation, Receiver for (Node o : list) { /* If this node is not in the list, add the node */ - System.out.println("Current Nodes & Their Status: "); - for (Node e : this.nodes.keySet()) + if (!nodes.containsKey(o)) { - System.out.println(e + ": " + this.nodes.get(e)); - } - - System.out.println("Nodes Received to add: "); - for (Node e : list) - { - System.out.println(e); - } - - /** - * @note We add the extra check since for some reason, treemap returns that it doesn't contain the localNode even though it does - */ - if (!nodes.containsKey(o) /*&& !o.equals(this.localNode)*/) - { - System.out.println(localNode + ": Adding node " + o.getNodeId() + " Equal to local: " + o.equals(this.localNode)); nodes.put(o, UNASKED); } } - -// System.out.println(this.localNode.getNodeId() + " Nodes List: "); -// for (Node o : this.nodes.keySet()) -// { -// System.out.println(o.getNodeId() + " hash: " + o.hashCode()); -// } } /** @@ -177,12 +154,6 @@ public class NodeLookupOperation implements Operation, Receiver /* Get unqueried nodes among the K closest seen that have not FAILED */ List unasked = this.closestNodesNotFailed(UNASKED); - System.out.println("Getting closest unasked Nodes not failed. Nodes "); - for (Node nn : unasked) - { - System.out.println(nn.getNodeId()); - } - System.out.println("Unasked printing finished"); if (unasked.isEmpty() && this.messagesTransiting.isEmpty()) { @@ -191,8 +162,6 @@ public class NodeLookupOperation implements Operation, Receiver return true; } - /* Sort nodes according to criteria */ - //Collections.sort(unasked, this.comparator); /** * Send messages to nodes in the list; * making sure than no more than CONCURRENCY messsages are in transit @@ -203,15 +172,7 @@ public class NodeLookupOperation implements Operation, Receiver int comm = server.sendMessage(n, lookupMessage, this); - System.out.println(this.localNode + "\n\n\n ************** Sent lookup message to: " + n + "; Comm: " + comm); this.nodes.put(n, AWAITING); - System.out.println("Awaiting: " + AWAITING); - System.out.println("Node: " + n + " status: " + this.nodes.get(n)); - System.out.println("\n\nPrinting entries: "); - for (Map.Entry e : this.nodes.entrySet()) - { - System.out.println("Node: " + e.getKey() + "; Value: " + e.getValue()); - } this.messagesTransiting.put(comm, n); } @@ -255,8 +216,6 @@ public class NodeLookupOperation implements Operation, Receiver */ private List closestNodesNotFailed(String status) { - - System.out.println(this.localNode + " - closestNodesNotFailed called, Status looking for: " + status); List closestNodes = new ArrayList<>(Configuration.K); int remainingSpaces = Configuration.K; @@ -267,7 +226,6 @@ public class NodeLookupOperation implements Operation, Receiver if (status.equals(e.getValue())) { /* We got one with the required status, now add it */ - System.out.println("Adding " + e.getKey() + "; status: " + e.getValue()); closestNodes.add(e.getKey()); } @@ -296,11 +254,9 @@ public class NodeLookupOperation implements Operation, Receiver /* Add the origin node to our routing table */ Node origin = msg.getOrigin(); - //System.out.println(this.localNode.getNodeId() + " Lookup Operation Response From: " + origin.getNodeId()); this.localNode.getRoutingTable().insert(origin); /* Set that we've completed ASKing the origin node */ - System.out.println("Setting " + origin + " as " + ASKED); this.nodes.put(origin, ASKED); /* Remove this msg from messagesTransiting since it's completed now */ diff --git a/src/kademlia/operation/Operation.java b/src/kademlia/operation/Operation.java index aec1602..e348b6b 100644 --- a/src/kademlia/operation/Operation.java +++ b/src/kademlia/operation/Operation.java @@ -1,13 +1,14 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc Interface for different Kademlia operations - */ package kademlia.operation; import java.io.IOException; import kademlia.exceptions.RoutingException; +/** + * An operation in the Kademlia routing protocol + * + * @author Joshua Kissoon + * @created 20140218 + */ public interface Operation { diff --git a/src/kademlia/operation/Receiver.java b/src/kademlia/operation/Receiver.java index f63596b..49af560 100644 --- a/src/kademlia/operation/Receiver.java +++ b/src/kademlia/operation/Receiver.java @@ -1,13 +1,14 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc A receiver waits for incoming messages and perform some action when the message is received - */ package kademlia.operation; import java.io.IOException; import kademlia.message.Message; +/** + * A receiver waits for incoming messages and perform some action when the message is received + * + * @author Joshua Kissoon + * @created 20140218 + */ public interface Receiver { @@ -16,6 +17,8 @@ public interface Receiver * * @param conversationId The ID of this conversation, used for further conversations * @param incoming The incoming + * + * @throws java.io.IOException */ public void receive(Message incoming, int conversationId) throws IOException; diff --git a/src/kademlia/operation/StoreOperation.java b/src/kademlia/operation/StoreOperation.java index 35b1f1d..b614472 100644 --- a/src/kademlia/operation/StoreOperation.java +++ b/src/kademlia/operation/StoreOperation.java @@ -41,8 +41,6 @@ public class StoreOperation implements Operation ndlo.execute(); List nodes = ndlo.getClosestNodes(); - System.out.println("Nodes to put content on: " + nodes); - /* Create the message */ Message msg = new StoreContentMessage(this.localNode, this.content); @@ -61,8 +59,6 @@ public class StoreOperation implements Operation this.server.sendMessage(n, msg, null); } } - - System.out.println("\n\n\n\nSTORE CONTENT FINISHED"); } /** diff --git a/src/kademlia/routing/Bucket.java b/src/kademlia/routing/Bucket.java index b27dd87..8a72820 100644 --- a/src/kademlia/routing/Bucket.java +++ b/src/kademlia/routing/Bucket.java @@ -1,12 +1,15 @@ -/** - * @author Joshua Kissoon - * @created 20140215 - * @desc A bucket for the DHT Protocol - */ package kademlia.routing; import kademlia.node.Node; +/** + * A bucket used to store Nodes in the routing table. + * + * @todo Update this interface and use this as parameter type, etc... instead of the KadBucket implementation used throughout the application + * + * @author Joshua Kissoon + * @created 20140215 + */ public interface Bucket { diff --git a/src/kademlia/routing/KadBucket.java b/src/kademlia/routing/KadBucket.java index 733e5dc..42bb52a 100644 --- a/src/kademlia/routing/KadBucket.java +++ b/src/kademlia/routing/KadBucket.java @@ -18,6 +18,7 @@ public class KadBucket implements Bucket private final int depth; private final Map nodes; + { nodes = new HashMap<>(); @@ -34,7 +35,7 @@ public class KadBucket implements Bucket @Override public void insert(Node n) { - /*@todo Check if the bucket is filled already and handle this */ + /* @todo Check if the bucket is filled already and handle the situation */ /* Check if the contact is already in the bucket */ if (this.nodes.containsKey(n.getNodeId())) { @@ -43,9 +44,7 @@ public class KadBucket implements Bucket } else { - //System.out.println("Adding new node - " + n.getNodeId() + " to bucket depth: " + this.depth); nodes.put(n.getNodeId(), n); - //System.out.println(this); } } diff --git a/src/kademlia/routing/RoutingTable.java b/src/kademlia/routing/RoutingTable.java index 3175366..1c410a2 100644 --- a/src/kademlia/routing/RoutingTable.java +++ b/src/kademlia/routing/RoutingTable.java @@ -58,8 +58,6 @@ public class RoutingTable /* bucketId is the distance between these nodes */ int bucketId = this.localNode.getNodeId().getDistance(n.getNodeId()); - //System.out.println(this.localNode.getNodeId() + " Adding Node " + n.getNodeId() + " to bucket at depth: " + bucketId); - /* Put this contact to the bucket that stores contacts prefixLength distance away */ this.buckets[bucketId].insert(n); } @@ -198,7 +196,6 @@ public class RoutingTable StringBuilder sb = new StringBuilder("\nPrinting Routing Table Started ***************** \n"); for (KadBucket b : this.buckets) { - // System.out.println("Bucket: " + b); if (b.numNodes() > 0) { sb.append("# nodes in Bucket with depth "); diff --git a/src/kademlia/tests/ContentSendingTest.java b/src/kademlia/tests/ContentSendingTest.java index 4707be0..aa3134c 100644 --- a/src/kademlia/tests/ContentSendingTest.java +++ b/src/kademlia/tests/ContentSendingTest.java @@ -32,7 +32,7 @@ public class ContentSendingTest */ DHTContentImpl c = new DHTContentImpl(kad2.getOwnerId(), "Some Data"); kad2.put(c); - + /** * Lets retrieve the content */ diff --git a/src/kademlia/tests/DHTContentImpl.java b/src/kademlia/tests/DHTContentImpl.java index 6a19c7c..37a5fc8 100644 --- a/src/kademlia/tests/DHTContentImpl.java +++ b/src/kademlia/tests/DHTContentImpl.java @@ -73,6 +73,6 @@ public class DHTContentImpl implements KadContent public String toString() { - return "DHTContentImpl[{data=" + this.data + "{ {key:"+this.key + "}]"; + return "DHTContentImpl[{data=" + this.data + "{ {key:" + this.key + "}]"; } } diff --git a/src/kademlia/tests/NodeConnectionTest.java b/src/kademlia/tests/NodeConnectionTest.java index 59f07fc..5df8bd5 100644 --- a/src/kademlia/tests/NodeConnectionTest.java +++ b/src/kademlia/tests/NodeConnectionTest.java @@ -5,7 +5,7 @@ import kademlia.core.Kademlia; import kademlia.node.NodeId; /** - * Testing connecting 2 nodes + * Testing connecting 2 nodes to each other * * @author Joshua Kissoon * @created 20140219 diff --git a/src/kademlia/tests/SimpleMessageTest.java b/src/kademlia/tests/SimpleMessageTest.java index 8ee01c3..e3ddabe 100644 --- a/src/kademlia/tests/SimpleMessageTest.java +++ b/src/kademlia/tests/SimpleMessageTest.java @@ -1,8 +1,3 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc Testing a simple send message - */ package kademlia.tests; import java.io.IOException; @@ -11,6 +6,12 @@ import kademlia.message.SimpleMessage; import kademlia.node.NodeId; import kademlia.message.SimpleReceiver; +/** + * Test 1: Try sending a simple message between nodes + * + * @author Joshua Kissoon + * @created 20140218 + */ public class SimpleMessageTest { diff --git a/src/kademlia/tests/TreeMapTest.java b/src/kademlia/tests/TreeMapTest.java deleted file mode 100644 index cf32d35..0000000 --- a/src/kademlia/tests/TreeMapTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package kademlia.tests; - -import java.io.IOException; -import java.util.Comparator; -import java.util.Map; -import java.util.SortedMap; -import java.util.TreeMap; -import kademlia.core.Kademlia; -import kademlia.node.KeyComparator; -import kademlia.node.Node; -import kademlia.node.NodeId; - -/** - * Had some problems with treemap, so trying out testing treemaps - * - * @author Joshua Kissoon - * @since 20140311 - */ -public class TreeMapTest -{ - - /* Constants */ - private static final Byte UNASKED = (byte) 0x00; - private static final Byte AWAITING = (byte) 0x01; - private static final Byte ASKED = (byte) 0x02; - private static final Byte FAILED = (byte) 0x03; - - private final SortedMap nodes; - - /* Used to sort nodes */ - private final Comparator comparator; - - public TreeMapTest() throws IOException - { - /* Setting up 2 Kad networks */ - Kademlia kad1 = new Kademlia("JoshuaK", new NodeId("ASF45678947584567467"), 8888); - Kademlia kad2 = new Kademlia("Crystal", new NodeId("AfERTKdvHGVHERJHGFdh"), 8889); - Kademlia kad3 = new Kademlia("Shameer", new NodeId("ASERTKyrHGVHERfHGFsy"), 8890); - Kademlia kad4 = new Kademlia("Lokesh", new NodeId("AS3RTKJsdjVHERJHGF94"), 8891); - Kademlia kad5 = new Kademlia("Chandu", new NodeId("ASERT47kfeVHERJHGF15"), 8892); - - this.comparator = new KeyComparator(kad1.getNode().getNodeId()); - this.nodes = new TreeMap(this.comparator); - - /* Add all nodes as unasked */ - this.nodes.put(kad1.getNode(), ASKED); - this.nodes.put(kad2.getNode(), UNASKED); - this.nodes.put(kad3.getNode(), UNASKED); - this.nodes.put(kad4.getNode(), UNASKED); - this.nodes.put(kad5.getNode(), UNASKED); - - this.printTree(); - } - - private void printTree() - { - for (Map.Entry e : this.nodes.entrySet()) - { - System.out.println("Node: " + e.getKey() + "; Value: " + e.getValue()); - } - } - - public static void main(String[] args) - { - try - { - new TreeMapTest(); - } - catch (IOException e) - { - e.printStackTrace(); - } - } -}