Make LSA buildable when one node is ACTIVE

refs: #2802

Change-Id: If3f99895522e22103130f32bb6634bf9a5691fe7
diff --git a/src/adjacency-list.cpp b/src/adjacency-list.cpp
index 3e535ee..88f50bd 100644
--- a/src/adjacency-list.cpp
+++ b/src/adjacency-list.cpp
@@ -1,7 +1,8 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  University of Memphis,
- *                     Regents of the University of California
+ * Copyright (c) 2014-2015,  The University of Memphis,
+ *                           Regents of the University of California,
+ *                           Arizona Board of Regents.
  *
  * This file is part of NLSR (Named-data Link State Routing).
  * See AUTHORS.md for complete list of NLSR authors and contributors.
@@ -16,19 +17,16 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- *
- * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
- *
  **/
-#include <algorithm>
-#include <ndn-cxx/common.hpp>
 
 #include "adjacency-list.hpp"
+
 #include "adjacent.hpp"
 #include "common.hpp"
-#include "nlsr.hpp"
 #include "logger.hpp"
 
+#include <algorithm>
+
 namespace nlsr {
 
 INIT_LOGGER("AdjacencyList");
@@ -197,25 +195,26 @@
 }
 
 bool
-AdjacencyList::isAdjLsaBuildable(Nlsr& pnlsr)
+AdjacencyList::isAdjLsaBuildable(const uint32_t interestRetryNo) const
 {
-  uint32_t nbrCount = 0;
-  for (std::list<Adjacent>::iterator it = m_adjList.begin(); it != m_adjList.end() ; it++) {
+  uint32_t nTimedOutNeighbors = 0;
 
-    if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
-      nbrCount++;
+  for (const Adjacent& adjacency : m_adjList) {
+
+    if (adjacency.getStatus() == Adjacent::STATUS_ACTIVE) {
+      return true;
     }
-    else {
-      if ((*it).getInterestTimedOutNo() >=
-          pnlsr.getConfParameter().getInterestRetryNumber()) {
-        nbrCount++;
-      }
+    else if (adjacency.getInterestTimedOutNo() >= interestRetryNo) {
+      nTimedOutNeighbors++;
     }
   }
-  if (nbrCount == m_adjList.size()) {
+
+  if (nTimedOutNeighbors == m_adjList.size()) {
     return true;
   }
-  return false;
+  else {
+    return false;
+  }
 }
 
 int32_t
diff --git a/src/adjacency-list.hpp b/src/adjacency-list.hpp
index 2325f49..259bf61 100644
--- a/src/adjacency-list.hpp
+++ b/src/adjacency-list.hpp
@@ -29,7 +29,6 @@
 #include "adjacent.hpp"
 
 namespace nlsr {
-class Nlsr;
 
 class AdjacencyList
 {
@@ -74,7 +73,7 @@
   addAdjacents(AdjacencyList& adl);
 
   bool
-  isAdjLsaBuildable(Nlsr& pnlsr);
+  isAdjLsaBuildable(const uint32_t interestRetryNo) const;
 
   int32_t
   getNumOfActiveNeighbor();
diff --git a/src/hello-protocol.hpp b/src/hello-protocol.hpp
index bc5267d..3fd4c03 100644
--- a/src/hello-protocol.hpp
+++ b/src/hello-protocol.hpp
@@ -22,7 +22,8 @@
 #ifndef NLSR_HELLO_PROTOCOL_HPP
 #define NLSR_HELLO_PROTOCOL_HPP
 
-#include <boost/cstdint.hpp>
+#include "test-access-control.hpp"
+
 #include <ndn-cxx/face.hpp>
 #include <ndn-cxx/management/nfd-control-parameters.hpp>
 #include <ndn-cxx/util/scheduler.hpp>
@@ -59,9 +60,11 @@
   void
   onContent(const ndn::Interest& interest, const ndn::Data& data);
 
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   void
   onContentValidated(const ndn::shared_ptr<const ndn::Data>& data);
 
+private:
   void
   onContentValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
                             const std::string& msg);
diff --git a/src/lsa.hpp b/src/lsa.hpp
index c797815..e149a6e 100644
--- a/src/lsa.hpp
+++ b/src/lsa.hpp
@@ -32,6 +32,8 @@
 
 namespace nlsr {
 
+class Nlsr;
+
 class Lsa
 {
 public:
diff --git a/src/lsdb.cpp b/src/lsdb.cpp
index 5b1fda6..1486c2c 100644
--- a/src/lsdb.cpp
+++ b/src/lsdb.cpp
@@ -557,32 +557,37 @@
 void
 Lsdb::buildAdjLsa()
 {
-  _LOG_TRACE("buildAdjLsa called");
+  _LOG_TRACE("Lsdb::buildAdjLsa called");
 
   m_nlsr.setIsBuildAdjLsaSheduled(false);
-  if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr)) {
+
+  if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
+
     int adjBuildCount = m_nlsr.getAdjBuildCount();
+
+    // Is the adjacency LSA build still necessary, or has another build
+    // fulfilled this request?
     if (adjBuildCount > 0) {
       if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
-        _LOG_DEBUG("Building and installing Adj LSA");
+        _LOG_DEBUG("Building and installing own Adj LSA");
         buildAndInstallOwnAdjLsa();
       }
       else {
+        _LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors")
         ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
         key.append(AdjLsa::TYPE_STRING);
+
         removeAdjLsa(key);
+
         m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
       }
+
+      // Since more adjacency LSA builds may have been scheduled while this build
+      // was in progress, decrease the build count by the number of scheduled
+      // builds at the beginning of this build.
       m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
     }
   }
-  else {
-    m_nlsr.setIsBuildAdjLsaSheduled(true);
-    int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
-                         m_nlsr.getConfParameter().getInterestResendTime();
-    m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
-                              ndn::bind(&Lsdb::buildAdjLsa, this));
-  }
 }
 
 
diff --git a/src/nlsr.cpp b/src/nlsr.cpp
index 6753a13..2f098fe 100644
--- a/src/nlsr.cpp
+++ b/src/nlsr.cpp
@@ -57,10 +57,10 @@
   , m_fib(m_nlsrFace, scheduler, m_adjacencyList, m_confParam, m_keyChain)
   , m_namePrefixTable(*this)
   , m_syncLogicHandler(m_nlsrFace, m_nlsrLsdb, m_confParam, m_sequencingManager)
-  , m_helloProtocol(*this, scheduler)
   , m_lsdbDatasetHandler(m_nlsrLsdb,
                          m_nlsrFace,
                          m_keyChain)
+  , m_helloProtocol(*this, scheduler)
   , m_certificateCache(new ndn::CertificateCacheTtl(ioService))
   , m_validator(m_nlsrFace, DEFAULT_BROADCAST_PREFIX, m_certificateCache, m_certStore)
   , m_prefixUpdateProcessor(m_nlsrFace,
diff --git a/src/nlsr.hpp b/src/nlsr.hpp
index 4e9cc23..f53ba4a 100644
--- a/src/nlsr.hpp
+++ b/src/nlsr.hpp
@@ -372,9 +372,11 @@
   Fib m_fib;
   NamePrefixTable m_namePrefixTable;
   SyncLogicHandler m_syncLogicHandler;
-  HelloProtocol m_helloProtocol;
   LsdbDatasetInterestHandler m_lsdbDatasetHandler;
 
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  HelloProtocol m_helloProtocol;
+
 private:
   ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
   security::CertificateStore m_certStore;