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