akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014 University of Memphis, |
| 4 | * Regents of the University of California |
| 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | * \author A K M Mahmudul Hoque <ahoque1@memphis.edu> |
| 21 | * |
| 22 | **/ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <cmath> |
| 25 | #include "lsdb.hpp" |
| 26 | #include "routing-table-calculator.hpp" |
| 27 | #include "map.hpp" |
| 28 | #include "lsa.hpp" |
| 29 | #include "nexthop.hpp" |
| 30 | #include "nlsr.hpp" |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 31 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | |
| 33 | namespace nlsr { |
| 34 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 35 | INIT_LOGGER("RoutingTableCalculator"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | using namespace std; |
| 37 | |
| 38 | void |
| 39 | RoutingTableCalculator::allocateAdjMatrix() |
| 40 | { |
| 41 | adjMatrix = new double*[numOfRouter]; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 42 | for (int i = 0; i < numOfRouter; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | adjMatrix[i] = new double[numOfRouter]; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | RoutingTableCalculator::initMatrix() |
| 49 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 50 | for (int i = 0; i < numOfRouter; i++) { |
| 51 | for (int j = 0; j < numOfRouter; j++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | adjMatrix[i][j] = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 53 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | RoutingTableCalculator::makeAdjMatrix(Nlsr& pnlsr, Map pMap) |
| 59 | { |
| 60 | std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb(); |
| 61 | for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 62 | it != adjLsdb.end() ; it++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 63 | int row = pMap.getMappingNoByRouterName((*it).getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 64 | std::list<Adjacent> adl = (*it).getAdl().getAdjList(); |
| 65 | for (std::list<Adjacent>::iterator itAdl = adl.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 66 | itAdl != adl.end() ; itAdl++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 67 | int col = pMap.getMappingNoByRouterName((*itAdl).getName()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | double cost = (*itAdl).getLinkCost(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 69 | if ((row >= 0 && row < numOfRouter) && (col >= 0 && col < numOfRouter)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 70 | adjMatrix[row][col] = cost; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 77 | RoutingTableCalculator::writeAdjMatrixLog() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 78 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | for (int i = 0; i < numOfRouter; i++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 80 | string line=""; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 81 | for (int j = 0; j < numOfRouter; j++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 82 | line += boost::lexical_cast<std::string>(adjMatrix[i][j]); |
| 83 | line += " "; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 84 | } |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 85 | _LOG_DEBUG(line); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 91 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 92 | for (int i = 0; i < numOfRouter; i++) { |
| 93 | if (i == link) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 94 | adjMatrix[source][i] = linkCost; |
| 95 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 96 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | adjMatrix[source][i] = 0; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | int |
| 103 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 104 | { |
| 105 | int noLink = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 106 | for (int i = 0; i < numOfRouter; i++) { |
| 107 | if (adjMatrix[sRouter][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 108 | noLink++; |
| 109 | } |
| 110 | } |
| 111 | return noLink; |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | RoutingTableCalculator::getLinksFromAdjMatrix(int* links, |
| 116 | double* linkCosts, int source) |
| 117 | { |
| 118 | int j = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 119 | for (int i = 0; i < numOfRouter; i++) { |
| 120 | if (adjMatrix[source][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | links[j] = i; |
| 122 | linkCosts[j] = adjMatrix[source][i]; |
| 123 | j++; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | RoutingTableCalculator::freeAdjMatrix() |
| 130 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 131 | for (int i = 0; i < numOfRouter; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 132 | delete [] adjMatrix[i]; |
| 133 | } |
| 134 | delete [] adjMatrix; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void |
| 139 | RoutingTableCalculator::allocateLinks() |
| 140 | { |
| 141 | links = new int[vNoLink]; |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | RoutingTableCalculator::allocateLinkCosts() |
| 146 | { |
| 147 | linkCosts = new double[vNoLink]; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | void |
| 152 | RoutingTableCalculator::freeLinks() |
| 153 | { |
| 154 | delete [] links; |
| 155 | } |
| 156 | void |
| 157 | RoutingTableCalculator::freeLinksCosts() |
| 158 | { |
| 159 | delete [] linkCosts; |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | LinkStateRoutingTableCalculator::calculatePath(Map& pMap, |
| 164 | RoutingTable& rt, Nlsr& pnlsr) |
| 165 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 166 | _LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | allocateAdjMatrix(); |
| 168 | initMatrix(); |
| 169 | makeAdjMatrix(pnlsr, pMap); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 170 | //printAdjMatrix(); |
| 171 | writeAdjMatrixLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 172 | int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 173 | allocateParent(); |
| 174 | allocateDistance(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 175 | if (pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 176 | // Single Path |
| 177 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 178 | // update routing table |
| 179 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 180 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 181 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 182 | // Multi Path |
| 183 | setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter)); |
| 184 | allocateLinks(); |
| 185 | allocateLinkCosts(); |
| 186 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 187 | for (int i = 0 ; i < vNoLink; i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 188 | adjustAdMatrix(sourceRouter, links[i], linkCosts[i]); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 189 | writeAdjMatrixLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | //update routing table |
| 192 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 193 | } |
| 194 | freeLinks(); |
| 195 | freeLinksCosts(); |
| 196 | } |
| 197 | freeParent(); |
| 198 | freeDistance(); |
| 199 | freeAdjMatrix(); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 204 | { |
| 205 | int i; |
| 206 | int v, u; |
| 207 | int* Q = new int[numOfRouter]; |
| 208 | int head = 0; |
| 209 | /* Initiate the Parent */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 210 | for (i = 0 ; i < numOfRouter; i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | m_parent[i] = EMPTY_PARENT; |
| 212 | m_distance[i] = INF_DISTANCE; |
| 213 | Q[i] = i; |
| 214 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 215 | if (sourceRouter != NO_MAPPING_NUM) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | m_distance[sourceRouter] = 0; |
| 217 | sortQueueByDistance(Q, m_distance, head, numOfRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 218 | while (head < numOfRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 219 | u = Q[head]; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 220 | if (m_distance[u] == INF_DISTANCE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 221 | break; |
| 222 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 223 | for (v = 0 ; v < numOfRouter; v++) { |
| 224 | if (adjMatrix[u][v] > 0) { |
| 225 | if (isNotExplored(Q, v, head + 1, numOfRouter)) { |
| 226 | if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | m_distance[v] = m_distance[u] + adjMatrix[u][v] ; |
| 228 | m_parent[v] = u; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | head++; |
| 234 | sortQueueByDistance(Q, m_distance, head, numOfRouter); |
| 235 | } |
| 236 | } |
| 237 | delete [] Q; |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, |
| 242 | RoutingTable& rt, Map& pMap, int sourceRouter) |
| 243 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 244 | _LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 245 | int nextHopRouter = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 246 | for (int i = 0; i < numOfRouter ; i++) { |
| 247 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 248 | nextHopRouter = getLsNextHop(i, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 249 | if (nextHopRouter != NO_NEXT_HOP) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 250 | double routeCost = m_distance[i]; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 251 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 252 | std::string nextHopFace = |
| 253 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 254 | // Add next hop to routing table |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 255 | NextHop nh(nextHopFace, routeCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 256 | rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | int |
| 263 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 264 | { |
| 265 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 266 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 267 | nextHop = dest; |
| 268 | dest = m_parent[dest]; |
| 269 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 270 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 271 | nextHop = NO_NEXT_HOP; |
| 272 | } |
| 273 | return nextHop; |
| 274 | } |
| 275 | |
| 276 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 277 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 278 | double* dist, |
| 279 | int start, int element) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 280 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 281 | for (int i = start ; i < element ; i++) { |
| 282 | for (int j = i + 1; j < element; j++) { |
| 283 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 284 | int tempU = Q[j]; |
| 285 | Q[j] = Q[i]; |
| 286 | Q[i] = tempU; |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | int |
| 293 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 294 | int u, int start, int element) |
| 295 | { |
| 296 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 297 | for (int i = start; i < element; i++) { |
| 298 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 299 | ret = 1; |
| 300 | break; |
| 301 | } |
| 302 | } |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | void |
| 307 | LinkStateRoutingTableCalculator::allocateParent() |
| 308 | { |
| 309 | m_parent = new int[numOfRouter]; |
| 310 | } |
| 311 | |
| 312 | void |
| 313 | LinkStateRoutingTableCalculator::allocateDistance() |
| 314 | { |
| 315 | m_distance = new double[numOfRouter]; |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | LinkStateRoutingTableCalculator::freeParent() |
| 320 | { |
| 321 | delete [] m_parent; |
| 322 | } |
| 323 | |
| 324 | void LinkStateRoutingTableCalculator::freeDistance() |
| 325 | { |
| 326 | delete [] m_distance; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | void |
| 332 | HypRoutingTableCalculator::calculatePath(Map& pMap, |
| 333 | RoutingTable& rt, Nlsr& pnlsr) |
| 334 | { |
| 335 | makeAdjMatrix(pnlsr, pMap); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 336 | //std::cout << pMap; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 337 | ndn::Name routerName = pnlsr.getConfParameter().getRouterPrefix(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 338 | int sourceRouter = pMap.getMappingNoByRouterName(routerName); |
| 339 | int noLink = getNumOfLinkfromAdjMatrix(sourceRouter); |
| 340 | setNoLink(noLink); |
| 341 | allocateLinks(); |
| 342 | allocateLinkCosts(); |
| 343 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 344 | for (int i = 0 ; i < numOfRouter ; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 345 | int k = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 346 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 347 | allocateLinkFaces(); |
| 348 | allocateDistanceToNeighbor(); |
| 349 | allocateDistFromNbrToDest(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 350 | for (int j = 0; j < vNoLink; j++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 351 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(links[j]); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 352 | std::string nextHopFaceUri = |
| 353 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 354 | double distToNbr = getHyperbolicDistance(pnlsr, pMap, |
| 355 | sourceRouter, links[j]); |
| 356 | double distToDestFromNbr = getHyperbolicDistance(pnlsr, |
| 357 | pMap, links[j], i); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 358 | if (distToDestFromNbr >= 0) { |
| 359 | m_linkFaceUris[k] = nextHopFaceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 360 | m_distanceToNeighbor[k] = distToNbr; |
| 361 | m_distFromNbrToDest[k] = distToDestFromNbr; |
| 362 | k++; |
| 363 | } |
| 364 | } |
| 365 | addHypNextHopsToRoutingTable(pnlsr, pMap, rt, k, i); |
| 366 | freeLinkFaces(); |
| 367 | freeDistanceToNeighbor(); |
| 368 | freeDistFromNbrToDest(); |
| 369 | } |
| 370 | } |
| 371 | freeLinks(); |
| 372 | freeLinksCosts(); |
| 373 | freeAdjMatrix(); |
| 374 | } |
| 375 | |
| 376 | void |
| 377 | HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr, Map& pMap, |
| 378 | RoutingTable& rt, int noFaces, int dest) |
| 379 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 380 | for (int i = 0 ; i < noFaces ; ++i) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 381 | ndn::Name destRouter = pMap.getRouterNameByMappingNo(dest); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 382 | NextHop nh(m_linkFaceUris[i], m_distFromNbrToDest[i]); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 383 | rt.addNextHop(destRouter, nh); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 384 | if (m_isDryRun) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 385 | rt.addNextHopToDryTable(destRouter, nh); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | double |
| 391 | HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr, |
| 392 | Map& pMap, int src, int dest) |
| 393 | { |
| 394 | double distance = 0.0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 395 | ndn::Name srcRouterKey = pMap.getRouterNameByMappingNo(src); |
| 396 | srcRouterKey.append("coordinate"); |
| 397 | ndn::Name destRouterKey = pMap.getRouterNameByMappingNo(dest); |
| 398 | destRouterKey.append("coordinate"); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 399 | double srcRadius = (pnlsr.getLsdb().findCoordinateLsa( |
| 400 | srcRouterKey))->getCorRadius(); |
| 401 | double srcTheta = (pnlsr.getLsdb().findCoordinateLsa( |
| 402 | srcRouterKey))->getCorTheta(); |
| 403 | double destRadius = (pnlsr.getLsdb().findCoordinateLsa( |
| 404 | destRouterKey))->getCorRadius(); |
| 405 | double destTheta = (pnlsr.getLsdb().findCoordinateLsa( |
| 406 | destRouterKey))->getCorTheta(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 407 | double diffTheta = fabs(srcTheta - destTheta); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 408 | if (diffTheta > MATH_PI) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 409 | diffTheta = 2 * MATH_PI - diffTheta; |
| 410 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 411 | if (srcRadius != -1 && destRadius != -1) { |
| 412 | if (diffTheta == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 413 | distance = fabs(srcRadius - destRadius); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 414 | } |
| 415 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 416 | distance = acosh((cosh(srcRadius) * cosh(destRadius)) - |
| 417 | (sinh(srcRadius) * sinh(destRadius) * cos(diffTheta))); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 418 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 419 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 420 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 421 | distance = -1; |
| 422 | } |
| 423 | return distance; |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | HypRoutingTableCalculator::allocateLinkFaces() |
| 428 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 429 | m_linkFaceUris.reserve(vNoLink); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void |
| 433 | HypRoutingTableCalculator::allocateDistanceToNeighbor() |
| 434 | { |
| 435 | m_distanceToNeighbor = new double[vNoLink]; |
| 436 | } |
| 437 | |
| 438 | void |
| 439 | HypRoutingTableCalculator::allocateDistFromNbrToDest() |
| 440 | { |
| 441 | m_distFromNbrToDest = new double[vNoLink]; |
| 442 | } |
| 443 | |
| 444 | void |
| 445 | HypRoutingTableCalculator::freeLinkFaces() |
| 446 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 447 | m_linkFaceUris.clear(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void |
| 451 | HypRoutingTableCalculator::freeDistanceToNeighbor() |
| 452 | { |
| 453 | delete [] m_distanceToNeighbor; |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | HypRoutingTableCalculator::freeDistFromNbrToDest() |
| 458 | { |
| 459 | delete [] m_distFromNbrToDest; |
| 460 | } |
| 461 | |
| 462 | }//namespace nlsr |