From a97905e43149ec9862126812e195b64b12adf66d Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Thu, 6 Mar 2014 20:42:30 +0530 Subject: [PATCH] Started working on the ContentRefreshOperation - Setup a method to get all storage entries for the different content - Started setting up the content refresh operation functionality --- src/kademlia/core/Kademlia.java | 6 ++-- src/kademlia/dht/DHT.java | 15 ++++++++-- src/kademlia/dht/StorageEntryManager.java | 17 +++++++++++ .../operation/BucketRefreshOperation.java | 3 +- .../operation/ContentRefreshOperation.java | 29 +++++++++++++++++-- .../operation/KadRefreshOperation.java | 8 +++-- 6 files changed, 65 insertions(+), 13 deletions(-) diff --git a/src/kademlia/core/Kademlia.java b/src/kademlia/core/Kademlia.java index 1186422..c21df31 100644 --- a/src/kademlia/core/Kademlia.java +++ b/src/kademlia/core/Kademlia.java @@ -79,8 +79,8 @@ public class Kademlia { try { - /* Runs a RefreshOperation to refresh K-Buckets and stored content */ - new BucketRefreshOperation(server, localNode).execute(); + /* Runs a DHT RefreshOperation */ + Kademlia.this.refresh(); } catch (IOException e) { @@ -182,7 +182,7 @@ public class Kademlia */ public void refresh() throws IOException { - new KadRefreshOperation(server, localNode).execute(); + new KadRefreshOperation(this.server, this.localNode, this.dht).execute(); } /** diff --git a/src/kademlia/dht/DHT.java b/src/kademlia/dht/DHT.java index 5533aa5..300cc74 100644 --- a/src/kademlia/dht/DHT.java +++ b/src/kademlia/dht/DHT.java @@ -7,6 +7,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.util.List; import java.util.NoSuchElementException; import kademlia.core.Configuration; import kademlia.core.GetParameter; @@ -77,12 +78,12 @@ public class DHT } /** - * Get the List for the content if any exist, - * retrieve the content from the storage system and return it + * Get the StorageEntry for the content if any exist, + * retrieve the KadContent from the storage system and return it * * @param param The parameters used to filter the content needed * - * @return List A list with the content found on the DHT satisfying the given criteria + * @return KadContent A KadContent found on the DHT satisfying the given criteria * * @throws java.io.IOException */ @@ -142,4 +143,12 @@ public class DHT return mainStorageFolder + File.separator + folderName; } + + /** + * @return A List of all StorageEntries for this node + */ + public List getStorageEntries() + { + return entriesManager.getAllEntries(); + } } diff --git a/src/kademlia/dht/StorageEntryManager.java b/src/kademlia/dht/StorageEntryManager.java index a24a52a..7267539 100644 --- a/src/kademlia/dht/StorageEntryManager.java +++ b/src/kademlia/dht/StorageEntryManager.java @@ -99,4 +99,21 @@ public class StorageEntryManager } } + /** + * @return A list of all storage entries + */ + public List getAllEntries() + { + List entriesRet = new ArrayList<>(); + + for (List entrySet : this.entries.values()) + { + if (entrySet.size() > 0) + { + entriesRet.addAll(entrySet); + } + } + + return entriesRet; + } } diff --git a/src/kademlia/operation/BucketRefreshOperation.java b/src/kademlia/operation/BucketRefreshOperation.java index b445bb7..7fd514f 100644 --- a/src/kademlia/operation/BucketRefreshOperation.java +++ b/src/kademlia/operation/BucketRefreshOperation.java @@ -50,8 +50,7 @@ public class BucketRefreshOperation implements Operation { try { - System.out.println("Distance: " + localNode.getNodeId().getDistance(current) + " - ID: " + current); - + //System.out.println("Distance: " + localNode.getNodeId().getDistance(current) + " - ID: " + current); new NodeLookupOperation(server, localNode, localNode.getNodeId()).execute(); } catch (IOException e) diff --git a/src/kademlia/operation/ContentRefreshOperation.java b/src/kademlia/operation/ContentRefreshOperation.java index 774ec39..cc2b0a5 100644 --- a/src/kademlia/operation/ContentRefreshOperation.java +++ b/src/kademlia/operation/ContentRefreshOperation.java @@ -1,19 +1,42 @@ package kademlia.operation; +import kademlia.core.KadServer; +import kademlia.dht.DHT; +import kademlia.node.Node; + /** - * Refresh the data on this node by sending the data to the K-Closest nodes to the data + * Refresh/Restore the data on this node by sending the data to the K-Closest nodes to the data * * @author Joshua Kissoon * @since 20140306 */ -public class ContentRefreshOperation +public class ContentRefreshOperation implements Operation { + private final KadServer server; + private final Node localNode; + private final DHT dht; + + public ContentRefreshOperation(KadServer server, Node localNode, DHT dht) + { + this.server = server; + this.localNode = localNode; + this.dht = dht; + } + + /** + * For each content stored on this DHT, distribute it to 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 + */ + @Override public void execute() { + /** * @todo Delete any content on this node that this node is not one of the K-Closest nodes to - * @todo Delete any expired content */ + } } diff --git a/src/kademlia/operation/KadRefreshOperation.java b/src/kademlia/operation/KadRefreshOperation.java index a56de57..16899cf 100644 --- a/src/kademlia/operation/KadRefreshOperation.java +++ b/src/kademlia/operation/KadRefreshOperation.java @@ -2,6 +2,7 @@ package kademlia.operation; import java.io.IOException; import kademlia.core.KadServer; +import kademlia.dht.DHT; import kademlia.node.Node; /** @@ -15,11 +16,13 @@ public class KadRefreshOperation implements Operation private final KadServer server; private final Node localNode; + private final DHT dht; - public KadRefreshOperation(KadServer server, Node localNode) + public KadRefreshOperation(KadServer server, Node localNode, DHT dht) { this.server = server; this.localNode = localNode; + this.dht = dht; } @Override @@ -27,7 +30,8 @@ public class KadRefreshOperation implements Operation { /* Run our BucketRefreshOperation to refresh buckets */ new BucketRefreshOperation(server, localNode).execute(); - + /* After buckets have been refreshed, we refresh content */ + new ContentRefreshOperation(server, localNode, dht).execute(); } }