blob: eb821e8154501b3def8c33c605db2231517dbd48 [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++) {
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500145 if (adjMatrix[i][j] == LinkStateRoutingTableCalculator::NO_NEXT_HOP) {
146 line += "0 ";
147 }
148 else {
149 line += boost::lexical_cast<std::string>(adjMatrix[i][j]);
150 line += " ";
151 }
akmhoque157b0a42014-05-13 00:26:37 -0500152 }
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000153 line = boost::lexical_cast<std::string>(i) + "|" + line;
dmcoomes5bcb39e2017-10-31 15:07:55 -0500154 NLSR_LOG_DEBUG(line);
akmhoque53353462014-04-22 08:43:45 -0500155 }
156}
157
158void
159RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost)
160{
Vince Lehman9a709032014-09-13 16:28:07 -0500161 for (int i = 0; i < static_cast<int>(m_nRouters); i++) {
akmhoque157b0a42014-05-13 00:26:37 -0500162 if (i == link) {
akmhoque53353462014-04-22 08:43:45 -0500163 adjMatrix[source][i] = linkCost;
164 }
akmhoque157b0a42014-05-13 00:26:37 -0500165 else {
dulalsaurabd0816a32019-07-26 13:11:24 +0000166 // if "i" is not a link to the source, set it's cost to a non adjacent value.
167 adjMatrix[source][i] = Adjacent::NON_ADJACENT_COST;
akmhoque53353462014-04-22 08:43:45 -0500168 }
169 }
170}
171
172int
173RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter)
174{
175 int noLink = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500176
177 for (size_t i = 0; i < m_nRouters; i++) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000178 if (adjMatrix[sRouter][i] >= 0 && i != static_cast<size_t>(sRouter)) { // make sure "i" is not self
akmhoque53353462014-04-22 08:43:45 -0500179 noLink++;
180 }
181 }
182 return noLink;
183}
184
185void
186RoutingTableCalculator::getLinksFromAdjMatrix(int* links,
187 double* linkCosts, int source)
188{
189 int j = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500190
191 for (size_t i = 0; i < m_nRouters; i++) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000192 if (adjMatrix[source][i] >= 0 && i != static_cast<size_t>(source)) {// make sure "i" is not self
akmhoque53353462014-04-22 08:43:45 -0500193 links[j] = i;
194 linkCosts[j] = adjMatrix[source][i];
195 j++;
196 }
197 }
198}
199
200void
201RoutingTableCalculator::freeAdjMatrix()
202{
Vince Lehman9a709032014-09-13 16:28:07 -0500203 for (size_t i = 0; i < m_nRouters; ++i) {
akmhoque53353462014-04-22 08:43:45 -0500204 delete [] adjMatrix[i];
205 }
206 delete [] adjMatrix;
207}
208
akmhoque53353462014-04-22 08:43:45 -0500209void
210RoutingTableCalculator::allocateLinks()
211{
212 links = new int[vNoLink];
213}
214
215void
216RoutingTableCalculator::allocateLinkCosts()
217{
218 linkCosts = new double[vNoLink];
219}
220
221
222void
223RoutingTableCalculator::freeLinks()
224{
225 delete [] links;
226}
227void
228RoutingTableCalculator::freeLinksCosts()
229{
230 delete [] linkCosts;
231}
232
233void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600234LinkStateRoutingTableCalculator::calculatePath(Map& pMap, RoutingTable& rt,
235 ConfParameter& confParam,
236 const std::list<AdjLsa>& adjLsaList)
akmhoque53353462014-04-22 08:43:45 -0500237{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500238 NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called");
akmhoque53353462014-04-22 08:43:45 -0500239 allocateAdjMatrix();
240 initMatrix();
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600241 makeAdjMatrix(adjLsaList, pMap);
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000242 writeAdjMatrixLog(pMap);
Nick Gordone40377d2017-08-11 15:10:02 -0500243 ndn::optional<int32_t> sourceRouter =
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600244 pMap.getMappingNoByRouterName(confParam.getRouterPrefix());
Nick G97e34942016-07-11 14:46:27 -0500245 allocateParent(); // These two matrices are used in Dijkstra's algorithm.
246 allocateDistance(); //
Nick Gordone40377d2017-08-11 15:10:02 -0500247 // We only bother to do the calculation if we have a router by that name.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600248 if (sourceRouter && confParam.getMaxFacesPerPrefix() == 1) {
Nick G97e34942016-07-11 14:46:27 -0500249 // In the single path case we can simply run Dijkstra's algorithm.
Nick Gordone40377d2017-08-11 15:10:02 -0500250 doDijkstraPathCalculation(*sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500251 // Inform the routing table of the new next hops.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600252 addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500253 }
akmhoque157b0a42014-05-13 00:26:37 -0500254 else {
akmhoque53353462014-04-22 08:43:45 -0500255 // Multi Path
Nick Gordone40377d2017-08-11 15:10:02 -0500256 setNoLink(getNumOfLinkfromAdjMatrix(*sourceRouter));
akmhoque53353462014-04-22 08:43:45 -0500257 allocateLinks();
258 allocateLinkCosts();
Nick G97e34942016-07-11 14:46:27 -0500259 // Gets a sparse listing of adjacencies for path calculation
Nick Gordone40377d2017-08-11 15:10:02 -0500260 getLinksFromAdjMatrix(links, linkCosts, *sourceRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500261 for (int i = 0 ; i < vNoLink; i++) {
Nick G97e34942016-07-11 14:46:27 -0500262 // Simulate that only the current neighbor is accessible
Nick Gordone40377d2017-08-11 15:10:02 -0500263 adjustAdMatrix(*sourceRouter, links[i], linkCosts[i]);
Saurab Dulal9da6aa72018-01-12 17:25:51 +0000264 writeAdjMatrixLog(pMap);
Nick G97e34942016-07-11 14:46:27 -0500265 // Do Dijkstra's algorithm using the current neighbor as your start.
Nick Gordone40377d2017-08-11 15:10:02 -0500266 doDijkstraPathCalculation(*sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500267 // Update the routing table with the calculations.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600268 addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500269 }
270 freeLinks();
271 freeLinksCosts();
272 }
273 freeParent();
274 freeDistance();
275 freeAdjMatrix();
276}
277
278void
279LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter)
280{
281 int i;
282 int v, u;
Nick G97e34942016-07-11 14:46:27 -0500283 int* Q = new int[m_nRouters]; // Each cell represents the router with that mapping no.
akmhoque53353462014-04-22 08:43:45 -0500284 int head = 0;
Nick Gordone98480b2017-05-24 11:23:03 -0500285 // Initiate the parent
Vince Lehman9a709032014-09-13 16:28:07 -0500286 for (i = 0 ; i < static_cast<int>(m_nRouters); i++) {
akmhoque53353462014-04-22 08:43:45 -0500287 m_parent[i] = EMPTY_PARENT;
Nick G97e34942016-07-11 14:46:27 -0500288 // Array where the ith element is the distance to the router with mapping no i.
akmhoque53353462014-04-22 08:43:45 -0500289 m_distance[i] = INF_DISTANCE;
290 Q[i] = i;
291 }
akmhoque157b0a42014-05-13 00:26:37 -0500292 if (sourceRouter != NO_MAPPING_NUM) {
Nick G97e34942016-07-11 14:46:27 -0500293 // Distance to source from source is always 0.
akmhoque53353462014-04-22 08:43:45 -0500294 m_distance[sourceRouter] = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500295 sortQueueByDistance(Q, m_distance, head, m_nRouters);
Nick G97e34942016-07-11 14:46:27 -0500296 // While we haven't visited every node.
Vince Lehman9a709032014-09-13 16:28:07 -0500297 while (head < static_cast<int>(m_nRouters)) {
Nick G97e34942016-07-11 14:46:27 -0500298 u = Q[head]; // Set u to be the current node pointed to by head.
akmhoque157b0a42014-05-13 00:26:37 -0500299 if (m_distance[u] == INF_DISTANCE) {
Nick G97e34942016-07-11 14:46:27 -0500300 break; // This can only happen when there are no accessible nodes.
akmhoque53353462014-04-22 08:43:45 -0500301 }
Nick G97e34942016-07-11 14:46:27 -0500302 // Iterate over the adjacent nodes to u.
Vince Lehman9a709032014-09-13 16:28:07 -0500303 for (v = 0 ; v < static_cast<int>(m_nRouters); v++) {
Nick G97e34942016-07-11 14:46:27 -0500304 // If the current node is accessible.
dulalsaurabd0816a32019-07-26 13:11:24 +0000305 if (adjMatrix[u][v] >= 0) {
Nick G97e34942016-07-11 14:46:27 -0500306 // And we haven't visited it yet.
Vince Lehman9a709032014-09-13 16:28:07 -0500307 if (isNotExplored(Q, v, head + 1, m_nRouters)) {
Nick G97e34942016-07-11 14:46:27 -0500308 // And if the distance to this node + from this node to v
309 // is less than the distance from our source node to v
310 // that we got when we built the adj LSAs
akmhoque157b0a42014-05-13 00:26:37 -0500311 if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) {
Nick G97e34942016-07-11 14:46:27 -0500312 // Set the new distance
akmhoque53353462014-04-22 08:43:45 -0500313 m_distance[v] = m_distance[u] + adjMatrix[u][v] ;
Nick G97e34942016-07-11 14:46:27 -0500314 // Set how we get there.
akmhoque53353462014-04-22 08:43:45 -0500315 m_parent[v] = u;
316 }
317 }
318 }
319 }
Nick G97e34942016-07-11 14:46:27 -0500320 // Increment the head position, resort the list by distance from where we are.
akmhoque53353462014-04-22 08:43:45 -0500321 head++;
Vince Lehman9a709032014-09-13 16:28:07 -0500322 sortQueueByDistance(Q, m_distance, head, m_nRouters);
akmhoque53353462014-04-22 08:43:45 -0500323 }
324 }
325 delete [] Q;
326}
327
328void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600329LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies,
330 RoutingTable& rt, Map& pMap,
331 uint32_t sourceRouter)
akmhoque53353462014-04-22 08:43:45 -0500332{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500333 NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called");
Vince Lehman9a709032014-09-13 16:28:07 -0500334
akmhoque53353462014-04-22 08:43:45 -0500335 int nextHopRouter = 0;
Vince Lehman9a709032014-09-13 16:28:07 -0500336
Nick G97e34942016-07-11 14:46:27 -0500337 // For each router we have
Vince Lehman9a709032014-09-13 16:28:07 -0500338 for (size_t i = 0; i < m_nRouters ; i++) {
akmhoque157b0a42014-05-13 00:26:37 -0500339 if (i != sourceRouter) {
Vince Lehman9a709032014-09-13 16:28:07 -0500340
Nick G97e34942016-07-11 14:46:27 -0500341 // Obtain the next hop that was determined by the algorithm
akmhoque53353462014-04-22 08:43:45 -0500342 nextHopRouter = getLsNextHop(i, sourceRouter);
Nick G97e34942016-07-11 14:46:27 -0500343 // If this router is accessible at all
akmhoque157b0a42014-05-13 00:26:37 -0500344 if (nextHopRouter != NO_NEXT_HOP) {
Vince Lehman9a709032014-09-13 16:28:07 -0500345
Nick G97e34942016-07-11 14:46:27 -0500346 // Fetch its distance
akmhoque53353462014-04-22 08:43:45 -0500347 double routeCost = m_distance[i];
Nick G97e34942016-07-11 14:46:27 -0500348 // Fetch its actual name
Nick Gordone40377d2017-08-11 15:10:02 -0500349 ndn::optional<ndn::Name> nextHopRouterName= pMap.getRouterNameByMappingNo(nextHopRouter);
350 if (nextHopRouterName) {
351 std::string nextHopFace =
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600352 adjacencies.getAdjacent(*nextHopRouterName).getFaceUri().toString();
Nick Gordone40377d2017-08-11 15:10:02 -0500353 // Add next hop to routing table
354 NextHop nh(nextHopFace, routeCost);
355 rt.addNextHop(*(pMap.getRouterNameByMappingNo(i)), nh);
356
357 }
akmhoque53353462014-04-22 08:43:45 -0500358 }
359 }
360 }
361}
362
363int
364LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source)
365{
366 int nextHop = NO_NEXT_HOP;
akmhoque157b0a42014-05-13 00:26:37 -0500367 while (m_parent[dest] != EMPTY_PARENT) {
akmhoque53353462014-04-22 08:43:45 -0500368 nextHop = dest;
369 dest = m_parent[dest];
370 }
akmhoque157b0a42014-05-13 00:26:37 -0500371 if (dest != source) {
akmhoque53353462014-04-22 08:43:45 -0500372 nextHop = NO_NEXT_HOP;
373 }
374 return nextHop;
375}
376
377void
akmhoque53353462014-04-22 08:43:45 -0500378LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q,
akmhoque2f423352014-06-03 11:49:35 -0500379 double* dist,
380 int start, int element)
akmhoque53353462014-04-22 08:43:45 -0500381{
akmhoque157b0a42014-05-13 00:26:37 -0500382 for (int i = start ; i < element ; i++) {
383 for (int j = i + 1; j < element; j++) {
384 if (dist[Q[j]] < dist[Q[i]]) {
akmhoque53353462014-04-22 08:43:45 -0500385 int tempU = Q[j];
386 Q[j] = Q[i];
387 Q[i] = tempU;
388 }
389 }
390 }
391}
392
393int
394LinkStateRoutingTableCalculator::isNotExplored(int* Q,
395 int u, int start, int element)
396{
397 int ret = 0;
akmhoque157b0a42014-05-13 00:26:37 -0500398 for (int i = start; i < element; i++) {
399 if (Q[i] == u) {
akmhoque53353462014-04-22 08:43:45 -0500400 ret = 1;
401 break;
402 }
403 }
404 return ret;
405}
406
407void
408LinkStateRoutingTableCalculator::allocateParent()
409{
Vince Lehman9a709032014-09-13 16:28:07 -0500410 m_parent = new int[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -0500411}
412
413void
414LinkStateRoutingTableCalculator::allocateDistance()
415{
Vince Lehman9a709032014-09-13 16:28:07 -0500416 m_distance = new double[m_nRouters];
akmhoque53353462014-04-22 08:43:45 -0500417}
418
419void
420LinkStateRoutingTableCalculator::freeParent()
421{
422 delete [] m_parent;
423}
424
425void LinkStateRoutingTableCalculator::freeDistance()
426{
427 delete [] m_distance;
428}
429
Vince Lehman9a709032014-09-13 16:28:07 -0500430const double HyperbolicRoutingCalculator::MATH_PI = boost::math::constants::pi<double>();
akmhoque53353462014-04-22 08:43:45 -0500431
Vince Lehman9a709032014-09-13 16:28:07 -0500432const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0;
433const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS = -1.0;
434
akmhoque53353462014-04-22 08:43:45 -0500435void
Saurab Dulal72b2b252019-01-22 16:58:08 -0600436HyperbolicRoutingCalculator::calculatePath(Map& map, RoutingTable& rt,
437 Lsdb& lsdb, AdjacencyList& adjacencies)
akmhoque53353462014-04-22 08:43:45 -0500438{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500439 NLSR_LOG_TRACE("Calculating hyperbolic paths");
Vince Lehman9a709032014-09-13 16:28:07 -0500440
Nick Gordone40377d2017-08-11 15:10:02 -0500441 ndn::optional<int32_t> thisRouter = map.getMappingNoByRouterName(m_thisRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500442
443 // Iterate over directly connected neighbors
444 std::list<Adjacent> neighbors = adjacencies.getAdjList();
445 for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) {
446
447 // Don't calculate nexthops using an inactive router
448 if (adj->getStatus() == Adjacent::STATUS_INACTIVE) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500449 NLSR_LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop");
Vince Lehman9a709032014-09-13 16:28:07 -0500450 continue;
451 }
452
453 ndn::Name srcRouterName = adj->getName();
454
455 // Don't calculate nexthops for this router to other routers
456 if (srcRouterName == m_thisRouterName) {
457 continue;
458 }
459
Nick Gordone9733ed2017-04-26 10:48:39 -0500460 std::string srcFaceUri = adj->getFaceUri().toString();
Vince Lehman9a709032014-09-13 16:28:07 -0500461
462 // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link
463 addNextHop(srcRouterName, srcFaceUri, 0, rt);
464
Nick Gordone40377d2017-08-11 15:10:02 -0500465 ndn::optional<int32_t> src = map.getMappingNoByRouterName(srcRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500466
Nick Gordone40377d2017-08-11 15:10:02 -0500467 if (!src) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500468 NLSR_LOG_WARN(adj->getName() << " does not exist in the router map!");
Vince Lehman9a709032014-09-13 16:28:07 -0500469 continue;
470 }
471
472 // Get hyperbolic distance from direct neighbor to every other router
473 for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) {
474 // Don't calculate nexthops to this router or from a router to itself
Nick Gordone40377d2017-08-11 15:10:02 -0500475 if (thisRouter && dest != *thisRouter && dest != *src) {
Vince Lehman9a709032014-09-13 16:28:07 -0500476
Nick Gordone40377d2017-08-11 15:10:02 -0500477 ndn::optional<ndn::Name> destRouterName = map.getRouterNameByMappingNo(dest);
478 if (destRouterName) {
Saurab Dulal72b2b252019-01-22 16:58:08 -0600479 double distance = getHyperbolicDistance(lsdb, srcRouterName, *destRouterName);
Vince Lehman9a709032014-09-13 16:28:07 -0500480
Nick Gordone40377d2017-08-11 15:10:02 -0500481 // Could not compute distance
482 if (distance == UNKNOWN_DISTANCE) {
dulalsaurabd0816a32019-07-26 13:11:24 +0000483 NLSR_LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName
484 << " to " << *destRouterName);
Nick Gordone40377d2017-08-11 15:10:02 -0500485 continue;
486 }
Nick Gordone40377d2017-08-11 15:10:02 -0500487 addNextHop(*destRouterName, srcFaceUri, distance, rt);
akmhoque53353462014-04-22 08:43:45 -0500488 }
Vince Lehman9a709032014-09-13 16:28:07 -0500489 }
akmhoquedcee9362014-08-05 22:58:01 -0500490 }
akmhoque53353462014-04-22 08:43:45 -0500491 }
492}
493
494double
Saurab Dulal72b2b252019-01-22 16:58:08 -0600495HyperbolicRoutingCalculator::getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, ndn::Name dest)
akmhoque53353462014-04-22 08:43:45 -0500496{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500497 NLSR_LOG_TRACE("Calculating hyperbolic distance from " << src << " to " << dest);
akmhoquedcee9362014-08-05 22:58:01 -0500498
Vince Lehman9a709032014-09-13 16:28:07 -0500499 double distance = UNKNOWN_DISTANCE;
500
501 ndn::Name srcLsaKey = src;
Nick Gordon727d4832017-10-13 18:04:25 -0500502 srcLsaKey.append(std::to_string(Lsa::Type::COORDINATE));
Vince Lehman9a709032014-09-13 16:28:07 -0500503
504 CoordinateLsa* srcLsa = lsdb.findCoordinateLsa(srcLsaKey);
505
506 ndn::Name destLsaKey = dest;
Nick Gordon727d4832017-10-13 18:04:25 -0500507 destLsaKey.append(std::to_string(Lsa::Type::COORDINATE));
Vince Lehman9a709032014-09-13 16:28:07 -0500508
509 CoordinateLsa* destLsa = lsdb.findCoordinateLsa(destLsaKey);
510
511 // Coordinate LSAs do not exist for these routers
Nick Gordone9733ed2017-04-26 10:48:39 -0500512 if (srcLsa == nullptr || destLsa == nullptr) {
Vince Lehman9a709032014-09-13 16:28:07 -0500513 return UNKNOWN_DISTANCE;
akmhoquedcee9362014-08-05 22:58:01 -0500514 }
515
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600516 std::vector<double> srcTheta = srcLsa->getCorTheta();
517 std::vector<double> destTheta = destLsa->getCorTheta();
Vince Lehman9a709032014-09-13 16:28:07 -0500518
519 double srcRadius = srcLsa->getCorRadius();
520 double destRadius = destLsa->getCorRadius();
521
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600522 double diffTheta = calculateAngularDistance(srcTheta, destTheta);
523
524 if (srcRadius == UNKNOWN_RADIUS || destRadius == UNKNOWN_RADIUS ||
525 diffTheta == UNKNOWN_DISTANCE) {
Vince Lehman9a709032014-09-13 16:28:07 -0500526 return UNKNOWN_DISTANCE;
527 }
528
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600529 // double r_i, double r_j, double delta_theta, double zeta = 1 (default)
530 distance = calculateHyperbolicDistance(srcRadius, destRadius, diffTheta);
Vince Lehman9a709032014-09-13 16:28:07 -0500531
dmcoomes5bcb39e2017-10-31 15:07:55 -0500532 NLSR_LOG_TRACE("Distance from " << src << " to " << dest << " is " << distance);
Vince Lehman9a709032014-09-13 16:28:07 -0500533
akmhoque53353462014-04-22 08:43:45 -0500534 return distance;
535}
536
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600537double
538HyperbolicRoutingCalculator::calculateAngularDistance(std::vector<double> angleVectorI,
539 std::vector<double> angleVectorJ)
540{
541 // It is not possible for angle vector size to be zero as ensured by conf-file-processor
542
543 // https://en.wikipedia.org/wiki/N-sphere#Spherical_coordinates
544
545 // Check if two vector lengths are the same
546 if (angleVectorI.size() != angleVectorJ.size()) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500547 NLSR_LOG_ERROR("Angle vector sizes do not match");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600548 return UNKNOWN_DISTANCE;
549 }
550
551 // Check if all angles are within the [0, PI] and [0, 2PI] ranges
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)) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500556 NLSR_LOG_ERROR("Angle outside [0, PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600557 return UNKNOWN_DISTANCE;
558 }
559 }
560 }
561 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
562 angleVectorI[angleVectorI.size()-1] < 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500563 NLSR_LOG_ERROR("Angle not within [0, 2PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600564 return UNKNOWN_DISTANCE;
565 }
566
567 if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI ||
568 angleVectorI[angleVectorI.size()-1] < 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500569 NLSR_LOG_ERROR("Angle not within [0, 2PI]");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600570 return UNKNOWN_DISTANCE;
571 }
572
573 // deltaTheta = arccos(vectorI . vectorJ) -> do the inner product
574 double innerProduct = 0.0;
575
576 // Calculate x0 of the vectors
577 double x0i = std::cos(angleVectorI[0]);
578 double x0j = std::cos(angleVectorJ[0]);
579
580 // Calculate xn of the vectors
581 double xni = std::sin(angleVectorI[angleVectorI.size() - 1]);
582 double xnj = std::sin(angleVectorJ[angleVectorJ.size() - 1]);
583
584 // Do the aggregation of the (n-1) coordinates (if there is more than one angle)
585 // i.e contraction of all (n-1)-dimensional angular coordinates to one variable
586 for (unsigned int k = 0; k < angleVectorI.size() - 1; k++) {
587 xni *= std::sin(angleVectorI[k]);
588 xnj *= std::sin(angleVectorJ[k]);
589 }
590 innerProduct += (x0i * x0j) + (xni * xnj);
591
592 // If d > 1
593 if (angleVectorI.size() > 1) {
594 for (unsigned int m = 1; m < angleVectorI.size(); m++) {
595 // calculate euclidean coordinates given the angles and assuming R_sphere = 1
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]);
601 }
602 innerProduct += xmi * xmj;
603 }
604 }
605
606 // ArcCos of the inner product gives the angular distance
607 // between two points on a d-dimensional sphere
608 return std::acos(innerProduct);
609}
610
611double
612HyperbolicRoutingCalculator::calculateHyperbolicDistance(double rI, double rJ,
613 double deltaTheta)
614{
615 if (deltaTheta == UNKNOWN_DISTANCE) {
616 return UNKNOWN_DISTANCE;
617 }
618
619 // Usually, we set zeta = 1 in all experiments
620 double zeta = 1;
621
622 if (deltaTheta <= 0.0 || rI <= 0.0 || rJ <= 0.0) {
dmcoomes5bcb39e2017-10-31 15:07:55 -0500623 NLSR_LOG_ERROR("Delta theta or rI or rJ is <= 0");
Ashlesh Gawandee8d8bd52018-08-09 17:18:51 -0500624 NLSR_LOG_ERROR("Please make sure that no two nodes have the exact same HR coordinates");
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600625 return UNKNOWN_DISTANCE;
626 }
627
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));
630 return xij;
631}
632
633void
634HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri,
635 double cost, RoutingTable& rt)
akmhoque53353462014-04-22 08:43:45 -0500636{
Vince Lehman9a709032014-09-13 16:28:07 -0500637 NextHop hop(faceUri, cost);
Vince Lehman20fe4a92014-09-09 15:57:59 -0500638 hop.setHyperbolic(true);
akmhoque53353462014-04-22 08:43:45 -0500639
dmcoomes5bcb39e2017-10-31 15:07:55 -0500640 NLSR_LOG_TRACE("Calculated " << hop << " for destination: " << dest);
akmhoque53353462014-04-22 08:43:45 -0500641
Vince Lehman9a709032014-09-13 16:28:07 -0500642 if (m_isDryRun) {
643 rt.addNextHopToDryTable(dest, hop);
644 }
645 else {
646 rt.addNextHop(dest, hop);
647 }
akmhoque53353462014-04-22 08:43:45 -0500648}
649
Nick Gordonfad8e252016-08-11 14:21:38 -0500650} // namespace nlsr