src: Fix first event-interval scheduling

refs: #2615, #2593

Change-Id: I17da4d9acb83299a5666159fb988c1a97220eda8
diff --git a/src/hello-protocol.cpp b/src/hello-protocol.cpp
index 1af254e..09f05b6 100644
--- a/src/hello-protocol.cpp
+++ b/src/hello-protocol.cpp
@@ -74,6 +74,8 @@
 void
 HelloProtocol::scheduleInterest(uint32_t seconds)
 {
+  _LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
+
   m_scheduler.scheduleEvent(ndn::time::seconds(seconds),
                             ndn::bind(&HelloProtocol::sendScheduledInterest, this, seconds));
 }
@@ -150,14 +152,8 @@
   else if ((status == Adjacent::STATUS_ACTIVE) &&
            (infoIntTimedOutCount == m_nlsr.getConfParameter().getInterestRetryNumber())) {
     m_nlsr.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
-    m_nlsr.incrementAdjBuildCount();
-    if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
-      _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
-      m_nlsr.setIsBuildAdjLsaSheduled(true);
-      // event here
-      m_scheduler.scheduleEvent(m_adjLsaBuildInterval,
-                                ndn::bind(&Lsdb::scheduledAdjLsaBuild, &m_nlsr.getLsdb()));
-    }
+
+    m_nlsr.getLsdb().scheduleAdjLsaBuild();
   }
 }
 
@@ -194,15 +190,7 @@
     _LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
     // change in Adjacency list
     if ((oldStatus - newStatus) != 0) {
-      m_nlsr.incrementAdjBuildCount();
-      // Need to schedule event for Adjacency LSA building
-      if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
-        _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
-        m_nlsr.setIsBuildAdjLsaSheduled(true);
-        // event here
-        m_scheduler.scheduleEvent(m_adjLsaBuildInterval,
-                                  ndn::bind(&Lsdb::scheduledAdjLsaBuild, &m_nlsr.getLsdb()));
-      }
+      m_nlsr.getLsdb().scheduleAdjLsaBuild();
     }
   }
 }
@@ -280,14 +268,8 @@
       if (status == Adjacent::STATUS_ACTIVE) {
         adjacent->setStatus(Adjacent::STATUS_INACTIVE);
       }
-      m_nlsr.incrementAdjBuildCount();
-      if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
-        _LOG_DEBUG("Scheduling scheduledAdjLsaBuild");
-        m_nlsr.setIsBuildAdjLsaSheduled(true);
-        // event here
-        m_scheduler.scheduleEvent(m_adjLsaBuildInterval,
-                                  ndn::bind(&Lsdb::scheduledAdjLsaBuild, &m_nlsr.getLsdb()));
-      }
+
+      m_nlsr.getLsdb().scheduleAdjLsaBuild();
     }
   }
 }
diff --git a/src/hello-protocol.hpp b/src/hello-protocol.hpp
index 25868bd..a307a94 100644
--- a/src/hello-protocol.hpp
+++ b/src/hello-protocol.hpp
@@ -37,7 +37,6 @@
   HelloProtocol(Nlsr& nlsr, ndn::Scheduler& scheduler)
     : m_nlsr(nlsr)
     , m_scheduler(scheduler)
-    , m_adjLsaBuildInterval(static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT))
   {
   }
 
@@ -53,18 +52,6 @@
   void
   processInterest(const ndn::Name& name, const ndn::Interest& interest);
 
-  void
-  setAdjLsaBuildInterval(uint32_t interval)
-  {
-    m_adjLsaBuildInterval = ndn::time::seconds(interval);
-  }
-
-  const ndn::time::seconds&
-  getAdjLsaBuildInterval() const
-  {
-    return m_adjLsaBuildInterval;
-  }
-
 private:
   void
   processInterestTimedOut(const ndn::Interest& interest);
@@ -96,8 +83,6 @@
 
   static const std::string INFO_COMPONENT;
   static const std::string NLSR_COMPONENT;
-
-  ndn::time::seconds m_adjLsaBuildInterval;
 };
 
 } //namespace nlsr
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 79fbd82..8c41c9f 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -429,9 +429,23 @@
 }
 
 void
-Lsdb::scheduledAdjLsaBuild()
+Lsdb::scheduleAdjLsaBuild()
 {
-  _LOG_DEBUG("scheduledAdjLsaBuild Called");
+  m_nlsr.incrementAdjBuildCount();
+
+  if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
+    _LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
+
+    m_scheduler.scheduleEvent(m_adjLsaBuildInterval, ndn::bind(&Lsdb::buildAdjLsa, this));
+    m_nlsr.setIsBuildAdjLsaSheduled(true);
+  }
+}
+
+void
+Lsdb::buildAdjLsa()
+{
+  _LOG_TRACE("buildAdjLsa called");
+
   m_nlsr.setIsBuildAdjLsaSheduled(false);
   if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) {
     int adjBuildCount = m_nlsr.getAdjBuildCount();
@@ -454,7 +468,7 @@
     int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
                          m_nlsr.getConfParameter().getInterestResendTime();
     m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
-                              ndn::bind(&Lsdb::scheduledAdjLsaBuild, this));
+                              ndn::bind(&Lsdb::buildAdjLsa, this));
   }
 }
 
