add nsCache in iterative-query-controller

Change-Id: I3c75ea16eaa987273d157b762d523988c3b13316
diff --git a/src/clients/iterative-query-controller.cpp b/src/clients/iterative-query-controller.cpp
index 908b499..ab64adc 100644
--- a/src/clients/iterative-query-controller.cpp
+++ b/src/clients/iterative-query-controller.cpp
@@ -32,12 +32,14 @@
                                                    const QuerySucceedCallback& onSucceed,
                                                    const QueryFailCallback& onFail,
                                                    Face& face,
-                                                   security::v2::Validator* validator)
+                                                   security::v2::Validator* validator,
+                                                   ndn::InMemoryStorage* cache)
   : QueryController(dstLabel, rrType, interestLifetime, onSucceed, onFail, face)
   , m_validator(validator)
   , m_step(QUERY_STEP_QUERY_NS)
   , m_nFinishedComps(0)
   , m_nTryComps(1)
+  , m_nsCache(cache)
 {
 }
 
@@ -83,6 +85,10 @@
 void
 IterativeQueryController::onDataValidated(const Data& data, NdnsContentType contentType)
 {
+  if (m_nsCache != nullptr && contentType == NDNS_LINK) {
+    m_nsCache->insert(data);
+  }
+
   switch (m_step) {
   case QUERY_STEP_QUERY_NS:
     if (contentType == NDNS_NACK) {
@@ -174,6 +180,16 @@
 void
 IterativeQueryController::express(const Interest& interest)
 {
+  if (m_nsCache != nullptr) {
+    shared_ptr<const Data> cachedData = m_nsCache->find(interest);
+    if (cachedData != nullptr) {
+      NDNS_LOG_DEBUG("[* cached *] NS record has been cached before: "
+                     << interest.getName());
+      onData(interest, *cachedData);
+      return ;
+    }
+  }
+
   NDNS_LOG_DEBUG("[* <- *] send a Query: " << interest.getName());
   m_face.expressInterest(interest,
                          bind(&IterativeQueryController::onData, this, _1, _2),
diff --git a/src/clients/iterative-query-controller.hpp b/src/clients/iterative-query-controller.hpp
index 8a15d98..037284b 100644
--- a/src/clients/iterative-query-controller.hpp
+++ b/src/clients/iterative-query-controller.hpp
@@ -33,6 +33,7 @@
 #include <ndn-cxx/interest.hpp>
 #include <ndn-cxx/name.hpp>
 #include <ndn-cxx/link.hpp>
+#include <ndn-cxx/ims/in-memory-storage.hpp>
 
 namespace ndn {
 namespace ndns {
@@ -61,7 +62,8 @@
   IterativeQueryController(const Name& dstLabel, const name::Component& rrType,
                            const time::milliseconds& interestLifetime,
                            const QuerySucceedCallback& onSucceed, const QueryFailCallback& onFail,
-                           Face& face, security::v2::Validator* validator = nullptr);
+                           Face& face, security::v2::Validator* validator = nullptr,
+                           ndn::InMemoryStorage* cache = nullptr);
 
   virtual void
   start();
@@ -149,6 +151,7 @@
 
 private:
   Block m_lastLink;
+  ndn::InMemoryStorage* m_nsCache;
 };
 
 std::ostream&