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
This commit is contained in:
Joshua Kissoon 2014-05-01 16:52:31 +05:30
parent 66b6a14ebc
commit 0ce64529c6
4 changed files with 31 additions and 5 deletions

View File

@ -191,7 +191,7 @@ public class KademliaNode
* @section Read the routing table * @section Read the routing table
*/ */
din = new DataInputStream(new FileInputStream(getStateStorageFolderName(ownerId, iconfig) + File.separator + "routingtable.kns")); 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 * @section Read the node state
@ -392,7 +392,7 @@ public class KademliaNode
* This will cause a serialization recursion, and in turn a Stack Overflow * 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")); 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 * @section Save the DHT

View File

@ -1,6 +1,7 @@
package kademlia.routing; package kademlia.routing;
import java.util.List; import java.util.List;
import kademlia.core.KadConfiguration;
import kademlia.node.Node; import kademlia.node.Node;
import kademlia.node.NodeId; import kademlia.node.NodeId;
@ -18,6 +19,13 @@ public interface KadRoutingTable
*/ */
public void initialize(); 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. * Adds a contact to the routing table based on how far it is from the LocalNode.
* *

View File

@ -18,7 +18,7 @@ public class RoutingTable implements KadRoutingTable
private final Node localNode; // The current node private final Node localNode; // The current node
private transient KadBucket[] buckets; private transient KadBucket[] buckets;
private final KadConfiguration config; private transient KadConfiguration config;
public RoutingTable(Node localNode, 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. * Adds a contact to the routing table based on how far it is from the LocalNode.
* *

View File

@ -12,7 +12,7 @@ import java.io.OutputStreamWriter;
import kademlia.routing.RoutingTable; import kademlia.routing.RoutingTable;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List; import java.util.List;
import kademlia.node.Node; import kademlia.core.KadConfiguration;
import kademlia.routing.Contact; import kademlia.routing.Contact;
/** /**
@ -48,11 +48,23 @@ public class JsonRoutingTableSerializer implements KadSerializer<RoutingTable>
{ {
}.getType(); }.getType();
private final KadConfiguration config;
{ {
gson = new Gson(); gson = new Gson();
} }
/**
* Initialize the class
*
* @param config
*/
public JsonRoutingTableSerializer(KadConfiguration config)
{
this.config = config;
}
@Override @Override
public void write(RoutingTable data, DataOutputStream out) throws IOException public void write(RoutingTable data, DataOutputStream out) throws IOException
{ {
@ -80,6 +92,7 @@ public class JsonRoutingTableSerializer implements KadSerializer<RoutingTable>
/* Read the basic RoutingTable */ /* Read the basic RoutingTable */
RoutingTable tbl = gson.fromJson(reader, RoutingTable.class); RoutingTable tbl = gson.fromJson(reader, RoutingTable.class);
tbl.setConfiguration(config);
/* Now get the Contacts and add them back to the RoutingTable */ /* Now get the Contacts and add them back to the RoutingTable */
List<Contact> contacts = gson.fromJson(reader, contactCollectionType); List<Contact> contacts = gson.fromJson(reader, contactCollectionType);