diff --git a/src/lsdb.hpp b/src/lsdb.hpp
index da41a9d..34d7562 100644
--- a/src/lsdb.hpp
+++ b/src/lsdb.hpp
@@ -27,6 +27,7 @@
 #include <ndn-cxx/security/key-chain.hpp>
 #include <ndn-cxx/util/time.hpp>
 
+#include "conf-parameter.hpp"
 #include "lsa.hpp"
 #include "test-access-control.hpp"
 
@@ -46,6 +47,7 @@
     , m_sync(sync)
     , m_hasSyncPrefixBeenRegistered(false)
     , m_lsaRefreshTime(0)
+    , m_adjLsaBuildInterval(static_cast<uint32_t>(ADJ_LSA_BUILD_INTERVAL_DEFAULT))
   {
   }
 
@@ -91,8 +93,9 @@
   writeCorLsdbLog();
 
   //function related to Adj LSDB
+
   void
-  scheduledAdjLsaBuild();
+  scheduleAdjLsaBuild();
 
   bool
   buildAndInstallOwnAdjLsa();
@@ -112,6 +115,18 @@
   getAdjLsdb();
 
   void
+  setAdjLsaBuildInterval(uint32_t interval)
+  {
+    m_adjLsaBuildInterval = ndn::time::seconds(interval);
+  }
+
+  const ndn::time::seconds&
+  getAdjLsaBuildInterval() const
+  {
+    return m_adjLsaBuildInterval;
+  }
+
+  void
   writeAdjLsdbLog();
 
   void
@@ -134,6 +149,9 @@
   bool
   doesCoordinateLsaExist(const ndn::Name& key);
 
+  void
+  buildAdjLsa();
+
   bool
   addAdjLsa(AdjLsa& alsa);
 
@@ -261,6 +279,8 @@
 
   static const ndn::time::seconds GRACE_PERIOD;
   static const steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
+
+  ndn::time::seconds m_adjLsaBuildInterval;
 };
 
 }//namespace nlsr
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index 07eb43e..eb06be0 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -140,16 +140,17 @@
   _LOG_DEBUG("Default NLSR identity: " << m_defaultIdentity);
   setInfoInterestFilter();
   setLsaInterestFilter();
+
+  // Set event intervals
+  setFirstHelloInterval(m_confParam.getFirstHelloInterval());
+  m_nlsrLsdb.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
+  m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
+
   m_nlsrLsdb.buildAndInstallOwnNameLsa();
   m_nlsrLsdb.buildAndInstallOwnCoordinateLsa();
 
   registerKeyPrefix();
 
-  // Set event intervals
-  setFirstHelloInterval(m_confParam.getFirstHelloInterval());
-  m_helloProtocol.setAdjLsaBuildInterval(m_confParam.getAdjLsaBuildInterval());
-  m_routingTable.setRoutingCalcInterval(m_confParam.getRoutingCalcInterval());
-
   m_helloProtocol.scheduleInterest(m_firstHelloInterval);
 
   // Need to set direct neighbors' costs to 0 for hyperbolic routing
diff --git a/src/nlsr.hpp b/src/nlsr.hpp
index 639487f..7e73ebc 100644
--- a/src/nlsr.hpp
+++ b/src/nlsr.hpp
@@ -366,8 +366,6 @@
   Fib m_fib;
   NamePrefixTable m_namePrefixTable;
   SyncLogicHandler m_syncLogicHandler;
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   HelloProtocol m_helloProtocol;
 
 private:
diff --git a/src/route/routing-table.cpp b/src/route/routing-table.cpp
index 371d977..343767a 100644
--- a/src/route/routing-table.cpp
+++ b/src/route/routing-table.cpp
@@ -53,10 +53,13 @@
           pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency",
           std::string("adjacency"))) {
       if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
-        _LOG_DEBUG("CLearing old routing table .....");
+        _LOG_TRACE("Clearing old routing table");
         clearRoutingTable();
         // for dry run options
         clearDryRoutingTable();
+
+        _LOG_DEBUG("Calculating routing table");
+
         // calculate Link State routing
         if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
             || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
@@ -156,6 +159,8 @@
 RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
 {
   if (pnlsr.getIsRouteCalculationScheduled() != true) {
+    _LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
+
     m_scheduler.scheduleEvent(m_routingCalcInterval,
                               ndn::bind(&RoutingTable::calculate, this, ndn::ref(pnlsr)));
 
diff --git a/tests/test-nlsr.cpp b/tests/test-nlsr.cpp
index 01480d9..ec53820 100644
--- a/tests/test-nlsr.cpp
+++ b/tests/test-nlsr.cpp
@@ -98,10 +98,10 @@
 
   nlsr.initialize();
 
-  const HelloProtocol& hello = nlsr.m_helloProtocol;
+  const Lsdb& lsdb = nlsr.getLsdb();
   const RoutingTable& rt = nlsr.getRoutingTable();
 
-  BOOST_CHECK_EQUAL(hello.getAdjLsaBuildInterval(), ndn::time::seconds(3));
+  BOOST_CHECK_EQUAL(lsdb.getAdjLsaBuildInterval(), ndn::time::seconds(3));
   BOOST_CHECK_EQUAL(nlsr.getFirstHelloInterval(), 6);
   BOOST_CHECK_EQUAL(rt.getRoutingCalcInterval(), ndn::time::seconds(9));
 }