sync: removed ref to lsdb.

Sync now emits to a signal whenever a new LSA is detected, and the LSDB
listens to this signal.  Sync consults a function object gotten at ctor time
to determine LSA newness.  This means Sync no longer needs a reference to
the LSDB, and LSA newness criteria can be freely manipulated.

Change-Id: Ifce3ebfc921ad846fcfa12d99c78f3b337180827
refs: #4264
diff --git a/src/communication/sync-logic-handler.cpp b/src/communication/sync-logic-handler.cpp
index 5a4d709..95b656c 100644
--- a/src/communication/sync-logic-handler.cpp
+++ b/src/communication/sync-logic-handler.cpp
@@ -20,13 +20,11 @@
  **/
 
 #include "sync-logic-handler.hpp"
-
 #include "common.hpp"
 #include "conf-parameter.hpp"
-#include "logger.hpp"
 #include "lsa.hpp"
-#include "lsdb.hpp"
 #include "utility/name-helper.hpp"
+#include "logger.hpp"
 
 namespace nlsr {
 
@@ -45,10 +43,10 @@
   }
 };
 
-SyncLogicHandler::SyncLogicHandler(ndn::Face& face,
-                                   Lsdb& lsdb, ConfParameter& conf)
-  : m_syncFace(face)
-  , m_lsdb(lsdb)
+SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew, ConfParameter& conf)
+  : onNewLsa(ndn::make_unique<OnNewLsa>())
+  , m_syncFace(face)
+  , m_isLsaNew(isLsaNew)
   , m_confParam(conf)
 {
 }
@@ -125,7 +123,7 @@
     _LOG_DEBUG("Received sync update with higher " << lsaType
                << " sequence number than entry in LSDB");
 
-    if (isLsaNew(originRouter, lsaType, seqNo)) {
+    if (m_isLsaNew(originRouter, lsaType, seqNo)) {
       if (lsaType == AdjLsa::TYPE_STRING && seqNo != 0 &&
           m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
         _LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing"
@@ -139,44 +137,11 @@
                    << " is enabled. Not going to fetch.");
         return;
       }
-      expressInterestForLsa(updateName, seqNo);
+      (*onNewLsa)(updateName, seqNo);
     }
   }
 }
 
