2014-03-29 09:07:01 +00:00
|
|
|
package kademlia;
|
2014-02-18 20:37:07 +00:00
|
|
|
|
2014-03-09 15:34:18 +00:00
|
|
|
import java.io.DataInputStream;
|
2014-03-09 14:42:11 +00:00
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.File;
|
2014-03-09 15:34:18 +00:00
|
|
|
import java.io.FileInputStream;
|
2014-03-09 14:42:11 +00:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
2014-02-18 20:37:07 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetAddress;
|
2014-02-26 11:37:18 +00:00
|
|
|
import java.util.NoSuchElementException;
|
2014-02-18 20:37:07 +00:00
|
|
|
import java.util.Timer;
|
|
|
|
import java.util.TimerTask;
|
2014-03-29 09:07:01 +00:00
|
|
|
import kademlia.core.DefaultConfiguration;
|
2014-04-05 15:37:57 +00:00
|
|
|
import kademlia.dht.GetParameter;
|
2014-03-29 09:07:01 +00:00
|
|
|
import kademlia.core.KadConfiguration;
|
|
|
|
import kademlia.core.KadServer;
|
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-04-02 12:35:14 +00:00
|
|
|
import kademlia.dht.StorageEntry;
|
2014-04-26 16:39:45 +00:00
|
|
|
import kademlia.exceptions.ContentNotFoundException;
|
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-03-10 05:38:51 +00:00
|
|
|
import kademlia.routing.RoutingTable;
|
2014-04-05 17:06:46 +00:00
|
|
|
import kademlia.util.serializer.JsonDHTSerializer;
|
|
|
|
import kademlia.util.serializer.JsonRoutingTableSerializer;
|
|
|
|
import kademlia.util.serializer.JsonSerializer;
|
2014-02-18 20:37:07 +00:00
|
|
|
|
2014-02-24 15:56:49 +00:00
|
|
|
/**
|
2014-04-28 09:55:07 +00:00
|
|
|
* The main Kademlia Node on the network, this node manages everything for this local system.
|
2014-02-24 15:56:49 +00:00
|
|
|
*
|
|
|
|
* @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
|
2014-03-07 05:48:04 +00:00
|
|
|
*
|
2014-02-24 15:56:49 +00:00
|
|
|
*/
|
2014-04-28 09:55:07 +00:00
|
|
|
public class KademliaNode
|
2014-02-18 20:37:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* 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 */
|
2014-03-10 05:38:51 +00:00
|
|
|
private final transient Node localNode;
|
|
|
|
private final transient KadServer server;
|
|
|
|
private final transient DHT dht;
|
2014-04-28 09:55:07 +00:00
|
|
|
private transient RoutingTable routingTable;
|
2014-03-09 14:42:11 +00:00
|
|
|
private final int udpPort;
|
2014-03-31 17:20:57 +00:00
|
|
|
private transient KadConfiguration config;
|
2014-02-18 20:37:07 +00:00
|
|
|
|
2014-04-28 11:16:32 +00:00
|
|
|
/* Timer used to execute refresh operations */
|
2014-05-05 12:33:57 +00:00
|
|
|
private transient Timer refreshOperationTimer;
|
|
|
|
private transient TimerTask refreshOperationTTask;
|
|
|
|
|
2014-02-18 20:37:07 +00:00
|
|
|
/* Factories */
|
2014-03-10 05:38:51 +00:00
|
|
|
private final transient MessageFactory messageFactory;
|
2014-02-18 20:37:07 +00:00
|
|
|
|
2014-05-06 03:51:34 +00:00
|
|
|
/* Statistics */
|
2014-05-08 05:11:51 +00:00
|
|
|
private final transient KadStatistician statistician;
|
2014-05-06 03:51:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
statistician = new Statistician();
|
|
|
|
}
|
|
|
|
|
2014-02-18 20:37:07 +00:00
|
|
|
/**
|
|
|
|
* 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-04-28 09:55:07 +00:00
|
|
|
* @param ownerId The Name of this node used for storage
|
|
|
|
* @param localNode The Local Node for this Kad instance
|
|
|
|
* @param udpPort The UDP port to use for routing messages
|
|
|
|
* @param dht The DHT for this instance
|
2014-03-29 09:07:01 +00:00
|
|
|
* @param config
|
2014-04-28 09:55:07 +00:00
|
|
|
* @param routingTable
|
2014-02-18 20:37:07 +00:00
|
|
|
*
|
|
|
|
* @throws IOException If an error occurred while reading id or local map
|
|
|
|
* from disk <i>or</i> a network error occurred while
|
2014-03-22 09:03:31 +00:00
|
|
|
* attempting to bootstrap to the network
|
2014-02-18 20:37:07 +00:00
|
|
|
* */
|
2014-04-28 09:55:07 +00:00
|
|
|
public KademliaNode(String ownerId, Node localNode, int udpPort, DHT dht, RoutingTable routingTable, KadConfiguration config) throws IOException
|
2014-02-18 20:37:07 +00:00
|
|
|
{
|
2014-02-24 15:56:49 +00:00
|
|
|
this.ownerId = ownerId;
|
2014-03-09 14:42:11 +00:00
|
|
|
this.udpPort = udpPort;
|
|
|
|
this.localNode = localNode;
|
2014-03-09 15:34:18 +00:00
|
|
|
this.dht = dht;
|
2014-03-29 09:07:01 +00:00
|
|
|
this.config = config;
|
2014-04-28 09:55:07 +00:00
|
|
|
this.routingTable = routingTable;
|
|
|
|
this.messageFactory = new MessageFactory(this, this.dht, this.config);
|
2014-05-06 03:51:34 +00:00
|
|
|
this.server = new KadServer(udpPort, this.messageFactory, this.localNode, this.config, this.statistician);
|
2014-05-05 12:33:57 +00:00
|
|
|
this.startRefreshOperation();
|
|
|
|
}
|
2014-02-18 20:37:07 +00:00
|
|
|
|
2014-05-05 12:33:57 +00:00
|
|
|
/**
|
|
|
|
* Schedule the recurring refresh operation
|
|
|
|
*/
|
|
|
|
public final void startRefreshOperation()
|
|
|
|
{
|
|
|
|
this.refreshOperationTimer = new Timer(true);
|
2014-04-28 11:16:32 +00:00
|
|
|
refreshOperationTTask = new TimerTask()
|
2014-05-05 12:33:57 +00:00
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/* Runs a DHT RefreshOperation */
|
|
|
|
KademliaNode.this.refresh();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
2014-02-18 20:37:07 +00:00
|
|
|
{
|
2014-05-08 05:28:24 +00:00
|
|
|
System.err.println("KademliaNode: Refresh Operation Failed; Message: " + e.getMessage());
|
2014-05-05 12:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-28 11:16:32 +00:00
|
|
|
};
|
|
|
|
refreshOperationTimer.schedule(refreshOperationTTask, this.config.restoreInterval(), this.config.restoreInterval());
|
2014-05-05 12:33:57 +00:00
|
|
|
}
|
2014-05-06 03:51:34 +00:00
|
|
|
|
2014-05-05 12:33:57 +00:00
|
|
|
public final void stopRefreshOperation()
|
|
|
|
{
|
|
|
|
/* Close off the timer tasks */
|
|
|
|
this.refreshOperationTTask.cancel();
|
|
|
|
this.refreshOperationTimer.cancel();
|
|
|
|
this.refreshOperationTimer.purge();
|
2014-02-18 20:37:07 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:16:32 +00:00
|
|
|
public KademliaNode(String ownerId, Node node, int udpPort, RoutingTable routingTable, KadConfiguration config) throws IOException
|
2014-03-29 09:07:01 +00:00
|
|
|
{
|
2014-04-28 11:16:32 +00:00
|
|
|
this(
|
|
|
|
ownerId,
|
|
|
|
node,
|
2014-04-28 09:55:07 +00:00
|
|
|
udpPort,
|
2014-04-28 11:16:32 +00:00
|
|
|
new DHT(ownerId, config),
|
2014-04-28 09:55:07 +00:00
|
|
|
routingTable,
|
2014-04-28 11:16:32 +00:00
|
|
|
config
|
|
|
|
);
|
2014-03-29 09:07:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 09:55:07 +00:00
|
|
|
public KademliaNode(String ownerId, Node node, int udpPort, KadConfiguration config) throws IOException
|
2014-03-09 14:42:11 +00:00
|
|
|
{
|
2014-04-28 09:55:07 +00:00
|
|
|
this(
|
|
|
|
ownerId,
|
|
|
|
node,
|
|
|
|
udpPort,
|
|
|
|
new RoutingTable(node, config),
|
|
|
|
config
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public KademliaNode(String ownerId, NodeId defaultId, int udpPort) throws IOException
|
|
|
|
{
|
|
|
|
this(
|
|
|
|
ownerId,
|
|
|
|
new Node(defaultId, InetAddress.getLocalHost(), udpPort),
|
|
|
|
udpPort,
|
|
|
|
new DefaultConfiguration()
|
|
|
|
);
|
2014-03-09 14:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-29 09:07:01 +00:00
|
|
|
* Load Stored state using default configuration
|
2014-03-09 15:34:18 +00:00
|
|
|
*
|
|
|
|
* @param ownerId The ID of the owner for the stored state
|
|
|
|
*
|
2014-03-09 14:42:11 +00:00
|
|
|
* @return A Kademlia instance loaded from a stored state in a file
|
|
|
|
*
|
2014-03-09 15:34:18 +00:00
|
|
|
* @throws java.io.FileNotFoundException
|
2014-03-10 08:15:13 +00:00
|
|
|
* @throws java.lang.ClassNotFoundException
|
2014-03-09 14:42:11 +00:00
|
|
|
*/
|
2014-04-28 09:55:07 +00:00
|
|
|
public static KademliaNode loadFromFile(String ownerId) throws FileNotFoundException, IOException, ClassNotFoundException
|
2014-03-29 09:07:01 +00:00
|
|
|
{
|
2014-04-28 09:55:07 +00:00
|
|
|
return KademliaNode.loadFromFile(ownerId, new DefaultConfiguration());
|
2014-03-29 09:07:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load Stored state
|
|
|
|
*
|
|
|
|
* @param ownerId The ID of the owner for the stored state
|
|
|
|
* @param iconfig Configuration information to work with
|
|
|
|
*
|
|
|
|
* @return A Kademlia instance loaded from a stored state in a file
|
|
|
|
*
|
|
|
|
* @throws java.io.FileNotFoundException
|
|
|
|
* @throws java.lang.ClassNotFoundException
|
|
|
|
*/
|
2014-04-28 09:55:07 +00:00
|
|
|
public static KademliaNode loadFromFile(String ownerId, KadConfiguration iconfig) throws FileNotFoundException, IOException, ClassNotFoundException
|
2014-03-09 15:34:18 +00:00
|
|
|
{
|
2014-03-10 08:15:13 +00:00
|
|
|
DataInputStream din;
|
2014-03-09 15:34:18 +00:00
|
|
|
|
2014-03-10 08:15:13 +00:00
|
|
|
/**
|
|
|
|
* @section Read Basic Kad data
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId, iconfig) + File.separator + "kad.kns"));
|
2014-04-28 09:55:07 +00:00
|
|
|
KademliaNode ikad = new JsonSerializer<KademliaNode>().read(din);
|
2014-03-09 15:34:18 +00:00
|
|
|
|
2014-03-10 08:15:13 +00:00
|
|
|
/**
|
|
|
|
* @section Read the routing table
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId, iconfig) + File.separator + "routingtable.kns"));
|
2014-05-01 11:22:31 +00:00
|
|
|
RoutingTable irtbl = new JsonRoutingTableSerializer(iconfig).read(din);
|
2014-03-09 15:34:18 +00:00
|
|
|
|
2014-03-10 08:15:13 +00:00
|
|
|
/**
|
|
|
|
* @section Read the node state
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId, iconfig) + File.separator + "node.kns"));
|
2014-03-10 08:15:13 +00:00
|
|
|
Node inode = new JsonSerializer<Node>().read(din);
|
2014-03-09 15:34:18 +00:00
|
|
|
|
2014-03-10 08:15:13 +00:00
|
|
|
/**
|
|
|
|
* @section Read the DHT
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId, iconfig) + File.separator + "dht.kns"));
|
2014-03-10 09:07:08 +00:00
|
|
|
DHT idht = new JsonDHTSerializer().read(din);
|
2014-04-01 05:12:43 +00:00
|
|
|
idht.setConfiguration(iconfig);
|
2014-03-22 09:03:31 +00:00
|
|
|
|
2014-04-28 09:55:07 +00:00
|
|
|
return new KademliaNode(ownerId, inode, ikad.getPort(), idht, irtbl, iconfig);
|
2014-03-09 15:34:18 +00:00
|
|
|
}
|
2014-03-09 14:42:11 +00:00
|
|
|
|
2014-02-18 20:37:07 +00:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2014-03-29 05:22:55 +00:00
|
|
|
/**
|
|
|
|
* @return The DHT for this kad instance
|
|
|
|
*/
|
|
|
|
public DHT getDHT()
|
|
|
|
{
|
|
|
|
return this.dht;
|
|
|
|
}
|
|
|
|
|
2014-03-29 09:07:01 +00:00
|
|
|
/**
|
|
|
|
* @return The current KadConfiguration object being used
|
|
|
|
*/
|
|
|
|
public KadConfiguration getCurrentConfiguration()
|
|
|
|
{
|
|
|
|
return this.config;
|
|
|
|
}
|
|
|
|
|
2014-02-18 20:37:07 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* */
|
2014-03-22 05:23:05 +00:00
|
|
|
public synchronized final void bootstrap(Node n) throws IOException, RoutingException
|
2014-02-18 20:37:07 +00:00
|
|
|
{
|
2014-05-06 19:24:55 +00:00
|
|
|
long startTime = System.nanoTime();
|
2014-04-28 09:55:07 +00:00
|
|
|
Operation op = new ConnectOperation(this.server, this, n, this.config);
|
2014-02-18 20:37:07 +00:00
|
|
|
op.execute();
|
2014-05-06 19:24:55 +00:00
|
|
|
long endTime = System.nanoTime();
|
2014-05-06 19:11:49 +00:00
|
|
|
this.statistician.setBootstrapTime(endTime - startTime);
|
2014-02-18 20:37:07 +00:00
|
|
|
}
|
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-03-22 05:23:05 +00:00
|
|
|
public synchronized int put(KadContent content) throws IOException
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
2014-04-28 09:55:07 +00:00
|
|
|
StoreOperation sop = new StoreOperation(this.server, this, content, this.dht, this.config);
|
2014-03-07 05:44:45 +00:00
|
|
|
sop.execute();
|
|
|
|
|
|
|
|
/* Return how many nodes the content was stored on */
|
|
|
|
return sop.numNodesStoredAt();
|
2014-02-24 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 09:03:31 +00:00
|
|
|
/**
|
|
|
|
* Store a content on the local node's DHT
|
|
|
|
*
|
|
|
|
* @param content The content to put on the DHT
|
|
|
|
*
|
|
|
|
* @throws java.io.IOException
|
|
|
|
*/
|
|
|
|
public synchronized void putLocally(KadContent content) throws IOException
|
|
|
|
{
|
|
|
|
this.dht.store(content);
|
|
|
|
}
|
|
|
|
|
2014-02-24 15:56:49 +00:00
|
|
|
/**
|
|
|
|
* Get some content stored on the DHT
|
|
|
|
*
|
2014-04-26 16:39:45 +00:00
|
|
|
* @param param The parameters used to search for the content
|
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-04-26 17:49:13 +00:00
|
|
|
* @throws kademlia.exceptions.ContentNotFoundException
|
2014-02-24 15:56:49 +00:00
|
|
|
*/
|
2014-04-26 16:39:45 +00:00
|
|
|
public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException
|
2014-02-24 15:56:49 +00:00
|
|
|
{
|
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-04-26 16:39:45 +00:00
|
|
|
return this.dht.get(param);
|
2014-02-26 11:37:18 +00:00
|
|
|
}
|
2014-02-26 16:05:37 +00:00
|
|
|
|
2014-04-19 09:39:07 +00:00
|
|
|
/* Seems like it doesn't exist in our DHT, get it from other Nodes */
|
2014-05-07 17:13:15 +00:00
|
|
|
long startTime = System.nanoTime();
|
2014-04-28 09:55:07 +00:00
|
|
|
ContentLookupOperation clo = new ContentLookupOperation(server, this, param, this.config);
|
2014-04-19 09:39:07 +00:00
|
|
|
clo.execute();
|
2014-05-06 19:24:55 +00:00
|
|
|
long endTime = System.nanoTime();
|
2014-05-07 03:56:29 +00:00
|
|
|
this.statistician.addContentLookup(endTime - startTime, clo.routeLength());
|
2014-04-26 16:39:45 +00:00
|
|
|
return clo.getContentFound();
|
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-04-28 09:55:07 +00:00
|
|
|
new KadRefreshOperation(this.server, this, this.dht, this.config).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-03-09 14:42:11 +00:00
|
|
|
|
2014-03-10 08:15:13 +00:00
|
|
|
/**
|
|
|
|
* @return Integer The port on which this kad instance is running
|
|
|
|
*/
|
|
|
|
public int getPort()
|
|
|
|
{
|
|
|
|
return this.udpPort;
|
|
|
|
}
|
|
|
|
|
2014-03-09 14:42:11 +00:00
|
|
|
/**
|
|
|
|
* Here we handle properly shutting down the Kademlia instance
|
|
|
|
*
|
2014-03-26 10:53:17 +00:00
|
|
|
* @param saveState Whether to save the application state or not
|
|
|
|
*
|
2014-03-09 14:42:11 +00:00
|
|
|
* @throws java.io.FileNotFoundException
|
|
|
|
*/
|
2014-03-26 10:53:17 +00:00
|
|
|
public void shutdown(final boolean saveState) throws IOException
|
2014-03-09 14:42:11 +00:00
|
|
|
{
|
2014-03-09 15:34:18 +00:00
|
|
|
/* Shut down the server */
|
|
|
|
this.server.shutdown();
|
|
|
|
|
2014-05-05 12:33:57 +00:00
|
|
|
this.stopRefreshOperation();
|
|
|
|
|
2014-03-09 14:42:11 +00:00
|
|
|
/* Save this Kademlia instance's state if required */
|
2014-03-26 10:53:17 +00:00
|
|
|
if (saveState)
|
2014-03-09 14:42:11 +00:00
|
|
|
{
|
|
|
|
/* Save the system state */
|
|
|
|
this.saveKadState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the node state to a text file
|
|
|
|
*
|
|
|
|
* @throws java.io.FileNotFoundException
|
|
|
|
*/
|
2014-03-26 10:53:17 +00:00
|
|
|
private void saveKadState() throws IOException
|
2014-03-09 14:42:11 +00:00
|
|
|
{
|
2014-03-09 15:34:18 +00:00
|
|
|
DataOutputStream dout;
|
2014-03-09 14:42:11 +00:00
|
|
|
|
2014-03-10 05:38:51 +00:00
|
|
|
/**
|
|
|
|
* @section Store Basic Kad data
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId, this.config) + File.separator + "kad.kns"));
|
2014-04-28 09:55:07 +00:00
|
|
|
new JsonSerializer<KademliaNode>().write(this, dout);
|
2014-03-09 14:42:11 +00:00
|
|
|
|
2014-03-10 05:38:51 +00:00
|
|
|
/**
|
|
|
|
* @section Save the node state
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId, this.config) + File.separator + "node.kns"));
|
2014-03-09 14:42:11 +00:00
|
|
|
new JsonSerializer<Node>().write(this.localNode, dout);
|
|
|
|
|
2014-03-10 05:38:51 +00:00
|
|
|
/**
|
|
|
|
* @section Save the routing table
|
|
|
|
* We need to save the routing table separate from the node since the routing table will contain the node and the node will contain the routing table
|
|
|
|
* This will cause a serialization recursion, and in turn a Stack Overflow
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId, this.config) + File.separator + "routingtable.kns"));
|
2014-05-01 11:22:31 +00:00
|
|
|
new JsonRoutingTableSerializer(this.config).write(this.getRoutingTable(), dout);
|
2014-03-10 05:38:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @section Save the DHT
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId, this.config) + File.separator + "dht.kns"));
|
2014-03-10 09:07:08 +00:00
|
|
|
new JsonDHTSerializer().write(this.dht, dout);
|
2014-03-10 05:38:51 +00:00
|
|
|
|
2014-03-09 14:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the folder for which a content should be stored
|
|
|
|
*
|
|
|
|
* @return String The name of the folder to store node states
|
|
|
|
*/
|
2014-03-29 09:07:01 +00:00
|
|
|
private static String getStateStorageFolderName(String ownerId, KadConfiguration iconfig)
|
2014-03-09 14:42:11 +00:00
|
|
|
{
|
2014-03-29 05:22:55 +00:00
|
|
|
/* Setup the nodes storage folder if it doesn't exist */
|
2014-03-29 09:07:01 +00:00
|
|
|
String path = iconfig.getNodeDataFolder(ownerId) + File.separator + "nodeState";
|
2014-03-29 05:22:55 +00:00
|
|
|
File nodeStateFolder = new File(path);
|
|
|
|
if (!nodeStateFolder.isDirectory())
|
2014-03-10 05:38:51 +00:00
|
|
|
{
|
2014-03-29 05:22:55 +00:00
|
|
|
nodeStateFolder.mkdir();
|
2014-03-10 05:38:51 +00:00
|
|
|
}
|
2014-03-29 05:22:55 +00:00
|
|
|
return nodeStateFolder.toString();
|
2014-03-09 14:42:11 +00:00
|
|
|
}
|
2014-03-09 15:34:18 +00:00
|
|
|
|
2014-05-06 04:23:06 +00:00
|
|
|
/**
|
|
|
|
* @return The routing table for this node.
|
|
|
|
*/
|
2014-04-28 09:55:07 +00:00
|
|
|
public RoutingTable getRoutingTable()
|
|
|
|
{
|
|
|
|
return this.routingTable;
|
|
|
|
}
|
|
|
|
|
2014-05-06 04:23:06 +00:00
|
|
|
/**
|
|
|
|
* @return The statistician that manages all statistics
|
|
|
|
*/
|
2014-05-07 18:15:23 +00:00
|
|
|
public KadStatistician getStatistician()
|
2014-05-06 04:23:06 +00:00
|
|
|
{
|
|
|
|
return this.statistician;
|
|
|
|
}
|
|
|
|
|
2014-03-09 15:34:18 +00:00
|
|
|
/**
|
|
|
|
* Creates a string containing all data about this Kademlia instance
|
|
|
|
*
|
|
|
|
* @return The string representation of this Kad instance
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
StringBuilder sb = new StringBuilder("\n\nPrinting Kad State for instance with owner: ");
|
|
|
|
sb.append(this.ownerId);
|
|
|
|
sb.append("\n\n");
|
|
|
|
|
|
|
|
sb.append("\n");
|
|
|
|
sb.append("Local Node");
|
|
|
|
sb.append(this.localNode);
|
|
|
|
sb.append("\n");
|
|
|
|
|
|
|
|
sb.append("\n");
|
|
|
|
sb.append("Routing Table: ");
|
2014-04-28 09:55:07 +00:00
|
|
|
sb.append(this.getRoutingTable());
|
2014-03-09 15:34:18 +00:00
|
|
|
sb.append("\n");
|
|
|
|
|
2014-03-10 09:07:08 +00:00
|
|
|
sb.append("\n");
|
|
|
|
sb.append("DHT: ");
|
|
|
|
sb.append(this.dht);
|
|
|
|
sb.append("\n");
|
|
|
|
|
2014-03-09 15:34:18 +00:00
|
|
|
sb.append("\n\n\n");
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2014-02-18 20:37:07 +00:00
|
|
|
}
|