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/route/face-map.cpp b/src/route/face-map.cpp
deleted file mode 100644
index 52026d0..0000000
--- a/src/route/face-map.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2018,  The University of Memphis,
- *                           Regents of the University of California
- *
- * This file is part of NLSR (Named-data Link State Routing).
- * See AUTHORS.md for complete list of NLSR authors and contributors.
- *
- * NLSR is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * 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 "face-map.hpp"
-#include "common.hpp"
-#include "logger.hpp"
-
-#include <iostream>
-#include <utility>
-
-namespace nlsr {
-
-INIT_LOGGER(route.FaceMap);
-
-void
-FaceMap::writeLog()
-{
-  NLSR_LOG_DEBUG("------- Face Map-----------");
-  for (const auto& it : m_table) {
-    NLSR_LOG_DEBUG("Face Map Entry (FaceUri: " << (it.second).getFaceUri()
-               << " Face Id: " << (it.second).getFaceId() << ")");
-  }
-}
-
-} // namespace nlsr
diff --git a/src/route/face-map.hpp b/src/route/face-map.hpp
deleted file mode 100644
index 9505229..0000000
--- a/src/route/face-map.hpp
+++ /dev/null
@@ -1,121 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017,  The University of Memphis,
- *                           Regents of the University of California
- *
- * This file is part of NLSR (Named-data Link State Routing).
- * See AUTHORS.md for complete list of NLSR authors and contributors.
- *
- * NLSR is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE.  See the GNU General Public License for more details.
- *
- * 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/>.
- *
- **/
-
-#ifndef NLSR_FACE_MAP_HPP
-#define NLSR_FACE_MAP_HPP
-
-#include "common.hpp"
-
-#include <map>
-
-namespace nlsr {
-
-class FaceMapEntry
-{
-
-public:
-  FaceMapEntry(const std::string& faceUri, uint32_t faceId)
-    : m_faceUri(faceUri)
-    , m_faceId(faceId)
-  {
-  }
-
-  void
-  setFaceUri(const std::string& faceUri)
-  {
-    m_faceUri = faceUri;
-  }
-
-  const std::string&
-  getFaceUri() const
-  {
-    return m_faceUri;
-  }
-
-  void
-  setFaceId(uint32_t faceId)
-  {
-    m_faceId = faceId;
-  }
-
-  uint32_t
-  getFaceId() const
-  {
-    return m_faceId;
-  }
-
-  bool
-  compare(const FaceMapEntry& fme)
-  {
-    return m_faceUri == fme.getFaceUri();
-  }
-
-private:
-  std::string m_faceUri;
-  uint32_t m_faceId;
-};
-
-class FaceMap
-{
-
-public:
-  FaceMap()
-  {
-  }
-
-  ~FaceMap()
-  {
-  }
-
-  inline void
-  update(const std::string& faceUri, uint32_t faceId)
-  {
-    FaceMapEntry fme(faceUri, faceId);
-    std::map<std::string, FaceMapEntry>::iterator it = m_table.find(faceUri);
-    if (it == m_table.end()) {
-      m_table.emplace(faceUri, fme);
-    }
-    else {
-      (it->second).setFaceId(fme.getFaceId());
-    }
-  }
-
-  inline uint32_t
-  getFaceId(const std::string& faceUri)
-  {
-    FaceMapEntry fme(faceUri, 0);
-    std::map<std::string, FaceMapEntry>::iterator it = m_table.find(faceUri);
-    if (it != m_table.end()) {
-      return (it->second).getFaceId();
-    }
-    return 0;
-  }
-
-  void
-  writeLog();
-
-private:
-  std::map<std::string, FaceMapEntry> m_table;
-};
-
-} // namespace nlsr
-
-#endif // NLSR_FACE_MAP_HPP
diff --git a/src/route/fib.cpp b/src/route/fib.cpp
index 9ee14c7..cd2b28c 100644
--- a/src/route/fib.cpp
+++ b/src/route/fib.cpp
@@ -202,9 +202,9 @@
                     uint64_t faceCost, const ndn::time::milliseconds& timeout,
                     uint64_t flags, uint8_t times)
 {
-  uint64_t faceId = m_adjacencyList.getFaceId(ndn::FaceUri(faceUri));
+  uint64_t faceId = m_adjacencyList.getFaceId(faceUri);
 
-  if (faceId != 0) {
+  if (faceId > 0) {
     ndn::nfd::ControlParameters faceParameters;
     faceParameters
      .setName(namePrefix)
@@ -216,13 +216,9 @@
 
     NLSR_LOG_DEBUG("Registering prefix: " << faceParameters.getName() << " faceUri: " << faceUri);
     m_controller.start<ndn::nfd::RibRegisterCommand>(faceParameters,
-                                                     std::bind(&Fib::onRegistrationSuccess, this, _1,
-                                                               "Successful in name registration",
-                                                               faceUri),
-                                                     std::bind(&Fib::onRegistrationFailure, this, _1,
-                                                               "Failed in name registration",
-                                                               faceParameters,
-                                                               faceUri, times));
+      std::bind(&Fib::onRegistrationSuccess, this, _1, faceUri),
+      std::bind(&Fib::onRegistrationFailure, this, _1,
+                faceParameters, faceUri, times));
   }
   else {
     NLSR_LOG_WARN("Error: No Face Id for face uri: " << faceUri);
@@ -230,31 +226,28 @@
 }
 
 void
-Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
-                           const std::string& message, const ndn::FaceUri& faceUri)
+Fib::onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
+                           const ndn::FaceUri& faceUri)
 {
-  NLSR_LOG_DEBUG(message << ": " << commandSuccessResult.getName() <<
-                 " Face Uri: " << faceUri << " faceId: " << commandSuccessResult.getFaceId());
+  NLSR_LOG_DEBUG("Successful in name registration: " << param.getName() <<
+                 " Face Uri: " << faceUri << " faceId: " << param.getFaceId());
 
   auto adjacent = m_adjacencyList.findAdjacent(faceUri);
   if (adjacent != m_adjacencyList.end()) {
-    adjacent->setFaceId(commandSuccessResult.getFaceId());
+    adjacent->setFaceId(param.getFaceId());
   }
-
-  // Update the fast-access FaceMap with the new Face ID, too
-  m_faceMap.update(faceUri.toString(), commandSuccessResult.getFaceId());
-  m_faceMap.writeLog();
+  onPrefixRegistrationSuccess(param.getName());
 }
 
 void
 Fib::onRegistrationFailure(const ndn::nfd::ControlResponse& response,
-                           const std::string& message,
                            const ndn::nfd::ControlParameters& parameters,
                            const ndn::FaceUri& faceUri,
                            uint8_t times)
 {
-  NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
-  NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << times);
+  NLSR_LOG_DEBUG("Failed in name registration: " << response.getText() <<
+                 " (code: " << response.getCode() << ")");
+  NLSR_LOG_DEBUG("Prefix: " << parameters.getName() << " failed for: " << +times);
   if (times < 3) {
     NLSR_LOG_DEBUG("Trying to register again...");
     registerPrefix(parameters.getName(), faceUri,
@@ -270,7 +263,12 @@
 void
 Fib::unregisterPrefix(const ndn::Name& namePrefix, const std::string& faceUri)
 {
-  uint32_t faceId = m_faceMap.getFaceId(faceUri);
+  uint64_t faceId = 0;
+  auto adjacent = m_adjacencyList.findAdjacent(ndn::FaceUri(faceUri));
+  if (adjacent != m_adjacencyList.end()) {
+    faceId = adjacent->getFaceId();
+  }
+
   NLSR_LOG_DEBUG("Unregister prefix: " << namePrefix << " Face Uri: " << faceUri);
   if (faceId > 0) {
     ndn::nfd::ControlParameters controlParameters;
@@ -278,31 +276,20 @@
       .setName(namePrefix)
       .setFaceId(faceId)
       .setOrigin(ndn::nfd::ROUTE_ORIGIN_NLSR);
+
     m_controller.start<ndn::nfd::RibUnregisterCommand>(controlParameters,
-                                                       std::bind(&Fib::onUnregistrationSuccess, this, _1,
-                                                                 "Successful in unregistering name"),
-                                                       std::bind(&Fib::onUnregistrationFailure,
-                                                                 this, _1,
-                                                                 "Failed in unregistering name"));
+      [] (const ndn::nfd::ControlParameters& commandSuccessResult) {
+        NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
+                       " Face Id: " << commandSuccessResult.getFaceId());
+      },
+      [] (const ndn::nfd::ControlResponse& response) {
+        NLSR_LOG_DEBUG("Failed in unregistering name" << ": " << response.getText() <<
+                 " (code: " << response.getCode() << ")");
+      });
   }
 }
 
 void