-bool
-SyncLogicHandler::isLsaNew(const ndn::Name& originRouter, const std::string& lsaType,
-                           const uint64_t& seqNo)
-{
-  ndn::Name lsaKey = originRouter;
-  lsaKey.append(lsaType);
-
-  if (lsaType == NameLsa::TYPE_STRING)
-  {
-    return m_lsdb.isNameLsaNew(lsaKey, seqNo);
-  }
-  else if (lsaType == AdjLsa::TYPE_STRING)
-  {
-    return m_lsdb.isAdjLsaNew(lsaKey, seqNo);
-  }
-  else if (lsaType == CoordinateLsa::TYPE_STRING)
-  {
-    return m_lsdb.isCoordinateLsaNew(lsaKey, seqNo);
-  }
-
-  return false;
-}
-
-void
-SyncLogicHandler::expressInterestForLsa(const ndn::Name& updateName,
-                                        const uint64_t& seqNo)
-{
-  ndn::Name interest(updateName);
-  interest.appendNumber(seqNo);
-
-  m_lsdb.expressInterest(interest, 0);
-}
-
 void
 SyncLogicHandler::publishRoutingUpdate(const ndn::Name& type, const uint64_t& seqNo)
 {
diff --git a/src/communication/sync-logic-handler.hpp b/src/communication/sync-logic-handler.hpp
index 6de6113..b46ac7c 100644
--- a/src/communication/sync-logic-handler.hpp
+++ b/src/communication/sync-logic-handler.hpp
@@ -23,13 +23,11 @@
 #define NLSR_SYNC_LOGIC_HANDLER_HPP
 
 #include "test-access-control.hpp"
+#include "signals.hpp"
 
 #include <ndn-cxx/face.hpp>
+#include <ndn-cxx/util/signal.hpp>
 #include <ChronoSync/socket.hpp>
-
-#include <iostream>
-#include <unistd.h>
-#include <boost/cstdint.hpp>
 #include <boost/throw_exception.hpp>
 
 class InterestManager;
@@ -37,7 +35,6 @@
 namespace nlsr {
 
 class ConfParameter;
-class Lsdb;
 
 /*! \brief NLSR-to-ChronoSync interaction point
  *
@@ -50,6 +47,9 @@
 class SyncLogicHandler
 {
 public:
+  using IsLsaNew =
+    std::function<bool(const ndn::Name&, const std::string& lsaType, const uint64_t&)>;
+
   class Error : public std::runtime_error
   {
   public:
@@ -60,7 +60,7 @@
     }
   };
 
-  SyncLogicHandler(ndn::Face& face, Lsdb& lsdb, ConfParameter& conf);
+  SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew, ConfParameter& conf);
 
   /*! \brief Hook function to call whenever sync detects new data.
    *
@@ -112,21 +112,6 @@
   processUpdateFromSync(const ndn::Name& originRouter,
                         const ndn::Name& updateName, const uint64_t& seqNo);
 
-  /*! \brief Consults the LSDB to determine if a sync update has a new LSA.
-   *
-   * Given some information about an LSA, consult the LSDB to
-   * determine if the sequence number represents a new LSA from the
-   * origin router.
-   */
-  bool
-  isLsaNew(const ndn::Name& originRouter, const std::string& lsaType,
-           const uint64_t& seqNo);
-
-  /*! \brief Fetch an LSA of a certain type, with a certain sequence number.
-   */
-  void
-  expressInterestForLsa(const ndn::Name& updateName, const uint64_t& seqNo);
-
   /*! \brief Instruct ChronoSync, via the sync socket, to publish an update.
    *
    * Each ChronoSync instance maintains its own PIT for sync
@@ -136,13 +121,14 @@
   void
   publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo);
 
+public:
+  std::unique_ptr<OnNewLsa> onNewLsa;
+
 private:
   ndn::Face& m_syncFace;
   std::shared_ptr<chronosync::Socket> m_syncSocket;
   ndn::Name m_syncPrefix;
-
-private:
-  Lsdb& m_lsdb;
+  IsLsaNew m_isLsaNew;
   const ConfParameter& m_confParam;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 3fed788..4de69ef 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -65,10 +65,35 @@
 Lsdb::Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler)
   : m_nlsr(nlsr)
   , m_scheduler(scheduler)
-  , m_sync(m_nlsr.getNlsrFace(), *this, m_nlsr.getConfParameter())
+  , m_sync(m_nlsr.getNlsrFace(),
+           [this] (const ndn::Name& routerName, const std::string& lsaType,
+                   const uint64_t& sequenceNumber) {
+             ndn::Name lsaKey = routerName;
+             lsaKey.append(lsaType);
+
+             if (lsaType == NameLsa::TYPE_STRING) {
+                 return isNameLsaNew(lsaKey, sequenceNumber);
+             }
+             else if (lsaType == AdjLsa::TYPE_STRING) {
+                 return isAdjLsaNew(lsaKey, sequenceNumber);
+             }
+             else if (lsaType == CoordinateLsa::TYPE_STRING) {
+                 return isCoordinateLsaNew(lsaKey, sequenceNumber);
+             }
+             else {
+               return false;
+             }
+           },
+           m_nlsr.getConfParameter())
   , m_lsaRefreshTime(0)
   , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
   , m_sequencingManager()
+  , m_onNewLsaConnection(m_sync.onNewLsa->connect(
+      [this] (const ndn::Name& updateName, const uint64_t& sequenceNumber) {
+        ndn::Name lsaInterest{updateName};
+        lsaInterest.appendNumber(sequenceNumber);
+        expressInterest(lsaInterest, 0);
+      }))
 {
 }
 
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index 8d01155..042d727 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -32,21 +32,9 @@
 #include <ndn-cxx/security/key-chain.hpp>
 #include <ndn-cxx/util/signal.hpp>
 #include <ndn-cxx/util/time.hpp>
-
 #include <utility>
 #include <boost/cstdint.hpp>
 
-#include <utility>
-#include <boost/cstdint.hpp>
-
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/util/time.hpp>
-
-#include <utility>
-#include <boost/cstdint.hpp>
-#include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/util/time.hpp>
-
 namespace nlsr {
 
 class Nlsr;
@@ -411,6 +399,9 @@
   ndn::time::seconds m_adjLsaBuildInterval;
 
   SequencingManager m_sequencingManager;
+
+  ndn::util::signal::ScopedConnection m_onNewLsaConnection;
+
 };
 
 } // namespace nlsr
diff --git a/src/signals.hpp b/src/signals.hpp
index b8518b0..7da49ec 100644
--- a/src/signals.hpp
+++ b/src/signals.hpp
@@ -22,14 +22,18 @@
 #ifndef NLSR_SIGNALS_HPP
 #define NLSR_SIGNALS_HPP
 
+#include "common.hpp"
 #include <ndn-cxx/util/signal.hpp>
+#include <ndn-cxx/name.hpp>
 
 namespace nlsr {
 
 class RoutingTable;
 class RoutingTableEntry;
+class SyncLogicHandler;
 
 using AfterRoutingChange = ndn::util::Signal<RoutingTable, const std::list<RoutingTableEntry>&>;
+using OnNewLsa = ndn::util::Signal<SyncLogicHandler, const ndn::Name&, const uint64_t&>;
 
 } // namespace nlsr