blob: 04cc78f6db9023aabcf52653387602db96ec8bed [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Vince Lehman41b173e2015-05-07 14:13:26 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Nick Gordone40377d2017-08-11 15:10:02 -050021
akmhoque53353462014-04-22 08:43:45 -050022#include "routing-table-calculator.hpp"
Nick Gordone98480b2017-05-24 11:23:03 -050023#include "lsdb.hpp"
akmhoque53353462014-04-22 08:43:45 -050024#include "map.hpp"
akmhoque53353462014-04-22 08:43:45 -050025#include "nexthop.hpp"
26#include "nlsr.hpp"
akmhoque2f423352014-06-03 11:49:35 -050027#include "logger.hpp"
dulalsaurabd0816a32019-07-26 13:11:24 +000028#include "adjacent.hpp"
akmhoque53353462014-04-22 08:43:45 -050029
Vince Lehman9a709032014-09-13 16:28:07 -050030#include <boost/math/constants/constants.hpp>
Saurab Dulal9da6aa72018-01-12 17:25:51 +000031#include <ndn-cxx/util/logger.hpp>
Nick Gordone98480b2017-05-24 11:23:03 -050032#include <cmath>
Vince Lehman9a709032014-09-13 16:28:07 -050033
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
35
dmcoomescf8d0ed2017-02-21 11:39:01 -060036INIT_LOGGER(route.RoutingTableCalculator);
akmhoque53353462014-04-22 08:43:45 -050037
dulalsaurabd0816a32019-07-26 13:11:24 +000038const int LinkStateRoutingTableCalculator::EMPTY_PARENT = -12345;
39const double LinkStateRoutingTableCalculator::INF_DISTANCE = 2147483647;
40const int LinkStateRoutingTableCalculator::NO_MAPPING_NUM = -1;
41const int LinkStateRoutingTableCalculator::NO_NEXT_HOP = -12345;
42
akmhoque53353462014-04-22 08:43:45 -050043void
44RoutingTableCalculator::allocateAdjMatrix()
45{
Vince Lehman9a709032014-09-13 16:28:07 -050046 adjMatrix = new double*[m_nRouters];
47
48 for (size_t i = 0; i < m_nRouters; ++i) {
49 adjMatrix[i] = new double[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -050050 }
51}
52
53void
54RoutingTableCalculator::initMatrix()
55{
Vince Lehman9a709032014-09-13 16:28:07 -050056 for (size_t i = 0; i < m_nRouters; i++) {
57 for (size_t j = 0; j < m_nRouters; j++) {
dulalsaurabd0816a32019-07-26 13:11:24 +000058 adjMatrix[i][j] = Adjacent::NON_ADJACENT_COST;
akmhoque157b0a42014-05-13 00:26:37 -050059 }
akmhoque53353462014-04-22 08:43:45 -050060 }
61}
62
63void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060064RoutingTableCalculator::makeAdjMatrix(const std::list<AdjLsa>& adjLsaList, Map& pMap)
akmhoque53353462014-04-22 08:43:45 -050065{
Nick G97e34942016-07-11 14:46:27 -050066 // For each LSA represented in the map
Ashlesh Gawande85998a12017-12-07 22:22:13 -060067 for (const auto& adjLsa : adjLsaList) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080068 ndn::optional<int32_t> row = pMap.getMappingNoByRouterName(adjLsa.getOriginRouter());
Vince Lehman9a709032014-09-13 16:28:07 -050069
Ashlesh Gawande85998a12017-12-07 22:22:13 -060070 std::list<Adjacent> adl = adjLsa.getAdl().getAdjList();
Nick G97e34942016-07-11 14:46:27 -050071 // For each adjacency represented in the LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -060072 for (const auto& adjacent : adl) {
73 ndn::optional<int32_t> col = pMap.getMappingNoByRouterName(adjacent.getName());
74 double cost = adjacent.getLinkCost();
Vince Lehman9a709032014-09-13 16:28:07 -050075
Nick Gordone40377d2017-08-11 15:10:02 -050076 if (row && col && *row < static_cast<int32_t>(m_nRouters)
77 && *col < static_cast<int32_t>(m_nRouters))
Vince Lehman9a709032014-09-13 16:28:07 -050078 {
Nick Gordone40377d2017-08-11 15:10:02 -050079 adjMatrix[*row][*col] = cost;
akmhoque53353462014-04-22 08:43:45 -050080 }
81 }
82 }
Vince Lehman41b173e2015-05-07 14:13:26 -050083
Nick G97e34942016-07-11 14:46:27 -050084 // Links that do not have the same cost for both directions should
85 // have their costs corrected:
Vince Lehman41b173e2015-05-07 14:13:26 -050086 //
dulalsaurabd0816a32019-07-26 13:11:24 +000087 // If the cost of one side of the link is NON_ADJACENT_COST (i.e. broken) or negative,
88 // both direction of the link should have their cost corrected to NON_ADJACENT_COST.
Vince Lehman41b173e2015-05-07 14:13:26 -050089 //
90 // Otherwise, both sides of the link should use the larger of the two costs.
91 //
Nick G97e34942016-07-11 14:46:27 -050092 // Additionally, this means that we can halve the amount of space
93 // that the matrix uses by only maintaining a triangle.
94 // - But that is not yet implemented.
Vince Lehman41b173e2015-05-07 14:13:26 -050095 for (size_t row = 0; row < m_nRouters; ++row) {
96 for (size_t col = 0; col < m_nRouters; ++col) {
97 double toCost = adjMatrix[row][col];
98 double fromCost = adjMatrix[col][row];
99
100 if (fromCost != toCost) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000101 double correctedCost = Adjacent::NON_ADJACENT_COST;
Vince Lehman41b173e2015-05-07 14:13:26 -0500102
dulalsaurabd0816a32019-07-26 13:11:24 +0000103 if (toCost >= 0 && fromCost >= 0) {
104 // If both sides of the link are up, use the larger cost else break the link
Vince Lehman41b173e2015-05-07 14:13:26 -0500105 correctedCost = std::max(toCost, fromCost);
106 }
107
dmcoomes5bcb39e2017-10-31 15:07:55 -0500108 NLSR_LOG_WARN("Cost between [" << row << "][" << col << "] and [" << col << "][" << row <<
Vince Lehman41b173e2015-05-07 14:13:26 -0500109 "] are not the same (" << toCost << " != " << fromCost << "). " <<
110 "Correcting to cost: " << correctedCost);
111
112 adjMatrix[row][col] = correctedCost;
113 adjMatrix[col][row] = correctedCost;
114 }
115 }
116 }
akmhoque53353462014-04-22 08:43:45 -0500117}
118
119void
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000120RoutingTableCalculator::writeAdjMatrixLog(const Map& map) const
akmhoque53353462014-04-22 08:43:45 -0500121{
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500122 if (!ndn_cxx_getLogger().isLevelEnabled(ndn::util::LogLevel::DEBUG)) {
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000123 return;
124 }
125
126 NLSR_LOG_DEBUG("-----------Legend (routerName -> index)------");
127 std::string routerIndex;
128 std::string indexToNameMapping;
129 std::string lengthOfDash = "--";
130
Vince Lehman9a709032014-09-13 16:28:07 -0500131 for (size_t i = 0; i < m_nRouters; i++) {
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000132 routerIndex += boost::lexical_cast<std::string>(i);
133 routerIndex += " ";
134 lengthOfDash += "--";
135 NLSR_LOG_DEBUG("Router:" + map.getRouterNameByMappingNo(i)->toUri() +
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500136 " Index:" + boost::lexical_cast<std::string>(i));
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000137 }
138 NLSR_LOG_DEBUG(" |" + routerIndex);
139 NLSR_LOG_DEBUG(lengthOfDash);
140
141 for (size_t i = 0; i < m_nRouters; i++) {
142 std::string line;
Vince Lehman9a709032014-09-13 16:28:07 -0500143 for (size_t j = 0; j < m_nRouters; j++) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500144 if (adjMatrix[i][j] == LinkStateRoutingTableCalculator::NO_NEXT_HOP) {
145 line += "0 ";
146 }
147 else {
148 line += boost::lexical_cast<std::string>(adjMatrix[i][j]);
149 line += " ";
150 }
akmhoque157b0a42014-05-13 00:26:37 -0500151 }
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000152 line = boost::lexical_cast<std::string>(i) + "|" + line;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500153 NLSR_LOG_DEBUG(line);
akmhoque53353462014-04-22 08:43:45 -0500154 }
155}
156
157void
158RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
159{
Vince Lehman9a709032014-09-13 16:28:07 -0500160 for (int i = 0; i < static_cast<int>(m_nRouters); i++) {
akmhoque157b0a42014-05-13 00:26:37 -0500161 if (i == link) {
akmhoque53353462014-04-22 08:43:45 -0500162 adjMatrix[source][i] = linkCost;
163 }
akmhoque157b0a42014-05-13 00:26:37 -0500164 else {
dulalsaurabd0816a32019-07-26 13:11:24 +0000165 // if "i" is not a link to the source, set it's cost to a non adjacent value.
166 adjMatrix[source][i] = Adjacent::NON_ADJACENT_COST;
akmhoque53353462014-04-22 08:43:45 -0500167 }
168 }
169}
170
171int
172RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
173{
174 int noLink = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500175
176 for (size_t i = 0; i < m_nRouters; i++) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000177 if (adjMatrix[sRouter][i] >= 0 && i != static_cast<size_t>(sRouter)) { // make sure "i" is not self
akmhoque53353462014-04-22 08:43:45 -0500178 noLink++;
179 }
180 }
181 return noLink;
182}
183
184void
185RoutingTableCalculator::getLinksFromAdjMatrix(int* links,
186 double* linkCosts, int source)
187{
188 int j = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500189
190 for (size_t i = 0; i < m_nRouters; i++) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000191 if (adjMatrix[source][i] >= 0 && i != static_cast<size_t>(source)) {// make sure "i" is not self
akmhoque53353462014-04-22 08:43:45 -0500192 links[j] = i;
193 linkCosts[j] = adjMatrix[source][i];
194 j++;
195 }
196 }
197}
198
199void
200RoutingTableCalculator::freeAdjMatrix()
201{
Vince Lehman9a709032014-09-13 16:28:07 -0500202 for (size_t i = 0; i < m_nRouters; ++i) {
akmhoque53353462014-04-22 08:43:45 -0500203 delete [] adjMatrix[i];
204 }
205 delete [] adjMatrix;
206}
207
akmhoque53353462014-04-22 08:43:45 -0500208void
209RoutingTableCalculator::allocateLinks()
210{
211 links = new int[vNoLink];
212}
213
214void
215RoutingTableCalculator::allocateLinkCosts()
216{
217 linkCosts = new double[vNoLink];
218}
219
220
221void
222RoutingTableCalculator::freeLinks()
223{
224 delete [] links;
225}
226void
227RoutingTableCalculator::freeLinksCosts()
228{
229 delete [] linkCosts;
230}
231
232void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600233LinkStateRoutingTableCalculator::calculatePath(Map& pMap, RoutingTable& rt,
234 ConfParameter& confParam,
235 const std::list<AdjLsa>& adjLsaList)
akmhoque53353462014-04-22 08:43:45 -0500236{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500237 NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called");
akmhoque53353462014-04-22 08:43:45 -0500238 allocateAdjMatrix();
239 initMatrix();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600240 makeAdjMatrix(adjLsaList, pMap);
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000241 writeAdjMatrixLog(pMap);
Nick Gordone40377d2017-08-11 15:10:02 -0500242 ndn::optional<int32_t> sourceRouter =
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600243 pMap.getMappingNoByRouterName(confParam.getRouterPrefix());
Nick G97e34942016-07-11 14:46:27 -0500244 allocateParent(); // These two matrices are used in Dijkstra's algorithm.
245 allocateDistance(); //
Nick Gordone40377d2017-08-11 15:10:02 -0500246 // We only bother to do the calculation if we have a router by that name.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600247 if (sourceRouter && confParam.getMaxFacesPerPrefix() == 1) {
Nick G97e34942016-07-11 14:46:27 -0500248 // In the single path case we can simply run Dijkstra's algorithm.
Nick Gordone40377d2017-08-11 15:10:02 -0500249 doDijkstraPathCalculation(*sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500250 // Inform the routing table of the new next hops.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600251 addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500252 }
akmhoque157b0a42014-05-13 00:26:37 -0500253 else {
akmhoque53353462014-04-22 08:43:45 -0500254 // Multi Path
Nick Gordone40377d2017-08-11 15:10:02 -0500255 setNoLink(getNumOfLinkfromAdjMatrix(*sourceRouter));
akmhoque53353462014-04-22 08:43:45 -0500256 allocateLinks();
257 allocateLinkCosts();
Nick G97e34942016-07-11 14:46:27 -0500258 // Gets a sparse listing of adjacencies for path calculation
Nick Gordone40377d2017-08-11 15:10:02 -0500259 getLinksFromAdjMatrix(links, linkCosts, *sourceRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500260 for (int i = 0 ; i < vNoLink; i++) {
Nick G97e34942016-07-11 14:46:27 -0500261 // Simulate that only the current neighbor is accessible
Nick Gordone40377d2017-08-11 15:10:02 -0500262 adjustAdMatrix(*sourceRouter, links[i], linkCosts[i]);
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000263 writeAdjMatrixLog(pMap);
Nick G97e34942016-07-11 14:46:27 -0500264 // Do Dijkstra's algorithm using the current neighbor as your start.
Nick Gordone40377d2017-08-11 15:10:02 -0500265 doDijkstraPathCalculation(*sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500266 // Update the routing table with the calculations.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600267 addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500268 }
269 freeLinks();
270 freeLinksCosts();
271 }
272 freeParent();
273 freeDistance();
274 freeAdjMatrix();
275}
276
277void
278LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)
279{
280 int i;
281 int v, u;
Nick G97e34942016-07-11 14:46:27 -0500282 int* Q = new int[m_nRouters]; // Each cell represents the router with that mapping no.
akmhoque53353462014-04-22 08:43:45 -0500283 int head = 0;
Nick Gordone98480b2017-05-24 11:23:03 -0500284 // Initiate the parent
Vince Lehman9a709032014-09-13 16:28:07 -0500285 for (i = 0 ; i < static_cast<int>(m_nRouters); i++) {
akmhoque53353462014-04-22 08:43:45 -0500286 m_parent[i] = EMPTY_PARENT;
Nick G97e34942016-07-11 14:46:27 -0500287 // Array where the ith element is the distance to the router with mapping no i.
akmhoque53353462014-04-22 08:43:45 -0500288 m_distance[i] = INF_DISTANCE;
289 Q[i] = i;
290 }
akmhoque157b0a42014-05-13 00:26:37 -0500291 if (sourceRouter != NO_MAPPING_NUM) {
Nick G97e34942016-07-11 14:46:27 -0500292 // Distance to source from source is always 0.
akmhoque53353462014-04-22 08:43:45 -0500293 m_distance[sourceRouter] = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500294 sortQueueByDistance(Q, m_distance, head, m_nRouters);
Nick G97e34942016-07-11 14:46:27 -0500295 // While we haven't visited every node.
Vince Lehman9a709032014-09-13 16:28:07 -0500296 while (head < static_cast<int>(m_nRouters)) {
Nick G97e34942016-07-11 14:46:27 -0500297 u = Q[head]; // Set u to be the current node pointed to by head.
akmhoque157b0a42014-05-13 00:26:37 -0500298 if (m_distance[u] == INF_DISTANCE) {
Nick G97e34942016-07-11 14:46:27 -0500299 break; // This can only happen when there are no accessible nodes.
akmhoque53353462014-04-22 08:43:45 -0500300 }
Nick G97e34942016-07-11 14:46:27 -0500301 // Iterate over the adjacent nodes to u.
Vince Lehman9a709032014-09-13 16:28:07 -0500302 for (v = 0 ; v < static_cast<int>(m_nRouters); v++) {
Nick G97e34942016-07-11 14:46:27 -0500303 // If the current node is accessible.
dulalsaurabd0816a32019-07-26 13:11:24 +0000304 if (adjMatrix[u][v] >= 0) {
Nick G97e34942016-07-11 14:46:27 -0500305 // And we haven't visited it yet.
Vince Lehman9a709032014-09-13 16:28:07 -0500306 if (isNotExplored(Q, v, head + 1, m_nRouters)) {
Nick G97e34942016-07-11 14:46:27 -0500307 // And if the distance to this node + from this node to v
308 // is less than the distance from our source node to v
309 // that we got when we built the adj LSAs
akmhoque157b0a42014-05-13 00:26:37 -0500310 if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) {
Nick G97e34942016-07-11 14:46:27 -0500311 // Set the new distance
akmhoque53353462014-04-22 08:43:45 -0500312 m_distance[v] = m_distance[u] + adjMatrix[u][v] ;
Nick G97e34942016-07-11 14:46:27 -0500313 // Set how we get there.
akmhoque53353462014-04-22 08:43:45 -0500314 m_parent[v] = u;
315 }
316 }
317 }
318 }
Nick G97e34942016-07-11 14:46:27 -0500319 // Increment the head position, resort the list by distance from where we are.
akmhoque53353462014-04-22 08:43:45 -0500320 head++;
Vince Lehman9a709032014-09-13 16:28:07 -0500321 sortQueueByDistance(Q, m_distance, head, m_nRouters);
akmhoque53353462014-04-22 08:43:45 -0500322 }
323 }
324 delete [] Q;
325}
326
327void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600328LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies,
329 RoutingTable& rt, Map& pMap,
330 uint32_t sourceRouter)
akmhoque53353462014-04-22 08:43:45 -0500331{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500332 NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500333
akmhoque53353462014-04-22 08:43:45 -0500334 int nextHopRouter = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500335
Nick G97e34942016-07-11 14:46:27 -0500336 // For each router we have
Vince Lehman9a709032014-09-13 16:28:07 -0500337 for (size_t i = 0; i < m_nRouters ; i++) {
akmhoque157b0a42014-05-13 00:26:37 -0500338 if (i != sourceRouter) {
Vince Lehman9a709032014-09-13 16:28:07 -0500339
Nick G97e34942016-07-11 14:46:27 -0500340 // Obtain the next hop that was determined by the algorithm
akmhoque53353462014-04-22 08:43:45 -0500341 nextHopRouter = getLsNextHop(i, sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500342 // If this router is accessible at all
akmhoque157b0a42014-05-13 00:26:37 -0500343 if (nextHopRouter != NO_NEXT_HOP) {
Vince Lehman9a709032014-09-13 16:28:07 -0500344
Nick G97e34942016-07-11 14:46:27 -0500345 // Fetch its distance
akmhoque53353462014-04-22 08:43:45 -0500346 double routeCost = m_distance[i];
Nick G97e34942016-07-11 14:46:27 -0500347 // Fetch its actual name
Nick Gordone40377d2017-08-11 15:10:02 -0500348 ndn::optional<ndn::Name> nextHopRouterName= pMap.getRouterNameByMappingNo(nextHopRouter);
349 if (nextHopRouterName) {
350 std::string nextHopFace =
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600351 adjacencies.getAdjacent(*nextHopRouterName).getFaceUri().toString();
Nick Gordone40377d2017-08-11 15:10:02 -0500352 // Add next hop to routing table
353 NextHop nh(nextHopFace, routeCost);
354 rt.addNextHop(*(pMap.getRouterNameByMappingNo(i)), nh);
355
356 }
akmhoque53353462014-04-22 08:43:45 -0500357 }
358 }
359 }
360}
361
362int
363LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
364{
365 int nextHop = NO_NEXT_HOP;
akmhoque157b0a42014-05-13 00:26:37 -0500366 while (m_parent[dest] != EMPTY_PARENT) {
akmhoque53353462014-04-22 08:43:45 -0500367 nextHop = dest;
368 dest = m_parent[dest];
369 }
akmhoque157b0a42014-05-13 00:26:37 -0500370 if (dest != source) {
akmhoque53353462014-04-22 08:43:45 -0500371 nextHop = NO_NEXT_HOP;
372 }
373 return nextHop;
374}
375
376void
akmhoque53353462014-04-22 08:43:45 -0500377LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q,
akmhoque2f423352014-06-03 11:49:35 -0500378 double* dist,
379 int start, int element)
akmhoque53353462014-04-22 08:43:45 -0500380{
akmhoque157b0a42014-05-13 00:26:37 -0500381 for (int i = start ; i < element ; i++) {
382 for (int j = i + 1; j < element; j++) {
383 if (dist[Q[j]] < dist[Q[i]]) {
akmhoque53353462014-04-22 08:43:45 -0500384 int tempU = Q[j];
385 Q[j] = Q[i];
386 Q[i] = tempU;
387 }
388 }
389 }
390}
391
392int
393LinkStateRoutingTableCalculator::isNotExplored(int* Q,
394 int u, int start, int element)
395{
396 int ret = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500397 for (int i = start; i < element; i++) {
398 if (Q[i] == u) {
akmhoque53353462014-04-22 08:43:45 -0500399 ret = 1;
400 break;
401 }
402 }
403 return ret;
404}
405
406void
407LinkStateRoutingTableCalculator::allocateParent()
408{
Vince Lehman9a709032014-09-13 16:28:07 -0500409 m_parent = new int[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -0500410}
411
412void
413LinkStateRoutingTableCalculator::allocateDistance()
414{
Vince Lehman9a709032014-09-13 16:28:07 -0500415 m_distance = new double[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -0500416}
417
418void
419LinkStateRoutingTableCalculator::freeParent()
420{
421 delete [] m_parent;
422}
423
424void LinkStateRoutingTableCalculator::freeDistance()
425{
426 delete [] m_distance;
427}
428
Vince Lehman9a709032014-09-13 16:28:07 -0500429const double HyperbolicRoutingCalculator::MATH_PI = boost::math::constants::pi<double>();
akmhoque53353462014-04-22 08:43:45 -0500430
Vince Lehman9a709032014-09-13 16:28:07 -0500431const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0;
432const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS = -1.0;
433
akmhoque53353462014-04-22 08:43:45 -0500434void
Saurab Dulal72b2b252019-01-22 16:58:08 -0600435HyperbolicRoutingCalculator::calculatePath(Map& map, RoutingTable& rt,
436 Lsdb& lsdb, AdjacencyList& adjacencies)
akmhoque53353462014-04-22 08:43:45 -0500437{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500438 NLSR_LOG_TRACE("Calculating hyperbolic paths");
Vince Lehman9a709032014-09-13 16:28:07 -0500439
Nick Gordone40377d2017-08-11 15:10:02 -0500440 ndn::optional<int32_t> thisRouter = map.getMappingNoByRouterName(m_thisRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500441
442 // Iterate over directly connected neighbors
443 std::list<Adjacent> neighbors = adjacencies.getAdjList();
444 for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) {
445
446 // Don't calculate nexthops using an inactive router
447 if (adj->getStatus() == Adjacent::STATUS_INACTIVE) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500448 NLSR_LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop");
Vince Lehman9a709032014-09-13 16:28:07 -0500449 continue;
450 }
451
452 ndn::Name srcRouterName = adj->getName();
453
454 // Don't calculate nexthops for this router to other routers
455 if (srcRouterName == m_thisRouterName) {
456 continue;
457 }
458
Nick Gordone9733ed2017-04-26 10:48:39 -0500459 std::string srcFaceUri = adj->getFaceUri().toString();
Vince Lehman9a709032014-09-13 16:28:07 -0500460
461 // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link
462 addNextHop(srcRouterName, srcFaceUri, 0, rt);
463
Nick Gordone40377d2017-08-11 15:10:02 -0500464 ndn::optional<int32_t> src = map.getMappingNoByRouterName(srcRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500465
Nick Gordone40377d2017-08-11 15:10:02 -0500466 if (!src) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500467 NLSR_LOG_WARN(adj->getName() << " does not exist in the router map!");
Vince Lehman9a709032014-09-13 16:28:07 -0500468 continue;
469 }
470
471 // Get hyperbolic distance from direct neighbor to every other router
472 for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) {
473 // Don't calculate nexthops to this router or from a router to itself
Nick Gordone40377d2017-08-11 15:10:02 -0500474 if (thisRouter && dest != *thisRouter && dest != *src) {
Vince Lehman9a709032014-09-13 16:28:07 -0500475
Nick Gordone40377d2017-08-11 15:10:02 -0500476 ndn::optional<ndn::Name> destRouterName = map.getRouterNameByMappingNo(dest);
477 if (destRouterName) {
Saurab Dulal72b2b252019-01-22 16:58:08 -0600478 double distance = getHyperbolicDistance(lsdb, srcRouterName, *destRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500479
Nick Gordone40377d2017-08-11 15:10:02 -0500480 // Could not compute distance
481 if (distance == UNKNOWN_DISTANCE) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000482 NLSR_LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName
483 << " to " << *destRouterName);
Nick Gordone40377d2017-08-11 15:10:02 -0500484 continue;
485 }
Nick Gordone40377d2017-08-11 15:10:02 -0500486 addNextHop(*destRouterName, srcFaceUri, distance, rt);
akmhoque53353462014-04-22 08:43:45 -0500487 }
Vince Lehman9a709032014-09-13 16:28:07 -0500488 }
akmhoquedcee9362014-08-05 22:58:01 -0500489 }
akmhoque53353462014-04-22 08:43:45 -0500490 }
491}
492
493double
Saurab Dulal72b2b252019-01-22 16:58:08 -0600494HyperbolicRoutingCalculator::getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, ndn::Name dest)
akmhoque53353462014-04-22 08:43:45 -0500495{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500496 NLSR_LOG_TRACE("Calculating hyperbolic distance from " << src << " to " << dest);
akmhoquedcee9362014-08-05 22:58:01 -0500497
Vince Lehman9a709032014-09-13 16:28:07 -0500498 double distance = UNKNOWN_DISTANCE;
499
500 ndn::Name srcLsaKey = src;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800501 srcLsaKey.append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
Vince Lehman9a709032014-09-13 16:28:07 -0500502
503 CoordinateLsa* srcLsa = lsdb.findCoordinateLsa(srcLsaKey);
504
505 ndn::Name destLsaKey = dest;
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800506 destLsaKey.append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
Vince Lehman9a709032014-09-13 16:28:07 -0500507
508 CoordinateLsa* destLsa = lsdb.findCoordinateLsa(destLsaKey);
509
510 // Coordinate LSAs do not exist for these routers
Nick Gordone9733ed2017-04-26 10:48:39 -0500511 if (srcLsa == nullptr || destLsa == nullptr) {
Vince Lehman9a709032014-09-13 16:28:07 -0500512 return UNKNOWN_DISTANCE;
akmhoquedcee9362014-08-05 22:58:01 -0500513 }
514
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600515 std::vector<double> srcTheta = srcLsa->getCorTheta();
516 std::vector<double> destTheta = destLsa->getCorTheta();
Vince Lehman9a709032014-09-13 16:28:07 -0500517
518 double srcRadius = srcLsa->getCorRadius();
519 double destRadius = destLsa->getCorRadius();
520
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600521 double diffTheta = calculateAngularDistance(srcTheta, destTheta);
522
523 if (srcRadius == UNKNOWN_RADIUS || destRadius == UNKNOWN_RADIUS ||
524 diffTheta == UNKNOWN_DISTANCE) {
Vince Lehman9a709032014-09-13 16:28:07 -0500525 return UNKNOWN_DISTANCE;
526 }
527
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600528 // double r_i, double r_j, double delta_theta, double zeta = 1 (default)
529 distance = calculateHyperbolicDistance(srcRadius, destRadius, diffTheta);
Vince Lehman9a709032014-09-13 16:28:07 -0500530
dmcoomes5bcb39e2017-10-31 15:07:55 -0500531 NLSR_LOG_TRACE("Distance from " << src << " to " << dest << " is " << distance);
Vince Lehman9a709032014-09-13 16:28:07 -0500532
akmhoque53353462014-04-22 08:43:45 -0500533 return distance;
534}
535
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600536double
537HyperbolicRoutingCalculator::calculateAngularDistance(std::vector<double> angleVectorI,
538 std::vector<double> angleVectorJ)
539{
540 // It is not possible for angle vector size to be zero as ensured by conf-file-processor
541
542 // https://en.wikipedia.org/wiki/N-sphere#Spherical_coordinates
543
544 // Check if two vector lengths are the same
545 if (angleVectorI.size() != angleVectorJ.size()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500546 NLSR_LOG_ERROR("Angle vector sizes do not match");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600547 return UNKNOWN_DISTANCE;
548 }
549
550 // Check if all angles are within the [0, PI] and [0, 2PI] ranges
551 if (angleVectorI.size() > 1) {
552 for (unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
553 if ((angleVectorI[k] > M_PI && angleVectorI[k] < 0.0) ||
554 (angleVectorJ[k] > M_PI && angleVectorJ[k] < 0.0)) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500555 NLSR_LOG_ERROR("Angle outside [0, PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600556 return UNKNOWN_DISTANCE;
557 }
558 }
559 }
560 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
561 angleVectorI[angleVectorI.size()-1] < 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500562 NLSR_LOG_ERROR("Angle not within [0, 2PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600563 return UNKNOWN_DISTANCE;
564 }
565
566 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
567 angleVectorI[angleVectorI.size()-1] < 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500568 NLSR_LOG_ERROR("Angle not within [0, 2PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600569 return UNKNOWN_DISTANCE;
570 }
571
572 // deltaTheta = arccos(vectorI . vectorJ) -> do the inner product
573 double innerProduct = 0.0;
574
575 // Calculate x0 of the vectors
576 double x0i = std::cos(angleVectorI[0]);
577 double x0j = std::cos(angleVectorJ[0]);
578
579 // Calculate xn of the vectors
580 double xni = std::sin(angleVectorI[angleVectorI.size() - 1]);
581 double xnj = std::sin(angleVectorJ[angleVectorJ.size() - 1]);
582
583 // Do the aggregation of the (n-1) coordinates (if there is more than one angle)
584 // i.e contraction of all (n-1)-dimensional angular coordinates to one variable
585 for (unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
586 xni *= std::sin(angleVectorI[k]);
587 xnj *= std::sin(angleVectorJ[k]);
588 }
589 innerProduct += (x0i * x0j) + (xni * xnj);
590
591 // If d > 1
592 if (angleVectorI.size() > 1) {
593 for (unsigned int m = 1; m < angleVectorI.size(); m++) {
594 // calculate euclidean coordinates given the angles and assuming R_sphere = 1
595 double xmi = std::cos(angleVectorI[m]);
596 double xmj = std::cos(angleVectorJ[m]);
597 for (unsigned int l = 0; l < m; l++) {
598 xmi *= std::sin(angleVectorI[l]);
599 xmj *= std::sin(angleVectorJ[l]);
600 }
601 innerProduct += xmi * xmj;
602 }
603 }
604
605 // ArcCos of the inner product gives the angular distance
606 // between two points on a d-dimensional sphere
607 return std::acos(innerProduct);
608}
609
610double
611HyperbolicRoutingCalculator::calculateHyperbolicDistance(double rI, double rJ,
612 double deltaTheta)
613{
614 if (deltaTheta == UNKNOWN_DISTANCE) {
615 return UNKNOWN_DISTANCE;
616 }
617
618 // Usually, we set zeta = 1 in all experiments
619 double zeta = 1;
620
621 if (deltaTheta <= 0.0 || rI <= 0.0 || rJ <= 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500622 NLSR_LOG_ERROR("Delta theta or rI or rJ is <= 0");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500623 NLSR_LOG_ERROR("Please make sure that no two nodes have the exact same HR coordinates");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600624 return UNKNOWN_DISTANCE;
625 }
626
627 double xij = (1. / zeta) * std::acosh(std::cosh(zeta*rI) * std::cosh(zeta*rJ) -
628 std::sinh(zeta*rI)*std::sinh(zeta*rJ)*std::cos(deltaTheta));
629 return xij;
630}
631
632void
633HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri,
634 double cost, RoutingTable& rt)
akmhoque53353462014-04-22 08:43:45 -0500635{
Vince Lehman9a709032014-09-13 16:28:07 -0500636 NextHop hop(faceUri, cost);
Vince Lehman20fe4a92014-09-09 15:57:59 -0500637 hop.setHyperbolic(true);
akmhoque53353462014-04-22 08:43:45 -0500638
dmcoomes5bcb39e2017-10-31 15:07:55 -0500639 NLSR_LOG_TRACE("Calculated " << hop << " for destination: " << dest);
akmhoque53353462014-04-22 08:43:45 -0500640
Vince Lehman9a709032014-09-13 16:28:07 -0500641 if (m_isDryRun) {
642 rt.addNextHopToDryTable(dest, hop);
643 }
644 else {
645 rt.addNextHop(dest, hop);
646 }
akmhoque53353462014-04-22 08:43:45 -0500647}
648
Nick Gordonfad8e252016-08-11 14:21:38 -0500649} // namespace nlsr