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/hello-protocol.cpp b/src/hello-protocol.cpp
index c068905..3d67771 100644
--- a/src/hello-protocol.cpp
+++ b/src/hello-protocol.cpp
@@ -42,55 +42,55 @@
   , m_confParam(confParam)
   , m_routingTable(routingTable)
   , m_lsdb(lsdb)
+  , m_adjacencyList(m_confParam.getAdjacencyList())
 {
 }
 
 void
 HelloProtocol::expressInterest(const ndn::Name& interestName, uint32_t seconds)
 {
-  NLSR_LOG_DEBUG("Expressing Interest :" << interestName);
+  NLSR_LOG_DEBUG("Expressing Interest: " << interestName);
   ndn::Interest interest(interestName);
   interest.setInterestLifetime(ndn::time::seconds(seconds));
   interest.setMustBeFresh(true);
   interest.setCanBePrefix(true);
   m_face.expressInterest(interest,
-                         std::bind(&HelloProtocol::onContent, this, _1, _2),
-                         [this] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
-                         {
-                           NDN_LOG_TRACE("Received Nack with reason " << nack.getReason());
-                           NDN_LOG_TRACE("Treating as timeout");
-                           processInterestTimedOut(interest);
-                         },
-                         std::bind(&HelloProtocol::processInterestTimedOut, this, _1));
+    std::bind(&HelloProtocol::onContent, this, _1, _2),
+    [this, seconds] (const ndn::Interest& interest, const ndn::lp::Nack& nack)
+    {
+      NDN_LOG_TRACE("Received Nack with reason: " << nack.getReason());
+      NDN_LOG_TRACE("Will treat as timeout in " << 2 * seconds << " seconds");
+      m_scheduler.schedule(ndn::time::seconds(2 * seconds),
+        [this, interest] { processInterestTimedOut(interest); });
+    },
+    std::bind(&HelloProtocol::processInterestTimedOut, this, _1));
 
   // increment SENT_HELLO_INTEREST
   hpIncrementSignal(Statistics::PacketType::SENT_HELLO_INTEREST);
 }
 
 void
