src: use InMemoryStoragePersistent instead of custom LsaSegmentStorage

refs: #4788

Change-Id: Ie5d8721915864ead2f30e7f5e6181f56acefc559
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 58963f4..d917fa6 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -22,7 +22,6 @@
 #include "lsdb.hpp"
 
 #include "logger.hpp"
-#include "lsa-segment-storage.hpp"
 #include "nlsr.hpp"
 #include "utility/name-helper.hpp"
 
@@ -51,7 +50,6 @@
                    const uint64_t& sequenceNumber) {
              return isLsaNew(routerName, lsaType, sequenceNumber);
            }, m_confParam)
-  , m_lsaStorage(m_scheduler)
   , m_lsaRefreshTime(ndn::time::seconds(m_confParam.getLsaRefreshTime()))
   , m_thisRouterPrefix(m_confParam.getRouterPrefix().toUri())
   , m_adjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval())
@@ -747,7 +745,6 @@
 {
   auto it = std::find_if(m_adjLsdb.begin(), m_adjLsdb.end(),
                          std::bind(adjLsaCompareByKey, _1, key));
-
   if (it != m_adjLsdb.end()) {
     NLSR_LOG_DEBUG("Deleting Adj Lsa");
     it->writeLog();
@@ -956,22 +953,29 @@
 
   auto it = m_fetchers.insert(fetcher).first;
 
-  fetcher->onComplete.connect([this, interestName, it] (ndn::ConstBufferPtr bufferPtr) {
-                                afterFetchLsa(bufferPtr, interestName);
-                                m_fetchers.erase(it);
-                              });
-
-  fetcher->onError.connect([this, interestName, timeoutCount, deadline, lsaName, seqNo, it]
-                           (uint32_t errorCode, const std::string& msg) {
-                             onFetchLsaError(errorCode, msg, interestName,
-                                             timeoutCount, deadline, lsaName, seqNo);
-                             m_fetchers.erase(it);
-                           });
-
-  m_lsaStorage.connectToFetcher(*fetcher);
   fetcher->afterSegmentValidated.connect([this] (const ndn::Data& data) {
-                                          afterSegmentValidatedSignal(data);
-                                         });
+    // Nlsr class subscribes to this to fetch certificates
+    afterSegmentValidatedSignal(data);
+
+    // If we don't do this IMS throws: std::bad_weak_ptr: bad_weak_ptr
+    auto lsaSegment = std::make_shared<const ndn::Data>(data);
+    m_lsaStorage.insert(*lsaSegment);
+    const ndn::Name& segmentName = lsaSegment->getName();
+    // Schedule deletion of the segment
+    m_scheduler.schedule(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT),
+                         [this, segmentName] { m_lsaStorage.erase(segmentName); });
+  });
+
+  fetcher->onComplete.connect([=] (const ndn::ConstBufferPtr& bufferPtr) {
+    m_lsaStorage.erase(ndn::Name(lsaName).appendNumber(seqNo - 1));
+    afterFetchLsa(bufferPtr, interestName);
+    m_fetchers.erase(it);
+  });
+
+  fetcher->onError.connect([=] (uint32_t errorCode, const std::string& msg) {
+    onFetchLsaError(errorCode, msg, interestName, timeoutCount, deadline, lsaName, seqNo);
+    m_fetchers.erase(it);
+  });
 
   // increment a specific SENT_LSA_INTEREST
   Lsa::Type lsaType;
@@ -1043,8 +1047,8 @@
     lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
   }
   else { // else the interest is for other router's lsa, serve from LsaSegmentStorage
-    const ndn::Data* lsaSegment = m_lsaStorage.getLsaSegment(interest);
-    if (lsaSegment != nullptr) {
+    std::shared_ptr<const ndn::Data> lsaSegment = m_lsaStorage.find(interest);
+    if (lsaSegment) {
       NLSR_LOG_TRACE("Found data in lsa storage. Sending the data for " << interest.getName());
       m_face.put(*lsaSegment);
     }