routing: improved routing map data structure.

Change-Id: I1cde5b2b9cffcec5cbfd78c5dd4262b5fb919a73
refs: #4240
diff --git a/src/route/routing-table-calculator.cpp b/src/route/routing-table-calculator.cpp
index f4442f5..318ec64 100644
--- a/src/route/routing-table-calculator.cpp
+++ b/src/route/routing-table-calculator.cpp
@@ -18,6 +18,7 @@
  * 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/>.
  **/
+
 #include "routing-table-calculator.hpp"
 #include "lsdb.hpp"
 #include "map.hpp"
@@ -62,19 +63,19 @@
   for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); it != adjLsdb.end() ; it++) {
 
 
-    int32_t row = pMap.getMappingNoByRouterName((*it).getOrigRouter());
+    ndn::optional<int32_t> row = pMap.getMappingNoByRouterName((*it).getOrigRouter());
 
     std::list<Adjacent> adl = (*it).getAdl().getAdjList();
     // For each adjacency represented in the LSA
     for (std::list<Adjacent>::iterator itAdl = adl.begin(); itAdl != adl.end() ; itAdl++) {
 
-      int32_t col = pMap.getMappingNoByRouterName((*itAdl).getName());
+      ndn::optional<int32_t> col = pMap.getMappingNoByRouterName((*itAdl).getName());
       double cost = (*itAdl).getLinkCost();
 
-      if ((row >= 0 && row < static_cast<int32_t>(m_nRouters)) &&
-          (col >= 0 && col < static_cast<int32_t>(m_nRouters)))
+      if (row && col && *row < static_cast<int32_t>(m_nRouters)
+          && *col < static_cast<int32_t>(m_nRouters))
       {
-        adjMatrix[row][col] = cost;
+        adjMatrix[*row][*col] = cost;
       }
     }
   }
@@ -210,30 +211,32 @@
   initMatrix();
   makeAdjMatrix(pnlsr, pMap);
   writeAdjMatrixLog();
-  int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix());
+  ndn::optional<int32_t> sourceRouter =
+    pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix());
   allocateParent(); // These two matrices are used in Dijkstra's algorithm.
   allocateDistance(); //
-  if (pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) {
+  // We only bother to do the calculation if we have a router by that name.
+  if (sourceRouter && pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) {
     // In the single path case we can simply run Dijkstra's algorithm.
-    doDijkstraPathCalculation(sourceRouter);
+    doDijkstraPathCalculation(*sourceRouter);
     // Inform the routing table of the new next hops.
-    addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
+    addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, *sourceRouter);
   }
   else {
     // Multi Path
-    setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter));
+    setNoLink(getNumOfLinkfromAdjMatrix(*sourceRouter));
     allocateLinks();
     allocateLinkCosts();
     // Gets a sparse listing of adjacencies for path calculation
-    getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
+    getLinksFromAdjMatrix(links, linkCosts, *sourceRouter);
     for (int i = 0 ; i < vNoLink; i++) {
       // Simulate that only the current neighbor is accessible
-      adjustAdMatrix(sourceRouter, links[i], linkCosts[i]);
+      adjustAdMatrix(*sourceRouter, links[i], linkCosts[i]);
       writeAdjMatrixLog();
       // Do Dijkstra's algorithm using the current neighbor as your start.
-      doDijkstraPathCalculation(sourceRouter);
+      doDijkstraPathCalculation(*sourceRouter);
       // Update the routing table with the calculations.
-      addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
+      addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, *sourceRouter);
     }
     freeLinks();
     freeLinksCosts();
@@ -314,12 +317,15 @@
         // Fetch its distance
         double routeCost = m_distance[i];
         // Fetch its actual name
-        ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter);
-        std::string nextHopFace =
-          pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getFaceUri().toString();
-        // Add next hop to routing table
-        NextHop nh(nextHopFace, routeCost);
-        rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh);
+        ndn::optional<ndn::Name> nextHopRouterName= pMap.getRouterNameByMappingNo(nextHopRouter);
+        if (nextHopRouterName) {
+          std::string nextHopFace =
+            pnlsr.getAdjacencyList().getAdjacent(*nextHopRouterName).getFaceUri().toString();
+          // Add next hop to routing table
+          NextHop nh(nextHopFace, routeCost);
+          rt.addNextHop(*(pMap.getRouterNameByMappingNo(i)), nh);
+
+        }
       }
     }
   }
@@ -397,15 +403,13 @@
 const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0;
 const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS   = -1.0;
 
-const int32_t HyperbolicRoutingCalculator::ROUTER_NOT_FOUND = -1.0;
-
 void
 HyperbolicRoutingCalculator::calculatePaths(Map& map, RoutingTable& rt,
                                             Lsdb& lsdb, AdjacencyList& adjacencies)
 {
   _LOG_TRACE("Calculating hyperbolic paths");
 
-  int thisRouter = map.getMappingNoByRouterName(m_thisRouterName);
+  ndn::optional<int32_t> thisRouter = map.getMappingNoByRouterName(m_thisRouterName);
 
   // Iterate over directly connected neighbors
   std::list<Adjacent> neighbors = adjacencies.getAdjList();
@@ -429,9 +433,9 @@
     // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link
     addNextHop(srcRouterName, srcFaceUri, 0, rt);
 
-    int src = map.getMappingNoByRouterName(srcRouterName);
+    ndn::optional<int32_t> src = map.getMappingNoByRouterName(srcRouterName);
 
-    if (src == ROUTER_NOT_FOUND) {
+    if (!src) {
       _LOG_WARN(adj->getName() << " does not exist in the router map!");
       continue;
     }
@@ -439,20 +443,21 @@
     // Get hyperbolic distance from direct neighbor to every other router
     for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) {
       // Don't calculate nexthops to this router or from a router to itself
-      if (dest != thisRouter && dest != src) {
+      if (thisRouter && dest != *thisRouter && dest != *src) {
 
-        ndn::Name destRouterName = map.getRouterNameByMappingNo(dest);
+        ndn::optional<ndn::Name> destRouterName = map.getRouterNameByMappingNo(dest);
+        if (destRouterName) {
+          double distance = getHyperbolicDistance(map, lsdb, srcRouterName, *destRouterName);
 
-        double distance = getHyperbolicDistance(map, lsdb, srcRouterName, destRouterName);
+          // Could not compute distance
+          if (distance == UNKNOWN_DISTANCE) {
+            _LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName << " to " <<
+                      *destRouterName);
+            continue;
+          }
 
-        // Could not compute distance
-        if (distance == UNKNOWN_DISTANCE) {
-          _LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName << " to " <<
-                    destRouterName);
-          continue;
+          addNextHop(*destRouterName, srcFaceUri, distance, rt);
         }
-
-        addNextHop(destRouterName, srcFaceUri, distance, rt);
       }
     }
   }