-HelloProtocol::sendScheduledInterest()
+HelloProtocol::sendHelloInterest(const ndn::Name& neighbor)
 {
-  for (const auto& adjacent : m_confParam.getAdjacencyList().getAdjList()) {
-    // If this adjacency has a Face, just proceed as usual.
-    if(adjacent.getFaceId() != 0) {
-      // interest name: /<neighbor>/NLSR/INFO/<router>
-      ndn::Name interestName = adjacent.getName() ;
-      interestName.append(NLSR_COMPONENT);
-      interestName.append(INFO_COMPONENT);
-      interestName.append(m_confParam.getRouterPrefix().wireEncode());
-      expressInterest(interestName, m_confParam.getInterestResendTime());
-      NLSR_LOG_DEBUG("Sending scheduled interest: " << interestName);
-    }
+  auto adjacent = m_adjacencyList.findAdjacent(neighbor);
+
+  if (adjacent == m_adjacencyList.end()) {
+    return;
   }
-  scheduleInterest(m_confParam.getInfoInterestInterval());
-}
 
-void
-HelloProtocol::scheduleInterest(uint32_t seconds)
-{
-  NLSR_LOG_DEBUG("Scheduling HELLO Interests in " << ndn::time::seconds(seconds));
+  // If this adjacency has a Face, just proceed as usual.
+  if(adjacent->getFaceId() != 0) {
+    // interest name: /<neighbor>/NLSR/INFO/<router>
+    ndn::Name interestName = adjacent->getName() ;
+    interestName.append(NLSR_COMPONENT);
+    interestName.append(INFO_COMPONENT);
+    interestName.append(m_confParam.getRouterPrefix().wireEncode());
+    expressInterest(interestName, m_confParam.getInterestResendTime());
+    NLSR_LOG_DEBUG("Sending HELLO interest: " << interestName);
+  }
 
-  m_scheduler.schedule(ndn::time::seconds(seconds), [this] { sendScheduledInterest(); });
+  m_scheduler.schedule(ndn::time::seconds(m_confParam.getInfoInterestInterval()),
+                       [this, neighbor] { sendHelloInterest(neighbor); });
 }
 
 void
@@ -113,7 +113,7 @@
   ndn::Name neighbor;
   neighbor.wireDecode(interestName.get(-1).blockFromValue());
   NLSR_LOG_DEBUG("Neighbor: " << neighbor);
-  if (m_confParam.getAdjacencyList().isNeighbor(neighbor)) {
+  if (m_adjacencyList.isNeighbor(neighbor)) {
     std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>();
     data->setName(ndn::Name(interest.getName()).appendVersion());
     data->setFreshnessPeriod(ndn::time::seconds(10)); // 10 sec
@@ -128,7 +128,7 @@
     // increment SENT_HELLO_DATA
     hpIncrementSignal(Statistics::PacketType::SENT_HELLO_DATA);
 
-    auto adjacent = m_confParam.getAdjacencyList().findAdjacent(neighbor);
+    auto adjacent = m_adjacencyList.findAdjacent(neighbor);
     // If this neighbor was previously inactive, send our own hello interest, too
     if (adjacent->getStatus() == Adjacent::STATUS_INACTIVE) {
       // We can only do that if the neighbor currently has a face.
@@ -155,15 +155,14 @@
   }
   ndn::Name neighbor = interestName.getPrefix(-3);
   NLSR_LOG_DEBUG("Neighbor: " << neighbor);
-  m_confParam.getAdjacencyList().incrementTimedOutInterestCount(neighbor);
+  m_adjacencyList.incrementTimedOutInterestCount(neighbor);
 
-  Adjacent::Status status = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
+  Adjacent::Status status = m_adjacencyList.getStatusOfNeighbor(neighbor);
 
-  uint32_t infoIntTimedOutCount =
-    m_confParam.getAdjacencyList().getTimedOutInterestCount(neighbor);
+  uint32_t infoIntTimedOutCount = m_adjacencyList.getTimedOutInterestCount(neighbor);
   NLSR_LOG_DEBUG("Status: " << status);
   NLSR_LOG_DEBUG("Info Interest Timed out: " << infoIntTimedOutCount);
-  if (infoIntTimedOutCount < m_confParam.getInterestRetryNumber()) {
+  if (infoIntTimedOutCount <= m_confParam.getInterestRetryNumber()) {
     // interest name: /<neighbor>/NLSR/INFO/<router>
     ndn::Name interestName(neighbor);
     interestName.append(NLSR_COMPONENT);
@@ -174,7 +173,7 @@
   }
   else if ((status == Adjacent::STATUS_ACTIVE) &&
            (infoIntTimedOutCount == m_confParam.getInterestRetryNumber())) {
-    m_confParam.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
+    m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_INACTIVE);
 
     NLSR_LOG_DEBUG("Neighbor: " << neighbor << " status changed to INACTIVE");
 
@@ -189,10 +188,9 @@
 HelloProtocol::onContent(const ndn::Interest& interest, const ndn::Data& data)
 {
   NLSR_LOG_DEBUG("Received data for INFO(name): " << data.getName());
-  if (data.getSignature().hasKeyLocator()) {
-    if (data.getSignature().getKeyLocator().getType() == ndn::KeyLocator::KeyLocator_Name) {
-      NLSR_LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
-    }
+  if (data.getSignature().hasKeyLocator() &&
+      data.getSignature().getKeyLocator().getType() == ndn::tlv::Name) {
+    NLSR_LOG_DEBUG("Data signed with: " << data.getSignature().getKeyLocator().getName());
   }
   m_confParam.getValidator().validate(data,
                                       std::bind(&HelloProtocol::onContentValidated, this, _1),
@@ -210,10 +208,10 @@
   if (dataName.get(-3).toUri() == INFO_COMPONENT) {
     ndn::Name neighbor = dataName.getPrefix(-4);
 
-    Adjacent::Status oldStatus = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
-    m_confParam.getAdjacencyList().setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
-    m_confParam.getAdjacencyList().setTimedOutInterestCount(neighbor, 0);
-    Adjacent::Status newStatus = m_confParam.getAdjacencyList().getStatusOfNeighbor(neighbor);
+    Adjacent::Status oldStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
+    m_adjacencyList.setStatusOfNeighbor(neighbor, Adjacent::STATUS_ACTIVE);
+    m_adjacencyList.setTimedOutInterestCount(neighbor, 0);
+    Adjacent::Status newStatus = m_adjacencyList.getStatusOfNeighbor(neighbor);
 
     NLSR_LOG_DEBUG("Neighbor : " << neighbor);
     NLSR_LOG_DEBUG("Old Status: " << oldStatus << " New Status: " << newStatus);
@@ -226,6 +224,8 @@
         m_lsdb.scheduleAdjLsaBuild();
       }
     }
+
+    onHelloDataValidated(neighbor);
   }
   // increment RCV_HELLO_DATA
   hpIncrementSignal(Statistics::PacketType::RCV_HELLO_DATA);