diff --git a/src/kademlia/Kademlia.java b/src/kademlia/Kademlia.java index 25847d2..1536564 100644 --- a/src/kademlia/Kademlia.java +++ b/src/kademlia/Kademlia.java @@ -274,30 +274,32 @@ public class Kademlia * Get some content stored on the DHT * The content returned is a JSON String in byte format; this string is parsed into a class * - * @param param The parameters used to search for the content - * @param numResultsReq How many results are required from different nodes + * @param param The parameters used to search for the content + * @param numNodesToQuery How many nodes should we query to get this content. We return all content on these nodes. * * @return DHTContent The content * * @throws java.io.IOException */ - public List get(GetParameter param, int numResultsReq) throws NoSuchElementException, IOException + public List get(GetParameter param, int numNodesToQuery) throws NoSuchElementException, IOException { - List contentFound; + List contentFound = new ArrayList<>();; if (this.dht.contains(param)) { /* If the content exist in our own DHT, then return it. */ - contentFound = new ArrayList<>(); contentFound.add(this.dht.get(param)); } - else + + if (contentFound.size() == numNodesToQuery) { - /* Seems like it doesn't exist in our DHT, get it from other Nodes */ - ContentLookupOperation clo = new ContentLookupOperation(server, localNode, param, numResultsReq, this.config); - clo.execute(); - contentFound = clo.getContentFound(); + return contentFound; } + /* Seems like it doesn't exist in our DHT, get it from other Nodes */ + ContentLookupOperation clo = new ContentLookupOperation(server, localNode, param, numNodesToQuery, this.config); + clo.execute(); + contentFound = clo.getContentFound(); + return contentFound; } diff --git a/src/kademlia/dht/StorageEntryMetadata.java b/src/kademlia/dht/StorageEntryMetadata.java index 0fa6fe9..8b93954 100644 --- a/src/kademlia/dht/StorageEntryMetadata.java +++ b/src/kademlia/dht/StorageEntryMetadata.java @@ -47,7 +47,7 @@ public class StorageEntryMetadata { return this.contentHash; } - + public long getLastUpdatedTimestamp() { return this.updatedTs; @@ -76,7 +76,12 @@ public class StorageEntryMetadata } /* Check that key matches */ - return (params.getKey() != null) && (params.getKey().equals(this.key)); + if ((params.getKey() != null) && (!params.getKey().equals(this.key))) + { + return false; + } + + return true; } @Override diff --git a/src/kademlia/operation/ContentLookupOperation.java b/src/kademlia/operation/ContentLookupOperation.java index 9d8ffb0..8420d05 100644 --- a/src/kademlia/operation/ContentLookupOperation.java +++ b/src/kademlia/operation/ContentLookupOperation.java @@ -43,7 +43,7 @@ public class ContentLookupOperation implements Operation, Receiver private final Node localNode; private final GetParameter params; private final List contentFound; - private final int numResultsReq; + private final int numNodesToQuery; private final KadConfiguration config; private final ContentLookupMessage lookupMessage; @@ -67,11 +67,11 @@ public class ContentLookupOperation implements Operation, Receiver /** * @param server * @param localNode - * @param params The parameters to search for the content which we need to find - * @param numResultsReq The number of results for this content from different nodes required + * @param params The parameters to search for the content which we need to find + * @param numNodesToQuery The number of nodes to query to get this content. We return the content among these nodes. * @param config */ - public ContentLookupOperation(KadServer server, Node localNode, GetParameter params, int numResultsReq, KadConfiguration config) + public ContentLookupOperation(KadServer server, Node localNode, GetParameter params, int numNodesToQuery, KadConfiguration config) { /* Construct our lookup message */ this.lookupMessage = new ContentLookupMessage(localNode, params); @@ -79,7 +79,7 @@ public class ContentLookupOperation implements Operation, Receiver this.server = server; this.localNode = localNode; this.params = params; - this.numResultsReq = numResultsReq; + this.numNodesToQuery = numNodesToQuery; this.config = config; /** @@ -99,14 +99,15 @@ public class ContentLookupOperation implements Operation, Receiver { try { - error = true; - /* Set the local node as already asked */ nodes.put(this.localNode, ASKED); this.addNodes(this.localNode.getRoutingTable().getAllNodes()); - /* If we haven't finished as yet, wait for a maximum of config.operationTimeout() time */ + /** + * If we haven't found the requested amount of content as yet, + * keey trying until config.operationTimeout() time has expired + */ int totalTimeWaited = 0; int timeInterval = 100; // We re-check every 300 milliseconds while (totalTimeWaited < this.config.operationTimeout()) @@ -121,29 +122,6 @@ public class ContentLookupOperation implements Operation, Receiver break; } } - if (error) - { - /* If we still haven't received any responses by then, do a routing timeout */ - throw new RoutingException("ContentLookupOperation: Lookup Timeout."); - } - - /** - * @deprecated - replaced by the above code - * We just keep this code in case any problems are encountered later - */ -// if (!this.askNodesorFinish()) -// { -// /* If we haven't finished as yet, wait a while */ -// wait(this.config.operationTimeout()); -// -// /* If we still haven't received any responses by then, do a routing timeout */ -// if (error) -// { -// /* Lets not throw any exception */ -// -// //throw new RoutingException("Content Lookup Operation Timeout."); -// } -// } } catch (InterruptedException e) { @@ -274,11 +252,9 @@ public class ContentLookupOperation implements Operation, Receiver /*@todo Check if the content matches the given criteria */ this.contentFound.add(content); - if (this.contentFound.size() == this.numResultsReq) + if (this.contentFound.size() == this.numNodesToQuery) { /* We've got all the content required, let's stop the loopup operation */ - System.out.println("We good"); - this.error = false; this.contentsFound = true; } }