-Fib::onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
-                             const std::string& message)
-{
-  NLSR_LOG_DEBUG("Unregister successful Prefix: " << commandSuccessResult.getName() <<
-                 " Face Id: " << commandSuccessResult.getFaceId());
-}
-
-void
-Fib::onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
-                             const std::string& message)
-{
-  NLSR_LOG_DEBUG(message << ": " << response.getText() << " (code: " << response.getCode() << ")");
-}
-
-void
 Fib::setStrategy(const ndn::Name& name, const std::string& strategy, uint32_t count)
 {
   ndn::nfd::ControlParameters parameters;
diff --git a/src/route/fib.hpp b/src/route/fib.hpp
index f2d952a..25677e3 100644
--- a/src/route/fib.hpp
+++ b/src/route/fib.hpp
@@ -22,7 +22,6 @@
 #ifndef NLSR_ROUTE_FIB_HPP
 #define NLSR_ROUTE_FIB_HPP
 
-#include "face-map.hpp"
 #include "fib-entry.hpp"
 #include "test-access-control.hpp"
 
@@ -164,30 +163,17 @@
   /*! \brief Log registration success, and update the Face ID associated with a URI.
    */
   void
-  onRegistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
-                        const std::string& message, const ndn::FaceUri& faceUri);
+  onRegistrationSuccess(const ndn::nfd::ControlParameters& param,
+                        const ndn::FaceUri& faceUri);
 
   /*! \brief Retry a prefix (next-hop) registration up to three (3) times.
    */
   void
   onRegistrationFailure(const ndn::nfd::ControlResponse& response,
-                        const std::string& message,
                         const ndn::nfd::ControlParameters& parameters,
                         const ndn::FaceUri& faceUri,
                         uint8_t times);
 
