blob: a0799337029bd5b0597201dfb1301e85ff3affdb [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal72b2b252019-01-22 16:58:08 -06003 * Copyright (c) 2014-2019, 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
Nick Gordone98480b2017-05-24 11:23:03 -050030#include <iostream>
Vince Lehman9a709032014-09-13 16:28:07 -050031#include <boost/math/constants/constants.hpp>
Saurab Dulal9da6aa72018-01-12 17:25:51 +000032#include <ndn-cxx/util/logger.hpp>
Nick Gordone98480b2017-05-24 11:23:03 -050033#include <cmath>
Vince Lehman9a709032014-09-13 16:28:07 -050034
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
36
dmcoomescf8d0ed2017-02-21 11:39:01 -060037INIT_LOGGER(route.RoutingTableCalculator);
akmhoque53353462014-04-22 08:43:45 -050038
dulalsaurabd0816a32019-07-26 13:11:24 +000039const int LinkStateRoutingTableCalculator::EMPTY_PARENT = -12345;
40const double LinkStateRoutingTableCalculator::INF_DISTANCE = 2147483647;
41const int LinkStateRoutingTableCalculator::NO_MAPPING_NUM = -1;
42const int LinkStateRoutingTableCalculator::NO_NEXT_HOP = -12345;
43
akmhoque53353462014-04-22 08:43:45 -050044void
45RoutingTableCalculator::allocateAdjMatrix()
46{
Vince Lehman9a709032014-09-13 16:28:07 -050047 adjMatrix = new double*[m_nRouters];
48
49 for (size_t i = 0; i < m_nRouters; ++i) {
50 adjMatrix[i] = new double[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -050051 }
52}
53
54void
55RoutingTableCalculator::initMatrix()
56{
Vince Lehman9a709032014-09-13 16:28:07 -050057 for (size_t i = 0; i < m_nRouters; i++) {
58 for (size_t j = 0; j < m_nRouters; j++) {
dulalsaurabd0816a32019-07-26 13:11:24 +000059 adjMatrix[i][j] = Adjacent::NON_ADJACENT_COST;
akmhoque157b0a42014-05-13 00:26:37 -050060 }
akmhoque53353462014-04-22 08:43:45 -050061 }
62}
63
64void
Ashlesh Gawande85998a12017-12-07 22:22:13 -060065RoutingTableCalculator::makeAdjMatrix(const std::list<AdjLsa>& adjLsaList, Map& pMap)
akmhoque53353462014-04-22 08:43:45 -050066{
Nick G97e34942016-07-11 14:46:27 -050067 // For each LSA represented in the map
Ashlesh Gawande85998a12017-12-07 22:22:13 -060068 for (const auto& adjLsa : adjLsaList) {
69 ndn::optional<int32_t> row = pMap.getMappingNoByRouterName(adjLsa.getOrigRouter());
Vince Lehman9a709032014-09-13 16:28:07 -050070
Ashlesh Gawande85998a12017-12-07 22:22:13 -060071 std::list<Adjacent> adl = adjLsa.getAdl().getAdjList();
Nick G97e34942016-07-11 14:46:27 -050072 // For each adjacency represented in the LSA
Ashlesh Gawande85998a12017-12-07 22:22:13 -060073 for (const auto& adjacent : adl) {
74 ndn::optional<int32_t> col = pMap.getMappingNoByRouterName(adjacent.getName());
75 double cost = adjacent.getLinkCost();
Vince Lehman9a709032014-09-13 16:28:07 -050076
Nick Gordone40377d2017-08-11 15:10:02 -050077 if (row && col && *row < static_cast<int32_t>(m_nRouters)
78 && *col < static_cast<int32_t>(m_nRouters))
Vince Lehman9a709032014-09-13 16:28:07 -050079 {
Nick Gordone40377d2017-08-11 15:10:02 -050080 adjMatrix[*row][*col] = cost;
akmhoque53353462014-04-22 08:43:45 -050081 }
82 }
83 }
Vince Lehman41b173e2015-05-07 14:13:26 -050084
Nick G97e34942016-07-11 14:46:27 -050085 // Links that do not have the same cost for both directions should
86 // have their costs corrected:
Vince Lehman41b173e2015-05-07 14:13:26 -050087 //
dulalsaurabd0816a32019-07-26 13:11:24 +000088 // If the cost of one side of the link is NON_ADJACENT_COST (i.e. broken) or negative,
89 // both direction of the link should have their cost corrected to NON_ADJACENT_COST.
Vince Lehman41b173e2015-05-07 14:13:26 -050090 //
91 // Otherwise, both sides of the link should use the larger of the two costs.
92 //
Nick G97e34942016-07-11 14:46:27 -050093 // Additionally, this means that we can halve the amount of space
94 // that the matrix uses by only maintaining a triangle.
95 // - But that is not yet implemented.
Vince Lehman41b173e2015-05-07 14:13:26 -050096 for (size_t row = 0; row < m_nRouters; ++row) {
97 for (size_t col = 0; col < m_nRouters; ++col) {
98 double toCost = adjMatrix[row][col];
99 double fromCost = adjMatrix[col][row];
100
101 if (fromCost != toCost) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000102 double correctedCost = Adjacent::NON_ADJACENT_COST;
Vince Lehman41b173e2015-05-07 14:13:26 -0500103
dulalsaurabd0816a32019-07-26 13:11:24 +0000104 if (toCost >= 0 && fromCost >= 0) {
105 // If both sides of the link are up, use the larger cost else break the link
Vince Lehman41b173e2015-05-07 14:13:26 -0500106 correctedCost = std::max(toCost, fromCost);
107 }
108
dmcoomes5bcb39e2017-10-31 15:07:55 -0500109 NLSR_LOG_WARN("Cost between [" << row << "][" << col << "] and [" << col << "][" << row <<
Vince Lehman41b173e2015-05-07 14:13:26 -0500110 "] are not the same (" << toCost << " != " << fromCost << "). " <<
111 "Correcting to cost: " << correctedCost);
112
113 adjMatrix[row][col] = correctedCost;
114 adjMatrix[col][row] = correctedCost;
115 }
116 }
117 }
akmhoque53353462014-04-22 08:43:45 -0500118}
119
120void
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000121RoutingTableCalculator::writeAdjMatrixLog(const Map& map) const
akmhoque53353462014-04-22 08:43:45 -0500122{
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500123 if (!ndn_cxx_getLogger().isLevelEnabled(ndn::util::LogLevel::DEBUG)) {
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000124 return;
125 }
126
127 NLSR_LOG_DEBUG("-----------Legend (routerName -> index)------");
128 std::string routerIndex;
129 std::string indexToNameMapping;
130 std::string lengthOfDash = "--";
131
Vince Lehman9a709032014-09-13 16:28:07 -0500132 for (size_t i = 0; i < m_nRouters; i++) {
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000133 routerIndex += boost::lexical_cast<std::string>(i);
134 routerIndex += " ";
135 lengthOfDash += "--";
136 NLSR_LOG_DEBUG("Router:" + map.getRouterNameByMappingNo(i)->toUri() +
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -0500137 " Index:" + boost::lexical_cast<std::string>(i));
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000138 }
139 NLSR_LOG_DEBUG(" |" + routerIndex);
140 NLSR_LOG_DEBUG(lengthOfDash);
141
142 for (size_t i = 0; i < m_nRouters; i++) {
143 std::string line;
Vince Lehman9a709032014-09-13 16:28:07 -0500144 for (size_t j = 0; j < m_nRouters; j++) {
akmhoque2f423352014-06-03 11:49:35 -0500145 line += boost::lexical_cast<std::string>(adjMatrix[i][j]);
146 line += " ";
akmhoque157b0a42014-05-13 00:26:37 -0500147 }
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000148 line = boost::lexical_cast<std::string>(i) + "|" + line;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500149 NLSR_LOG_DEBUG(line);
akmhoque53353462014-04-22 08:43:45 -0500150 }
151}
152
153void
154RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
155{
Vince Lehman9a709032014-09-13 16:28:07 -0500156 for (int i = 0; i < static_cast<int>(m_nRouters); i++) {
akmhoque157b0a42014-05-13 00:26:37 -0500157 if (i == link) {
akmhoque53353462014-04-22 08:43:45 -0500158 adjMatrix[source][i] = linkCost;
159 }
akmhoque157b0a42014-05-13 00:26:37 -0500160 else {
dulalsaurabd0816a32019-07-26 13:11:24 +0000161 // if "i" is not a link to the source, set it's cost to a non adjacent value.
162 adjMatrix[source][i] = Adjacent::NON_ADJACENT_COST;
akmhoque53353462014-04-22 08:43:45 -0500163 }
164 }
165}
166
167int
168RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
169{
170 int noLink = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500171
172 for (size_t i = 0; i < m_nRouters; i++) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000173 if (adjMatrix[sRouter][i] >= 0 && i != static_cast<size_t>(sRouter)) { // make sure "i" is not self
akmhoque53353462014-04-22 08:43:45 -0500174 noLink++;
175 }
176 }
177 return noLink;
178}
179
180void
181RoutingTableCalculator::getLinksFromAdjMatrix(int* links,
182 double* linkCosts, int source)
183{
184 int j = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500185
186 for (size_t i = 0; i < m_nRouters; i++) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000187 if (adjMatrix[source][i] >= 0 && i != static_cast<size_t>(source)) {// make sure "i" is not self
akmhoque53353462014-04-22 08:43:45 -0500188 links[j] = i;
189 linkCosts[j] = adjMatrix[source][i];
190 j++;
191 }
192 }
193}
194
195void
196RoutingTableCalculator::freeAdjMatrix()
197{
Vince Lehman9a709032014-09-13 16:28:07 -0500198 for (size_t i = 0; i < m_nRouters; ++i) {
akmhoque53353462014-04-22 08:43:45 -0500199 delete [] adjMatrix[i];
200 }
201 delete [] adjMatrix;
202}
203
akmhoque53353462014-04-22 08:43:45 -0500204void
205RoutingTableCalculator::allocateLinks()
206{
207 links = new int[vNoLink];
208}
209
210void
211RoutingTableCalculator::allocateLinkCosts()
212{
213 linkCosts = new double[vNoLink];
214}
215
216
217void
218RoutingTableCalculator::freeLinks()
219{
220 delete [] links;
221}
222void
223RoutingTableCalculator::freeLinksCosts()
224{
225 delete [] linkCosts;
226}
227
228void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600229LinkStateRoutingTableCalculator::calculatePath(Map& pMap, RoutingTable& rt,
230 ConfParameter& confParam,
231 const std::list<AdjLsa>& adjLsaList)
akmhoque53353462014-04-22 08:43:45 -0500232{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500233 NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called");
akmhoque53353462014-04-22 08:43:45 -0500234 allocateAdjMatrix();
235 initMatrix();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600236 makeAdjMatrix(adjLsaList, pMap);
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000237 writeAdjMatrixLog(pMap);
Nick Gordone40377d2017-08-11 15:10:02 -0500238 ndn::optional<int32_t> sourceRouter =
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600239 pMap.getMappingNoByRouterName(confParam.getRouterPrefix());
Nick G97e34942016-07-11 14:46:27 -0500240 allocateParent(); // These two matrices are used in Dijkstra's algorithm.
241 allocateDistance(); //
Nick Gordone40377d2017-08-11 15:10:02 -0500242 // We only bother to do the calculation if we have a router by that name.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600243 if (sourceRouter && confParam.getMaxFacesPerPrefix() == 1) {
Nick G97e34942016-07-11 14:46:27 -0500244 // In the single path case we can simply run Dijkstra's algorithm.
Nick Gordone40377d2017-08-11 15:10:02 -0500245 doDijkstraPathCalculation(*sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500246 // Inform the routing table of the new next hops.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600247 addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500248 }
akmhoque157b0a42014-05-13 00:26:37 -0500249 else {
akmhoque53353462014-04-22 08:43:45 -0500250 // Multi Path
Nick Gordone40377d2017-08-11 15:10:02 -0500251 setNoLink(getNumOfLinkfromAdjMatrix(*sourceRouter));
akmhoque53353462014-04-22 08:43:45 -0500252 allocateLinks();
253 allocateLinkCosts();
Nick G97e34942016-07-11 14:46:27 -0500254 // Gets a sparse listing of adjacencies for path calculation
Nick Gordone40377d2017-08-11 15:10:02 -0500255 getLinksFromAdjMatrix(links, linkCosts, *sourceRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500256 for (int i = 0 ; i < vNoLink; i++) {
Nick G97e34942016-07-11 14:46:27 -0500257 // Simulate that only the current neighbor is accessible
Nick Gordone40377d2017-08-11 15:10:02 -0500258 adjustAdMatrix(*sourceRouter, links[i], linkCosts[i]);
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000259 writeAdjMatrixLog(pMap);
Nick G97e34942016-07-11 14:46:27 -0500260 // Do Dijkstra's algorithm using the current neighbor as your start.
Nick Gordone40377d2017-08-11 15:10:02 -0500261 doDijkstraPathCalculation(*sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500262 // Update the routing table with the calculations.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600263 addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500264 }
265 freeLinks();
266 freeLinksCosts();
267 }
268 freeParent();
269 freeDistance();
270 freeAdjMatrix();
271}
272
273void
274LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)
275{
276 int i;
277 int v, u;
Nick G97e34942016-07-11 14:46:27 -0500278 int* Q = new int[m_nRouters]; // Each cell represents the router with that mapping no.
akmhoque53353462014-04-22 08:43:45 -0500279 int head = 0;
Nick Gordone98480b2017-05-24 11:23:03 -0500280 // Initiate the parent
Vince Lehman9a709032014-09-13 16:28:07 -0500281 for (i = 0 ; i < static_cast<int>(m_nRouters); i++) {
akmhoque53353462014-04-22 08:43:45 -0500282 m_parent[i] = EMPTY_PARENT;
Nick G97e34942016-07-11 14:46:27 -0500283 // Array where the ith element is the distance to the router with mapping no i.
akmhoque53353462014-04-22 08:43:45 -0500284 m_distance[i] = INF_DISTANCE;
285 Q[i] = i;
286 }
akmhoque157b0a42014-05-13 00:26:37 -0500287 if (sourceRouter != NO_MAPPING_NUM) {
Nick G97e34942016-07-11 14:46:27 -0500288 // Distance to source from source is always 0.
akmhoque53353462014-04-22 08:43:45 -0500289 m_distance[sourceRouter] = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500290 sortQueueByDistance(Q, m_distance, head, m_nRouters);
Nick G97e34942016-07-11 14:46:27 -0500291 // While we haven't visited every node.
Vince Lehman9a709032014-09-13 16:28:07 -0500292 while (head < static_cast<int>(m_nRouters)) {
Nick G97e34942016-07-11 14:46:27 -0500293 u = Q[head]; // Set u to be the current node pointed to by head.
akmhoque157b0a42014-05-13 00:26:37 -0500294 if (m_distance[u] == INF_DISTANCE) {
Nick G97e34942016-07-11 14:46:27 -0500295 break; // This can only happen when there are no accessible nodes.
akmhoque53353462014-04-22 08:43:45 -0500296 }
Nick G97e34942016-07-11 14:46:27 -0500297 // Iterate over the adjacent nodes to u.
Vince Lehman9a709032014-09-13 16:28:07 -0500298 for (v = 0 ; v < static_cast<int>(m_nRouters); v++) {
Nick G97e34942016-07-11 14:46:27 -0500299 // If the current node is accessible.
dulalsaurabd0816a32019-07-26 13:11:24 +0000300 if (adjMatrix[u][v] >= 0) {
Nick G97e34942016-07-11 14:46:27 -0500301 // And we haven't visited it yet.
Vince Lehman9a709032014-09-13 16:28:07 -0500302 if (isNotExplored(Q, v, head + 1, m_nRouters)) {
Nick G97e34942016-07-11 14:46:27 -0500303 // And if the distance to this node + from this node to v
304 // is less than the distance from our source node to v
305 // that we got when we built the adj LSAs
akmhoque157b0a42014-05-13 00:26:37 -0500306 if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) {
Nick G97e34942016-07-11 14:46:27 -0500307 // Set the new distance
akmhoque53353462014-04-22 08:43:45 -0500308 m_distance[v] = m_distance[u] + adjMatrix[u][v] ;
Nick G97e34942016-07-11 14:46:27 -0500309 // Set how we get there.
akmhoque53353462014-04-22 08:43:45 -0500310 m_parent[v] = u;
311 }
312 }
313 }
314 }
Nick G97e34942016-07-11 14:46:27 -0500315 // Increment the head position, resort the list by distance from where we are.
akmhoque53353462014-04-22 08:43:45 -0500316 head++;
Vince Lehman9a709032014-09-13 16:28:07 -0500317 sortQueueByDistance(Q, m_distance, head, m_nRouters);
akmhoque53353462014-04-22 08:43:45 -0500318 }
319 }
320 delete [] Q;
321}
322
323void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600324LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies,
325 RoutingTable& rt, Map& pMap,
326 uint32_t sourceRouter)
akmhoque53353462014-04-22 08:43:45 -0500327{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500328 NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500329
akmhoque53353462014-04-22 08:43:45 -0500330 int nextHopRouter = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500331
Nick G97e34942016-07-11 14:46:27 -0500332 // For each router we have
Vince Lehman9a709032014-09-13 16:28:07 -0500333 for (size_t i = 0; i < m_nRouters ; i++) {
akmhoque157b0a42014-05-13 00:26:37 -0500334 if (i != sourceRouter) {
Vince Lehman9a709032014-09-13 16:28:07 -0500335
Nick G97e34942016-07-11 14:46:27 -0500336 // Obtain the next hop that was determined by the algorithm
akmhoque53353462014-04-22 08:43:45 -0500337 nextHopRouter = getLsNextHop(i, sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500338 // If this router is accessible at all
akmhoque157b0a42014-05-13 00:26:37 -0500339 if (nextHopRouter != NO_NEXT_HOP) {
Vince Lehman9a709032014-09-13 16:28:07 -0500340
Nick G97e34942016-07-11 14:46:27 -0500341 // Fetch its distance
akmhoque53353462014-04-22 08:43:45 -0500342 double routeCost = m_distance[i];
Nick G97e34942016-07-11 14:46:27 -0500343 // Fetch its actual name
Nick Gordone40377d2017-08-11 15:10:02 -0500344 ndn::optional<ndn::Name> nextHopRouterName= pMap.getRouterNameByMappingNo(nextHopRouter);
345 if (nextHopRouterName) {
346 std::string nextHopFace =
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600347 adjacencies.getAdjacent(*nextHopRouterName).getFaceUri().toString();
Nick Gordone40377d2017-08-11 15:10:02 -0500348 // Add next hop to routing table
349 NextHop nh(nextHopFace, routeCost);
350 rt.addNextHop(*(pMap.getRouterNameByMappingNo(i)), nh);
351
352 }
akmhoque53353462014-04-22 08:43:45 -0500353 }
354 }
355 }
356}
357
358int
359LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
360{
361 int nextHop = NO_NEXT_HOP;
akmhoque157b0a42014-05-13 00:26:37 -0500362 while (m_parent[dest] != EMPTY_PARENT) {
akmhoque53353462014-04-22 08:43:45 -0500363 nextHop = dest;
364 dest = m_parent[dest];
365 }
akmhoque157b0a42014-05-13 00:26:37 -0500366 if (dest != source) {
akmhoque53353462014-04-22 08:43:45 -0500367 nextHop = NO_NEXT_HOP;
368 }
369 return nextHop;
370}
371
372void
akmhoque53353462014-04-22 08:43:45 -0500373LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q,
akmhoque2f423352014-06-03 11:49:35 -0500374 double* dist,
375 int start, int element)
akmhoque53353462014-04-22 08:43:45 -0500376{
akmhoque157b0a42014-05-13 00:26:37 -0500377 for (int i = start ; i < element ; i++) {
378 for (int j = i + 1; j < element; j++) {
379 if (dist[Q[j]] < dist[Q[i]]) {
akmhoque53353462014-04-22 08:43:45 -0500380 int tempU = Q[j];
381 Q[j] = Q[i];
382 Q[i] = tempU;
383 }
384 }
385 }
386}
387
388int
389LinkStateRoutingTableCalculator::isNotExplored(int* Q,
390 int u, int start, int element)
391{
392 int ret = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500393 for (int i = start; i < element; i++) {
394 if (Q[i] == u) {
akmhoque53353462014-04-22 08:43:45 -0500395 ret = 1;
396 break;
397 }
398 }
399 return ret;
400}
401
402void
403LinkStateRoutingTableCalculator::allocateParent()
404{
Vince Lehman9a709032014-09-13 16:28:07 -0500405 m_parent = new int[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -0500406}
407
408void
409LinkStateRoutingTableCalculator::allocateDistance()
410{
Vince Lehman9a709032014-09-13 16:28:07 -0500411 m_distance = new double[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -0500412}
413
414void
415LinkStateRoutingTableCalculator::freeParent()
416{
417 delete [] m_parent;
418}
419
420void LinkStateRoutingTableCalculator::freeDistance()
421{
422 delete [] m_distance;
423}
424
Vince Lehman9a709032014-09-13 16:28:07 -0500425const double HyperbolicRoutingCalculator::MATH_PI = boost::math::constants::pi<double>();
akmhoque53353462014-04-22 08:43:45 -0500426
Vince Lehman9a709032014-09-13 16:28:07 -0500427const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0;
428const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS = -1.0;
429
akmhoque53353462014-04-22 08:43:45 -0500430void
Saurab Dulal72b2b252019-01-22 16:58:08 -0600431HyperbolicRoutingCalculator::calculatePath(Map& map, RoutingTable& rt,
432 Lsdb& lsdb, AdjacencyList& adjacencies)
akmhoque53353462014-04-22 08:43:45 -0500433{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500434 NLSR_LOG_TRACE("Calculating hyperbolic paths");
Vince Lehman9a709032014-09-13 16:28:07 -0500435
Nick Gordone40377d2017-08-11 15:10:02 -0500436 ndn::optional<int32_t> thisRouter = map.getMappingNoByRouterName(m_thisRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500437
438 // Iterate over directly connected neighbors
439 std::list<Adjacent> neighbors = adjacencies.getAdjList();
440 for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) {
441
442 // Don't calculate nexthops using an inactive router
443 if (adj->getStatus() == Adjacent::STATUS_INACTIVE) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500444 NLSR_LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop");
Vince Lehman9a709032014-09-13 16:28:07 -0500445 continue;
446 }
447
448 ndn::Name srcRouterName = adj->getName();
449
450 // Don't calculate nexthops for this router to other routers
451 if (srcRouterName == m_thisRouterName) {
452 continue;
453 }
454
Nick Gordone9733ed2017-04-26 10:48:39 -0500455 std::string srcFaceUri = adj->getFaceUri().toString();
Vince Lehman9a709032014-09-13 16:28:07 -0500456
457 // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link
458 addNextHop(srcRouterName, srcFaceUri, 0, rt);
459
Nick Gordone40377d2017-08-11 15:10:02 -0500460 ndn::optional<int32_t> src = map.getMappingNoByRouterName(srcRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500461
Nick Gordone40377d2017-08-11 15:10:02 -0500462 if (!src) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500463 NLSR_LOG_WARN(adj->getName() << " does not exist in the router map!");
Vince Lehman9a709032014-09-13 16:28:07 -0500464 continue;
465 }
466
467 // Get hyperbolic distance from direct neighbor to every other router
468 for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) {
469 // Don't calculate nexthops to this router or from a router to itself
Nick Gordone40377d2017-08-11 15:10:02 -0500470 if (thisRouter && dest != *thisRouter && dest != *src) {
Vince Lehman9a709032014-09-13 16:28:07 -0500471
Nick Gordone40377d2017-08-11 15:10:02 -0500472 ndn::optional<ndn::Name> destRouterName = map.getRouterNameByMappingNo(dest);
473 if (destRouterName) {
Saurab Dulal72b2b252019-01-22 16:58:08 -0600474 double distance = getHyperbolicDistance(lsdb, srcRouterName, *destRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500475
Nick Gordone40377d2017-08-11 15:10:02 -0500476 // Could not compute distance
477 if (distance == UNKNOWN_DISTANCE) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000478 NLSR_LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName
479 << " to " << *destRouterName);
Nick Gordone40377d2017-08-11 15:10:02 -0500480 continue;
481 }
Nick Gordone40377d2017-08-11 15:10:02 -0500482 addNextHop(*destRouterName, srcFaceUri, distance, rt);
akmhoque53353462014-04-22 08:43:45 -0500483 }
Vince Lehman9a709032014-09-13 16:28:07 -0500484 }
akmhoquedcee9362014-08-05 22:58:01 -0500485 }
akmhoque53353462014-04-22 08:43:45 -0500486 }
487}
488
489double
Saurab Dulal72b2b252019-01-22 16:58:08 -0600490HyperbolicRoutingCalculator::getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, ndn::Name dest)
akmhoque53353462014-04-22 08:43:45 -0500491{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500492 NLSR_LOG_TRACE("Calculating hyperbolic distance from " << src << " to " << dest);
akmhoquedcee9362014-08-05 22:58:01 -0500493
Vince Lehman9a709032014-09-13 16:28:07 -0500494 double distance = UNKNOWN_DISTANCE;
495
496 ndn::Name srcLsaKey = src;
Nick Gordon727d4832017-10-13 18:04:25 -0500497 srcLsaKey.append(std::to_string(Lsa::Type::COORDINATE));
Vince Lehman9a709032014-09-13 16:28:07 -0500498
499 CoordinateLsa* srcLsa = lsdb.findCoordinateLsa(srcLsaKey);
500
501 ndn::Name destLsaKey = dest;
Nick Gordon727d4832017-10-13 18:04:25 -0500502 destLsaKey.append(std::to_string(Lsa::Type::COORDINATE));
Vince Lehman9a709032014-09-13 16:28:07 -0500503
504 CoordinateLsa* destLsa = lsdb.findCoordinateLsa(destLsaKey);
505
506 // Coordinate LSAs do not exist for these routers
Nick Gordone9733ed2017-04-26 10:48:39 -0500507 if (srcLsa == nullptr || destLsa == nullptr) {
Vince Lehman9a709032014-09-13 16:28:07 -0500508 return UNKNOWN_DISTANCE;
akmhoquedcee9362014-08-05 22:58:01 -0500509 }
510
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600511 std::vector<double> srcTheta = srcLsa->getCorTheta();
512 std::vector<double> destTheta = destLsa->getCorTheta();
Vince Lehman9a709032014-09-13 16:28:07 -0500513
514 double srcRadius = srcLsa->getCorRadius();
515 double destRadius = destLsa->getCorRadius();
516
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600517 double diffTheta = calculateAngularDistance(srcTheta, destTheta);
518
519 if (srcRadius == UNKNOWN_RADIUS || destRadius == UNKNOWN_RADIUS ||
520 diffTheta == UNKNOWN_DISTANCE) {
Vince Lehman9a709032014-09-13 16:28:07 -0500521 return UNKNOWN_DISTANCE;
522 }
523
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600524 // double r_i, double r_j, double delta_theta, double zeta = 1 (default)
525 distance = calculateHyperbolicDistance(srcRadius, destRadius, diffTheta);
Vince Lehman9a709032014-09-13 16:28:07 -0500526
dmcoomes5bcb39e2017-10-31 15:07:55 -0500527 NLSR_LOG_TRACE("Distance from " << src << " to " << dest << " is " << distance);
Vince Lehman9a709032014-09-13 16:28:07 -0500528
akmhoque53353462014-04-22 08:43:45 -0500529 return distance;
530}
531
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600532double
533HyperbolicRoutingCalculator::calculateAngularDistance(std::vector<double> angleVectorI,
534 std::vector<double> angleVectorJ)
535{
536 // It is not possible for angle vector size to be zero as ensured by conf-file-processor
537
538 // https://en.wikipedia.org/wiki/N-sphere#Spherical_coordinates
539
540 // Check if two vector lengths are the same
541 if (angleVectorI.size() != angleVectorJ.size()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500542 NLSR_LOG_ERROR("Angle vector sizes do not match");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600543 return UNKNOWN_DISTANCE;
544 }
545
546 // Check if all angles are within the [0, PI] and [0, 2PI] ranges
547 if (angleVectorI.size() > 1) {
548 for (unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
549 if ((angleVectorI[k] > M_PI && angleVectorI[k] < 0.0) ||
550 (angleVectorJ[k] > M_PI && angleVectorJ[k] < 0.0)) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500551 NLSR_LOG_ERROR("Angle outside [0, PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600552 return UNKNOWN_DISTANCE;
553 }
554 }
555 }
556 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
557 angleVectorI[angleVectorI.size()-1] < 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500558 NLSR_LOG_ERROR("Angle not within [0, 2PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600559 return UNKNOWN_DISTANCE;
560 }
561
562 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
563 angleVectorI[angleVectorI.size()-1] < 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500564 NLSR_LOG_ERROR("Angle not within [0, 2PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600565 return UNKNOWN_DISTANCE;
566 }
567
568 // deltaTheta = arccos(vectorI . vectorJ) -> do the inner product
569 double innerProduct = 0.0;
570
571 // Calculate x0 of the vectors
572 double x0i = std::cos(angleVectorI[0]);
573 double x0j = std::cos(angleVectorJ[0]);
574
575 // Calculate xn of the vectors
576 double xni = std::sin(angleVectorI[angleVectorI.size() - 1]);
577 double xnj = std::sin(angleVectorJ[angleVectorJ.size() - 1]);
578
579 // Do the aggregation of the (n-1) coordinates (if there is more than one angle)
580 // i.e contraction of all (n-1)-dimensional angular coordinates to one variable
581 for (unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
582 xni *= std::sin(angleVectorI[k]);
583 xnj *= std::sin(angleVectorJ[k]);
584 }
585 innerProduct += (x0i * x0j) + (xni * xnj);
586
587 // If d > 1
588 if (angleVectorI.size() > 1) {
589 for (unsigned int m = 1; m < angleVectorI.size(); m++) {
590 // calculate euclidean coordinates given the angles and assuming R_sphere = 1
591 double xmi = std::cos(angleVectorI[m]);
592 double xmj = std::cos(angleVectorJ[m]);
593 for (unsigned int l = 0; l < m; l++) {
594 xmi *= std::sin(angleVectorI[l]);
595 xmj *= std::sin(angleVectorJ[l]);
596 }
597 innerProduct += xmi * xmj;
598 }
599 }
600
601 // ArcCos of the inner product gives the angular distance
602 // between two points on a d-dimensional sphere
603 return std::acos(innerProduct);
604}
605
606double
607HyperbolicRoutingCalculator::calculateHyperbolicDistance(double rI, double rJ,
608 double deltaTheta)
609{
610 if (deltaTheta == UNKNOWN_DISTANCE) {
611 return UNKNOWN_DISTANCE;
612 }
613
614 // Usually, we set zeta = 1 in all experiments
615 double zeta = 1;
616
617 if (deltaTheta <= 0.0 || rI <= 0.0 || rJ <= 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500618 NLSR_LOG_ERROR("Delta theta or rI or rJ is <= 0");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500619 NLSR_LOG_ERROR("Please make sure that no two nodes have the exact same HR coordinates");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600620 return UNKNOWN_DISTANCE;
621 }
622
623 double xij = (1. / zeta) * std::acosh(std::cosh(zeta*rI) * std::cosh(zeta*rJ) -
624 std::sinh(zeta*rI)*std::sinh(zeta*rJ)*std::cos(deltaTheta));
625 return xij;
626}
627
628void
629HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri,
630 double cost, RoutingTable& rt)
akmhoque53353462014-04-22 08:43:45 -0500631{
Vince Lehman9a709032014-09-13 16:28:07 -0500632 NextHop hop(faceUri, cost);
Vince Lehman20fe4a92014-09-09 15:57:59 -0500633 hop.setHyperbolic(true);
akmhoque53353462014-04-22 08:43:45 -0500634
dmcoomes5bcb39e2017-10-31 15:07:55 -0500635 NLSR_LOG_TRACE("Calculated " << hop << " for destination: " << dest);
akmhoque53353462014-04-22 08:43:45 -0500636
Vince Lehman9a709032014-09-13 16:28:07 -0500637 if (m_isDryRun) {
638 rt.addNextHopToDryTable(dest, hop);
639 }
640 else {
641 rt.addNextHop(dest, hop);
642 }
akmhoque53353462014-04-22 08:43:45 -0500643}
644
Nick Gordonfad8e252016-08-11 14:21:38 -0500645} // namespace nlsr