From 219e6073edb2f382aea6391902f2146e3fbb9b82 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Sat, 22 Mar 2014 15:52:37 +0530 Subject: [PATCH] Content deletion from a node that is no longer one of the K-Closest is working! --- src/kademlia/dht/DHT.java | 31 +++++++++++++++++++ src/kademlia/dht/StorageEntryManager.java | 18 +++++++++++ .../exceptions/ContentNotFoundException.java | 21 +++++++++++++ .../operation/ContentRefreshOperation.java | 17 ++++++++-- 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/kademlia/exceptions/ContentNotFoundException.java diff --git a/src/kademlia/dht/DHT.java b/src/kademlia/dht/DHT.java index 7e1efdc..7fd22be 100644 --- a/src/kademlia/dht/DHT.java +++ b/src/kademlia/dht/DHT.java @@ -12,6 +12,7 @@ import java.util.NoSuchElementException; import kademlia.core.Configuration; import kademlia.core.GetParameter; import kademlia.exceptions.ContentExistException; +import kademlia.exceptions.ContentNotFoundException; import kademlia.node.NodeId; import kademlia.serializer.JsonSerializer; @@ -156,6 +157,36 @@ public class DHT throw new NoSuchElementException(); } + /** + * Delete a content from local storage + * + * @param content The Content to Remove + * + * + * @throws kademlia.exceptions.ContentNotFoundException + */ + public void remove(KadContent content) throws ContentNotFoundException + { + this.remove(new StorageEntry(content)); + } + + public void remove(StorageEntry entry) throws ContentNotFoundException + { + String folder = this.getContentStorageFolderName(entry.getKey()); + File file = new File(folder + File.separator + entry.hashCode() + ".kct"); + + entriesManager.remove(entry); + + if (file.exists()) + { + file.delete(); + } + else + { + throw new ContentNotFoundException(); + } + } + /** * Get the name of the folder for which a content should be stored * diff --git a/src/kademlia/dht/StorageEntryManager.java b/src/kademlia/dht/StorageEntryManager.java index 9e09924..70bec3f 100644 --- a/src/kademlia/dht/StorageEntryManager.java +++ b/src/kademlia/dht/StorageEntryManager.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.NoSuchElementException; import kademlia.core.GetParameter; import kademlia.exceptions.ContentExistException; +import kademlia.exceptions.ContentNotFoundException; import kademlia.node.NodeId; /** @@ -159,6 +160,23 @@ class StorageEntryManager return entriesRet; } + public void remove(KadContent content) throws ContentNotFoundException + { + this.remove(new StorageEntry(content)); + } + + public void remove(StorageEntry entry) throws ContentNotFoundException + { + if (contains(entry)) + { + this.entries.get(entry.getKey()).remove(entry); + } + else + { + throw new ContentNotFoundException("This content does not exist in the Storage Entries"); + } + } + @Override public String toString() { diff --git a/src/kademlia/exceptions/ContentNotFoundException.java b/src/kademlia/exceptions/ContentNotFoundException.java new file mode 100644 index 0000000..4b7bf84 --- /dev/null +++ b/src/kademlia/exceptions/ContentNotFoundException.java @@ -0,0 +1,21 @@ +package kademlia.exceptions; + +/** + * An exception used to indicate that a content does not exist on the DHT + * + * @author Joshua Kissoon + * @created 20140322 + */ +public class ContentNotFoundException extends Exception +{ + + public ContentNotFoundException() + { + super(); + } + + public ContentNotFoundException(String message) + { + super(message); + } +} diff --git a/src/kademlia/operation/ContentRefreshOperation.java b/src/kademlia/operation/ContentRefreshOperation.java index 99ff028..2302e58 100644 --- a/src/kademlia/operation/ContentRefreshOperation.java +++ b/src/kademlia/operation/ContentRefreshOperation.java @@ -6,6 +6,7 @@ import kademlia.core.Configuration; import kademlia.core.KadServer; import kademlia.dht.DHT; import kademlia.dht.StorageEntry; +import kademlia.exceptions.ContentNotFoundException; import kademlia.message.Message; import kademlia.message.StoreContentMessage; import kademlia.node.Node; @@ -69,9 +70,19 @@ public class ContentRefreshOperation implements Operation } } - /** - * @todo Delete any content on this node that this node is not one of the K-Closest nodes to - */ + /* Delete any content on this node that this node is not one of the K-Closest nodes to */ + try + { + if (!closestNodes.contains(this.localNode)) + { + this.dht.remove(e); + } + } + catch (ContentNotFoundException cnfe) + { + /* It would be weird if the content is not found here */ + System.err.println("ContentRefreshOperation: Removing content from local node, content not found... Message: " + cnfe.getMessage()); + } } }