mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
Cleaned up the core code, added comments and removed all Netbeans generated warnings
This commit is contained in:
parent
796e41dd9a
commit
689a35b7bf
@ -3,7 +3,8 @@ package kademlia.core;
|
|||||||
/**
|
/**
|
||||||
* A set of Kademlia configuration parameters. Default values are
|
* A set of Kademlia configuration parameters. Default values are
|
||||||
* supplied and can be changed by the application as necessary.
|
* supplied and can be changed by the application as necessary.
|
||||||
* */
|
*
|
||||||
|
*/
|
||||||
public class Configuration
|
public class Configuration
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ public class Configuration
|
|||||||
* Number of times a node can be marked as stale before it is actually removed.
|
* Number of times a node can be marked as stale before it is actually removed.
|
||||||
* */
|
* */
|
||||||
public static int STALE = 1;
|
public static int STALE = 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Local Storage location - Relative to the user's home folder (Cross-Platform)
|
* Local Storage location - Relative to the user's home folder (Cross-Platform)
|
||||||
*/
|
*/
|
||||||
|
@ -19,17 +19,35 @@ public class GetParameter
|
|||||||
private String ownerId = null;
|
private String ownerId = null;
|
||||||
private String type = null;
|
private String type = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a GetParameter to search for data by NodeId
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
public GetParameter(NodeId key)
|
public GetParameter(NodeId key)
|
||||||
{
|
{
|
||||||
this.key = key;
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a GetParameter to search for data by NodeId and owner
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param owner
|
||||||
|
*/
|
||||||
public GetParameter(NodeId key, String owner)
|
public GetParameter(NodeId key, String owner)
|
||||||
{
|
{
|
||||||
this(key);
|
this(key);
|
||||||
this.ownerId = owner;
|
this.ownerId = owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a GetParameter to search for data by NodeId, owner, type
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param owner
|
||||||
|
* @param type
|
||||||
|
*/
|
||||||
public GetParameter(NodeId key, String owner, String type)
|
public GetParameter(NodeId key, String owner, String type)
|
||||||
{
|
{
|
||||||
this(key, owner);
|
this(key, owner);
|
||||||
|
@ -19,14 +19,15 @@ import kademlia.node.Node;
|
|||||||
import kademlia.operation.Receiver;
|
import kademlia.operation.Receiver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* The server that handles sending and receiving messages between nodes on the Kad Network
|
||||||
|
*
|
||||||
* @author Joshua Kissoon
|
* @author Joshua Kissoon
|
||||||
* @created 20140215
|
* @created 20140215
|
||||||
* @desc This server handles sending and receiving messages
|
|
||||||
*/
|
*/
|
||||||
public class KadServer
|
public class KadServer
|
||||||
{
|
{
|
||||||
|
|
||||||
/* Constants */
|
/* Maximum size of a Datagram Packet */
|
||||||
private static final int DATAGRAM_BUFFER_SIZE = 64 * 1024; // 64KB
|
private static final int DATAGRAM_BUFFER_SIZE = 64 * 1024; // 64KB
|
||||||
|
|
||||||
/* Server Objects */
|
/* Server Objects */
|
||||||
@ -50,6 +51,15 @@ public class KadServer
|
|||||||
this.timer = new Timer(true);
|
this.timer = new Timer(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize our KadServer
|
||||||
|
*
|
||||||
|
* @param udpPort The port to listen on
|
||||||
|
* @param mFactory Factory used to create messages
|
||||||
|
* @param localNode Local node on which this server runs on
|
||||||
|
*
|
||||||
|
* @throws java.net.SocketException
|
||||||
|
*/
|
||||||
public KadServer(int udpPort, MessageFactory mFactory, Node localNode) throws SocketException
|
public KadServer(int udpPort, MessageFactory mFactory, Node localNode) throws SocketException
|
||||||
{
|
{
|
||||||
this.udpPort = udpPort;
|
this.udpPort = udpPort;
|
||||||
@ -85,6 +95,8 @@ public class KadServer
|
|||||||
* @param to The node to send the message to
|
* @param to The node to send the message to
|
||||||
* @param recv The receiver to handle the response message
|
* @param recv The receiver to handle the response message
|
||||||
*
|
*
|
||||||
|
* @return Integer The communication ID of this message
|
||||||
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public synchronized int sendMessage(Node to, Message msg, Receiver recv) throws IOException
|
public synchronized int sendMessage(Node to, Message msg, Receiver recv) throws IOException
|
||||||
@ -95,7 +107,7 @@ public class KadServer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Generate a random communication ID */
|
/* Generate a random communication ID */
|
||||||
int comm = new Integer(new Random().nextInt());
|
int comm = new Random().nextInt();
|
||||||
|
|
||||||
/* If we have a receiver */
|
/* If we have a receiver */
|
||||||
if (recv != null)
|
if (recv != null)
|
||||||
@ -109,9 +121,19 @@ public class KadServer
|
|||||||
|
|
||||||
/* Send the message */
|
/* Send the message */
|
||||||
sendMessage(to, msg, comm);
|
sendMessage(to, msg, comm);
|
||||||
|
|
||||||
return comm;
|
return comm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method called to reply to a message received
|
||||||
|
*
|
||||||
|
* @param to The Node to send the reply to
|
||||||
|
* @param msg The reply message
|
||||||
|
* @param comm The communication ID - the one received
|
||||||
|
*
|
||||||
|
* @throws java.io.IOException
|
||||||
|
*/
|
||||||
public synchronized void reply(Node to, Message msg, int comm) throws IOException
|
public synchronized void reply(Node to, Message msg, int comm) throws IOException
|
||||||
{
|
{
|
||||||
if (!isRunning)
|
if (!isRunning)
|
||||||
@ -121,27 +143,32 @@ public class KadServer
|
|||||||
sendMessage(to, msg, comm);
|
sendMessage(to, msg, comm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal sendMessage method called by the public sendMessage method after a communicationId is generated
|
||||||
|
*/
|
||||||
private void sendMessage(Node to, Message msg, int comm) throws IOException
|
private void sendMessage(Node to, Message msg, int comm) throws IOException
|
||||||
{
|
{
|
||||||
/* Setup the message for transmission */
|
/* Use a try-with resource to auto-close streams after usage */
|
||||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
try (ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout);)
|
||||||
DataOutputStream dout = new DataOutputStream(bout);
|
|
||||||
dout.writeInt(comm);
|
|
||||||
dout.writeByte(msg.code());
|
|
||||||
msg.toStream(dout);
|
|
||||||
dout.close();
|
|
||||||
|
|
||||||
byte[] data = bout.toByteArray();
|
|
||||||
|
|
||||||
if (data.length > DATAGRAM_BUFFER_SIZE)
|
|
||||||
{
|
{
|
||||||
throw new IOException("Message is too big");
|
/* Setup the message for transmission */
|
||||||
}
|
dout.writeInt(comm);
|
||||||
|
dout.writeByte(msg.code());
|
||||||
|
msg.toStream(dout);
|
||||||
|
dout.close();
|
||||||
|
|
||||||
/* Everything is good, now create the packet and send it */
|
byte[] data = bout.toByteArray();
|
||||||
DatagramPacket pkt = new DatagramPacket(data, 0, data.length);
|
|
||||||
pkt.setSocketAddress(to.getSocketAddress());
|
if (data.length > DATAGRAM_BUFFER_SIZE)
|
||||||
socket.send(pkt);
|
{
|
||||||
|
throw new IOException("Message is too big");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Everything is good, now create the packet and send it */
|
||||||
|
DatagramPacket pkt = new DatagramPacket(data, 0, data.length);
|
||||||
|
pkt.setSocketAddress(to.getSocketAddress());
|
||||||
|
socket.send(pkt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -161,46 +188,48 @@ public class KadServer
|
|||||||
socket.receive(packet);
|
socket.receive(packet);
|
||||||
|
|
||||||
/* We've received a packet, now handle it */
|
/* We've received a packet, now handle it */
|
||||||
ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength());
|
try (ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData(), packet.getOffset(), packet.getLength());
|
||||||
DataInputStream din = new DataInputStream(bin);
|
DataInputStream din = new DataInputStream(bin);)
|
||||||
|
|
||||||
/* Read in the conversation Id to know which handler to handle this response */
|
|
||||||
int comm = din.readInt();
|
|
||||||
byte messCode = din.readByte();
|
|
||||||
|
|
||||||
Message msg = messageFactory.createMessage(messCode, din);
|
|
||||||
din.close();
|
|
||||||
//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");
|
|
||||||
/* If there is a reciever in the receivers to handle this */
|
/* Read in the conversation Id to know which handler to handle this response */
|
||||||
synchronized (this)
|
int comm = din.readInt();
|
||||||
|
byte messCode = din.readByte();
|
||||||
|
|
||||||
|
Message msg = messageFactory.createMessage(messCode, din);
|
||||||
|
din.close();
|
||||||
|
|
||||||
|
//System.out.println(this.localNode.getNodeId() + " Message Received: [Comm: " + comm + "] " + msg);
|
||||||
|
|
||||||
|
/* Get a receiver for this message */
|
||||||
|
Receiver receiver;
|
||||||
|
if (this.receivers.containsKey(comm))
|
||||||
{
|
{
|
||||||
receiver = this.receivers.remove(comm);
|
/* If there is a reciever in the receivers to handle this */
|
||||||
TimerTask task = (TimerTask) tasks.remove(comm);
|
synchronized (this)
|
||||||
task.cancel();
|
{
|
||||||
|
receiver = this.receivers.remove(comm);
|
||||||
|
TimerTask task = (TimerTask) tasks.remove(comm);
|
||||||
|
task.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* There is currently no receivers, try to get one */
|
||||||
|
receiver = messageFactory.createReceiver(messCode, this);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* There is currently no receivers, try to get one */
|
|
||||||
receiver = messageFactory.createReceiver(messCode, this);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Invoke the receiver */
|
/* Invoke the receiver */
|
||||||
if (receiver != null)
|
if (receiver != null)
|
||||||
{
|
{
|
||||||
receiver.receive(msg, comm);
|
receiver.receive(msg, comm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
this.isRunning = false;
|
this.isRunning = false;
|
||||||
e.printStackTrace();
|
System.out.println("Server ran into a problem in listener method. Message: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -221,7 +250,7 @@ public class KadServer
|
|||||||
*/
|
*/
|
||||||
private synchronized void unregister(int comm)
|
private synchronized void unregister(int comm)
|
||||||
{
|
{
|
||||||
Integer key = new Integer(comm);
|
Integer key = comm;
|
||||||
receivers.remove(key);
|
receivers.remove(key);
|
||||||
this.tasks.remove(key);
|
this.tasks.remove(key);
|
||||||
}
|
}
|
||||||
@ -254,7 +283,7 @@ public class KadServer
|
|||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
System.out.println("Cannot unregister a receiver. Message: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,10 +31,10 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
{
|
{
|
||||||
|
|
||||||
/* Constants */
|
/* Constants */
|
||||||
private static final Byte UNASKED = new Byte((byte) 0x00);
|
private static final Byte UNASKED = (byte) 0x00;
|
||||||
private static final Byte AWAITING = new Byte((byte) 0x01);
|
private static final Byte AWAITING = (byte) 0x01;
|
||||||
private static final Byte ASKED = new Byte((byte) 0x02);
|
private static final Byte ASKED = (byte) 0x02;
|
||||||
private static final Byte FAILED = new Byte((byte) 0x03);
|
private static final Byte FAILED = (byte) 0x03;
|
||||||
|
|
||||||
private final KadServer server;
|
private final KadServer server;
|
||||||
private final Node localNode;
|
private final Node localNode;
|
||||||
@ -187,7 +187,7 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
int comm = server.sendMessage(n, lookupMessage, this);
|
int comm = server.sendMessage(n, lookupMessage, this);
|
||||||
|
|
||||||
this.nodes.put(n, AWAITING);
|
this.nodes.put(n, AWAITING);
|
||||||
this.messagesTransiting.put(new Integer(comm), n);
|
this.messagesTransiting.put(comm, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We're not finished as yet, return false */
|
/* We're not finished as yet, return false */
|
||||||
@ -268,14 +268,14 @@ 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());
|
//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 */
|
||||||
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 */
|
||||||
this.messagesTransiting.remove(new Integer(comm));
|
this.messagesTransiting.remove(comm);
|
||||||
|
|
||||||
/* Add the received nodes to our nodes list to query */
|
/* Add the received nodes to our nodes list to query */
|
||||||
this.addNodes(msg.getNodes());
|
this.addNodes(msg.getNodes());
|
||||||
@ -303,7 +303,7 @@ public class NodeLookupOperation implements Operation, Receiver
|
|||||||
/* Mark this node as failed */
|
/* Mark this node as failed */
|
||||||
this.nodes.put(n, FAILED);
|
this.nodes.put(n, FAILED);
|
||||||
this.localNode.getRoutingTable().remove(n);
|
this.localNode.getRoutingTable().remove(n);
|
||||||
this.messagesTransiting.remove(new Integer(comm));
|
this.messagesTransiting.remove(comm);
|
||||||
|
|
||||||
this.askNodesorFinish();
|
this.askNodesorFinish();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user