31 #include <boost/math/constants/constants.hpp> 32 #include <ndn-cxx/util/logger.hpp> 39 const int LinkStateRoutingTableCalculator::EMPTY_PARENT = -12345;
40 const double LinkStateRoutingTableCalculator::INF_DISTANCE = 2147483647;
41 const int LinkStateRoutingTableCalculator::NO_MAPPING_NUM = -1;
68 for (
const auto& adjLsa : adjLsaList) {
71 std::list<Adjacent> adl = adjLsa.getAdl().getAdjList();
73 for (
const auto& adjacent : adl) {
75 double cost = adjacent.getLinkCost();
77 if (row && col && *row < static_cast<int32_t>(
m_nRouters)
96 for (
size_t row = 0; row <
m_nRouters; ++row) {
97 for (
size_t col = 0; col <
m_nRouters; ++col) {
101 if (fromCost != toCost) {
104 if (toCost >= 0 && fromCost >= 0) {
106 correctedCost = std::max(toCost, fromCost);
109 NLSR_LOG_WARN(
"Cost between [" << row <<
"][" << col <<
"] and [" << col <<
"][" << row <<
110 "] are not the same (" << toCost <<
" != " << fromCost <<
"). " <<
111 "Correcting to cost: " << correctedCost);
123 if (!ndn_cxx_getLogger().isLevelEnabled(ndn::util::LogLevel::DEBUG)) {
128 std::string routerIndex;
129 std::string indexToNameMapping;
130 std::string lengthOfDash =
"--";
133 routerIndex += boost::lexical_cast<std::string>(i);
135 lengthOfDash +=
"--";
137 " Index:" + boost::lexical_cast<std::string>(i));
145 if (
adjMatrix[i][j] == LinkStateRoutingTableCalculator::NO_NEXT_HOP) {
149 line += boost::lexical_cast<std::string>(
adjMatrix[i][j]);
153 line = boost::lexical_cast<std::string>(i) +
"|" + line;
161 for (
int i = 0; i < static_cast<int>(
m_nRouters); i++) {
178 if (
adjMatrix[sRouter][i] >= 0 && i != static_cast<size_t>(sRouter)) {
192 if (
adjMatrix[source][i] >= 0 && i != static_cast<size_t>(source)) {
236 const std::list<AdjLsa>& adjLsaList)
238 NLSR_LOG_DEBUG(
"LinkStateRoutingTableCalculator::calculatePath Called");
243 ndn::optional<int32_t> sourceRouter =
250 doDijkstraPathCalculation(*sourceRouter);
252 addAllLsNextHopsToRoutingTable(confParam.
getAdjacencyList(), rt, pMap, *sourceRouter);
261 for (
int i = 0 ; i <
vNoLink; i++) {
266 doDijkstraPathCalculation(*sourceRouter);
268 addAllLsNextHopsToRoutingTable(confParam.
getAdjacencyList(), rt, pMap, *sourceRouter);
279 LinkStateRoutingTableCalculator::doDijkstraPathCalculation(
int sourceRouter)
286 for (i = 0 ; i < static_cast<int>(
m_nRouters); i++) {
287 m_parent[i] = EMPTY_PARENT;
289 m_distance[i] = INF_DISTANCE;
292 if (sourceRouter != NO_MAPPING_NUM) {
294 m_distance[sourceRouter] = 0;
295 sortQueueByDistance(Q, m_distance, head,
m_nRouters);
299 if (m_distance[u] == INF_DISTANCE) {
303 for (v = 0 ; v < static_cast<int>(
m_nRouters); v++) {
307 if (isNotExplored(Q, v, head + 1,
m_nRouters)) {
311 if (m_distance[u] +
adjMatrix[u][v] < m_distance[v]) {
313 m_distance[v] = m_distance[u] +
adjMatrix[u][v] ;
322 sortQueueByDistance(Q, m_distance, head,
m_nRouters);
329 LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(
AdjacencyList& adjacencies,
331 uint32_t sourceRouter)
333 NLSR_LOG_DEBUG(
"LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called");
335 int nextHopRouter = 0;
339 if (i != sourceRouter) {
342 nextHopRouter = getLsNextHop(i, sourceRouter);
344 if (nextHopRouter != NO_NEXT_HOP) {
347 double routeCost = m_distance[i];
350 if (nextHopRouterName) {
351 std::string nextHopFace =
354 NextHop nh(nextHopFace, routeCost);
364 LinkStateRoutingTableCalculator::getLsNextHop(
int dest,
int source)
366 int nextHop = NO_NEXT_HOP;
367 while (m_parent[dest] != EMPTY_PARENT) {
369 dest = m_parent[dest];
371 if (dest != source) {
372 nextHop = NO_NEXT_HOP;
378 LinkStateRoutingTableCalculator::sortQueueByDistance(
int* Q,
380 int start,
int element)
382 for (
int i = start ; i < element ; i++) {
383 for (
int j = i + 1; j < element; j++) {
384 if (dist[Q[j]] < dist[Q[i]]) {
394 LinkStateRoutingTableCalculator::isNotExplored(
int* Q,
395 int u,
int start,
int element)
398 for (
int i = start; i < element; i++) {
408 LinkStateRoutingTableCalculator::allocateParent()
414 LinkStateRoutingTableCalculator::allocateDistance()
420 LinkStateRoutingTableCalculator::freeParent()
425 void LinkStateRoutingTableCalculator::freeDistance()
427 delete [] m_distance;
430 const double HyperbolicRoutingCalculator::MATH_PI = boost::math::constants::pi<double>();
432 const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0;
433 const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS = -1.0;
444 std::list<Adjacent> neighbors = adjacencies.
getAdjList();
445 for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) {
449 NLSR_LOG_TRACE(adj->getName() <<
" is inactive; not using it as a nexthop");
453 ndn::Name srcRouterName = adj->getName();
456 if (srcRouterName == m_thisRouterName) {
460 std::string srcFaceUri = adj->getFaceUri().toString();
463 addNextHop(srcRouterName, srcFaceUri, 0, rt);
468 NLSR_LOG_WARN(adj->getName() <<
" does not exist in the router map!");
473 for (
int dest = 0; dest < static_cast<int>(
m_nRouters); ++dest) {
475 if (thisRouter && dest != *thisRouter && dest != *src) {
478 if (destRouterName) {
479 double distance = getHyperbolicDistance(lsdb, srcRouterName, *destRouterName);
482 if (distance == UNKNOWN_DISTANCE) {
483 NLSR_LOG_WARN(
"Could not calculate hyperbolic distance from " << srcRouterName
484 <<
" to " << *destRouterName);
487 addNextHop(*destRouterName, srcFaceUri, distance, rt);
495 HyperbolicRoutingCalculator::getHyperbolicDistance(
Lsdb& lsdb, ndn::Name src, ndn::Name dest)
497 NLSR_LOG_TRACE(
"Calculating hyperbolic distance from " << src <<
" to " << dest);
499 double distance = UNKNOWN_DISTANCE;
501 ndn::Name srcLsaKey = src;
506 ndn::Name destLsaKey = dest;
512 if (srcLsa ==
nullptr || destLsa ==
nullptr) {
513 return UNKNOWN_DISTANCE;
516 std::vector<double> srcTheta = srcLsa->
getCorTheta();
517 std::vector<double> destTheta = destLsa->
getCorTheta();
522 double diffTheta = calculateAngularDistance(srcTheta, destTheta);
524 if (srcRadius == UNKNOWN_RADIUS || destRadius == UNKNOWN_RADIUS ||
525 diffTheta == UNKNOWN_DISTANCE) {
526 return UNKNOWN_DISTANCE;
530 distance = calculateHyperbolicDistance(srcRadius, destRadius, diffTheta);
532 NLSR_LOG_TRACE(
"Distance from " << src <<
" to " << dest <<
" is " << distance);
538 HyperbolicRoutingCalculator::calculateAngularDistance(std::vector<double> angleVectorI,
539 std::vector<double> angleVectorJ)
546 if (angleVectorI.size() != angleVectorJ.size()) {
548 return UNKNOWN_DISTANCE;
552 if (angleVectorI.size() > 1) {
553 for (
unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
554 if ((angleVectorI[k] > M_PI && angleVectorI[k] < 0.0) ||
555 (angleVectorJ[k] > M_PI && angleVectorJ[k] < 0.0)) {
557 return UNKNOWN_DISTANCE;
561 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
562 angleVectorI[angleVectorI.size()-1] < 0.0) {
564 return UNKNOWN_DISTANCE;
567 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
568 angleVectorI[angleVectorI.size()-1] < 0.0) {
570 return UNKNOWN_DISTANCE;
574 double innerProduct = 0.0;
577 double x0i = std::cos(angleVectorI[0]);
578 double x0j = std::cos(angleVectorJ[0]);
581 double xni = std::sin(angleVectorI[angleVectorI.size() - 1]);
582 double xnj = std::sin(angleVectorJ[angleVectorJ.size() - 1]);
586 for (
unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
587 xni *= std::sin(angleVectorI[k]);
588 xnj *= std::sin(angleVectorJ[k]);
590 innerProduct += (x0i * x0j) + (xni * xnj);
593 if (angleVectorI.size() > 1) {
594 for (
unsigned int m = 1; m < angleVectorI.size(); m++) {
596 double xmi = std::cos(angleVectorI[m]);
597 double xmj = std::cos(angleVectorJ[m]);
598 for (
unsigned int l = 0; l < m; l++) {
599 xmi *= std::sin(angleVectorI[l]);
600 xmj *= std::sin(angleVectorJ[l]);
602 innerProduct += xmi * xmj;
608 return std::acos(innerProduct);
612 HyperbolicRoutingCalculator::calculateHyperbolicDistance(
double rI,
double rJ,
615 if (deltaTheta == UNKNOWN_DISTANCE) {
616 return UNKNOWN_DISTANCE;
622 if (deltaTheta <= 0.0 || rI <= 0.0 || rJ <= 0.0) {
624 NLSR_LOG_ERROR(
"Please make sure that no two nodes have the exact same HR coordinates");
625 return UNKNOWN_DISTANCE;
628 double xij = (1. / zeta) * std::acosh(std::cosh(zeta*rI) * std::cosh(zeta*rJ) -
629 std::sinh(zeta*rI)*std::sinh(zeta*rJ)*std::cos(deltaTheta));
634 HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri,
640 NLSR_LOG_TRACE(
"Calculated " << hop <<
" for destination: " << dest);
A class to house all the configuration parameters for NLSR.
const ndn::FaceUri & getFaceUri() const
void getLinksFromAdjMatrix(int *links, double *linkCosts, int source)
Populates temp. variables with the link costs for some router.
void calculatePath(Map &map, RoutingTable &rt, Lsdb &lsdb, AdjacencyList &adjacencies)
void allocateAdjMatrix()
Allocate the space needed for the adj. matrix.
void adjustAdMatrix(int source, int link, double linkCost)
Adjust a link cost in the adj. matrix.
static const double NON_ADJACENT_COST
AdjacencyList & getAdjacencyList()
ndn::optional< int32_t > getMappingNoByRouterName(const ndn::Name &rName)
#define NLSR_LOG_DEBUG(x)
const ndn::Name & getRouterPrefix() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
double getCorRadius() const
Adjacent getAdjacent(const ndn::Name &adjName)
#define INIT_LOGGER(name)
void makeAdjMatrix(const std::list< AdjLsa > &adjLsaList, Map &pMap)
Constructs an adj. matrix to calculate with.
ndn::optional< ndn::Name > getRouterNameByMappingNo(int32_t mn) const
void initMatrix()
set NON_ADJACENT_COST i.e. -12345 to every cell of the matrix to ensure that the memory is safe...
CoordinateLsa * findCoordinateLsa(const ndn::Name &key)
Finds a cor. LSA in the LSDB.
void writeAdjMatrixLog(const Map &map) const
Writes a formated adjacent matrix to DEBUG log.
uint32_t getMaxFacesPerPrefix() const
const std::vector< double > getCorTheta() const
#define NLSR_LOG_ERROR(x)
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void calculatePath(Map &pMap, RoutingTable &rt, ConfParameter &confParam, const std::list< AdjLsa > &adjLsaList)
void addNextHopToDryTable(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry in a dry run scenario.
int getNumOfLinkfromAdjMatrix(int sRouter)
Returns how many links a router in the matrix has.
static const int NO_NEXT_HOP
void setHyperbolic(bool b)
void addNextHop(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry.
std::list< Adjacent > & getAdjList()
#define NLSR_LOG_TRACE(x)