mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-25 11:42:19 +00:00
Got storage to work!
This commit is contained in:
parent
d31f0e337f
commit
e1e6e4e40d
@ -9,6 +9,7 @@ import java.io.FileOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
@ -24,6 +25,7 @@ import kademlia.operation.ContentLookupOperation;
|
|||||||
import kademlia.operation.Operation;
|
import kademlia.operation.Operation;
|
||||||
import kademlia.operation.KadRefreshOperation;
|
import kademlia.operation.KadRefreshOperation;
|
||||||
import kademlia.operation.StoreOperation;
|
import kademlia.operation.StoreOperation;
|
||||||
|
import kademlia.routing.RoutingTable;
|
||||||
import kademlia.serializer.JsonSerializer;
|
import kademlia.serializer.JsonSerializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,14 +51,14 @@ public class Kademlia
|
|||||||
private final String ownerId;
|
private final String ownerId;
|
||||||
|
|
||||||
/* Objects to be used */
|
/* Objects to be used */
|
||||||
private final Node localNode;
|
private final transient Node localNode;
|
||||||
private final KadServer server;
|
private final transient KadServer server;
|
||||||
private final DHT dht;
|
private final transient DHT dht;
|
||||||
private final Timer timer;
|
private final transient Timer timer;
|
||||||
private final int udpPort;
|
private final int udpPort;
|
||||||
|
|
||||||
/* Factories */
|
/* Factories */
|
||||||
private final MessageFactory messageFactory;
|
private final transient MessageFactory messageFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a Kademlia DistributedMap using the specified name as filename base.
|
* Creates a Kademlia DistributedMap using the specified name as filename base.
|
||||||
@ -267,33 +269,37 @@ public class Kademlia
|
|||||||
*
|
*
|
||||||
* @throws java.io.FileNotFoundException
|
* @throws java.io.FileNotFoundException
|
||||||
*/
|
*/
|
||||||
private void saveKadState() throws FileNotFoundException, IOException, ClassNotFoundException
|
private void saveKadState() throws FileNotFoundException, IOException
|
||||||
{
|
{
|
||||||
/* Setup the file in which we store the state */
|
|
||||||
DataOutputStream dout;
|
|
||||||
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName() + File.separator + this.ownerId + ".kns"));
|
|
||||||
|
|
||||||
System.out.println("Saving state");
|
System.out.println("Saving state");
|
||||||
/* Save the UDP Port that this app is running on */
|
DataOutputStream dout;
|
||||||
new JsonSerializer<Integer>().write(this.udpPort, dout);
|
|
||||||
|
|
||||||
/* Save the node state */
|
/**
|
||||||
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName() + File.separator + this.ownerId + ".kns"));
|
* @section Store Basic Kad data
|
||||||
|
*/
|
||||||
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId) + File.separator + "kad.kns"));
|
||||||
|
new JsonSerializer<Kademlia>().write(this, dout);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @section Save the node state
|
||||||
|
*/
|
||||||
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId) + File.separator + "node.kns"));
|
||||||
new JsonSerializer<Node>().write(this.localNode, dout);
|
new JsonSerializer<Node>().write(this.localNode, dout);
|
||||||
|
|
||||||
/* Save the DHT */
|
/**
|
||||||
// dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName() + File.separator + this.ownerId + ".kns"));
|
* @section Save the routing table
|
||||||
//new JsonSerializer<DHT>().write(this.dht, dout);
|
* 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
|
||||||
|
*/
|
||||||
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId) + File.separator + "routingtable.kns"));
|
||||||
|
new JsonSerializer<RoutingTable>().write(this.localNode.getRoutingTable(), dout);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @section Save the DHT
|
||||||
|
*/
|
||||||
|
dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId) + File.separator + "dht.kns"));
|
||||||
|
new JsonSerializer<DHT>().write(this.dht, dout);
|
||||||
|
|
||||||
// System.out.println(dht.getStorageEntries());
|
|
||||||
//
|
|
||||||
// DataInputStream din = new DataInputStream(new FileInputStream(getStateStorageFolderName() + File.separator + ownerId + ".kns"));
|
|
||||||
// DHT dddht = new JsonSerializer<DHT>().read(din);
|
|
||||||
// System.out.println();
|
|
||||||
// System.out.println();
|
|
||||||
// System.out.println();
|
|
||||||
// System.out.println();
|
|
||||||
// System.out.println(dddht);
|
|
||||||
System.out.println("FInished saving state");
|
System.out.println("FInished saving state");
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -303,26 +309,33 @@ public class Kademlia
|
|||||||
*
|
*
|
||||||
* @return String The name of the folder to store node states
|
* @return String The name of the folder to store node states
|
||||||
*/
|
*/
|
||||||
private static String getStateStorageFolderName()
|
private static String getStateStorageFolderName(String ownerId)
|
||||||
{
|
{
|
||||||
String storagePath = System.getProperty("user.home") + File.separator + Configuration.LOCAL_FOLDER;
|
String path = System.getProperty("user.home") + File.separator + Configuration.LOCAL_FOLDER;
|
||||||
File mainStorageFolder = new File(storagePath);
|
File folder = new File(path);
|
||||||
|
|
||||||
/* Create the main storage folder if it doesn't exist */
|
/* Create the main storage folder if it doesn't exist */
|
||||||
if (!mainStorageFolder.isDirectory())
|
if (!folder.isDirectory())
|
||||||
{
|
{
|
||||||
mainStorageFolder.mkdir();
|
folder.mkdir();
|
||||||
}
|
}
|
||||||
|
|
||||||
File contentStorageFolder = new File(mainStorageFolder + File.separator + "nodes");
|
/* Create the nodes storage folder if it doesn't exist */
|
||||||
|
path = folder + File.separator + "nodes";
|
||||||
/* Create the content folder if it doesn't exist */
|
folder = new File(path);
|
||||||
if (!contentStorageFolder.isDirectory())
|
if (!folder.isDirectory())
|
||||||
{
|
{
|
||||||
contentStorageFolder.mkdir();
|
folder.mkdir();
|
||||||
}
|
}
|
||||||
|
|
||||||
return mainStorageFolder + File.separator + "nodes";
|
/* Create this Kad instance storage folder */
|
||||||
|
path += File.separator + ownerId;
|
||||||
|
folder = new File(path);
|
||||||
|
if (!folder.isDirectory())
|
||||||
|
{
|
||||||
|
folder.mkdir();
|
||||||
|
}
|
||||||
|
return folder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user