Routing Table Calculation Complete(Ls, Hp)
diff --git a/nlsr_rtc.cpp b/nlsr_rtc.cpp
index 4dabf6a..f7ec5cd 100644
--- a/nlsr_rtc.cpp
+++ b/nlsr_rtc.cpp
@@ -1,8 +1,10 @@
#include <iostream>
+#include <cmath>
#include "nlsr_lsdb.hpp"
#include "nlsr_rtc.hpp"
#include "nlsr_map.hpp"
#include "nlsr_lsa.hpp"
+#include "nlsr_nexthop.hpp"
#include "nlsr.hpp"
using namespace std;
@@ -17,6 +19,16 @@
}
}
+void
+RoutingTableCalculator::initMatrix()
+{
+ for(int i=0;i<numOfRouter;i++)
+ {
+ for(int j=0;j<numOfRouter;j++)
+ adjMatrix[i][j]=0;
+ }
+}
+
void
RoutingTableCalculator::makeAdjMatrix(nlsr& pnlsr, Map pMap)
{
@@ -106,18 +118,41 @@
delete [] adjMatrix;
}
+
+void
+RoutingTableCalculator::allocateLinks()
+{
+ links=new int[vNoLink];
+}
+
+void RoutingTableCalculator::allocateLinkCosts()
+{
+ linkCosts=new double[vNoLink];
+}
+
+void
+RoutingTableCalculator::freeLinks()
+{
+ delete [] links;
+}
+void
+RoutingTableCalculator::freeLinksCosts()
+{
+ delete [] linkCosts;
+}
+
void
LinkStateRoutingTableCalculator::calculatePath(Map& pMap,
RoutingTable& rt, nlsr& pnlsr)
{
cout<<"LinkStateRoutingTableCalculator::calculatePath Called"<<endl;
allocateAdjMatrix();
+ initMatrix();
makeAdjMatrix(pnlsr,pMap);
cout<<pMap;
printAdjMatrix();
string routerName=pnlsr.getConfParameter().getRouterPrefix();
int sourceRouter=pMap.getMappingNoByRouterName(routerName);
- cout<<"Calculating Router: "<< routerName <<" Mapping no: "<<sourceRouter<<endl;
int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
allocateParent();
allocateDistance();
@@ -129,7 +164,7 @@
// print all ls path -- debugging purpose
printAllLsPath(sourceRouter);
// update routing table
- // update NPT ( FIB )
+ addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
}
else
{
@@ -141,10 +176,14 @@
getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
for (int i=0 ; i < vNoLink; i++)
{
+
adjustAdMatrix(sourceRouter,links[i], linkCosts[i]);
+ printAdjMatrix();
doDijkstraPathCalculation(sourceRouter);
+ // print all ls path -- debugging purpose
+ printAllLsPath(sourceRouter);
//update routing table
- //update NPT ( FIB )
+ addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter);
}
freeLinks();
@@ -207,6 +246,53 @@
delete [] Q;
}
+void
+LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(nlsr& pnlsr,
+ RoutingTable& rt, Map& pMap, int sourceRouter)
+{
+ cout<<"LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called";
+ cout<<endl;
+ for(int i=0; i < numOfRouter ; i++)
+ {
+ if ( i!= sourceRouter )
+ {
+ int nextHopRouter=getLsNextHop(i,sourceRouter);
+ double routeCost=distance[i];
+ string nextHopRouterName=pMap.getRouterNameByMappingNo(nextHopRouter);
+ int nxtHopFace=
+ pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
+ cout<<"Dest Router: "<<pMap.getRouterNameByMappingNo(i)<<endl;
+ cout<<"Next hop Router: "<<nextHopRouterName<<endl;
+ cout<<"Next hop Face: "<<nxtHopFace<<endl;
+ cout<<"Route Cost: "<<routeCost<<endl;
+ cout<<endl;
+ // Add next hop to routing table
+ NextHop nh(nxtHopFace,routeCost);
+ rt.addNextHop(pMap.getRouterNameByMappingNo(i),nh);
+
+ }
+ }
+}
+
+int
+LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
+{
+ int nextHop;
+ while ( parent[dest] != EMPTY_PARENT )
+ {
+ nextHop=dest;
+ dest=parent[dest];
+
+ }
+
+ if ( dest != source )
+ {
+ nextHop=NO_NEXT_HOP;
+ }
+
+ return nextHop;
+}
+
void
LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter)
{
@@ -291,37 +377,146 @@
delete [] distance;
}
-void
-LinkStateRoutingTableCalculator::allocateLinks()
-{
- links=new int[vNoLink];
-}
-void LinkStateRoutingTableCalculator::allocateLinkCosts()
-{
- linkCosts=new double[vNoLink];
-}
-
-void
-LinkStateRoutingTableCalculator::freeLinks()
-{
- delete [] links;
-}
-void
-LinkStateRoutingTableCalculator::freeLinksCosts()
-{
- delete [] linkCosts;
-}
void
HypRoutingTableCalculator::calculatePath(Map& pMap,
RoutingTable& rt, nlsr& pnlsr)
{
+ makeAdjMatrix(pnlsr,pMap);
+ string routerName=pnlsr.getConfParameter().getRouterPrefix();
+ int sourceRouter=pMap.getMappingNoByRouterName(routerName);
+ int noLink=getNumOfLinkfromAdjMatrix(sourceRouter);
+ setNoLink(noLink);
+ allocateLinks();
+ allocateLinkCosts();
+
+ getLinksFromAdjMatrix(links, linkCosts, sourceRouter);
+
+ for(int i=0 ; i < numOfRouter ; ++i)
+ {
+ int k=0;
+ if ( i != sourceRouter)
+ {
+ allocateLinkFaces();
+ allocateDistanceToNeighbor();
+ allocateDistFromNbrToDest();
+
+ for(int j=0; j<vNoLink; j++)
+ {
+ string nextHopRouterName=pMap.getRouterNameByMappingNo(links[j]);
+ int nextHopFace=
+ pnlsr.getAdl().getAdjacent(nextHopRouterName).getConnectingFace();
+ double distToNbr=getHyperbolicDistance(pnlsr,pMap,
+ sourceRouter,links[j]);
+ double distToDestFromNbr=getHyperbolicDistance(pnlsr,
+ pMap,links[j],i);
+ if ( distToDestFromNbr >= 0 )
+ {
+ linkFaces[k] = nextHopFace;
+ distanceToNeighbor[k] = distToNbr;
+ distFromNbrToDest[k] = distToDestFromNbr;
+ k++;
+ }
+ }
+
+ addHypNextHopsToRoutingTable(pnlsr,pMap,rt,k,i);
+
+ freeLinkFaces();
+ freeDistanceToNeighbor();
+ freeDistFromNbrToDest();
+ }
+ }
+
+ freeLinks();
+ freeLinksCosts();
+ freeAdjMatrix();
}
void
-HypDryRoutingTableCalculator::calculatePath(Map& pMap,
- RoutingTable& rt, nlsr& pnlsr)
+HypRoutingTableCalculator::addHypNextHopsToRoutingTable(nlsr& pnlsr,Map& pMap,
+ RoutingTable& rt, int noFaces, int dest)
{
+ for(int i=0 ; i < noFaces ; ++i)
+ {
+ string destRouter=pMap.getRouterNameByMappingNo(dest);
+ NextHop nh(linkFaces[i],distFromNbrToDest[i]);
+ rt.addNextHop(destRouter,nh);
+ if( isDryRun == 1 )
+ {
+ rt.addNextHopToDryTable(destRouter,nh);
+ }
+ }
+
+}
+
+double
+HypRoutingTableCalculator::getHyperbolicDistance(nlsr& pnlsr,
+ Map& pMap, int src, int dest)
+{
+ double distance=0.0;
+
+ string srcRouterKey=pMap.getRouterNameByMappingNo(src)+"/3";
+ string destRouterKey=pMap.getRouterNameByMappingNo(dest)+"/3";
+
+ double srcRadius=pnlsr.getLsdb().getCorLsa(srcRouterKey).getCorRadius();
+ double srcTheta=pnlsr.getLsdb().getCorLsa(srcRouterKey).getCorTheta();
+
+ double destRadius=pnlsr.getLsdb().getCorLsa(destRouterKey).getCorRadius();
+ double destTheta=pnlsr.getLsdb().getCorLsa(destRouterKey).getCorTheta();
+
+
+ double diffTheta = fabs (srcTheta - destTheta);
+
+ if (diffTheta > MATH_PI){
+ diffTheta = 2 * MATH_PI - diffTheta;
+ }
+
+ if ( srcRadius != -1 && destRadius != -1 ){
+ if (diffTheta == 0)
+ distance = fabs (srcRadius - destRadius);
+ else
+ distance = acosh((cosh(srcRadius)*cosh(destRadius))-
+ (sinh(srcRadius)*sinh(destRadius)*cos(diffTheta)));
+ }else{
+ distance = -1;
+ }
+
+ return distance;
}
+void
+HypRoutingTableCalculator::allocateLinkFaces()
+{
+ linkFaces=new int[vNoLink];
+}
+
+void
+HypRoutingTableCalculator::allocateDistanceToNeighbor()
+{
+ distanceToNeighbor=new double[vNoLink];
+}
+
+void
+HypRoutingTableCalculator::allocateDistFromNbrToDest()
+{
+ distFromNbrToDest=new double[vNoLink];
+}
+
+void
+HypRoutingTableCalculator::freeLinkFaces()
+{
+ delete [] linkFaces;
+}
+
+void
+HypRoutingTableCalculator::freeDistanceToNeighbor()
+{
+ delete [] distanceToNeighbor;
+}
+
+void
+HypRoutingTableCalculator::freeDistFromNbrToDest()
+{
+ delete [] distFromNbrToDest;
+}