Added the getNodeStorageFolder method to the Configuration class rather than having it repeated in DHT and Kademlia

Added a getDHT method to Kademlia
Changed the folder structure in which content is stored in the DHT from Kademlia/substr(contentid, 10)/hash(content).kct TO Kademlia/ownerId/substr(contentid, 10)/hash(content).kct
This commit is contained in:
Joshua Kissoon 2014-03-29 10:52:55 +05:30
parent 426af4d345
commit c232fccc69
4 changed files with 51 additions and 38 deletions

View File

@ -1,5 +1,7 @@
package kademlia.core;
import java.io.File;
/**
* A set of Kademlia configuration parameters. Default values are
* supplied and can be changed by the application as necessary.
@ -53,4 +55,32 @@ public class Configuration
* Local Storage location - Relative to the user's home folder (Cross-Platform)
*/
public static String LOCAL_FOLDER = "kademlia";
/**
* Creates the folder in which this node data is to be stored
*
* @param ownerId
*
* @return The folder path
*/
public static String getNodeDataFolder(String ownerId)
{
/* Setup the main storage folder if it doesn't exist */
String path = System.getProperty("user.home") + File.separator + Configuration.LOCAL_FOLDER;
File folder = new File(path);
if (!folder.isDirectory())
{
folder.mkdir();
}
/* Setup subfolder for this owner if it doesn't exist */
File ownerFolder = new File(folder + File.separator + ownerId);
if (!ownerFolder.isDirectory())
{
ownerFolder.mkdir();
}
/* Return the path */
return ownerFolder.toString();
}
}

View File

@ -42,7 +42,6 @@ import kademlia.serializer.JsonSerializer;
* @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
* @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
* @todo Move DHT.getContentStorageFolderName to the Configuration class
* @todo Implement Kademlia.ping() operation.
*
*/
@ -112,7 +111,7 @@ public class Kademlia
public Kademlia(String ownerId, NodeId defaultId, int udpPort) throws IOException
{
this(ownerId, new Node(defaultId, InetAddress.getLocalHost(), udpPort), udpPort, new DHT());
this(ownerId, new Node(defaultId, InetAddress.getLocalHost(), udpPort), udpPort, new DHT(ownerId));
}
/**
@ -175,6 +174,14 @@ public class Kademlia
return this.server;
}
/**
* @return The DHT for this kad instance
*/
public DHT getDHT()
{
return this.dht;
}
/**
* Connect to an existing peer-to-peer network.
*
@ -343,31 +350,14 @@ public class Kademlia
*/
private static String getStateStorageFolderName(String ownerId)
{
String path = System.getProperty("user.home") + File.separator + Configuration.LOCAL_FOLDER;
File folder = new File(path);
/* Create the main storage folder if it doesn't exist */
if (!folder.isDirectory())
/* Setup the nodes storage folder if it doesn't exist */
String path = Configuration.getNodeDataFolder(ownerId) + File.separator + "nodeState";
File nodeStateFolder = new File(path);
if (!nodeStateFolder.isDirectory())
{
folder.mkdir();
nodeStateFolder.mkdir();
}
/* Create the nodes storage folder if it doesn't exist */
path = folder + File.separator + "nodes";
folder = new File(path);
if (!folder.isDirectory())
{
folder.mkdir();
}
/* Create this Kad instance storage folder */
path += File.separator + ownerId;
folder = new File(path);
if (!folder.isDirectory())
{
folder.mkdir();
}
return folder.toString();
return nodeStateFolder.toString();
}
/**

View File

@ -28,13 +28,16 @@ public class DHT
private transient StorageEntryManager entriesManager;
private transient final JsonSerializer<KadContent> contentSerializer;
private final String ownerId;
{
contentSerializer = new JsonSerializer<>();
}
public DHT()
public DHT(String ownerId)
{
this.ownerId = ownerId;
this.initialize();
}
@ -201,17 +204,8 @@ public class DHT
*
* The name of the file containing the content is the hash of this content
*/
String storagePath = System.getProperty("user.home") + File.separator + Configuration.LOCAL_FOLDER;
File mainStorageFolder = new File(storagePath);
/* Create the main storage folder if it doesn't exist */
if (!mainStorageFolder.isDirectory())
{
mainStorageFolder.mkdir();
}
String folderName = key.hexRepresentation().substring(0, 10);
File contentStorageFolder = new File(mainStorageFolder + File.separator + folderName);
File contentStorageFolder = new File(Configuration.getNodeDataFolder(ownerId) + File.separator + folderName);
/* Create the content folder if it doesn't exist */
if (!contentStorageFolder.isDirectory())
@ -219,7 +213,7 @@ public class DHT
contentStorageFolder.mkdir();
}
return mainStorageFolder + File.separator + folderName;
return contentStorageFolder.toString();
}
/**

View File

@ -1,6 +1,5 @@
package kademlia.tests;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import kademlia.core.Configuration;