akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | **/ |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 21 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 22 | #include <iostream> |
| 23 | #include <cmath> |
| 24 | #include "lsdb.hpp" |
| 25 | #include "routing-table-calculator.hpp" |
| 26 | #include "map.hpp" |
| 27 | #include "lsa.hpp" |
| 28 | #include "nexthop.hpp" |
| 29 | #include "nlsr.hpp" |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 30 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 32 | #include <boost/math/constants/constants.hpp> |
| 33 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 34 | namespace nlsr { |
| 35 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 36 | INIT_LOGGER("RoutingTableCalculator"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | using namespace std; |
| 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 |
| 60 | RoutingTableCalculator::makeAdjMatrix(Nlsr& pnlsr, Map pMap) |
| 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 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 67 | int32_t row = pMap.getMappingNoByRouterName((*it).getOrigRouter()); |
| 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 | |
| 73 | 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 | |
| 76 | if ((row >= 0 && row < static_cast<int32_t>(m_nRouters)) && |
| 77 | (col >= 0 && col < static_cast<int32_t>(m_nRouters))) |
| 78 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | adjMatrix[row][col] = cost; |
| 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 | |
| 108 | _LOG_WARN("Cost between [" << row << "][" << col << "] and [" << col << "][" << row << |
| 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 |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 120 | RoutingTableCalculator::writeAdjMatrixLog() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 122 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 123 | string line=""; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 124 | for (size_t j = 0; j < m_nRouters; j++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 125 | line += boost::lexical_cast<std::string>(adjMatrix[i][j]); |
| 126 | line += " "; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 127 | } |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 128 | _LOG_DEBUG(line); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 134 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 135 | for (int i = 0; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 136 | if (i == link) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | adjMatrix[source][i] = linkCost; |
| 138 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 139 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 140 | adjMatrix[source][i] = 0; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 147 | { |
| 148 | int noLink = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 149 | |
| 150 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 151 | if (adjMatrix[sRouter][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 152 | noLink++; |
| 153 | } |
| 154 | } |
| 155 | return noLink; |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | RoutingTableCalculator::getLinksFromAdjMatrix(int* links, |
| 160 | double* linkCosts, int source) |
| 161 | { |
| 162 | int j = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 163 | |
| 164 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 165 | if (adjMatrix[source][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | links[j] = i; |
| 167 | linkCosts[j] = adjMatrix[source][i]; |
| 168 | j++; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | RoutingTableCalculator::freeAdjMatrix() |
| 175 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 176 | for (size_t i = 0; i < m_nRouters; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | delete [] adjMatrix[i]; |
| 178 | } |
| 179 | delete [] adjMatrix; |
| 180 | } |
| 181 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | void |
| 183 | RoutingTableCalculator::allocateLinks() |
| 184 | { |
| 185 | links = new int[vNoLink]; |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | RoutingTableCalculator::allocateLinkCosts() |
| 190 | { |
| 191 | linkCosts = new double[vNoLink]; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | void |
| 196 | RoutingTableCalculator::freeLinks() |
| 197 | { |
| 198 | delete [] links; |
| 199 | } |
| 200 | void |
| 201 | RoutingTableCalculator::freeLinksCosts() |
| 202 | { |
| 203 | delete [] linkCosts; |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | LinkStateRoutingTableCalculator::calculatePath(Map& pMap, |
| 208 | RoutingTable& rt, Nlsr& pnlsr) |
| 209 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 210 | _LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | allocateAdjMatrix(); |
| 212 | initMatrix(); |
| 213 | makeAdjMatrix(pnlsr, pMap); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 214 | writeAdjMatrixLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 215 | int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix()); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 216 | allocateParent(); // These two matrices are used in Dijkstra's algorithm. |
| 217 | allocateDistance(); // |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 218 | if (pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 219 | // In the single path case we can simply run Dijkstra's algorithm. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 220 | doDijkstraPathCalculation(sourceRouter); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 221 | // Inform the routing table of the new next hops. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 222 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 223 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 224 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 225 | // Multi Path |
| 226 | setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter)); |
| 227 | allocateLinks(); |
| 228 | allocateLinkCosts(); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 229 | // Gets a sparse listing of adjacencies for path calculation |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 231 | for (int i = 0 ; i < vNoLink; i++) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 232 | // Simulate that only the current neighbor is accessible |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 233 | adjustAdMatrix(sourceRouter, links[i], linkCosts[i]); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 234 | writeAdjMatrixLog(); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 235 | // Do Dijkstra's algorithm using the current neighbor as your start. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 236 | doDijkstraPathCalculation(sourceRouter); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 237 | // Update the routing table with the calculations. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 238 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 239 | } |
| 240 | freeLinks(); |
| 241 | freeLinksCosts(); |
| 242 | } |
| 243 | freeParent(); |
| 244 | freeDistance(); |
| 245 | freeAdjMatrix(); |
| 246 | } |
| 247 | |
| 248 | void |
| 249 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 250 | { |
| 251 | int i; |
| 252 | int v, u; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 253 | 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] | 254 | int head = 0; |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 255 | // Initiate the Parent |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 256 | for (i = 0 ; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 257 | m_parent[i] = EMPTY_PARENT; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 258 | // 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] | 259 | m_distance[i] = INF_DISTANCE; |
| 260 | Q[i] = i; |
| 261 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 262 | if (sourceRouter != NO_MAPPING_NUM) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 263 | // Distance to source from source is always 0. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 264 | m_distance[sourceRouter] = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 265 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 266 | // While we haven't visited every node. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 267 | while (head < static_cast<int>(m_nRouters)) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 268 | 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] | 269 | if (m_distance[u] == INF_DISTANCE) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 270 | break; // This can only happen when there are no accessible nodes. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 271 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 272 | // Iterate over the adjacent nodes to u. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 273 | for (v = 0 ; v < static_cast<int>(m_nRouters); v++) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 274 | // If the current node is accessible. |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 275 | if (adjMatrix[u][v] > 0) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 276 | // And we haven't visited it yet. |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 277 | if (isNotExplored(Q, v, head + 1, m_nRouters)) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 278 | // And if the distance to this node + from this node to v |
| 279 | // is less than the distance from our source node to v |
| 280 | // that we got when we built the adj LSAs |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 281 | if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) { |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 282 | // Set the new distance |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 283 | m_distance[v] = m_distance[u] + adjMatrix[u][v] ; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 284 | // Set how we get there. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 285 | m_parent[v] = u; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 290 | // Increment the head position, resort the list by distance from where we are. |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 291 | head++; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 292 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | delete [] Q; |
| 296 | } |
| 297 | |
| 298 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 299 | LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, RoutingTable& rt, |
| 300 | Map& pMap, uint32_t sourceRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 301 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 302 | _LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 303 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 304 | int nextHopRouter = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 305 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 306 | // For each router we have |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 307 | for (size_t i = 0; i < m_nRouters ; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 308 | if (i != sourceRouter) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 309 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 310 | // Obtain the next hop that was determined by the algorithm |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 311 | nextHopRouter = getLsNextHop(i, sourceRouter); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 312 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 313 | // If this router is accessible at all |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 314 | if (nextHopRouter != NO_NEXT_HOP) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 315 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 316 | // Fetch its distance |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 317 | double routeCost = m_distance[i]; |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 318 | // Fetch its actual name |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 319 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 320 | std::string nextHopFace = |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 321 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getFaceUri().toString(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 322 | // Add next hop to routing table |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 323 | NextHop nh(nextHopFace, routeCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 324 | rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh); |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | int |
| 331 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 332 | { |
| 333 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 334 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 335 | nextHop = dest; |
| 336 | dest = m_parent[dest]; |
| 337 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 338 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 339 | nextHop = NO_NEXT_HOP; |
| 340 | } |
| 341 | return nextHop; |
| 342 | } |
| 343 | |
| 344 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 345 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 346 | double* dist, |
| 347 | int start, int element) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 348 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 349 | for (int i = start ; i < element ; i++) { |
| 350 | for (int j = i + 1; j < element; j++) { |
| 351 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 352 | int tempU = Q[j]; |
| 353 | Q[j] = Q[i]; |
| 354 | Q[i] = tempU; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | int |
| 361 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 362 | int u, int start, int element) |
| 363 | { |
| 364 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 365 | for (int i = start; i < element; i++) { |
| 366 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 367 | ret = 1; |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | void |
| 375 | LinkStateRoutingTableCalculator::allocateParent() |
| 376 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 377 | m_parent = new int[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void |
| 381 | LinkStateRoutingTableCalculator::allocateDistance() |
| 382 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 383 | m_distance = new double[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void |
| 387 | LinkStateRoutingTableCalculator::freeParent() |
| 388 | { |
| 389 | delete [] m_parent; |
| 390 | } |
| 391 | |
| 392 | void LinkStateRoutingTableCalculator::freeDistance() |
| 393 | { |
| 394 | delete [] m_distance; |
| 395 | } |
| 396 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 397 | const double HyperbolicRoutingCalculator::MATH_PI = boost::math::constants::pi<double>(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 398 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 399 | const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0; |
| 400 | const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS = -1.0; |
| 401 | |
| 402 | const int32_t HyperbolicRoutingCalculator::ROUTER_NOT_FOUND = -1.0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 403 | |
| 404 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 405 | HyperbolicRoutingCalculator::calculatePaths(Map& map, RoutingTable& rt, |
| 406 | Lsdb& lsdb, AdjacencyList& adjacencies) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 407 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 408 | _LOG_TRACE("Calculating hyperbolic paths"); |
| 409 | |
| 410 | int thisRouter = map.getMappingNoByRouterName(m_thisRouterName); |
| 411 | |
| 412 | // Iterate over directly connected neighbors |
| 413 | std::list<Adjacent> neighbors = adjacencies.getAdjList(); |
| 414 | for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) { |
| 415 | |
| 416 | // Don't calculate nexthops using an inactive router |
| 417 | if (adj->getStatus() == Adjacent::STATUS_INACTIVE) { |
| 418 | _LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop"); |
| 419 | continue; |
| 420 | } |
| 421 | |
| 422 | ndn::Name srcRouterName = adj->getName(); |
| 423 | |
| 424 | // Don't calculate nexthops for this router to other routers |
| 425 | if (srcRouterName == m_thisRouterName) { |
| 426 | continue; |
| 427 | } |
| 428 | |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 429 | std::string srcFaceUri = adj->getFaceUri().toString(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 430 | |
| 431 | // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link |
| 432 | addNextHop(srcRouterName, srcFaceUri, 0, rt); |
| 433 | |
| 434 | int src = map.getMappingNoByRouterName(srcRouterName); |
| 435 | |
| 436 | if (src == ROUTER_NOT_FOUND) { |
| 437 | _LOG_WARN(adj->getName() << " does not exist in the router map!"); |
| 438 | continue; |
| 439 | } |
| 440 | |
| 441 | // Get hyperbolic distance from direct neighbor to every other router |
| 442 | for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) { |
| 443 | // Don't calculate nexthops to this router or from a router to itself |
| 444 | if (dest != thisRouter && dest != src) { |
| 445 | |
| 446 | ndn::Name destRouterName = map.getRouterNameByMappingNo(dest); |
| 447 | |
| 448 | double distance = getHyperbolicDistance(map, lsdb, srcRouterName, destRouterName); |
| 449 | |
| 450 | // Could not compute distance |
| 451 | if (distance == UNKNOWN_DISTANCE) { |
| 452 | _LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName << " to " << |
| 453 | destRouterName); |
| 454 | continue; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 455 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 456 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 457 | addNextHop(destRouterName, srcFaceUri, distance, rt); |
| 458 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 459 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | |
| 463 | double |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 464 | HyperbolicRoutingCalculator::getHyperbolicDistance(Map& map, Lsdb& lsdb, |
| 465 | ndn::Name src, ndn::Name dest) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 466 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 467 | _LOG_TRACE("Calculating hyperbolic distance from " << src << " to " << dest); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 468 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 469 | double distance = UNKNOWN_DISTANCE; |
| 470 | |
| 471 | ndn::Name srcLsaKey = src; |
| 472 | srcLsaKey.append("coordinate"); |
| 473 | |
| 474 | CoordinateLsa* srcLsa = lsdb.findCoordinateLsa(srcLsaKey); |
| 475 | |
| 476 | ndn::Name destLsaKey = dest; |
| 477 | destLsaKey.append("coordinate"); |
| 478 | |
| 479 | CoordinateLsa* destLsa = lsdb.findCoordinateLsa(destLsaKey); |
| 480 | |
| 481 | // Coordinate LSAs do not exist for these routers |
Nick Gordon | e9733ed | 2017-04-26 10:48:39 -0500 | [diff] [blame^] | 482 | if (srcLsa == nullptr || destLsa == nullptr) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 483 | return UNKNOWN_DISTANCE; |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 484 | } |
| 485 | |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 486 | double srcTheta = srcLsa->getCorTheta(); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 487 | double destTheta = destLsa->getCorTheta(); |
| 488 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 489 | double diffTheta = fabs(srcTheta - destTheta); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 490 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 491 | if (diffTheta > MATH_PI) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 492 | diffTheta = 2 * MATH_PI - diffTheta; |
| 493 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 494 | |
| 495 | double srcRadius = srcLsa->getCorRadius(); |
| 496 | double destRadius = destLsa->getCorRadius(); |
| 497 | |
| 498 | if (srcRadius == UNKNOWN_RADIUS && destRadius == UNKNOWN_RADIUS) { |
| 499 | return UNKNOWN_DISTANCE; |
| 500 | } |
| 501 | |
| 502 | if (diffTheta == 0) { |
| 503 | distance = fabs(srcRadius - destRadius); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 504 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 505 | else { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 506 | distance = acosh((cosh(srcRadius) * cosh(destRadius)) - |
| 507 | (sinh(srcRadius) * sinh(destRadius) * cos(diffTheta))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 508 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 509 | |
| 510 | _LOG_TRACE("Distance from " << src << " to " << dest << " is " << distance); |
| 511 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 512 | return distance; |
| 513 | } |
| 514 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 515 | void HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri, |
| 516 | double cost, RoutingTable& rt) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 517 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 518 | NextHop hop(faceUri, cost); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 519 | hop.setHyperbolic(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 520 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 521 | _LOG_TRACE("Calculated " << hop << " for destination: " << dest); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 522 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 523 | if (m_isDryRun) { |
| 524 | rt.addNextHopToDryTable(dest, hop); |
| 525 | } |
| 526 | else { |
| 527 | rt.addNextHop(dest, hop); |
| 528 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 529 | } |
| 530 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 531 | } // namespace nlsr |