Did some code cleanups

- Add Comments
	- Remove println statements
	- Deleted useless comments, etc
This commit is contained in:
Joshua Kissoon 2014-03-22 12:35:00 +05:30
parent 21053a16ff
commit 3f82c4a0ef
32 changed files with 114 additions and 246 deletions

View File

@ -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));
}

View File

@ -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");
}
/**

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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 */

View File

@ -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

View File

@ -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<KadContent>().read(in);

View File

@ -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 <code>byte</code> type there can
* be at most 256 different message types.
**/
*
* @return byte A unique code representing the message type
* */
public byte code();
}

View File

@ -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
{

View File

@ -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<Node> 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 */

View File

@ -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
{

View File

@ -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);
}

View File

@ -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.");
}
}

View File

@ -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 */

View File

@ -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;

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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<Node> 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<Node> closestNodesNotFailed(String status)
{
System.out.println(this.localNode + " - closestNodesNotFailed called, Status looking for: " + status);
List<Node> 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 */

View File

@ -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
{

View File

@ -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;

View File

@ -41,8 +41,6 @@ public class StoreOperation implements Operation
ndlo.execute();
List<Node> 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");
}
/**

View File

@ -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
{

View File

@ -18,6 +18,7 @@ public class KadBucket implements Bucket
private final int depth;
private final Map<NodeId, Node> 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);
}
}

View File

@ -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 ");

View File

@ -32,7 +32,7 @@ public class ContentSendingTest
*/
DHTContentImpl c = new DHTContentImpl(kad2.getOwnerId(), "Some Data");
kad2.put(c);
/**
* Lets retrieve the content
*/

View File

@ -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 + "}]";
}
}

View File

@ -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

View File

@ -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
{

View File

@ -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<Node, Byte> 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<Node, Byte> 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();
}
}
}