30 #include <boost/math/constants/constants.hpp>
31 #include <ndn-cxx/util/logger.hpp>
38 const int LinkStateRoutingTableCalculator::EMPTY_PARENT = -12345;
39 const double LinkStateRoutingTableCalculator::INF_DISTANCE = 2147483647;
40 const int LinkStateRoutingTableCalculator::NO_MAPPING_NUM = -1;
68 for (
auto lsaIt = lsaRange.first; lsaIt != lsaRange.second; ++lsaIt) {
69 auto adjLsa = std::static_pointer_cast<AdjLsa>(*lsaIt);
72 std::list<Adjacent> adl = adjLsa->getAdl().getAdjList();
74 for (
const auto& adjacent : adl) {
76 double cost = adjacent.getLinkCost();
78 if (row && col && *row <
static_cast<int32_t
>(
m_nRouters)
97 for (
size_t row = 0; row <
m_nRouters; ++row) {
98 for (
size_t col = 0; col <
m_nRouters; ++col) {
102 if (fromCost != toCost) {
105 if (toCost >= 0 && fromCost >= 0) {
107 correctedCost = std::max(toCost, fromCost);
110 NLSR_LOG_WARN(
"Cost between [" << row <<
"][" << col <<
"] and [" << col <<
"][" << row <<
111 "] are not the same (" << toCost <<
" != " << fromCost <<
"). " <<
112 "Correcting to cost: " << correctedCost);
124 if (!ndn_cxx_getLogger().isLevelEnabled(ndn::util::LogLevel::DEBUG)) {
129 std::string routerIndex;
130 std::string indexToNameMapping;
131 std::string lengthOfDash =
"--";
134 routerIndex += boost::lexical_cast<std::string>(i);
136 lengthOfDash +=
"--";
138 " Index:" + boost::lexical_cast<std::string>(i));
150 line += boost::lexical_cast<std::string>(
adjMatrix[i][j]);
154 line = boost::lexical_cast<std::string>(i) +
"|" + line;
162 for (
int i = 0; i < static_cast<int>(
m_nRouters); i++) {
179 if (
adjMatrix[sRouter][i] >= 0 && i !=
static_cast<size_t>(sRouter)) {
188 double* linkCosts,
int source)
193 if (
adjMatrix[source][i] >= 0 && i !=
static_cast<size_t>(source)) {
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);
347 double routeCost = m_distance[i];
349 ndn::optional<ndn::Name> nextHopRouterName= pMap.getRouterNameByMappingNo(nextHopRouter);
350 if (nextHopRouterName) {
351 std::string nextHopFace =
352 adjacencies.getAdjacent(*nextHopRouterName).getFaceUri().toString();
354 NextHop nh(nextHopFace, routeCost);
355 rt.addNextHop(*(pMap.getRouterNameByMappingNo(i)), nh);
364 LinkStateRoutingTableCalculator::getLsNextHop(
int dest,
int source)
367 while (m_parent[dest] != EMPTY_PARENT) {
369 dest = m_parent[dest];
371 if (dest != source) {
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;
505 if (srcLsa ==
nullptr || destLsa ==
nullptr) {
506 return UNKNOWN_DISTANCE;
509 std::vector<double> srcTheta = srcLsa->getCorTheta();
510 std::vector<double> destTheta = destLsa->
getCorTheta();
512 double srcRadius = srcLsa->getCorRadius();
515 double diffTheta = calculateAngularDistance(srcTheta, destTheta);
517 if (srcRadius == UNKNOWN_RADIUS || destRadius == UNKNOWN_RADIUS ||
518 diffTheta == UNKNOWN_DISTANCE) {
519 return UNKNOWN_DISTANCE;
523 distance = calculateHyperbolicDistance(srcRadius, destRadius, diffTheta);
525 NLSR_LOG_TRACE(
"Distance from " << src <<
" to " << dest <<
" is " << distance);
531 HyperbolicRoutingCalculator::calculateAngularDistance(std::vector<double> angleVectorI,
532 std::vector<double> angleVectorJ)
539 if (angleVectorI.size() != angleVectorJ.size()) {
541 return UNKNOWN_DISTANCE;
545 if (angleVectorI.size() > 1) {
546 for (
unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
547 if ((angleVectorI[k] > M_PI && angleVectorI[k] < 0.0) ||
548 (angleVectorJ[k] > M_PI && angleVectorJ[k] < 0.0)) {
550 return UNKNOWN_DISTANCE;
554 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
555 angleVectorI[angleVectorI.size()-1] < 0.0) {
557 return UNKNOWN_DISTANCE;
560 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
561 angleVectorI[angleVectorI.size()-1] < 0.0) {
563 return UNKNOWN_DISTANCE;
567 double innerProduct = 0.0;
570 double x0i = std::cos(angleVectorI[0]);
571 double x0j = std::cos(angleVectorJ[0]);
574 double xni = std::sin(angleVectorI[angleVectorI.size() - 1]);
575 double xnj = std::sin(angleVectorJ[angleVectorJ.size() - 1]);
579 for (
unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
580 xni *= std::sin(angleVectorI[k]);
581 xnj *= std::sin(angleVectorJ[k]);
583 innerProduct += (x0i * x0j) + (xni * xnj);
586 if (angleVectorI.size() > 1) {
587 for (
unsigned int m = 1; m < angleVectorI.size(); m++) {
589 double xmi = std::cos(angleVectorI[m]);
590 double xmj = std::cos(angleVectorJ[m]);
591 for (
unsigned int l = 0; l < m; l++) {
592 xmi *= std::sin(angleVectorI[l]);
593 xmj *= std::sin(angleVectorJ[l]);
595 innerProduct += xmi * xmj;
601 return std::acos(innerProduct);
605 HyperbolicRoutingCalculator::calculateHyperbolicDistance(
double rI,
double rJ,
608 if (deltaTheta == UNKNOWN_DISTANCE) {
609 return UNKNOWN_DISTANCE;
615 if (deltaTheta <= 0.0 || rI <= 0.0 || rJ <= 0.0) {
617 NLSR_LOG_ERROR(
"Please make sure that no two nodes have the exact same HR coordinates");
618 return UNKNOWN_DISTANCE;
621 double xij = (1. / zeta) * std::acosh(std::cosh(zeta*rI) * std::cosh(zeta*rJ) -
622 std::sinh(zeta*rI)*std::sinh(zeta*rJ)*std::cos(deltaTheta));
627 HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri,
631 hop.setHyperbolic(
true);
633 NLSR_LOG_TRACE(
"Calculated " << hop <<
" for destination: " << dest);
636 rt.addNextHopToDryTable(dest, hop);
639 rt.addNextHop(dest, hop);
Data abstraction for AdjLsa AdjacencyLsa := ADJACENCY-LSA-TYPE TLV-LENGTH Lsa Adjacency*.
std::list< Adjacent > & getAdjList()
static const double NON_ADJACENT_COST
A class to house all the configuration parameters for NLSR.
const ndn::Name & getRouterPrefix() const
AdjacencyList & getAdjacencyList()
uint32_t getMaxFacesPerPrefix() const
Data abstraction for CoordinateLsa CoordinateLsa := COORDINATE-LSA-TYPE TLV-LENGTH Lsa HyperbolicRadi...
double getCorRadius() const
const std::vector< double > getCorTheta() const
void calculatePath(Map &map, RoutingTable &rt, Lsdb &lsdb, AdjacencyList &adjacencies)
static const int NO_NEXT_HOP
void calculatePath(Map &pMap, RoutingTable &rt, ConfParameter &confParam, const Lsdb &lsdb)
std::shared_ptr< T > findLsa(const ndn::Name &router) const
std::pair< LsaContainer::index< Lsdb::byType >::type::iterator, LsaContainer::index< Lsdb::byType >::type::iterator > getLsdbIterator() const
ndn::optional< int32_t > getMappingNoByRouterName(const ndn::Name &rName)
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....
int getNumOfLinkfromAdjMatrix(int sRouter)
Returns how many links a router in the matrix has.
void adjustAdMatrix(int source, int link, double linkCost)
Adjust a link cost in the adj. matrix.
void allocateAdjMatrix()
Allocate the space needed for the adj. matrix.
void makeAdjMatrix(const Lsdb &lsdb, Map &pMap)
Constructs an adj. matrix to calculate with.
void writeAdjMatrixLog(const Map &map) const
Writes a formated adjacent matrix to DEBUG log.
void getLinksFromAdjMatrix(int *links, double *linkCosts, int source)
Populates temp. variables with the link costs for some router.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
#define INIT_LOGGER(name)
#define NLSR_LOG_ERROR(x)
#define NLSR_LOG_TRACE(x)
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California,...