route: Fixing segfault issue for Hyperbolic Routing Table Calculation

Refs: #1815

Change-Id: I317259d299e4117f0846ed98c432b39d1404b643
diff --git a/src/route/nexthop-list.cpp b/src/route/nexthop-list.cpp
index 7a8572d..0822d38 100644
--- a/src/route/nexthop-list.cpp
+++ b/src/route/nexthop-list.cpp
@@ -99,6 +99,7 @@
 NexthopList::writeLog()
 {
   int i = 1;
+  sort();
   for (std::list<NextHop>::iterator it = m_nexthopList.begin();
        it != m_nexthopList.end() ; it++, i++) {
     _LOG_DEBUG("Nexthop " << i << ": " << (*it).getConnectingFaceUri()
diff --git a/src/route/routing-table-calculator.cpp b/src/route/routing-table-calculator.cpp
index 57bf6b2..00db7b7 100644
--- a/src/route/routing-table-calculator.cpp
+++ b/src/route/routing-table-calculator.cpp
@@ -167,7 +167,6 @@
   allocateAdjMatrix();
   initMatrix();
   makeAdjMatrix(pnlsr, pMap);
-  //printAdjMatrix();
   writeAdjMatrixLog();
   int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix());
   allocateParent();
@@ -332,8 +331,9 @@
 HypRoutingTableCalculator::calculatePath(Map& pMap,
                                          RoutingTable& rt, Nlsr& pnlsr)
 {
+  allocateAdjMatrix();
+  initMatrix();
   makeAdjMatrix(pnlsr, pMap);
-  //std::cout << pMap;
   ndn::Name routerName = pnlsr.getConfParameter().getRouterPrefix();
   int sourceRouter = pMap.getMappingNoByRouterName(routerName);
   int noLink = getNumOfLinkfromAdjMatrix(sourceRouter);
@@ -344,26 +344,23 @@
   for (int i = 0 ; i < numOfRouter ; ++i) {
     int k = 0;
     if (i != sourceRouter) {
-      allocateLinkFaces();
+      allocateNexthopRouters();
       allocateDistanceToNeighbor();
       allocateDistFromNbrToDest();
       for (int j = 0; j < vNoLink; j++) {
-        ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(links[j]);
-        std::string nextHopFaceUri =
-          pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri();
         double distToNbr = getHyperbolicDistance(pnlsr, pMap,
                                                  sourceRouter, links[j]);
         double distToDestFromNbr = getHyperbolicDistance(pnlsr,
                                                          pMap, links[j], i);
         if (distToDestFromNbr >= 0) {
-          m_linkFaceUris[k] = nextHopFaceUri;
+          m_nexthopRouters[k] = links[j];
           m_distanceToNeighbor[k] = distToNbr;
           m_distFromNbrToDest[k] = distToDestFromNbr;
           k++;
         }
       }
       addHypNextHopsToRoutingTable(pnlsr, pMap, rt, k, i);
-      freeLinkFaces();
+      freeNexthopRouters();
       freeDistanceToNeighbor();
       freeDistFromNbrToDest();
     }
@@ -377,13 +374,18 @@
 HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr, Map& pMap,
                                                         RoutingTable& rt, int noFaces, int dest)
 {
+  ndn::Name destRouter = pMap.getRouterNameByMappingNo(dest);
   for (int i = 0 ; i < noFaces ; ++i) {
-    ndn::Name destRouter = pMap.getRouterNameByMappingNo(dest);
-    NextHop nh(m_linkFaceUris[i], m_distFromNbrToDest[i]);
-    rt.addNextHop(destRouter, nh);
+    ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(m_nexthopRouters[i]);
+    std::string nextHopFaceUri =
+         pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri();
+    NextHop nh(nextHopFaceUri, m_distFromNbrToDest[i]);
     if (m_isDryRun) {
       rt.addNextHopToDryTable(destRouter, nh);
     }
+    else {
+      rt.addNextHop(destRouter, nh);
+    }
   }
 }
 
@@ -396,14 +398,18 @@
   srcRouterKey.append("coordinate");
   ndn::Name destRouterKey = pMap.getRouterNameByMappingNo(dest);
   destRouterKey.append("coordinate");
-  double srcRadius = (pnlsr.getLsdb().findCoordinateLsa(
-                        srcRouterKey))->getCorRadius();
-  double srcTheta = (pnlsr.getLsdb().findCoordinateLsa(
-                       srcRouterKey))->getCorTheta();
-  double destRadius = (pnlsr.getLsdb().findCoordinateLsa(
-                         destRouterKey))->getCorRadius();
-  double destTheta = (pnlsr.getLsdb().findCoordinateLsa(
-                        destRouterKey))->getCorTheta();
+  CoordinateLsa* srcLsa = pnlsr.getLsdb().findCoordinateLsa(srcRouterKey);
+  CoordinateLsa* destLsa = pnlsr.getLsdb().findCoordinateLsa(destRouterKey);
+
+  if ((srcLsa == 0) || (destLsa == 0)) {
+    return -1;
+  }
+
+  double srcRadius = srcLsa->getCorRadius();
+  double srcTheta = srcLsa->getCorTheta();
+  double destRadius = destLsa->getCorRadius();
+  double destTheta = destLsa->getCorTheta();
+
   double diffTheta = fabs(srcTheta - destTheta);
   if (diffTheta > MATH_PI) {
     diffTheta = 2 * MATH_PI - diffTheta;
@@ -424,9 +430,9 @@
 }
 
 void
-HypRoutingTableCalculator::allocateLinkFaces()
+HypRoutingTableCalculator::allocateNexthopRouters()
 {
-  m_linkFaceUris.reserve(vNoLink);
+  m_nexthopRouters = new uint32_t[vNoLink];
 }
 
 void
@@ -442,9 +448,9 @@
 }
 
 void
-HypRoutingTableCalculator::freeLinkFaces()
+HypRoutingTableCalculator::freeNexthopRouters()
 {
-  m_linkFaceUris.clear();
+  delete [] m_nexthopRouters;
 }
 
 void
diff --git a/src/route/routing-table-calculator.hpp b/src/route/routing-table-calculator.hpp
index 3f1b53f..a56fed3 100644
--- a/src/route/routing-table-calculator.hpp
+++ b/src/route/routing-table-calculator.hpp
@@ -181,7 +181,7 @@
 
 private:
   void
-  allocateLinkFaces();
+  allocateNexthopRouters();
 
   void
   allocateDistanceToNeighbor();
@@ -190,7 +190,7 @@
   allocateDistFromNbrToDest();
 
   void
-  freeLinkFaces();
+  freeNexthopRouters();
 
   void
   freeDistanceToNeighbor();
@@ -208,7 +208,7 @@
 private:
   bool m_isDryRun;
 
-  std::vector<std::string> m_linkFaceUris;
+  uint32_t* m_nexthopRouters;
   double* m_distanceToNeighbor;
   double* m_distFromNbrToDest;
 
diff --git a/src/route/routing-table.cpp b/src/route/routing-table.cpp
index 613371f..7ab5b8e 100644
--- a/src/route/routing-table.cpp
+++ b/src/route/routing-table.cpp
@@ -73,7 +73,7 @@
         //need to update NPT here
         _LOG_DEBUG("Calling Update NPT With new Route");
         pnlsr.getNamePrefixTable().updateWithNewRoute();
-        writeLog();
+        writeLog(pnlsr.getConfParameter().getHyperbolicState());
         pnlsr.getNamePrefixTable().writeLog();
         pnlsr.getFib().writeLog();
       }
@@ -90,7 +90,7 @@
       // need to update NPT here
       _LOG_DEBUG("Calling Update NPT With new Route");
       pnlsr.getNamePrefixTable().updateWithNewRoute();
-      writeLog();
+      writeLog(pnlsr.getConfParameter().getHyperbolicState());
       pnlsr.getNamePrefixTable().writeLog();
       pnlsr.getFib().writeLog();
       //debugging purpose end
@@ -184,7 +184,7 @@
 }
 
 void
-RoutingTable::writeLog()
+RoutingTable::writeLog(int hyperbolicState)
 {
   _LOG_DEBUG("---------------Routing Table------------------");
   for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
@@ -193,6 +193,16 @@
     _LOG_DEBUG("Nexthops: ");
     (*it).getNexthopList().writeLog();
   }
+
+  if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
+    _LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
+    for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
+        it != m_dryTable.end(); ++it) {
+      _LOG_DEBUG("Destination: " << (*it).getDestination());
+      _LOG_DEBUG("Nexthops: ");
+      (*it).getNexthopList().writeLog();
+    }
+  }
 }
 
 //function related to manipulation of dry routing table
diff --git a/src/route/routing-table.hpp b/src/route/routing-table.hpp
index 952a07e..3a3c96a 100644
--- a/src/route/routing-table.hpp
+++ b/src/route/routing-table.hpp
@@ -80,7 +80,7 @@
   clearDryRoutingTable();
 
   void
-  writeLog();
+  writeLog(int hyperbolicState);
 
   const int m_NO_NEXT_HOP;