Added a method to store content only locally

Updated the StoreOperation to store content locally if the local Node is a part of the K-Closest to the content
This commit is contained in:
Joshua Kissoon 2014-03-22 14:33:31 +05:30
parent 42be8498c0
commit b93133337c
3 changed files with 95 additions and 6 deletions

View File

@ -75,7 +75,7 @@ public class Kademlia
* *
* @throws IOException If an error occurred while reading id or local map * @throws IOException If an error occurred while reading id or local map
* from disk <i>or</i> a network error occurred while * from disk <i>or</i> a network error occurred while
attempting to bootstrap to the network * attempting to bootstrap to the network
* */ * */
public Kademlia(String ownerId, Node localNode, int udpPort, DHT dht) throws IOException public Kademlia(String ownerId, Node localNode, int udpPort, DHT dht) throws IOException
{ {
@ -203,13 +203,25 @@ public class Kademlia
*/ */
public synchronized int put(KadContent content) throws IOException public synchronized int put(KadContent content) throws IOException
{ {
StoreOperation sop = new StoreOperation(server, localNode, content); StoreOperation sop = new StoreOperation(this.server, this.localNode, content, this.dht);
sop.execute(); sop.execute();
/* Return how many nodes the content was stored on */ /* Return how many nodes the content was stored on */
return sop.numNodesStoredAt(); return sop.numNodesStoredAt();
} }
/**
* Store a content on the local node's DHT
*
* @param content The content to put on the DHT
*
* @throws java.io.IOException
*/
public synchronized void putLocally(KadContent content) throws IOException
{
this.dht.store(content);
}
/** /**
* Get some content stored on the DHT * Get some content stored on the DHT
* The content returned is a JSON String in byte format; this string is parsed into a class * The content returned is a JSON String in byte format; this string is parsed into a class

View File

@ -3,6 +3,7 @@ package kademlia.operation;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import kademlia.core.KadServer; import kademlia.core.KadServer;
import kademlia.dht.DHT;
import kademlia.dht.KadContent; import kademlia.dht.KadContent;
import kademlia.message.Message; import kademlia.message.Message;
import kademlia.message.StoreContentMessage; import kademlia.message.StoreContentMessage;
@ -20,17 +21,20 @@ public class StoreOperation implements Operation
private final KadServer server; private final KadServer server;
private final Node localNode; private final Node localNode;
private final KadContent content; private final KadContent content;
private final DHT localDht;
/** /**
* @param server * @param server
* @param localNode * @param localNode
* @param content The content to be stored on the DHT * @param content The content to be stored on the DHT
* @param localDht The local DHT
*/ */
public StoreOperation(KadServer server, Node localNode, KadContent content) public StoreOperation(KadServer server, Node localNode, KadContent content, DHT localDht)
{ {
this.server = server; this.server = server;
this.localNode = localNode; this.localNode = localNode;
this.content = content; this.content = content;
this.localDht = localDht;
} }
@Override @Override
@ -49,12 +53,13 @@ public class StoreOperation implements Operation
{ {
if (n.equals(this.localNode)) if (n.equals(this.localNode))
{ {
/* @todo Store the content locally */ /* Store the content locally */
this.localDht.store(content);
} }
else else
{ {
/** /**
* @todo Create a receiver that recieves a store acknowledgement message to count how many nodes a content have been stored at * @todo Create a receiver that receives a store acknowledgement message to count how many nodes a content have been stored at
*/ */
this.server.sendMessage(n, msg, null); this.server.sendMessage(n, msg, null);
} }

View File

@ -0,0 +1,72 @@
package kademlia.tests;
import kademlia.core.Kademlia;
import kademlia.node.NodeId;
/**
* Testing the Kademlia Auto Content and Node table refresh operations
*
* @author Joshua Kissoon
* @since 20140309
*/
public class AutoRefreshOperationTest
{
public AutoRefreshOperationTest()
{
try
{
/* Setting up 2 Kad networks */
Kademlia kad1 = new Kademlia("JoshuaK", new NodeId("ASF45678947584567463"), 12049);
Kademlia kad2 = new Kademlia("Crystal", new NodeId("ASF45678947584567464"), 4585);
Kademlia kad3 = new Kademlia("Shameer", new NodeId("ASF45678947584567465"), 8104);
Kademlia kad4 = new Kademlia("Lokesh", new NodeId("ASF45678947584567466"), 8335);
Kademlia kad5 = new Kademlia("Chandu", new NodeId("ASF45678947584567467"), 13345);
/* Connecting nodes */
System.out.println("Connecting Nodes 1 & 2");
kad2.bootstrap(kad1.getNode());
kad3.bootstrap(kad2.getNode());
kad4.bootstrap(kad2.getNode());
kad5.bootstrap(kad4.getNode());
System.out.println(kad1);
System.out.println(kad2);
System.out.println(kad3);
System.out.println(kad4);
System.out.println(kad5);
DHTContentImpl c = new DHTContentImpl(kad2.getOwnerId(), "Some Data");
kad2.put(c);
System.out.println("\n\n\n\nSTORING CONTENT 2\n\n\n\n");
DHTContentImpl c2 = new DHTContentImpl(kad2.getOwnerId(), "Some other Data");
System.out.println(c2);
kad4.put(c2);
System.out.println(kad1);
System.out.println(kad2);
System.out.println(kad3);
System.out.println(kad4);
System.out.println(kad5);
/* Shutting down kad1 and restarting it */
System.out.println("\n\n\nShutting down Kad instance");
System.out.println(kad2);
kad1.shutdown();
System.out.println("\n\n\nReloading Kad instance from file");
Kademlia kadR2 = Kademlia.loadFromFile("JoshuaK");
System.out.println(kadR2);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new AutoRefreshOperationTest();
}
}