akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Muktadir R Chowdhury | 800833b | 2016-07-29 13:43:59 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 63 | for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); it != adjLsdb.end() ; it++) { |
| 64 | |
| 65 | int32_t row = pMap.getMappingNoByRouterName((*it).getOrigRouter()); |
| 66 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | std::list<Adjacent> adl = (*it).getAdl().getAdjList(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 68 | for (std::list<Adjacent>::iterator itAdl = adl.begin(); itAdl != adl.end() ; itAdl++) { |
| 69 | |
| 70 | int32_t col = pMap.getMappingNoByRouterName((*itAdl).getName()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | double cost = (*itAdl).getLinkCost(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 72 | |
| 73 | if ((row >= 0 && row < static_cast<int32_t>(m_nRouters)) && |
| 74 | (col >= 0 && col < static_cast<int32_t>(m_nRouters))) |
| 75 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 76 | adjMatrix[row][col] = cost; |
| 77 | } |
| 78 | } |
| 79 | } |
Vince Lehman | 41b173e | 2015-05-07 14:13:26 -0500 | [diff] [blame] | 80 | |
| 81 | // Links that do not have the same cost for both directions should have their |
| 82 | // costs corrected: |
| 83 | // |
| 84 | // If the cost of one side of the link is 0, both sides of the link should have their cost |
| 85 | // corrected to 0. |
| 86 | // |
| 87 | // Otherwise, both sides of the link should use the larger of the two costs. |
| 88 | // |
| 89 | for (size_t row = 0; row < m_nRouters; ++row) { |
| 90 | for (size_t col = 0; col < m_nRouters; ++col) { |
| 91 | double toCost = adjMatrix[row][col]; |
| 92 | double fromCost = adjMatrix[col][row]; |
| 93 | |
| 94 | if (fromCost != toCost) { |
| 95 | double correctedCost = 0.0; |
| 96 | |
| 97 | if (toCost != 0 && fromCost != 0) { |
| 98 | // If both sides of the link are up, use the larger cost |
| 99 | correctedCost = std::max(toCost, fromCost); |
| 100 | } |
| 101 | |
| 102 | _LOG_WARN("Cost between [" << row << "][" << col << "] and [" << col << "][" << row << |
| 103 | "] are not the same (" << toCost << " != " << fromCost << "). " << |
| 104 | "Correcting to cost: " << correctedCost); |
| 105 | |
| 106 | adjMatrix[row][col] = correctedCost; |
| 107 | adjMatrix[col][row] = correctedCost; |
| 108 | } |
| 109 | } |
| 110 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 114 | RoutingTableCalculator::writeAdjMatrixLog() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 115 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 116 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 117 | string line=""; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 118 | for (size_t j = 0; j < m_nRouters; j++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 119 | line += boost::lexical_cast<std::string>(adjMatrix[i][j]); |
| 120 | line += " "; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 121 | } |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 122 | _LOG_DEBUG(line); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 128 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 129 | for (int i = 0; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 130 | if (i == link) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | adjMatrix[source][i] = linkCost; |
| 132 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 133 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 134 | adjMatrix[source][i] = 0; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | int |
| 140 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 141 | { |
| 142 | int noLink = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 143 | |
| 144 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 145 | if (adjMatrix[sRouter][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 146 | noLink++; |
| 147 | } |
| 148 | } |
| 149 | return noLink; |
| 150 | } |
| 151 | |
| 152 | void |
| 153 | RoutingTableCalculator::getLinksFromAdjMatrix(int* links, |
| 154 | double* linkCosts, int source) |
| 155 | { |
| 156 | int j = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 157 | |
| 158 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 159 | if (adjMatrix[source][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 160 | links[j] = i; |
| 161 | linkCosts[j] = adjMatrix[source][i]; |
| 162 | j++; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | RoutingTableCalculator::freeAdjMatrix() |
| 169 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 170 | for (size_t i = 0; i < m_nRouters; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 171 | delete [] adjMatrix[i]; |
| 172 | } |
| 173 | delete [] adjMatrix; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | void |
| 178 | RoutingTableCalculator::allocateLinks() |
| 179 | { |
| 180 | links = new int[vNoLink]; |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | RoutingTableCalculator::allocateLinkCosts() |
| 185 | { |
| 186 | linkCosts = new double[vNoLink]; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | void |
| 191 | RoutingTableCalculator::freeLinks() |
| 192 | { |
| 193 | delete [] links; |
| 194 | } |
| 195 | void |
| 196 | RoutingTableCalculator::freeLinksCosts() |
| 197 | { |
| 198 | delete [] linkCosts; |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | LinkStateRoutingTableCalculator::calculatePath(Map& pMap, |
| 203 | RoutingTable& rt, Nlsr& pnlsr) |
| 204 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 205 | _LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 206 | allocateAdjMatrix(); |
| 207 | initMatrix(); |
| 208 | makeAdjMatrix(pnlsr, pMap); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 209 | writeAdjMatrixLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 210 | int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | allocateParent(); |
| 212 | allocateDistance(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 213 | if (pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 214 | // Single Path |
| 215 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | // update routing table |
| 217 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 218 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 219 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 220 | // Multi Path |
| 221 | setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter)); |
| 222 | allocateLinks(); |
| 223 | allocateLinkCosts(); |
| 224 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 225 | for (int i = 0 ; i < vNoLink; i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 226 | adjustAdMatrix(sourceRouter, links[i], linkCosts[i]); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 227 | writeAdjMatrixLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 228 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 229 | //update routing table |
| 230 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 231 | } |
| 232 | freeLinks(); |
| 233 | freeLinksCosts(); |
| 234 | } |
| 235 | freeParent(); |
| 236 | freeDistance(); |
| 237 | freeAdjMatrix(); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 242 | { |
| 243 | int i; |
| 244 | int v, u; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 245 | int* Q = new int[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 246 | int head = 0; |
| 247 | /* Initiate the Parent */ |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 248 | for (i = 0 ; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 249 | m_parent[i] = EMPTY_PARENT; |
| 250 | m_distance[i] = INF_DISTANCE; |
| 251 | Q[i] = i; |
| 252 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 253 | if (sourceRouter != NO_MAPPING_NUM) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 254 | m_distance[sourceRouter] = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 255 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
| 256 | while (head < static_cast<int>(m_nRouters)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 257 | u = Q[head]; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 258 | if (m_distance[u] == INF_DISTANCE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 259 | break; |
| 260 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 261 | for (v = 0 ; v < static_cast<int>(m_nRouters); v++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 262 | if (adjMatrix[u][v] > 0) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 263 | if (isNotExplored(Q, v, head + 1, m_nRouters)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 264 | if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 265 | m_distance[v] = m_distance[u] + adjMatrix[u][v] ; |
| 266 | m_parent[v] = u; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | head++; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 272 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | delete [] Q; |
| 276 | } |
| 277 | |
| 278 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 279 | LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, RoutingTable& rt, |
| 280 | Map& pMap, uint32_t sourceRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 281 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 282 | _LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 283 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 284 | int nextHopRouter = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 285 | |
| 286 | for (size_t i = 0; i < m_nRouters ; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 287 | if (i != sourceRouter) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 288 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 289 | nextHopRouter = getLsNextHop(i, sourceRouter); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 290 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 291 | if (nextHopRouter != NO_NEXT_HOP) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 292 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 293 | double routeCost = m_distance[i]; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 294 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 295 | std::string nextHopFace = |
| 296 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 297 | // Add next hop to routing table |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 298 | NextHop nh(nextHopFace, routeCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 299 | rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | int |
| 306 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 307 | { |
| 308 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 309 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 310 | nextHop = dest; |
| 311 | dest = m_parent[dest]; |
| 312 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 313 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 314 | nextHop = NO_NEXT_HOP; |
| 315 | } |
| 316 | return nextHop; |
| 317 | } |
| 318 | |
| 319 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 320 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 321 | double* dist, |
| 322 | int start, int element) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 323 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 324 | for (int i = start ; i < element ; i++) { |
| 325 | for (int j = i + 1; j < element; j++) { |
| 326 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 327 | int tempU = Q[j]; |
| 328 | Q[j] = Q[i]; |
| 329 | Q[i] = tempU; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | int |
| 336 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 337 | int u, int start, int element) |
| 338 | { |
| 339 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 340 | for (int i = start; i < element; i++) { |
| 341 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 342 | ret = 1; |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | void |
| 350 | LinkStateRoutingTableCalculator::allocateParent() |
| 351 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 352 | m_parent = new int[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void |
| 356 | LinkStateRoutingTableCalculator::allocateDistance() |
| 357 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 358 | m_distance = new double[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
| 362 | LinkStateRoutingTableCalculator::freeParent() |
| 363 | { |
| 364 | delete [] m_parent; |
| 365 | } |
| 366 | |
| 367 | void LinkStateRoutingTableCalculator::freeDistance() |
| 368 | { |
| 369 | delete [] m_distance; |
| 370 | } |
| 371 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 372 | const double HyperbolicRoutingCalculator::MATH_PI = boost::math::constants::pi<double>(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 373 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 374 | const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0; |
| 375 | const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS = -1.0; |
| 376 | |
| 377 | const int32_t HyperbolicRoutingCalculator::ROUTER_NOT_FOUND = -1.0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 378 | |
| 379 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 380 | HyperbolicRoutingCalculator::calculatePaths(Map& map, RoutingTable& rt, |
| 381 | Lsdb& lsdb, AdjacencyList& adjacencies) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 382 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 383 | _LOG_TRACE("Calculating hyperbolic paths"); |
| 384 | |
| 385 | int thisRouter = map.getMappingNoByRouterName(m_thisRouterName); |
| 386 | |
| 387 | // Iterate over directly connected neighbors |
| 388 | std::list<Adjacent> neighbors = adjacencies.getAdjList(); |
| 389 | for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) { |
| 390 | |
| 391 | // Don't calculate nexthops using an inactive router |
| 392 | if (adj->getStatus() == Adjacent::STATUS_INACTIVE) { |
| 393 | _LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop"); |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | ndn::Name srcRouterName = adj->getName(); |
| 398 | |
| 399 | // Don't calculate nexthops for this router to other routers |
| 400 | if (srcRouterName == m_thisRouterName) { |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | std::string srcFaceUri = adj->getConnectingFaceUri(); |
| 405 | |
| 406 | // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link |
| 407 | addNextHop(srcRouterName, srcFaceUri, 0, rt); |
| 408 | |
| 409 | int src = map.getMappingNoByRouterName(srcRouterName); |
| 410 | |
| 411 | if (src == ROUTER_NOT_FOUND) { |
| 412 | _LOG_WARN(adj->getName() << " does not exist in the router map!"); |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | // Get hyperbolic distance from direct neighbor to every other router |
| 417 | for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) { |
| 418 | // Don't calculate nexthops to this router or from a router to itself |
| 419 | if (dest != thisRouter && dest != src) { |
| 420 | |
| 421 | ndn::Name destRouterName = map.getRouterNameByMappingNo(dest); |
| 422 | |
| 423 | double distance = getHyperbolicDistance(map, lsdb, srcRouterName, destRouterName); |
| 424 | |
| 425 | // Could not compute distance |
| 426 | if (distance == UNKNOWN_DISTANCE) { |
| 427 | _LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName << " to " << |
| 428 | destRouterName); |
| 429 | continue; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 430 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 431 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 432 | addNextHop(destRouterName, srcFaceUri, distance, rt); |
| 433 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 434 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | double |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 439 | HyperbolicRoutingCalculator::getHyperbolicDistance(Map& map, Lsdb& lsdb, |
| 440 | ndn::Name src, ndn::Name dest) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 441 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 442 | _LOG_TRACE("Calculating hyperbolic distance from " << src << " to " << dest); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 443 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 444 | double distance = UNKNOWN_DISTANCE; |
| 445 | |
| 446 | ndn::Name srcLsaKey = src; |
| 447 | srcLsaKey.append("coordinate"); |
| 448 | |
| 449 | CoordinateLsa* srcLsa = lsdb.findCoordinateLsa(srcLsaKey); |
| 450 | |
| 451 | ndn::Name destLsaKey = dest; |
| 452 | destLsaKey.append("coordinate"); |
| 453 | |
| 454 | CoordinateLsa* destLsa = lsdb.findCoordinateLsa(destLsaKey); |
| 455 | |
| 456 | // Coordinate LSAs do not exist for these routers |
| 457 | if (srcLsa == NULL || destLsa == NULL) { |
| 458 | return UNKNOWN_DISTANCE; |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 459 | } |
| 460 | |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 461 | double srcTheta = srcLsa->getCorTheta(); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 462 | double destTheta = destLsa->getCorTheta(); |
| 463 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 464 | double diffTheta = fabs(srcTheta - destTheta); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 465 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 466 | if (diffTheta > MATH_PI) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 467 | diffTheta = 2 * MATH_PI - diffTheta; |
| 468 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 469 | |
| 470 | double srcRadius = srcLsa->getCorRadius(); |
| 471 | double destRadius = destLsa->getCorRadius(); |
| 472 | |
| 473 | if (srcRadius == UNKNOWN_RADIUS && destRadius == UNKNOWN_RADIUS) { |
| 474 | return UNKNOWN_DISTANCE; |
| 475 | } |
| 476 | |
| 477 | if (diffTheta == 0) { |
| 478 | distance = fabs(srcRadius - destRadius); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 479 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 480 | else { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 481 | distance = acosh((cosh(srcRadius) * cosh(destRadius)) - |
| 482 | (sinh(srcRadius) * sinh(destRadius) * cos(diffTheta))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 483 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 484 | |
| 485 | _LOG_TRACE("Distance from " << src << " to " << dest << " is " << distance); |
| 486 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 487 | return distance; |
| 488 | } |
| 489 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 490 | void HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri, |
| 491 | double cost, RoutingTable& rt) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 492 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 493 | NextHop hop(faceUri, cost); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame] | 494 | hop.setHyperbolic(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 495 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 496 | _LOG_TRACE("Calculated " << hop << " for destination: " << dest); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 497 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 498 | if (m_isDryRun) { |
| 499 | rt.addNextHopToDryTable(dest, hop); |
| 500 | } |
| 501 | else { |
| 502 | rt.addNextHop(dest, hop); |
| 503 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 504 | } |
| 505 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame^] | 506 | } // namespace nlsr |