From 0ce64529c686f859f16171f225e31d9fd1d7f787 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Thu, 1 May 2014 16:52:31 +0530 Subject: [PATCH] Saving State & Reloading - Routing Table had a save state issue because of the KadConfiguration -- I removed saving the configuration since this may change from boot to boot -- Provided a way to set a new configuration to the routing table -- Updated the serializer/deserializer --- src/kademlia/KademliaNode.java | 4 ++-- src/kademlia/routing/KadRoutingTable.java | 8 ++++++++ src/kademlia/routing/RoutingTable.java | 7 ++++++- .../serializer/JsonRoutingTableSerializer.java | 17 +++++++++++++++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/kademlia/KademliaNode.java b/src/kademlia/KademliaNode.java index b821851..b53c686 100644 --- a/src/kademlia/KademliaNode.java +++ b/src/kademlia/KademliaNode.java @@ -191,7 +191,7 @@ public class KademliaNode * @section Read the routing table */ din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId, iconfig) + File.separator + "routingtable.kns")); - RoutingTable irtbl = new JsonRoutingTableSerializer().read(din); + RoutingTable irtbl = new JsonRoutingTableSerializer(iconfig).read(din); /** * @section Read the node state @@ -392,7 +392,7 @@ public class KademliaNode * This will cause a serialization recursion, and in turn a Stack Overflow */ dout = new DataOutputStream(new FileOutputStream(getStateStorageFolderName(this.ownerId, this.config) + File.separator + "routingtable.kns")); - new JsonRoutingTableSerializer().write(this.getRoutingTable(), dout); + new JsonRoutingTableSerializer(this.config).write(this.getRoutingTable(), dout); /** * @section Save the DHT diff --git a/src/kademlia/routing/KadRoutingTable.java b/src/kademlia/routing/KadRoutingTable.java index f50139a..d1c1b49 100644 --- a/src/kademlia/routing/KadRoutingTable.java +++ b/src/kademlia/routing/KadRoutingTable.java @@ -1,6 +1,7 @@ package kademlia.routing; import java.util.List; +import kademlia.core.KadConfiguration; import kademlia.node.Node; import kademlia.node.NodeId; @@ -18,6 +19,13 @@ public interface KadRoutingTable */ public void initialize(); + /** + * Sets the configuration file for this routing table + * + * @param config + */ + public void setConfiguration(KadConfiguration config); + /** * Adds a contact to the routing table based on how far it is from the LocalNode. * diff --git a/src/kademlia/routing/RoutingTable.java b/src/kademlia/routing/RoutingTable.java index 133d12d..401ec45 100644 --- a/src/kademlia/routing/RoutingTable.java +++ b/src/kademlia/routing/RoutingTable.java @@ -18,7 +18,7 @@ public class RoutingTable implements KadRoutingTable private final Node localNode; // The current node private transient KadBucket[] buckets; - private final KadConfiguration config; + private transient KadConfiguration config; public RoutingTable(Node localNode, KadConfiguration config) { @@ -45,6 +45,11 @@ public class RoutingTable implements KadRoutingTable } } + public void setConfiguration(KadConfiguration config) + { + this.config = config; + } + /** * Adds a contact to the routing table based on how far it is from the LocalNode. * diff --git a/src/kademlia/util/serializer/JsonRoutingTableSerializer.java b/src/kademlia/util/serializer/JsonRoutingTableSerializer.java index d55e3b9..0ec55ed 100644 --- a/src/kademlia/util/serializer/JsonRoutingTableSerializer.java +++ b/src/kademlia/util/serializer/JsonRoutingTableSerializer.java @@ -12,7 +12,7 @@ import java.io.OutputStreamWriter; import kademlia.routing.RoutingTable; import java.lang.reflect.Type; import java.util.List; -import kademlia.node.Node; +import kademlia.core.KadConfiguration; import kademlia.routing.Contact; /** @@ -48,11 +48,23 @@ public class JsonRoutingTableSerializer implements KadSerializer { }.getType(); + private final KadConfiguration config; + { gson = new Gson(); } + /** + * Initialize the class + * + * @param config + */ + public JsonRoutingTableSerializer(KadConfiguration config) + { + this.config = config; + } + @Override public void write(RoutingTable data, DataOutputStream out) throws IOException { @@ -80,7 +92,8 @@ public class JsonRoutingTableSerializer implements KadSerializer /* Read the basic RoutingTable */ RoutingTable tbl = gson.fromJson(reader, RoutingTable.class); - + tbl.setConfiguration(config); + /* Now get the Contacts and add them back to the RoutingTable */ List contacts = gson.fromJson(reader, contactCollectionType); tbl.initialize();