src: change initialization order

Send Hello Interest after route to neighbor is successfully registered
--> First Hello interval is eliminated
--> After Hello Nack, wait exponentially before processing it as a timeout
Register sync route for each neighbor after its Hello Data is validated

refs: #5009

Change-Id: Ice39a591f1e58e474b494d93c913fa45e10f24f2
diff --git a/src/communication/sync-logic-handler.cpp b/src/communication/sync-logic-handler.cpp
index 1784267..308457e 100644
--- a/src/communication/sync-logic-handler.cpp
+++ b/src/communication/sync-logic-handler.cpp
@@ -33,59 +33,29 @@
 
 INIT_LOGGER(SyncLogicHandler);
 
-template<class T>
-class NullDeleter
-{
-public:
-  void
-  operator()(T*)
-  {
-  }
-};
-
 SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew,
                                    const ConfParameter& conf)
   : onNewLsa(std::make_unique<OnNewLsa>())
   , m_syncFace(face)
   , m_isLsaNew(isLsaNew)
   , m_confParam(conf)
+  , m_nameLsaUserPrefix(ndn::Name(m_confParam.getSyncUserPrefix()).append(std::to_string(Lsa::Type::NAME)))
+  , m_syncLogic(m_syncFace, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
+                m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
+                std::bind(&SyncLogicHandler::processUpdate, this, _1, _2))
 {
-  createSyncLogic(conf.getSyncPrefix());
-}
+  m_adjLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
+                         .append(std::to_string(Lsa::Type::ADJACENCY));
+  m_coorLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
+                         .append(std::to_string(Lsa::Type::COORDINATE));
 
-void
-SyncLogicHandler::createSyncLogic(const ndn::Name& syncPrefix, const ndn::time::milliseconds& syncInterestLifetime)
-{
-  if (m_syncLogic != nullptr) {
-    NLSR_LOG_WARN("Trying to create Sync Logic object, but Sync Logic object already exists");
-    return;
+  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF ||
+      m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
+    m_syncLogic.addUserNode(m_adjLsaUserPrefix);
   }
-
-  // Build LSA sync update prefix
-  buildUpdatePrefix();
-
-  NLSR_LOG_DEBUG("Creating Sync Logic object. Sync Prefix: " << syncPrefix);
-
-  // The face's lifetime is managed in main.cpp; Logic should not manage the memory
-  // of the object
-  std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>());
-
-  m_syncLogic = std::make_shared<SyncProtocolAdapter>(*facePtr,
-                  m_confParam.getSyncProtocol(),
-                  syncPrefix,
-                  m_nameLsaUserPrefix,
-                  syncInterestLifetime,
-                  std::bind(&SyncLogicHandler::processUpdate, this, _1, _2));
-
-  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
-    m_syncLogic->addUserNode(m_adjLsaUserPrefix);
-  }
-  else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
-    m_syncLogic->addUserNode(m_coorLsaUserPrefix);
-  }
-  else {
-    m_syncLogic->addUserNode(m_adjLsaUserPrefix);
-    m_syncLogic->addUserNode(m_coorLsaUserPrefix);
+  else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON ||
+           m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
+    m_syncLogic.addUserNode(m_coorLsaUserPrefix);
   }
 }
 
@@ -148,42 +118,19 @@
 void
 SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
 {
-  if (m_syncLogic == nullptr) {
-    NLSR_LOG_FATAL("Cannot publish routing update; SyncLogic does not exist");
-
-    BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncLogic does not exist"));
-  }
-
   switch (type) {
   case Lsa::Type::ADJACENCY:
-    m_syncLogic->publishUpdate(m_adjLsaUserPrefix, seqNo);
+    m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
     break;
   case Lsa::Type::COORDINATE:
-    m_syncLogic->publishUpdate(m_coorLsaUserPrefix, seqNo);
+    m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
     break;
   case Lsa::Type::NAME:
-    m_syncLogic->publishUpdate(m_nameLsaUserPrefix, seqNo);
+    m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
     break;
   default:
     break;
   }
 }
 
-void
-SyncLogicHandler::buildUpdatePrefix()
-{
-  ndn::Name updatePrefix = m_confParam.getLsaPrefix();
-  updatePrefix.append(m_confParam.getSiteName());
-  updatePrefix.append(m_confParam.getRouterName());
-
-  m_nameLsaUserPrefix = updatePrefix;
-  m_nameLsaUserPrefix.append(std::to_string(Lsa::Type::NAME));
-
-  m_adjLsaUserPrefix = updatePrefix;
-  m_adjLsaUserPrefix.append(std::to_string(Lsa::Type::ADJACENCY));
-
-  m_coorLsaUserPrefix = updatePrefix;
-  m_coorLsaUserPrefix.append(std::to_string(Lsa::Type::COORDINATE));
-}
-
 } // namespace nlsr
diff --git a/src/communication/sync-logic-handler.hpp b/src/communication/sync-logic-handler.hpp
index b9461c2..4dc2d3d 100644
--- a/src/communication/sync-logic-handler.hpp
+++ b/src/communication/sync-logic-handler.hpp
@@ -77,19 +77,6 @@
   publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo);
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  /*! \brief Create and configure a Logic object to enable Sync for this NLSR.
-   *
-   * In a typical situation this only needs to be called once, when NLSR starts.
-   * \param syncPrefix The sync prefix you want this Sync to use
-   * \param syncInterestLifetime ChronoSync/PSync sends a periodic sync interest every
-   *        \p syncInterestLifetime / 2 ms
-   * \sa Nlsr::initialize
-   */
-  void
-  createSyncLogic(const ndn::Name& syncPrefix,
-                  const ndn::time::milliseconds& syncInterestLifetime =
-                    ndn::time::milliseconds(SYNC_INTEREST_LIFETIME_DEFAULT));
-
   /*! \brief Callback from Sync protocol
    *
    * In a typical situation this only needs to be called once, when NLSR starts.
@@ -99,11 +86,6 @@
   void
   processUpdate(const ndn::Name& updateName, uint64_t highSeq);
 
-  /*! \brief Simple function to glue Name components together
-   */
-  void
-  buildUpdatePrefix();
-
   /*! \brief Determine which kind of LSA was updated and fetch it.
    *
    * Checks that the received update is not from us, which can happen,
@@ -121,9 +103,6 @@
 
 private:
   ndn::Face& m_syncFace;
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  std::shared_ptr<SyncProtocolAdapter> m_syncLogic;
-private:
   IsLsaNew m_isLsaNew;
   const ConfParameter& m_confParam;
 
@@ -132,6 +111,8 @@
   ndn::Name m_adjLsaUserPrefix;
   ndn::Name m_coorLsaUserPrefix;
 
+  SyncProtocolAdapter m_syncLogic;
+
 private:
   static const std::string NLSR_COMPONENT;
   static const std::string LSA_COMPONENT;