-  /*! \brief Log a successful unregistration.
-   */
-  void
-  onUnregistrationSuccess(const ndn::nfd::ControlParameters& commandSuccessResult,
-                          const std::string& message);
-
-  /*! \brief Log an unregistration failure. Does not retry.
-   */
-  void
-  onUnregistrationFailure(const ndn::nfd::ControlResponse& response,
-                               const std::string& message);
-
   /*! \brief Log a successful strategy setting.
    */
   void
@@ -240,6 +226,7 @@
 public:
   static const std::string MULTICAST_STRATEGY;
   static const std::string BEST_ROUTE_V2_STRATEGY;
+  ndn::util::Signal<Fib, const ndn::Name&> onPrefixRegistrationSuccess;
 
 private:
   ndn::Scheduler& m_scheduler;
@@ -247,7 +234,6 @@
   ndn::nfd::Controller m_controller;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  FaceMap m_faceMap;
   std::map<ndn::Name, FibEntry> m_table;
 
 private:
diff --git a/src/route/routing-table-calculator.cpp b/src/route/routing-table-calculator.cpp
index a079933..eb821e8 100644
--- a/src/route/routing-table-calculator.cpp
+++ b/src/route/routing-table-calculator.cpp
@@ -142,8 +142,13 @@
   for (size_t i = 0; i < m_nRouters; i++) {
     std::string line;
     for (size_t j = 0; j < m_nRouters; j++) {
-      line += boost::lexical_cast<std::string>(adjMatrix[i][j]);
-      line += " ";
+      if (adjMatrix[i][j] == LinkStateRoutingTableCalculator::NO_NEXT_HOP) {
+        line += "0 ";
+      }
+      else {
+        line += boost::lexical_cast<std::string>(adjMatrix[i][j]);
+        line += " ";
+      }
     }
     line = boost::lexical_cast<std::string>(i) + "|" + line;
     NLSR_LOG_DEBUG(line);
diff --git a/src/route/routing-table-calculator.hpp b/src/route/routing-table-calculator.hpp
index 0a40a1c..683eb58 100644
--- a/src/route/routing-table-calculator.hpp
+++ b/src/route/routing-table-calculator.hpp
@@ -200,6 +200,7 @@
   static const int EMPTY_PARENT;
   static const double INF_DISTANCE;
   static const int NO_MAPPING_NUM;
+public:
   static const int NO_NEXT_HOP;
 
 };
