diff --git a/src/kademlia/Kademlia.java b/src/kademlia/Kademlia.java index 810878f..9b596f9 100644 --- a/src/kademlia/Kademlia.java +++ b/src/kademlia/Kademlia.java @@ -268,6 +268,7 @@ public class Kademlia * @return DHTContent The content * * @throws java.io.IOException + * @throws kademlia.exceptions.ContentNotFoundException */ public StorageEntry get(GetParameter param) throws NoSuchElementException, IOException, ContentNotFoundException { diff --git a/src/kademlia/dht/DHT.java b/src/kademlia/dht/DHT.java index 2ad1c80..e5f194c 100644 --- a/src/kademlia/dht/DHT.java +++ b/src/kademlia/dht/DHT.java @@ -86,6 +86,10 @@ public class DHT if (this.entriesManager.contains(content.getContentMetadata())) { StorageEntryMetadata current = this.entriesManager.get(content.getContentMetadata()); + + /* update the last republished time */ + current.updateLastRepublished(); + if (current.getLastUpdatedTimestamp() >= content.getContentMetadata().getLastUpdatedTimestamp()) { /* We have the current content, no need to update it! just leave this method now */ diff --git a/src/kademlia/dht/StorageEntryMetadata.java b/src/kademlia/dht/StorageEntryMetadata.java index 8b93954..2620750 100644 --- a/src/kademlia/dht/StorageEntryMetadata.java +++ b/src/kademlia/dht/StorageEntryMetadata.java @@ -19,6 +19,9 @@ public class StorageEntryMetadata private final int contentHash; private final long updatedTs; + /* This value is the last time this content was last updated from the network */ + private long lastRepublished; + public StorageEntryMetadata(KadContent content) { this.key = content.getKey(); @@ -26,6 +29,8 @@ public class StorageEntryMetadata this.type = content.getType(); this.contentHash = content.hashCode(); this.updatedTs = content.getLastUpdatedTimestamp(); + + this.lastRepublished = System.currentTimeMillis() / 1000L; } public NodeId getKey() @@ -84,6 +89,19 @@ public class StorageEntryMetadata return true; } + public long lastRepublished() + { + return this.lastRepublished; + } + + /** + * Whenever we republish a content or get this content from the network, we update the last republished time + */ + public void updateLastRepublished() + { + this.lastRepublished = System.currentTimeMillis() / 1000L; + } + @Override public boolean equals(Object o) { diff --git a/src/kademlia/operation/ContentRefreshOperation.java b/src/kademlia/operation/ContentRefreshOperation.java index 052fdad..90c6744 100644 --- a/src/kademlia/operation/ContentRefreshOperation.java +++ b/src/kademlia/operation/ContentRefreshOperation.java @@ -47,14 +47,21 @@ public class ContentRefreshOperation implements Operation /* Get a list of all storage entries for content */ List entries = this.dht.getStorageEntries(); + /* If a content was last republished before this time, then we need to republish it */ + final long minRepublishTime = (System.currentTimeMillis() / 1000L) - this.config.restoreInterval(); + /* For each storage entry, distribute it */ for (StorageEntryMetadata 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 - */ + /* Check last update time of this entry and only distribute it if it has been last updated > 1 hour ago */ + if (e.lastRepublished() > minRepublishTime) + { + continue; + } + + /* Set that this content is now republished */ + e.updateLastRepublished(); + /* Get the K closest nodes to this entries */ List closestNodes = this.localNode.getRoutingTable().findClosest(e.getKey(), this.config.k());