mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
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:
parent
42be8498c0
commit
b93133337c
@ -75,7 +75,7 @@ public class Kademlia
|
||||
*
|
||||
* @throws IOException If an error occurred while reading id or local map
|
||||
* 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
|
||||
{
|
||||
@ -203,13 +203,25 @@ public class Kademlia
|
||||
*/
|
||||
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();
|
||||
|
||||
/* Return how many nodes the content was stored on */
|
||||
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
|
||||
* The content returned is a JSON String in byte format; this string is parsed into a class
|
||||
|
@ -3,6 +3,7 @@ package kademlia.operation;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import kademlia.core.KadServer;
|
||||
import kademlia.dht.DHT;
|
||||
import kademlia.dht.KadContent;
|
||||
import kademlia.message.Message;
|
||||
import kademlia.message.StoreContentMessage;
|
||||
@ -20,17 +21,20 @@ public class StoreOperation implements Operation
|
||||
private final KadServer server;
|
||||
private final Node localNode;
|
||||
private final KadContent content;
|
||||
private final DHT localDht;
|
||||
|
||||
/**
|
||||
* @param server
|
||||
* @param localNode
|
||||
* @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.localNode = localNode;
|
||||
this.content = content;
|
||||
this.localDht = localDht;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -49,12 +53,13 @@ public class StoreOperation implements Operation
|
||||
{
|
||||
if (n.equals(this.localNode))
|
||||
{
|
||||
/* @todo Store the content locally */
|
||||
/* Store the content locally */
|
||||
this.localDht.store(content);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
72
src/kademlia/tests/AutoRefreshOperationTest.java
Normal file
72
src/kademlia/tests/AutoRefreshOperationTest.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user