mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
Did some code cleanups
- Add Comments - Remove println statements - Deleted useless comments, etc
This commit is contained in:
parent
21053a16ff
commit
3f82c4a0ef
@ -113,7 +113,7 @@ public class KadServer
|
|||||||
if (recv != null)
|
if (recv != null)
|
||||||
{
|
{
|
||||||
/* Setup the receiver to handle message response */
|
/* 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);
|
receivers.put(comm, recv);
|
||||||
TimerTask task = new TimeoutTask(comm, recv);
|
TimerTask task = new TimeoutTask(comm, recv);
|
||||||
timer.schedule(task, Configuration.RESPONSE_TIMEOUT);
|
timer.schedule(task, Configuration.RESPONSE_TIMEOUT);
|
||||||
@ -200,18 +200,15 @@ public class KadServer
|
|||||||
Message msg = messageFactory.createMessage(messCode, din);
|
Message msg = messageFactory.createMessage(messCode, din);
|
||||||
din.close();
|
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 */
|
/* Get a receiver for this message */
|
||||||
Receiver receiver;
|
Receiver receiver;
|
||||||
if (this.receivers.containsKey(comm))
|
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 */
|
/* If there is a reciever in the receivers to handle this */
|
||||||
synchronized (this)
|
synchronized (this)
|
||||||
{
|
{
|
||||||
System.out.println("Sync Block Receiver found: " + this.receivers.get(comm));
|
|
||||||
System.out.println(this.localNode + " Receivers: ");
|
|
||||||
this.printReceivers();
|
this.printReceivers();
|
||||||
receiver = this.receivers.remove(comm);
|
receiver = this.receivers.remove(comm);
|
||||||
TimerTask task = (TimerTask) tasks.remove(comm);
|
TimerTask task = (TimerTask) tasks.remove(comm);
|
||||||
@ -234,7 +231,7 @@ public class KadServer
|
|||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
this.isRunning = false;
|
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)
|
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()
|
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));
|
System.out.println("Receiver for comm: " + r + "; Receiver: " + this.receivers.get(r));
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,7 @@ public class Kademlia
|
|||||||
*/
|
*/
|
||||||
din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId) + File.separator + "dht.kns"));
|
din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId) + File.separator + "dht.kns"));
|
||||||
DHT idht = new JsonDHTSerializer().read(din);
|
DHT idht = new JsonDHTSerializer().read(din);
|
||||||
System.out.println("Finished reading data.");
|
|
||||||
return new Kademlia(ownerId, inode, ikad.getPort(), idht);
|
return new Kademlia(ownerId, inode, ikad.getPort(), idht);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,14 +227,12 @@ public class Kademlia
|
|||||||
if (this.dht.contains(param))
|
if (this.dht.contains(param))
|
||||||
{
|
{
|
||||||
/* If the content exist in our own DHT, then return it. */
|
/* If the content exist in our own DHT, then return it. */
|
||||||
System.out.println("Found content locally");
|
|
||||||
contentFound = new ArrayList<>();
|
contentFound = new ArrayList<>();
|
||||||
contentFound.add(this.dht.get(param));
|
contentFound.add(this.dht.get(param));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Seems like it doesn't exist in our DHT, get it from other Nodes */
|
/* 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);
|
ContentLookupOperation clo = new ContentLookupOperation(server, localNode, param, numResultsReq);
|
||||||
clo.execute();
|
clo.execute();
|
||||||
contentFound = clo.getContentFound();
|
contentFound = clo.getContentFound();
|
||||||
@ -294,7 +292,6 @@ public class Kademlia
|
|||||||
*/
|
*/
|
||||||
private void saveKadState() throws FileNotFoundException, IOException
|
private void saveKadState() throws FileNotFoundException, IOException
|
||||||
{
|
{
|
||||||
System.out.println("Saving state");
|
|
||||||
DataOutputStream dout;
|
DataOutputStream dout;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -323,8 +320,6 @@ public class Kademlia
|
|||||||
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId) + File.separator + "dht.kns"));
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId) + File.separator + "dht.kns"));
|
||||||
new JsonDHTSerializer().write(this.dht, dout);
|
new JsonDHTSerializer().write(this.dht, dout);
|
||||||
|
|
||||||
System.out.println("FInished saving state");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140219
|
|
||||||
* @desc An exception to be thrown whenever there is a routing problem
|
|
||||||
*/
|
|
||||||
package kademlia.exceptions;
|
package kademlia.exceptions;
|
||||||
|
|
||||||
import java.io.IOException;
|
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
|
public class RoutingException extends IOException
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -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;
|
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
|
public class UnknownMessageException extends RuntimeException
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140218
|
|
||||||
* @desc A message used to acknowledge a request from a node
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
@ -10,6 +5,13 @@ import java.io.DataOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import kademlia.node.Node;
|
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
|
public class AcknowledgeMessage implements Message
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140218
|
|
||||||
* @desc A message used to connect nodes
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
@ -10,6 +5,12 @@ import java.io.DataOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import kademlia.node.Node;
|
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
|
public class ConnectMessage implements Message
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140219
|
|
||||||
* @desc Receives a ConnectMessage and sends an AcknowledgeMessage as reply
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -10,6 +5,12 @@ import kademlia.core.KadServer;
|
|||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
import kademlia.operation.Receiver;
|
import kademlia.operation.Receiver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives a ConnectMessage and sends an AcknowledgeMessage as reply.
|
||||||
|
*
|
||||||
|
* @author Joshua Kissoon
|
||||||
|
* @created 20140219
|
||||||
|
*/
|
||||||
public class ConnectReceiver implements Receiver
|
public class ConnectReceiver implements Receiver
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -38,7 +39,6 @@ public class ConnectReceiver implements Receiver
|
|||||||
this.localNode.getRoutingTable().insert(mess.getOrigin());
|
this.localNode.getRoutingTable().insert(mess.getOrigin());
|
||||||
|
|
||||||
/* Respond to the connect request */
|
/* 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);
|
AcknowledgeMessage msg = new AcknowledgeMessage(this.localNode);
|
||||||
|
|
||||||
/* Reply to the connect message with an Acknowledgement */
|
/* Reply to the connect message with an Acknowledgement */
|
||||||
|
@ -1,16 +1,14 @@
|
|||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import com.google.gson.JsonSerializationContext;
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import kademlia.core.GetParameter;
|
import kademlia.core.GetParameter;
|
||||||
import kademlia.dht.KadContent;
|
|
||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
import kademlia.serializer.JsonSerializer;
|
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
|
* @author Joshua Kissoon
|
||||||
* @since 20140226
|
* @since 20140226
|
||||||
|
@ -50,7 +50,7 @@ public class ContentMessage implements Message
|
|||||||
public final void fromStream(DataInputStream in) throws IOException
|
public final void fromStream(DataInputStream in) throws IOException
|
||||||
{
|
{
|
||||||
this.origin = new Node(in);
|
this.origin = new Node(in);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.content = new JsonSerializer<KadContent>().read(in);
|
this.content = new JsonSerializer<KadContent>().read(in);
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
package kademlia.message;
|
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
|
* 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
|
* from each other. Since this is of <code>byte</code> type there can
|
||||||
* be at most 256 different message types.
|
* be at most 256 different message types.
|
||||||
**/
|
*
|
||||||
|
* @return byte A unique code representing the message type
|
||||||
|
* */
|
||||||
public byte code();
|
public byte code();
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140218
|
|
||||||
* @desc A message used to connect nodes
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
@ -11,6 +6,12 @@ import java.io.IOException;
|
|||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
import kademlia.node.NodeId;
|
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
|
public class NodeLookupMessage implements Message
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140219
|
|
||||||
* @desc Receives a ConnectMessage and sends an AcknowledgeMessage as reply
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -12,6 +7,12 @@ import kademlia.core.KadServer;
|
|||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
import kademlia.operation.Receiver;
|
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
|
public class NodeLookupReceiver implements Receiver
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -45,15 +46,7 @@ public class NodeLookupReceiver implements Receiver
|
|||||||
/* Find nodes closest to the LookupId */
|
/* Find nodes closest to the LookupId */
|
||||||
List<Node> nodes = this.localNode.getRoutingTable().findClosest(msg.getLookupId(), Configuration.K);
|
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 */
|
/* Respond to the NodeLookupMessage */
|
||||||
System.out.println(this.localNode + " Sending NodeReplyMessage to " + origin);
|
|
||||||
Message reply = new NodeReplyMessage(this.localNode, nodes);
|
Message reply = new NodeReplyMessage(this.localNode, nodes);
|
||||||
|
|
||||||
/* Let the Server send the reply */
|
/* Let the Server send the reply */
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140218
|
|
||||||
* @desc A message used to connect nodes
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
@ -12,6 +7,13 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import kademlia.node.Node;
|
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
|
public class NodeReplyMessage implements Message
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140217
|
|
||||||
* @desc A simple message used for testing the system
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
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
|
public class SimpleMessage implements Message
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -24,7 +25,6 @@ public class SimpleMessage implements Message
|
|||||||
|
|
||||||
public SimpleMessage(DataInputStream in)
|
public SimpleMessage(DataInputStream in)
|
||||||
{
|
{
|
||||||
System.out.println("Creating message from input stream.");
|
|
||||||
this.fromStream(in);
|
this.fromStream(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
/**
|
|
||||||
* @author Joshua
|
|
||||||
* @created
|
|
||||||
* @desc
|
|
||||||
*/
|
|
||||||
package kademlia.message;
|
package kademlia.message;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import kademlia.message.Message;
|
|
||||||
import kademlia.operation.Receiver;
|
import kademlia.operation.Receiver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default receiver if none other is called
|
||||||
|
*
|
||||||
|
* @author Joshua Kissoon
|
||||||
|
* @created 20140202
|
||||||
|
*/
|
||||||
public class SimpleReceiver implements Receiver
|
public class SimpleReceiver implements Receiver
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -21,6 +21,6 @@ public class SimpleReceiver implements Receiver
|
|||||||
@Override
|
@Override
|
||||||
public void timeout(int conversationId) throws IOException
|
public void timeout(int conversationId) throws IOException
|
||||||
{
|
{
|
||||||
System.out.println("");
|
System.out.println("SimpleReceiver message timeout.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,6 @@ public class StoreContentReceiver implements Receiver
|
|||||||
/* Insert the message sender into this node's routing table */
|
/* Insert the message sender into this node's routing table */
|
||||||
this.localNode.getRoutingTable().insert(msg.getOrigin());
|
this.localNode.getRoutingTable().insert(msg.getOrigin());
|
||||||
|
|
||||||
System.out.println(this.localNode + " - Received a store content message");
|
|
||||||
System.out.println(msg);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
/* Store this Content into the DHT */
|
/* Store this Content into the DHT */
|
||||||
|
@ -6,7 +6,6 @@ import java.io.IOException;
|
|||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
import java.util.Comparator;
|
|
||||||
import kademlia.message.Streamable;
|
import kademlia.message.Streamable;
|
||||||
import kademlia.routing.RoutingTable;
|
import kademlia.routing.RoutingTable;
|
||||||
|
|
||||||
@ -23,7 +22,7 @@ public class Node implements Streamable
|
|||||||
private NodeId nodeId;
|
private NodeId nodeId;
|
||||||
private InetAddress inetAddress;
|
private InetAddress inetAddress;
|
||||||
private int port;
|
private int port;
|
||||||
private String strRep;
|
private final String strRep;
|
||||||
|
|
||||||
private transient RoutingTable routingTable;
|
private transient RoutingTable routingTable;
|
||||||
|
|
||||||
|
@ -239,7 +239,7 @@ public class NodeId implements Streamable
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fromStream(DataInputStream in) throws IOException
|
public final void fromStream(DataInputStream in) throws IOException
|
||||||
{
|
{
|
||||||
byte[] input = new byte[ID_LENGTH / 8];
|
byte[] input = new byte[ID_LENGTH / 8];
|
||||||
in.readFully(input);
|
in.readFully(input);
|
||||||
|
@ -50,7 +50,6 @@ public class BucketRefreshOperation implements Operation
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//System.out.println("Distance: " + localNode.getNodeId().getDistance(current) + " - ID: " + current);
|
|
||||||
new NodeLookupOperation(server, localNode, localNode.getNodeId()).execute();
|
new NodeLookupOperation(server, localNode, localNode.getNodeId()).execute();
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
|
@ -61,7 +61,6 @@ public class ConnectOperation implements Operation, Receiver
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Perform lookup for our own ID to get nodes close to us */
|
/* 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());
|
Operation lookup = new NodeLookupOperation(this.server, this.localNode, this.localNode.getNodeId());
|
||||||
lookup.execute();
|
lookup.execute();
|
||||||
|
|
||||||
@ -70,7 +69,6 @@ public class ConnectOperation implements Operation, Receiver
|
|||||||
* I think after the above lookup operation, K buckets will be filled
|
* I think after the above lookup operation, K buckets will be filled
|
||||||
* Not sure if this operation is needed here
|
* Not sure if this operation is needed here
|
||||||
*/
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (IOException | InterruptedException e)
|
catch (IOException | InterruptedException e)
|
||||||
{
|
{
|
||||||
@ -88,7 +86,6 @@ public class ConnectOperation implements Operation, Receiver
|
|||||||
{
|
{
|
||||||
/* The incoming message will be an acknowledgement message */
|
/* The incoming message will be an acknowledgement message */
|
||||||
AcknowledgeMessage msg = (AcknowledgeMessage) incoming;
|
AcknowledgeMessage msg = (AcknowledgeMessage) incoming;
|
||||||
System.out.println("ConnectOperation now handling Acknowledgement Message: " + msg);
|
|
||||||
|
|
||||||
/* The bootstrap node has responded, insert it into our space */
|
/* The bootstrap node has responded, insert it into our space */
|
||||||
this.localNode.getRoutingTable().insert(this.bootstrapNode);
|
this.localNode.getRoutingTable().insert(this.bootstrapNode);
|
||||||
@ -111,7 +108,6 @@ public class ConnectOperation implements Operation, Receiver
|
|||||||
@Override
|
@Override
|
||||||
public synchronized void timeout(int comm) throws IOException
|
public synchronized void timeout(int comm) throws IOException
|
||||||
{
|
{
|
||||||
System.out.println("Timeout function called");
|
|
||||||
if (++this.attempts < MAX_CONNECT_ATTEMPTS)
|
if (++this.attempts < MAX_CONNECT_ATTEMPTS)
|
||||||
{
|
{
|
||||||
this.server.sendMessage(this.bootstrapNode, new ConnectMessage(this.localNode), this);
|
this.server.sendMessage(this.bootstrapNode, new ConnectMessage(this.localNode), this);
|
||||||
|
@ -6,7 +6,6 @@ import java.util.Comparator;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.SortedMap;
|
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import kademlia.core.Configuration;
|
import kademlia.core.Configuration;
|
||||||
import kademlia.core.KadServer;
|
import kademlia.core.KadServer;
|
||||||
@ -127,33 +126,11 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
for (Node o : list)
|
for (Node o : list)
|
||||||
{
|
{
|
||||||
/* If this node is not in the list, add the node */
|
/* If this node is not in the list, add the node */
|
||||||
System.out.println("Current Nodes & Their Status: ");
|
if (!nodes.containsKey(o))
|
||||||
for (Node e : this.nodes.keySet())
|
|
||||||
{
|
{
|
||||||
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);
|
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 */
|
/* Get unqueried nodes among the K closest seen that have not FAILED */
|
||||||
List<Node> unasked = this.closestNodesNotFailed(UNASKED);
|
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())
|
if (unasked.isEmpty() && this.messagesTransiting.isEmpty())
|
||||||
{
|
{
|
||||||
@ -191,8 +162,6 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Sort nodes according to criteria */
|
|
||||||
//Collections.sort(unasked, this.comparator);
|
|
||||||
/**
|
/**
|
||||||
* Send messages to nodes in the list;
|
* Send messages to nodes in the list;
|
||||||
* making sure than no more than CONCURRENCY messsages are in transit
|
* 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);
|
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);
|
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);
|
this.messagesTransiting.put(comm, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -255,8 +216,6 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
*/
|
*/
|
||||||
private List<Node> closestNodesNotFailed(String status)
|
private List<Node> closestNodesNotFailed(String status)
|
||||||
{
|
{
|
||||||
|
|
||||||
System.out.println(this.localNode + " - closestNodesNotFailed called, Status looking for: " + status);
|
|
||||||
List<Node> closestNodes = new ArrayList<>(Configuration.K);
|
List<Node> closestNodes = new ArrayList<>(Configuration.K);
|
||||||
int remainingSpaces = Configuration.K;
|
int remainingSpaces = Configuration.K;
|
||||||
|
|
||||||
@ -267,7 +226,6 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
if (status.equals(e.getValue()))
|
if (status.equals(e.getValue()))
|
||||||
{
|
{
|
||||||
/* We got one with the required status, now add it */
|
/* We got one with the required status, now add it */
|
||||||
System.out.println("Adding " + e.getKey() + "; status: " + e.getValue());
|
|
||||||
closestNodes.add(e.getKey());
|
closestNodes.add(e.getKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,11 +254,9 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
|
|
||||||
/* Add the origin node to our routing table */
|
/* Add the origin node to our routing table */
|
||||||
Node origin = msg.getOrigin();
|
Node origin = msg.getOrigin();
|
||||||
//System.out.println(this.localNode.getNodeId() + " Lookup Operation Response From: " + origin.getNodeId());
|
|
||||||
this.localNode.getRoutingTable().insert(origin);
|
this.localNode.getRoutingTable().insert(origin);
|
||||||
|
|
||||||
/* Set that we've completed ASKing the origin node */
|
/* Set that we've completed ASKing the origin node */
|
||||||
System.out.println("Setting " + origin + " as " + ASKED);
|
|
||||||
this.nodes.put(origin, ASKED);
|
this.nodes.put(origin, ASKED);
|
||||||
|
|
||||||
/* Remove this msg from messagesTransiting since it's completed now */
|
/* Remove this msg from messagesTransiting since it's completed now */
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140218
|
|
||||||
* @desc Interface for different Kademlia operations
|
|
||||||
*/
|
|
||||||
package kademlia.operation;
|
package kademlia.operation;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import kademlia.exceptions.RoutingException;
|
import kademlia.exceptions.RoutingException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An operation in the Kademlia routing protocol
|
||||||
|
*
|
||||||
|
* @author Joshua Kissoon
|
||||||
|
* @created 20140218
|
||||||
|
*/
|
||||||
public interface Operation
|
public interface Operation
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -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;
|
package kademlia.operation;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import kademlia.message.Message;
|
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
|
public interface Receiver
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -16,6 +17,8 @@ public interface Receiver
|
|||||||
*
|
*
|
||||||
* @param conversationId The ID of this conversation, used for further conversations
|
* @param conversationId The ID of this conversation, used for further conversations
|
||||||
* @param incoming The incoming
|
* @param incoming The incoming
|
||||||
|
*
|
||||||
|
* @throws java.io.IOException
|
||||||
*/
|
*/
|
||||||
public void receive(Message incoming, int conversationId) throws IOException;
|
public void receive(Message incoming, int conversationId) throws IOException;
|
||||||
|
|
||||||
|
@ -41,8 +41,6 @@ public class StoreOperation implements Operation
|
|||||||
ndlo.execute();
|
ndlo.execute();
|
||||||
List<Node> nodes = ndlo.getClosestNodes();
|
List<Node> nodes = ndlo.getClosestNodes();
|
||||||
|
|
||||||
System.out.println("Nodes to put content on: " + nodes);
|
|
||||||
|
|
||||||
/* Create the message */
|
/* Create the message */
|
||||||
Message msg = new StoreContentMessage(this.localNode, this.content);
|
Message msg = new StoreContentMessage(this.localNode, this.content);
|
||||||
|
|
||||||
@ -61,8 +59,6 @@ public class StoreOperation implements Operation
|
|||||||
this.server.sendMessage(n, msg, null);
|
this.server.sendMessage(n, msg, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("\n\n\n\nSTORE CONTENT FINISHED");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140215
|
|
||||||
* @desc A bucket for the DHT Protocol
|
|
||||||
*/
|
|
||||||
package kademlia.routing;
|
package kademlia.routing;
|
||||||
|
|
||||||
import kademlia.node.Node;
|
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
|
public interface Bucket
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ public class KadBucket implements Bucket
|
|||||||
|
|
||||||
private final int depth;
|
private final int depth;
|
||||||
private final Map<NodeId, Node> nodes;
|
private final Map<NodeId, Node> nodes;
|
||||||
|
|
||||||
|
|
||||||
{
|
{
|
||||||
nodes = new HashMap<>();
|
nodes = new HashMap<>();
|
||||||
@ -34,7 +35,7 @@ public class KadBucket implements Bucket
|
|||||||
@Override
|
@Override
|
||||||
public void insert(Node n)
|
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 */
|
/* Check if the contact is already in the bucket */
|
||||||
if (this.nodes.containsKey(n.getNodeId()))
|
if (this.nodes.containsKey(n.getNodeId()))
|
||||||
{
|
{
|
||||||
@ -43,9 +44,7 @@ public class KadBucket implements Bucket
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//System.out.println("Adding new node - " + n.getNodeId() + " to bucket depth: " + this.depth);
|
|
||||||
nodes.put(n.getNodeId(), n);
|
nodes.put(n.getNodeId(), n);
|
||||||
//System.out.println(this);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,8 +58,6 @@ public class RoutingTable
|
|||||||
/* bucketId is the distance between these nodes */
|
/* bucketId is the distance between these nodes */
|
||||||
int bucketId = this.localNode.getNodeId().getDistance(n.getNodeId());
|
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 */
|
/* Put this contact to the bucket that stores contacts prefixLength distance away */
|
||||||
this.buckets[bucketId].insert(n);
|
this.buckets[bucketId].insert(n);
|
||||||
}
|
}
|
||||||
@ -198,7 +196,6 @@ public class RoutingTable
|
|||||||
StringBuilder sb = new StringBuilder("\nPrinting Routing Table Started ***************** \n");
|
StringBuilder sb = new StringBuilder("\nPrinting Routing Table Started ***************** \n");
|
||||||
for (KadBucket b : this.buckets)
|
for (KadBucket b : this.buckets)
|
||||||
{
|
{
|
||||||
// System.out.println("Bucket: " + b);
|
|
||||||
if (b.numNodes() > 0)
|
if (b.numNodes() > 0)
|
||||||
{
|
{
|
||||||
sb.append("# nodes in Bucket with depth ");
|
sb.append("# nodes in Bucket with depth ");
|
||||||
|
@ -32,7 +32,7 @@ public class ContentSendingTest
|
|||||||
*/
|
*/
|
||||||
DHTContentImpl c = new DHTContentImpl(kad2.getOwnerId(), "Some Data");
|
DHTContentImpl c = new DHTContentImpl(kad2.getOwnerId(), "Some Data");
|
||||||
kad2.put(c);
|
kad2.put(c);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lets retrieve the content
|
* Lets retrieve the content
|
||||||
*/
|
*/
|
||||||
|
@ -73,6 +73,6 @@ public class DHTContentImpl implements KadContent
|
|||||||
|
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
return "DHTContentImpl[{data=" + this.data + "{ {key:"+this.key + "}]";
|
return "DHTContentImpl[{data=" + this.data + "{ {key:" + this.key + "}]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import kademlia.core.Kademlia;
|
|||||||
import kademlia.node.NodeId;
|
import kademlia.node.NodeId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Testing connecting 2 nodes
|
* Testing connecting 2 nodes to each other
|
||||||
*
|
*
|
||||||
* @author Joshua Kissoon
|
* @author Joshua Kissoon
|
||||||
* @created 20140219
|
* @created 20140219
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
/**
|
|
||||||
* @author Joshua Kissoon
|
|
||||||
* @created 20140218
|
|
||||||
* @desc Testing a simple send message
|
|
||||||
*/
|
|
||||||
package kademlia.tests;
|
package kademlia.tests;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -11,6 +6,12 @@ import kademlia.message.SimpleMessage;
|
|||||||
import kademlia.node.NodeId;
|
import kademlia.node.NodeId;
|
||||||
import kademlia.message.SimpleReceiver;
|
import kademlia.message.SimpleReceiver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test 1: Try sending a simple message between nodes
|
||||||
|
*
|
||||||
|
* @author Joshua Kissoon
|
||||||
|
* @created 20140218
|
||||||
|
*/
|
||||||
public class SimpleMessageTest
|
public class SimpleMessageTest
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user