diff --git a/src/route/routing-table-entry.hpp b/src/route/routing-table-entry.hpp
index 6e01c45..fd98c32 100644
--- a/src/route/routing-table-entry.hpp
+++ b/src/route/routing-table-entry.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2017,  The University of Memphis,
+ * Copyright (c) 2014-2019,  The University of Memphis,
  *                           Regents of the University of California
  *
  * This file is part of NLSR (Named-data Link State Routing).
@@ -66,9 +66,8 @@
   inline bool
   operator==(RoutingTableEntry& rhs)
   {
-    return ((*this).getDestination() == rhs.getDestination()
-            &&
-           (*this).getNexthopList() == rhs.getNexthopList());
+    return ((*this).getDestination() == rhs.getDestination() &&
+            (*this).getNexthopList() == rhs.getNexthopList());
   }
 
 protected:
diff --git a/src/route/routing-table-pool-entry.cpp b/src/route/routing-table-pool-entry.cpp
index 7e081ae..9102ed0 100644
--- a/src/route/routing-table-pool-entry.cpp
+++ b/src/route/routing-table-pool-entry.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2017,  The University of Memphis,
+ * Copyright (c) 2014-2019,  The University of Memphis,
  *                           Regents of the University of California
  *
  * This file is part of NLSR (Named-data Link State Routing).
@@ -43,8 +43,7 @@
 bool
 operator==(const RoutingTablePoolEntry& lhs, const RoutingTablePoolEntry& rhs)
 {
-  return (lhs.getDestination() == rhs.getDestination()
-          &&
+  return (lhs.getDestination() == rhs.getDestination() &&
           lhs.getNexthopList() == rhs.getNexthopList());
 }
 
diff --git a/src/route/routing-table.cpp b/src/route/routing-table.cpp
index f0e4aac..60b05f9 100644
--- a/src/route/routing-table.cpp
+++ b/src/route/routing-table.cpp
@@ -27,7 +27,6 @@
 #include "name-prefix-table.hpp"
 #include "logger.hpp"
 
-#include <iostream>
 #include <list>
 #include <string>
 
@@ -42,7 +41,6 @@
   , m_fib(fib)
   , m_lsdb(lsdb)
   , m_namePrefixTable(namePrefixTable)
-  , m_NO_NEXT_HOP{-12345}
   , m_routingCalcInterval{confParam.getRoutingCalcInterval()}
   , m_isRoutingTableCalculating(false)
   , m_isRouteCalculationScheduled(false)
diff --git a/src/route/routing-table.hpp b/src/route/routing-table.hpp
index f43ad11..c149eb7 100644
--- a/src/route/routing-table.hpp
+++ b/src/route/routing-table.hpp
@@ -75,12 +75,6 @@
   void
   scheduleRoutingTableCalculation();
 
-  int
-  getNoNextHop()
-  {
-    return m_NO_NEXT_HOP;
-  }
-
   void
   setRoutingCalcInterval(uint32_t interval)
   {
@@ -141,8 +135,6 @@
   Lsdb& m_lsdb;
   NamePrefixTable& m_namePrefixTable;
 
-  const int m_NO_NEXT_HOP;
-
   std::list<RoutingTableEntry> m_dryTable;
 
   ndn::time::seconds m_routingCalcInterval;