akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, The University of Memphis, |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 6 | * |
| 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/>. |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 20 | */ |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 21 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | #include "routing-table-calculator.hpp" |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 23 | #include "lsdb.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | #include "map.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | #include "nexthop.hpp" |
| 26 | #include "nlsr.hpp" |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 27 | #include "logger.hpp" |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 28 | #include "adjacent.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 30 | #include <cmath> |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 31 | #include <boost/lexical_cast.hpp> |
| 32 | #include <ndn-cxx/util/logger.hpp> |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 33 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 34 | namespace nlsr { |
| 35 | |
dmcoomes | cf8d0ed | 2017-02-21 11:39:01 -0600 | [diff] [blame] | 36 | INIT_LOGGER(route.RoutingTableCalculator); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | |
Davide Pesavento | 658fd85 | 2023-05-10 22:15:03 -0400 | [diff] [blame] | 38 | constexpr int EMPTY_PARENT = -12345; |
| 39 | constexpr double INF_DISTANCE = 2147483647; |
| 40 | constexpr int NO_MAPPING_NUM = -1; |
| 41 | constexpr int NO_NEXT_HOP = -12345; |
| 42 | constexpr double UNKNOWN_DISTANCE = -1.0; |
| 43 | constexpr double UNKNOWN_RADIUS = -1.0; |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 44 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | void |
| 46 | RoutingTableCalculator::allocateAdjMatrix() |
| 47 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 48 | adjMatrix = new double*[m_nRouters]; |
| 49 | |
| 50 | for (size_t i = 0; i < m_nRouters; ++i) { |
| 51 | adjMatrix[i] = new double[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | RoutingTableCalculator::initMatrix() |
| 57 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 58 | for (size_t i = 0; i < m_nRouters; i++) { |
| 59 | for (size_t j = 0; j < m_nRouters; j++) { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 60 | adjMatrix[i][j] = Adjacent::NON_ADJACENT_COST; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 61 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | void |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 66 | RoutingTableCalculator::makeAdjMatrix(const Lsdb& lsdb, Map& pMap) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 68 | // For each LSA represented in the map |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 69 | auto lsaRange = lsdb.getLsdbIterator<AdjLsa>(); |
| 70 | for (auto lsaIt = lsaRange.first; lsaIt != lsaRange.second; ++lsaIt) { |
| 71 | auto adjLsa = std::static_pointer_cast<AdjLsa>(*lsaIt); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 72 | auto row = pMap.getMappingNoByRouterName(adjLsa->getOriginRouter()); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 73 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 74 | std::list<Adjacent> adl = adjLsa->getAdl().getAdjList(); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 75 | // For each adjacency represented in the LSA |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 76 | for (const auto& adjacent : adl) { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 77 | auto col = pMap.getMappingNoByRouterName(adjacent.getName()); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 78 | double cost = adjacent.getLinkCost(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 79 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 80 | if (row && col && *row < static_cast<int32_t>(m_nRouters) |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 81 | && *col < static_cast<int32_t>(m_nRouters)) { |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 82 | adjMatrix[*row][*col] = cost; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 86 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 87 | // Links that do not have the same cost for both directions should |
| 88 | // have their costs corrected: |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 89 | // |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 90 | // If the cost of one side of the link is NON_ADJACENT_COST (i.e. broken) or negative, |
| 91 | // both direction of the link should have their cost corrected to NON_ADJACENT_COST. |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 92 | // |
| 93 | // Otherwise, both sides of the link should use the larger of the two costs. |
| 94 | // |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 95 | // Additionally, this means that we can halve the amount of space |
| 96 | // that the matrix uses by only maintaining a triangle. |
| 97 | // - But that is not yet implemented. |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 98 | for (size_t row = 0; row < m_nRouters; ++row) { |
| 99 | for (size_t col = 0; col < m_nRouters; ++col) { |
| 100 | double toCost = adjMatrix[row][col]; |
| 101 | double fromCost = adjMatrix[col][row]; |
| 102 | |
| 103 | if (fromCost != toCost) { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 104 | double correctedCost = Adjacent::NON_ADJACENT_COST; |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 105 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 106 | if (toCost >= 0 && fromCost >= 0) { |
| 107 | // If both sides of the link are up, use the larger cost else break the link |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 108 | correctedCost = std::max(toCost, fromCost); |
| 109 | } |
| 110 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 111 | NLSR_LOG_WARN("Cost between [" << row << "][" << col << "] and [" << col << "][" << row << |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 112 | "] are not the same (" << toCost << " != " << fromCost << "). " << |
| 113 | "Correcting to cost: " << correctedCost); |
| 114 | |
| 115 | adjMatrix[row][col] = correctedCost; |
| 116 | adjMatrix[col][row] = correctedCost; |
| 117 | } |
| 118 | } |
| 119 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 123 | RoutingTableCalculator::writeAdjMatrixLog(const Map& map) const |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 124 | { |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 125 | if (!ndn_cxx_getLogger().isLevelEnabled(ndn::util::LogLevel::DEBUG)) { |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
| 129 | NLSR_LOG_DEBUG("-----------Legend (routerName -> index)------"); |
| 130 | std::string routerIndex; |
| 131 | std::string indexToNameMapping; |
| 132 | std::string lengthOfDash = "--"; |
| 133 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 134 | for (size_t i = 0; i < m_nRouters; i++) { |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 135 | routerIndex += boost::lexical_cast<std::string>(i); |
| 136 | routerIndex += " "; |
| 137 | lengthOfDash += "--"; |
| 138 | NLSR_LOG_DEBUG("Router:" + map.getRouterNameByMappingNo(i)->toUri() + |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 139 | " Index:" + boost::lexical_cast<std::string>(i)); |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 140 | } |
| 141 | NLSR_LOG_DEBUG(" |" + routerIndex); |
| 142 | NLSR_LOG_DEBUG(lengthOfDash); |
| 143 | |
| 144 | for (size_t i = 0; i < m_nRouters; i++) { |
| 145 | std::string line; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 146 | for (size_t j = 0; j < m_nRouters; j++) { |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 147 | if (adjMatrix[i][j] == NO_NEXT_HOP) { |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 148 | line += "0 "; |
| 149 | } |
| 150 | else { |
| 151 | line += boost::lexical_cast<std::string>(adjMatrix[i][j]); |
| 152 | line += " "; |
| 153 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 154 | } |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 155 | line = boost::lexical_cast<std::string>(i) + "|" + line; |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 156 | NLSR_LOG_DEBUG(line); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 162 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 163 | for (int i = 0; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 164 | if (i == link) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 165 | adjMatrix[source][i] = linkCost; |
| 166 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 167 | else { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 168 | // if "i" is not a link to the source, set it's cost to a non adjacent value. |
| 169 | adjMatrix[source][i] = Adjacent::NON_ADJACENT_COST; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | int |
| 175 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 176 | { |
| 177 | int noLink = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 178 | |
| 179 | for (size_t i = 0; i < m_nRouters; i++) { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 180 | if (adjMatrix[sRouter][i] >= 0 && i != static_cast<size_t>(sRouter)) { // make sure "i" is not self |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 181 | noLink++; |
| 182 | } |
| 183 | } |
| 184 | return noLink; |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | RoutingTableCalculator::getLinksFromAdjMatrix(int* links, |
| 189 | double* linkCosts, int source) |
| 190 | { |
| 191 | int j = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 192 | |
| 193 | for (size_t i = 0; i < m_nRouters; i++) { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 194 | if (adjMatrix[source][i] >= 0 && i != static_cast<size_t>(source)) {// make sure "i" is not self |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 195 | links[j] = i; |
| 196 | linkCosts[j] = adjMatrix[source][i]; |
| 197 | j++; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | RoutingTableCalculator::freeAdjMatrix() |
| 204 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 205 | for (size_t i = 0; i < m_nRouters; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 206 | delete [] adjMatrix[i]; |
| 207 | } |
| 208 | delete [] adjMatrix; |
| 209 | } |
| 210 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | void |
| 212 | RoutingTableCalculator::allocateLinks() |
| 213 | { |
| 214 | links = new int[vNoLink]; |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | RoutingTableCalculator::allocateLinkCosts() |
| 219 | { |
| 220 | linkCosts = new double[vNoLink]; |
| 221 | } |
| 222 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 223 | void |
| 224 | RoutingTableCalculator::freeLinks() |
| 225 | { |
| 226 | delete [] links; |
| 227 | } |
| 228 | void |
| 229 | RoutingTableCalculator::freeLinksCosts() |
| 230 | { |
| 231 | delete [] linkCosts; |
| 232 | } |
| 233 | |
| 234 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 235 | LinkStateRoutingTableCalculator::calculatePath(Map& pMap, RoutingTable& rt, |
| 236 | ConfParameter& confParam, |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 237 | const Lsdb& lsdb) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 238 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 239 | NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 240 | allocateAdjMatrix(); |
| 241 | initMatrix(); |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 242 | makeAdjMatrix(lsdb, pMap); |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 243 | writeAdjMatrixLog(pMap); |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 244 | auto sourceRouter = pMap.getMappingNoByRouterName(confParam.getRouterPrefix()); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 245 | allocateParent(); // These two matrices are used in Dijkstra's algorithm. |
| 246 | allocateDistance(); // |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 247 | // We only bother to do the calculation if we have a router by that name. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 248 | if (sourceRouter && confParam.getMaxFacesPerPrefix() == 1) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 249 | // In the single path case we can simply run Dijkstra's algorithm. |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 250 | doDijkstraPathCalculation(*sourceRouter); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 251 | // Inform the routing table of the new next hops. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 252 | addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 253 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 254 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 255 | // Multi Path |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 256 | setNoLink(getNumOfLinkfromAdjMatrix(*sourceRouter)); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 257 | allocateLinks(); |
| 258 | allocateLinkCosts(); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 259 | // Gets a sparse listing of adjacencies for path calculation |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 260 | getLinksFromAdjMatrix(links, linkCosts, *sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 261 | for (int i = 0 ; i < vNoLink; i++) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 262 | // Simulate that only the current neighbor is accessible |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 263 | adjustAdMatrix(*sourceRouter, links[i], linkCosts[i]); |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 264 | writeAdjMatrixLog(pMap); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 265 | // Do Dijkstra's algorithm using the current neighbor as your start. |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 266 | doDijkstraPathCalculation(*sourceRouter); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 267 | // Update the routing table with the calculations. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 268 | addAllLsNextHopsToRoutingTable(confParam.getAdjacencyList(), rt, pMap, *sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 269 | } |
| 270 | freeLinks(); |
| 271 | freeLinksCosts(); |
| 272 | } |
| 273 | freeParent(); |
| 274 | freeDistance(); |
| 275 | freeAdjMatrix(); |
| 276 | } |
| 277 | |
| 278 | void |
| 279 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 280 | { |
| 281 | int i; |
| 282 | int v, u; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 283 | int* Q = new int[m_nRouters]; // Each cell represents the router with that mapping no. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 284 | int head = 0; |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 285 | // Initiate the parent |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 286 | for (i = 0 ; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 287 | m_parent[i] = EMPTY_PARENT; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 288 | // Array where the ith element is the distance to the router with mapping no i. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 289 | m_distance[i] = INF_DISTANCE; |
| 290 | Q[i] = i; |
| 291 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 292 | if (sourceRouter != NO_MAPPING_NUM) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 293 | // Distance to source from source is always 0. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 294 | m_distance[sourceRouter] = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 295 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 296 | // While we haven't visited every node. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 297 | while (head < static_cast<int>(m_nRouters)) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 298 | u = Q[head]; // Set u to be the current node pointed to by head. |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 299 | if (m_distance[u] == INF_DISTANCE) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 300 | break; // This can only happen when there are no accessible nodes. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 301 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 302 | // Iterate over the adjacent nodes to u. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 303 | for (v = 0 ; v < static_cast<int>(m_nRouters); v++) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 304 | // If the current node is accessible. |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 305 | if (adjMatrix[u][v] >= 0) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 306 | // And we haven't visited it yet. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 307 | if (isNotExplored(Q, v, head + 1, m_nRouters)) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 308 | // 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 |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 311 | if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 312 | // Set the new distance |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 313 | m_distance[v] = m_distance[u] + adjMatrix[u][v] ; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 314 | // Set how we get there. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 315 | m_parent[v] = u; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 320 | // Increment the head position, resort the list by distance from where we are. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 321 | head++; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 322 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | delete [] Q; |
| 326 | } |
| 327 | |
| 328 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 329 | LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies, |
| 330 | RoutingTable& rt, Map& pMap, |
| 331 | uint32_t sourceRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 332 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 333 | NLSR_LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 334 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 335 | int nextHopRouter = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 336 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 337 | // For each router we have |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 338 | for (size_t i = 0; i < m_nRouters ; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 339 | if (i != sourceRouter) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 340 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 341 | // Obtain the next hop that was determined by the algorithm |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 342 | nextHopRouter = getLsNextHop(i, sourceRouter); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 343 | // If this router is accessible at all |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 344 | if (nextHopRouter != NO_NEXT_HOP) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 345 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 346 | // Fetch its distance |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 347 | double routeCost = m_distance[i]; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 348 | // Fetch its actual name |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 349 | auto nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 350 | if (nextHopRouterName) { |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 351 | auto nextHopFace = adjacencies.getAdjacent(*nextHopRouterName).getFaceUri(); |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 352 | // Add next hop to routing table |
| 353 | NextHop nh(nextHopFace, routeCost); |
| 354 | rt.addNextHop(*(pMap.getRouterNameByMappingNo(i)), nh); |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 355 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | int |
| 362 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 363 | { |
| 364 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 365 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 366 | nextHop = dest; |
| 367 | dest = m_parent[dest]; |
| 368 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 369 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 370 | nextHop = NO_NEXT_HOP; |
| 371 | } |
| 372 | return nextHop; |
| 373 | } |
| 374 | |
| 375 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 376 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 377 | double* dist, |
| 378 | int start, int element) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 379 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 380 | for (int i = start ; i < element ; i++) { |
| 381 | for (int j = i + 1; j < element; j++) { |
| 382 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 383 | int tempU = Q[j]; |
| 384 | Q[j] = Q[i]; |
| 385 | Q[i] = tempU; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | int |
| 392 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 393 | int u, int start, int element) |
| 394 | { |
| 395 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 396 | for (int i = start; i < element; i++) { |
| 397 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 398 | ret = 1; |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | LinkStateRoutingTableCalculator::allocateParent() |
| 407 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 408 | m_parent = new int[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void |
| 412 | LinkStateRoutingTableCalculator::allocateDistance() |
| 413 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 414 | m_distance = new double[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void |
| 418 | LinkStateRoutingTableCalculator::freeParent() |
| 419 | { |
| 420 | delete [] m_parent; |
| 421 | } |
| 422 | |
| 423 | void LinkStateRoutingTableCalculator::freeDistance() |
| 424 | { |
| 425 | delete [] m_distance; |
| 426 | } |
| 427 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 428 | void |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 429 | HyperbolicRoutingCalculator::calculatePath(Map& map, RoutingTable& rt, |
| 430 | Lsdb& lsdb, AdjacencyList& adjacencies) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 431 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 432 | NLSR_LOG_TRACE("Calculating hyperbolic paths"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 433 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 434 | auto thisRouter = map.getMappingNoByRouterName(m_thisRouterName); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 435 | |
| 436 | // Iterate over directly connected neighbors |
| 437 | std::list<Adjacent> neighbors = adjacencies.getAdjList(); |
Davide Pesavento | fd1e940 | 2023-11-13 15:40:41 -0500 | [diff] [blame^] | 438 | for (auto adj = neighbors.begin(); adj != neighbors.end(); ++adj) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 439 | |
| 440 | // Don't calculate nexthops using an inactive router |
| 441 | if (adj->getStatus() == Adjacent::STATUS_INACTIVE) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 442 | NLSR_LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 443 | continue; |
| 444 | } |
| 445 | |
| 446 | ndn::Name srcRouterName = adj->getName(); |
| 447 | |
| 448 | // Don't calculate nexthops for this router to other routers |
| 449 | if (srcRouterName == m_thisRouterName) { |
| 450 | continue; |
| 451 | } |
| 452 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 453 | // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 454 | addNextHop(srcRouterName, adj->getFaceUri(), 0, rt); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 455 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 456 | auto src = map.getMappingNoByRouterName(srcRouterName); |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 457 | if (!src) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 458 | NLSR_LOG_WARN(adj->getName() << " does not exist in the router map!"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 459 | continue; |
| 460 | } |
| 461 | |
| 462 | // Get hyperbolic distance from direct neighbor to every other router |
| 463 | for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) { |
| 464 | // Don't calculate nexthops to this router or from a router to itself |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 465 | if (thisRouter && dest != *thisRouter && dest != *src) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 466 | |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 467 | auto destRouterName = map.getRouterNameByMappingNo(dest); |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 468 | if (destRouterName) { |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 469 | double distance = getHyperbolicDistance(lsdb, srcRouterName, *destRouterName); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 470 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 471 | // Could not compute distance |
| 472 | if (distance == UNKNOWN_DISTANCE) { |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 473 | NLSR_LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName |
| 474 | << " to " << *destRouterName); |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 475 | continue; |
| 476 | } |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 477 | addNextHop(*destRouterName, adj->getFaceUri(), distance, rt); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 478 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 479 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 480 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | double |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 485 | HyperbolicRoutingCalculator::getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, ndn::Name dest) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 486 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 487 | NLSR_LOG_TRACE("Calculating hyperbolic distance from " << src << " to " << dest); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 488 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 489 | double distance = UNKNOWN_DISTANCE; |
| 490 | |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 491 | auto srcLsa = lsdb.findLsa<CoordinateLsa>(src); |
| 492 | auto destLsa = lsdb.findLsa<CoordinateLsa>(dest); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 493 | |
| 494 | // Coordinate LSAs do not exist for these routers |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame] | 495 | if (srcLsa == nullptr || destLsa == nullptr) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 496 | return UNKNOWN_DISTANCE; |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 497 | } |
| 498 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 499 | std::vector<double> srcTheta = srcLsa->getCorTheta(); |
| 500 | std::vector<double> destTheta = destLsa->getCorTheta(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 501 | |
| 502 | double srcRadius = srcLsa->getCorRadius(); |
| 503 | double destRadius = destLsa->getCorRadius(); |
| 504 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 505 | double diffTheta = calculateAngularDistance(srcTheta, destTheta); |
| 506 | |
| 507 | if (srcRadius == UNKNOWN_RADIUS || destRadius == UNKNOWN_RADIUS || |
| 508 | diffTheta == UNKNOWN_DISTANCE) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 509 | return UNKNOWN_DISTANCE; |
| 510 | } |
| 511 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 512 | // double r_i, double r_j, double delta_theta, double zeta = 1 (default) |
| 513 | distance = calculateHyperbolicDistance(srcRadius, destRadius, diffTheta); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 514 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 515 | NLSR_LOG_TRACE("Distance from " << src << " to " << dest << " is " << distance); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 516 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 517 | return distance; |
| 518 | } |
| 519 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 520 | double |
| 521 | HyperbolicRoutingCalculator::calculateAngularDistance(std::vector<double> angleVectorI, |
| 522 | std::vector<double> angleVectorJ) |
| 523 | { |
| 524 | // It is not possible for angle vector size to be zero as ensured by conf-file-processor |
| 525 | |
| 526 | // https://en.wikipedia.org/wiki/N-sphere#Spherical_coordinates |
| 527 | |
| 528 | // Check if two vector lengths are the same |
| 529 | if (angleVectorI.size() != angleVectorJ.size()) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 530 | NLSR_LOG_ERROR("Angle vector sizes do not match"); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 531 | return UNKNOWN_DISTANCE; |
| 532 | } |
| 533 | |
| 534 | // Check if all angles are within the [0, PI] and [0, 2PI] ranges |
| 535 | if (angleVectorI.size() > 1) { |
| 536 | for (unsigned int k = 0; k < angleVectorI.size() - 1; k++) { |
| 537 | if ((angleVectorI[k] > M_PI && angleVectorI[k] < 0.0) || |
| 538 | (angleVectorJ[k] > M_PI && angleVectorJ[k] < 0.0)) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 539 | NLSR_LOG_ERROR("Angle outside [0, PI]"); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 540 | return UNKNOWN_DISTANCE; |
| 541 | } |
| 542 | } |
| 543 | } |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 544 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 545 | if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI || |
| 546 | angleVectorI[angleVectorI.size()-1] < 0.0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 547 | NLSR_LOG_ERROR("Angle not within [0, 2PI]"); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 548 | return UNKNOWN_DISTANCE; |
| 549 | } |
| 550 | |
| 551 | if (angleVectorI[angleVectorI.size()-1] > 2.*M_PI || |
| 552 | angleVectorI[angleVectorI.size()-1] < 0.0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 553 | NLSR_LOG_ERROR("Angle not within [0, 2PI]"); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 554 | return UNKNOWN_DISTANCE; |
| 555 | } |
| 556 | |
| 557 | // deltaTheta = arccos(vectorI . vectorJ) -> do the inner product |
| 558 | double innerProduct = 0.0; |
| 559 | |
| 560 | // Calculate x0 of the vectors |
| 561 | double x0i = std::cos(angleVectorI[0]); |
| 562 | double x0j = std::cos(angleVectorJ[0]); |
| 563 | |
| 564 | // Calculate xn of the vectors |
| 565 | double xni = std::sin(angleVectorI[angleVectorI.size() - 1]); |
| 566 | double xnj = std::sin(angleVectorJ[angleVectorJ.size() - 1]); |
| 567 | |
| 568 | // Do the aggregation of the (n-1) coordinates (if there is more than one angle) |
| 569 | // i.e contraction of all (n-1)-dimensional angular coordinates to one variable |
| 570 | for (unsigned int k = 0; k < angleVectorI.size() - 1; k++) { |
| 571 | xni *= std::sin(angleVectorI[k]); |
| 572 | xnj *= std::sin(angleVectorJ[k]); |
| 573 | } |
| 574 | innerProduct += (x0i * x0j) + (xni * xnj); |
| 575 | |
| 576 | // If d > 1 |
| 577 | if (angleVectorI.size() > 1) { |
| 578 | for (unsigned int m = 1; m < angleVectorI.size(); m++) { |
| 579 | // calculate euclidean coordinates given the angles and assuming R_sphere = 1 |
| 580 | double xmi = std::cos(angleVectorI[m]); |
| 581 | double xmj = std::cos(angleVectorJ[m]); |
| 582 | for (unsigned int l = 0; l < m; l++) { |
| 583 | xmi *= std::sin(angleVectorI[l]); |
| 584 | xmj *= std::sin(angleVectorJ[l]); |
| 585 | } |
| 586 | innerProduct += xmi * xmj; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // ArcCos of the inner product gives the angular distance |
| 591 | // between two points on a d-dimensional sphere |
| 592 | return std::acos(innerProduct); |
| 593 | } |
| 594 | |
| 595 | double |
| 596 | HyperbolicRoutingCalculator::calculateHyperbolicDistance(double rI, double rJ, |
| 597 | double deltaTheta) |
| 598 | { |
| 599 | if (deltaTheta == UNKNOWN_DISTANCE) { |
| 600 | return UNKNOWN_DISTANCE; |
| 601 | } |
| 602 | |
| 603 | // Usually, we set zeta = 1 in all experiments |
| 604 | double zeta = 1; |
| 605 | |
| 606 | if (deltaTheta <= 0.0 || rI <= 0.0 || rJ <= 0.0) { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 607 | NLSR_LOG_ERROR("Delta theta or rI or rJ is <= 0"); |
Ashlesh Gawande | e8d8bd5 | 2018-08-09 17:18:51 -0500 | [diff] [blame] | 608 | NLSR_LOG_ERROR("Please make sure that no two nodes have the exact same HR coordinates"); |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 609 | return UNKNOWN_DISTANCE; |
| 610 | } |
| 611 | |
| 612 | double xij = (1. / zeta) * std::acosh(std::cosh(zeta*rI) * std::cosh(zeta*rJ) - |
| 613 | std::sinh(zeta*rI)*std::sinh(zeta*rJ)*std::cos(deltaTheta)); |
| 614 | return xij; |
| 615 | } |
| 616 | |
| 617 | void |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 618 | HyperbolicRoutingCalculator::addNextHop(const ndn::Name& dest, const ndn::FaceUri& faceUri, |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 619 | double cost, RoutingTable& rt) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 620 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 621 | NextHop hop(faceUri, cost); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 622 | hop.setHyperbolic(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 623 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame] | 624 | NLSR_LOG_TRACE("Calculated " << hop << " for destination: " << dest); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 625 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 626 | if (m_isDryRun) { |
| 627 | rt.addNextHopToDryTable(dest, hop); |
| 628 | } |
| 629 | else { |
| 630 | rt.addNextHop(dest, hop); |
| 631 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 632 | } |
| 633 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 634 | } // namespace nlsr |