2014-02-18 20:37:07 +00:00
|
|
|
package kademlia.core;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
2014-02-26 16:05:37 +00:00
|
|
|
import java.util.ArrayList;
|
2014-02-26 11:37:18 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.NoSuchElementException;
|
2014-02-18 20:37:07 +00:00
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
2014-02-26 06:10:06 +00:00
|
|
|
import kademlia.dht.DHT;
|
2014-02-25 08:12:08 +00:00
|
|
|
import kademlia.dht.KadContent;
|
2014-02-18 20:37:07 +00:00
|
|
|
import kademlia.exceptions.RoutingException;
|
|
|
|
import kademlia.message.MessageFactory;
|
|
|
|
import kademlia.node.Node;
|
|
|
|
import kademlia.node.NodeId;
|
|
|
|
import kademlia.operation.ConnectOperation;
|
2014-02-26 13:28:55 +00:00
|
|
|
import kademlia.operation.ContentLookupOperation;
|
2014-02-18 20:37:07 +00:00
|
|
|
import kademlia.operation.Operation;
|
2014-03-06 05:51:08 +00:00
|
|
|
import kademlia.operation.KadRefreshOperation;
|
2014-02-25 07:31:06 +00:00
|
|
|
import kademlia.operation.StoreOperation;
|
2014-02-18 20:37:07 +00:00
|
|
|
|
2014-02-24 15:56:49 +00:00
|
|
|
/**
|
|
|
|
* The main Kademlia network management class
|
|
|
|
*
|
|
|
|
* @author Joshua Kissoon
|
|
|
|
* @since 20140215
|
2014-02-25 07:31:06 +00:00
|
|
|
*
|
|
|
|
* @todo When we receive a store message - if we have a newer version of the content, re-send this newer version to that node so as to update their version
|
|
|
|
* @todo Handle IPv6 Addresses
|
|
|
|
* @todo Handle compressing data
|
2014-02-26 11:37:18 +00:00
|
|
|
* @todo Allow optional storing of content locally using the put method
|
2014-02-26 13:28:55 +00:00
|
|
|
* @todo Instead of using a StoreContentMessage to send a store RPC and a ContentMessage to receive a FIND rpc, make them 1 message with different operation type
|
2014-03-07 05:48:04 +00:00
|
|
|
* @todo If we're trying to send a message to this node, just cancel the sending process and handle the message right here
|
|
|
|
* @todo Keep this node in it's own routing table - it helps for ContentRefresh operation - easy to check whether this node is one of the k-nodes for a content
|
|
|
|
*
|
2014-02-24 15:56:49 +00:00
|
|
|
*/
|
2014-02-18 20:37:07 +00:00
|
|
|
public class Kademlia
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Kademlia Attributes */
|
2014-02-24 15:56:49 +00:00
|
|
|
private final String ownerId;
|
2014-02-18 20:37:07 +00:00
|
|
|
|
|
|
|
/* Objects to be used */
|
|
|
|
private final Node localNode;
|
|
|
|
private final KadServer server;
|
2014-02-26 06:10:06 +00:00
|
|
|
private final DHT dht;
|
2014-02-18 20:37:07 +00:00
|
|
|
private final Timer timer;
|
|
|
|
|
|
|
|
/* Factories */
|
|
|
|
private final MessageFactory messageFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Kademlia DistributedMap using the specified name as filename base.
|
|
|
|
* If the id cannot be read from disk the specified defaultId is used.
|
|
|
|
* The instance is bootstraped to an existing network by specifying the
|
|
|
|
* address of a bootstrap node in the network.
|
|
|
|
*
|
2014-02-24 15:56:49 +00:00
|
|
|
* @param ownerId The Name of this node used for storage
|
2014-02-18 20:37:07 +00:00
|
|
|
* @param defaultId Default id for the node
|
|
|
|
* @param udpPort The UDP port to use for routing messages
|
|
|
|
*
|
|
|
|
* @throws IOException If an error occurred while reading id or local map
|
|
|
|
* from disk <i>or</i> a network error occurred while
|
|
|
|
* attempting to connect to the network
|
|
|
|
* */
|
2014-02-24 15:56:49 +00:00
|
|
|
public Kademlia(String ownerId, NodeId defaultId, int udpPort) throws IOException
|
2014-02-18 20:37:07 +00:00
|
|
|
{
|
2014-02-24 15:56:49 +00:00
|
|
|
this.ownerId = ownerId;
|
2014-02-18 20:37:07 +00:00
|
|
|
this.localNode = new Node(defaultId, InetAddress.getLocalHost(), udpPort);
|
2014-02-26 06:10:06 +00:00
|
|
|
this.dht = new DHT();
|
|
|
|
this.messageFactory = new MessageFactory(localNode, this.dht);
|
2014-02-22 14:07:04 +00:00
|
|
|
this.server = new KadServer(udpPort, this.messageFactory, this.localNode);
|
2014-02-18 20:37:07 +00:00
|
|
|
this.timer = new Timer(true);
|
|
|
|
|
|
|
|
/* Schedule Recurring RestoreOperation */
|
|
|
|
timer.schedule(
|
|
|
|
new TimerTask()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2014-02-24 15:56:49 +00:00
|
|
|
try
|
|
|
|
{
|
2014-03-06 15:12:30 +00:00
|
|
|
/* Runs a DHT RefreshOperation */
|
|
|
|
Kademlia.this.refresh();
|
2014-02-24 15:56:49 +00:00
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
System.err.println("Refresh Operation Failed; Message: " + e.getMessage());
|
|
|
|
}
|
2014-02-18 20:37:07 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// Delay // Interval
|
|
|
|
Configuration.RESTORE_INTERVAL, Configuration.RESTORE_INTERVAL
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Node The local node for this system
|
|
|
|
*/
|
|
|
|
public Node getNode()
|
|
|
|
{
|
|
|
|
return this.localNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The KadServer used to send/receive messages
|
|
|
|
*/
|
|
|
|
public KadServer getServer()
|
|
|
|
{
|
|
|
|
return this.server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to an existing peer-to-peer network.
|
|
|
|
*
|
|
|
|
* @param n The known node in the peer-to-peer network
|
|
|
|
*
|
|
|
|
* @throws RoutingException If the bootstrap node could not be contacted
|
|
|
|
* @throws IOException If a network error occurred
|
|
|
|
* @throws IllegalStateException If this object is closed
|
|
|
|
* */
|
|
|
|
public final void connect(Node n) throws IOException, RoutingException
|
|
|
|
{
|
|
|
|
Operation op = new ConnectOperation(this.server, this.localNode, n);
|
|
|
|
op.execute();
|
|
|
|
}
|
2014-02-24 15:56:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the specified value under the given key
|
|
|
|
* This value is stored on K nodes on the network, or all nodes if there are > K total nodes in the network
|
|
|
|
*
|
|
|
|
* @param content The content to put onto the DHT
|
|
|
|
*
|
2014-02-25 07:31:06 +00:00
|
|
|
* @return Integer How many nodes the content was stored on
|
|
|
|
*
|
|
|
|
* @throws java.io.IOException
|
|
|
|
*
|
2014-02-24 15:56:49 +00:00
|
|
|
*/
|
2014-02-25 08:12:08 +00:00
|
|
|
public int put(KadContent content) throws IOException
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
2014-03-07 05:44:45 +00:00
|
|
|
StoreOperation sop = new StoreOperation(server, localNode, content);
|
|
|
|
sop.execute();
|
|
|
|
|
|
|
|
/* Return how many nodes the content was stored on */
|
|
|
|
return sop.numNodesStoredAt();
|
2014-02-24 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get some content stored on the DHT
|
2014-02-25 07:31:06 +00:00
|
|
|
* The content returned is a JSON String in byte format; this string is parsed into a class
|
2014-02-24 15:56:49 +00:00
|
|
|
*
|
2014-02-26 13:28:55 +00:00
|
|
|
* @param param The parameters used to search for the content
|
|
|
|
* @param numResultsReq How many results are required from different nodes
|
2014-02-24 15:56:49 +00:00
|
|
|
*
|
|
|
|
* @return DHTContent The content
|
2014-02-26 11:37:18 +00:00
|
|
|
*
|
|
|
|
* @throws java.io.IOException
|
2014-02-24 15:56:49 +00:00
|
|
|
*/
|
2014-02-26 13:28:55 +00:00
|
|
|
public List<KadContent> get(GetParameter param, int numResultsReq) throws NoSuchElementException, IOException
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
2014-02-26 16:05:37 +00:00
|
|
|
List contentFound;
|
2014-02-26 11:37:18 +00:00
|
|
|
if (this.dht.contains(param))
|
|
|
|
{
|
|
|
|
/* If the content exist in our own DHT, then return it. */
|
2014-02-26 16:05:37 +00:00
|
|
|
System.out.println("Found content locally");
|
|
|
|
contentFound = new ArrayList<>();
|
|
|
|
contentFound.add(this.dht.get(param));
|
2014-02-26 11:37:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Seems like it doesn't exist in our DHT, get it from other Nodes */
|
2014-02-26 16:05:37 +00:00
|
|
|
System.out.println("Looking for content on foreign nodes");
|
2014-02-26 13:28:55 +00:00
|
|
|
ContentLookupOperation clo = new ContentLookupOperation(server, localNode, param, numResultsReq);
|
|
|
|
clo.execute();
|
2014-02-26 16:05:37 +00:00
|
|
|
contentFound = clo.getContentFound();
|
2014-02-26 11:37:18 +00:00
|
|
|
}
|
2014-02-26 16:05:37 +00:00
|
|
|
|
|
|
|
return contentFound;
|
2014-02-24 15:56:49 +00:00
|
|
|
}
|
2014-02-25 07:31:06 +00:00
|
|
|
|
2014-03-06 05:51:08 +00:00
|
|
|
/**
|
|
|
|
* Allow the user of the System to call refresh even out of the normal Kad refresh timing
|
|
|
|
*
|
|
|
|
* @throws java.io.IOException
|
|
|
|
*/
|
|
|
|
public void refresh() throws IOException
|
|
|
|
{
|
2014-03-06 15:12:30 +00:00
|
|
|
new KadRefreshOperation(this.server, this.localNode, this.dht).execute();
|
2014-03-06 05:51:08 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 07:31:06 +00:00
|
|
|
/**
|
|
|
|
* @return String The ID of the owner of this local network
|
|
|
|
*/
|
|
|
|
public String getOwnerId()
|
|
|
|
{
|
|
|
|
return this.ownerId;
|
|
|
|
}
|
2014-02-18 20:37:07 +00:00
|
|
|
}
|