mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-22 02:02:21 +00:00
Finished a Basic version of the content refresh operation
This commit is contained in:
parent
a97905e431
commit
46147eddd1
@ -16,7 +16,6 @@ import kademlia.node.NodeId;
|
|||||||
import kademlia.operation.ConnectOperation;
|
import kademlia.operation.ConnectOperation;
|
||||||
import kademlia.operation.ContentLookupOperation;
|
import kademlia.operation.ContentLookupOperation;
|
||||||
import kademlia.operation.Operation;
|
import kademlia.operation.Operation;
|
||||||
import kademlia.operation.BucketRefreshOperation;
|
|
||||||
import kademlia.operation.KadRefreshOperation;
|
import kademlia.operation.KadRefreshOperation;
|
||||||
import kademlia.operation.StoreOperation;
|
import kademlia.operation.StoreOperation;
|
||||||
|
|
||||||
@ -137,9 +136,11 @@ public class Kademlia
|
|||||||
*/
|
*/
|
||||||
public int put(KadContent content) throws IOException
|
public int put(KadContent content) throws IOException
|
||||||
{
|
{
|
||||||
new StoreOperation(server, localNode, content).execute();
|
StoreOperation sop = new StoreOperation(server, localNode, content);
|
||||||
/*@todo Return how many nodes the content was stored on */
|
sop.execute();
|
||||||
return 10;
|
|
||||||
|
/* Return how many nodes the content was stored on */
|
||||||
|
return sop.numNodesStoredAt();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -77,6 +77,34 @@ public class DHT
|
|||||||
return this.entriesManager.contains(param);
|
return this.entriesManager.contains(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve and create a KadContent object given the StorageEntry object
|
||||||
|
*
|
||||||
|
* @param entry The StorageEntry used to retrieve this content
|
||||||
|
*
|
||||||
|
* @return KadContent The content object
|
||||||
|
*
|
||||||
|
* @throws java.io.IOException
|
||||||
|
*/
|
||||||
|
public KadContent get(StorageEntry entry) throws IOException, NoSuchElementException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return this.retrieve(entry.getKey(), entry.getContentHash());
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException e)
|
||||||
|
{
|
||||||
|
System.err.println("Error while loading file for content. Message: " + e.getMessage());
|
||||||
|
}
|
||||||
|
catch (ClassNotFoundException e)
|
||||||
|
{
|
||||||
|
System.err.println("The class for some content was not found. Message: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we got here, means we got no entries */
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the StorageEntry for the content if any exist,
|
* Get the StorageEntry for the content if any exist,
|
||||||
* retrieve the KadContent from the storage system and return it
|
* retrieve the KadContent from the storage system and return it
|
||||||
@ -94,15 +122,14 @@ public class DHT
|
|||||||
{
|
{
|
||||||
StorageEntry e = this.entriesManager.get(param);
|
StorageEntry e = this.entriesManager.get(param);
|
||||||
return this.retrieve(e.getKey(), e.getContentHash());
|
return this.retrieve(e.getKey(), e.getContentHash());
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (FileNotFoundException e)
|
catch (FileNotFoundException e)
|
||||||
{
|
{
|
||||||
System.err.println("Error while loading file for content.");
|
System.err.println("Error while loading file for content. Message: " + e.getMessage());
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException e)
|
catch (ClassNotFoundException e)
|
||||||
{
|
{
|
||||||
System.err.println("The class for some content was not found.");
|
System.err.println("The class for some content was not found. Message: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If we got here, means we got no entries */
|
/* If we got here, means we got no entries */
|
||||||
|
@ -1,7 +1,14 @@
|
|||||||
package kademlia.operation;
|
package kademlia.operation;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import kademlia.core.Configuration;
|
||||||
import kademlia.core.KadServer;
|
import kademlia.core.KadServer;
|
||||||
import kademlia.dht.DHT;
|
import kademlia.dht.DHT;
|
||||||
|
import kademlia.dht.StorageEntry;
|
||||||
|
import kademlia.message.Message;
|
||||||
|
import kademlia.message.StoreContentMessage;
|
||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,14 +36,44 @@ public class ContentRefreshOperation implements Operation
|
|||||||
* Also delete the content if this node is no longer one of the K closest nodes
|
* Also delete the content if this node is no longer one of the K closest nodes
|
||||||
*
|
*
|
||||||
* We assume that our RoutingTable is updated, and we can get the K closest nodes from that table
|
* We assume that our RoutingTable is updated, and we can get the K closest nodes from that table
|
||||||
|
*
|
||||||
|
* @throws java.io.IOException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void execute()
|
public void execute() throws IOException
|
||||||
{
|
{
|
||||||
|
/* Get a list of all storage entries for content */
|
||||||
/**
|
List<StorageEntry> entries = this.dht.getStorageEntries();
|
||||||
* @todo Delete any content on this node that this node is not one of the K-Closest nodes to
|
|
||||||
*/
|
/* For each storage entry, distribute it */
|
||||||
|
for (StorageEntry e : entries)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @todo - Paper improvement 1 -
|
||||||
|
* Check last update time of this entry and
|
||||||
|
* only distribute it if it has been last updated > 1 hour ago
|
||||||
|
*/
|
||||||
|
/* Get the K closest nodes to this entries */
|
||||||
|
List<Node> closestNodes = this.localNode.getRoutingTable().findClosest(e.getKey(), Configuration.K);
|
||||||
|
|
||||||
|
/* Create the message */
|
||||||
|
Message msg = new StoreContentMessage(this.localNode, dht.get(e));
|
||||||
|
|
||||||
|
/*Store the message on all of the K-Nodes*/
|
||||||
|
for (Node n : closestNodes)
|
||||||
|
{
|
||||||
|
/*We don't need to again store the content locally, it's already here*/
|
||||||
|
if (!n.equals(this.localNode))
|
||||||
|
{
|
||||||
|
/* Send a contentstore operation to the K-Closest nodes */
|
||||||
|
this.server.sendMessage(n, msg, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo Delete any content on this node that this node is not one of the K-Closest nodes to
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,12 +51,25 @@ public class StoreOperation implements Operation
|
|||||||
{
|
{
|
||||||
if (n.equals(this.localNode))
|
if (n.equals(this.localNode))
|
||||||
{
|
{
|
||||||
/* Store the content locally */
|
/* @todo Store the content locally */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @todo Create a receiver that recieves 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The number of nodes that have stored this content
|
||||||
|
*
|
||||||
|
* @todo Implement this method
|
||||||
|
*/
|
||||||
|
public int numNodesStoredAt()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
package kademlia.routing;
|
package kademlia.routing;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.BitSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import kademlia.node.Node;
|
import kademlia.node.Node;
|
||||||
import kademlia.node.NodeId;
|
import kademlia.node.NodeId;
|
||||||
|
Loading…
Reference in New Issue
Block a user