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