From c232fccc69a7e767e48ae09953c3499d7c6fd5d1 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Sat, 29 Mar 2014 10:52:55 +0530 Subject: [PATCH] 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 --- src/kademlia/core/Configuration.java | 30 ++++++++++++++ src/kademlia/core/Kademlia.java | 40 +++++++------------ src/kademlia/dht/DHT.java | 18 +++------ .../tests/AutoRefreshOperationTest.java | 1 - 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/kademlia/core/Configuration.java b/src/kademlia/core/Configuration.java index f161a16..4c0a37b 100644 --- a/src/kademlia/core/Configuration.java +++ b/src/kademlia/core/Configuration.java @@ -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(); + } } diff --git a/src/kademlia/core/Kademlia.java b/src/kademlia/core/Kademlia.java index 00ab142..0c539fe 100644 --- a/src/kademlia/core/Kademlia.java +++ b/src/kademlia/core/Kademlia.java @@ -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(); } /** diff --git a/src/kademlia/dht/DHT.java b/src/kademlia/dht/DHT.java index 7fd22be..df5354a 100644 --- a/src/kademlia/dht/DHT.java +++ b/src/kademlia/dht/DHT.java @@ -28,13 +28,16 @@ public class DHT private transient StorageEntryManager entriesManager; private transient final JsonSerializer 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(); } /** diff --git a/src/kademlia/tests/AutoRefreshOperationTest.java b/src/kademlia/tests/AutoRefreshOperationTest.java index 3281d21..6ccf3e8 100644 --- a/src/kademlia/tests/AutoRefreshOperationTest.java +++ b/src/kademlia/tests/AutoRefreshOperationTest.java @@ -1,6 +1,5 @@ package kademlia.tests; -import java.io.IOException; import java.util.Timer; import java.util.TimerTask; import kademlia.core.Configuration;