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" |
| 31 | |
| 32 | namespace nlsr { |
| 33 | |
| 34 | using namespace std; |
| 35 | |
| 36 | void |
| 37 | RoutingTableCalculator::allocateAdjMatrix() |
| 38 | { |
| 39 | adjMatrix = new double*[numOfRouter]; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 40 | for (int i = 0; i < numOfRouter; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | adjMatrix[i] = new double[numOfRouter]; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | RoutingTableCalculator::initMatrix() |
| 47 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 48 | for (int i = 0; i < numOfRouter; i++) { |
| 49 | for (int j = 0; j < numOfRouter; j++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | adjMatrix[i][j] = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 51 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | RoutingTableCalculator::makeAdjMatrix(Nlsr& pnlsr, Map pMap) |
| 57 | { |
| 58 | std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb(); |
| 59 | for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 60 | it != adjLsdb.end() ; it++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 61 | int row = pMap.getMappingNoByRouterName((*it).getOrigRouter()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | std::list<Adjacent> adl = (*it).getAdl().getAdjList(); |
| 63 | for (std::list<Adjacent>::iterator itAdl = adl.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 64 | itAdl != adl.end() ; itAdl++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 65 | int col = pMap.getMappingNoByRouterName((*itAdl).getName()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 66 | double cost = (*itAdl).getLinkCost(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 67 | if ((row >= 0 && row < numOfRouter) && (col >= 0 && col < numOfRouter)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | adjMatrix[row][col] = cost; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | RoutingTableCalculator::printAdjMatrix() |
| 76 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 77 | for (int i = 0; i < numOfRouter; i++) { |
| 78 | for (int j = 0; j < numOfRouter; j++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | printf("%f ", adjMatrix[i][j]); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 80 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | printf("\n"); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 87 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 88 | for (int i = 0; i < numOfRouter; i++) { |
| 89 | if (i == link) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 90 | adjMatrix[source][i] = linkCost; |
| 91 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 92 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | adjMatrix[source][i] = 0; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 100 | { |
| 101 | int noLink = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 102 | for (int i = 0; i < numOfRouter; i++) { |
| 103 | if (adjMatrix[sRouter][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | noLink++; |
| 105 | } |
| 106 | } |
| 107 | return noLink; |
| 108 | } |
| 109 | |
| 110 | void |
| 111 | RoutingTableCalculator::getLinksFromAdjMatrix(int* links, |
| 112 | double* linkCosts, int source) |
| 113 | { |
| 114 | int j = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 115 | for (int i = 0; i < numOfRouter; i++) { |
| 116 | if (adjMatrix[source][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 117 | links[j] = i; |
| 118 | linkCosts[j] = adjMatrix[source][i]; |
| 119 | j++; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void |
| 125 | RoutingTableCalculator::freeAdjMatrix() |
| 126 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 127 | for (int i = 0; i < numOfRouter; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 128 | delete [] adjMatrix[i]; |
| 129 | } |
| 130 | delete [] adjMatrix; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | void |
| 135 | RoutingTableCalculator::allocateLinks() |
| 136 | { |
| 137 | links = new int[vNoLink]; |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | RoutingTableCalculator::allocateLinkCosts() |
| 142 | { |
| 143 | linkCosts = new double[vNoLink]; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void |
| 148 | RoutingTableCalculator::freeLinks() |
| 149 | { |
| 150 | delete [] links; |
| 151 | } |
| 152 | void |
| 153 | RoutingTableCalculator::freeLinksCosts() |
| 154 | { |
| 155 | delete [] linkCosts; |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | LinkStateRoutingTableCalculator::calculatePath(Map& pMap, |
| 160 | RoutingTable& rt, Nlsr& pnlsr) |
| 161 | { |
| 162 | std::cout << "LinkStateRoutingTableCalculator::calculatePath Called" << |
| 163 | std::endl; |
| 164 | allocateAdjMatrix(); |
| 165 | initMatrix(); |
| 166 | makeAdjMatrix(pnlsr, pMap); |
| 167 | std::cout << pMap; |
| 168 | printAdjMatrix(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 169 | int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | //int noLink=getNumOfLinkfromAdjMatrix(sourceRouter); |
| 171 | allocateParent(); |
| 172 | allocateDistance(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 173 | if (pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | // Single Path |
| 175 | doDijkstraPathCalculation(sourceRouter); |
| 176 | // print all ls path -- debugging purpose |
| 177 | printAllLsPath(sourceRouter); |
| 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]); |
| 189 | printAdjMatrix(); |
| 190 | doDijkstraPathCalculation(sourceRouter); |
| 191 | // print all ls path -- debugging purpose |
| 192 | printAllLsPath(sourceRouter); |
| 193 | //update routing table |
| 194 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 195 | } |
| 196 | freeLinks(); |
| 197 | freeLinksCosts(); |
| 198 | } |
| 199 | freeParent(); |
| 200 | freeDistance(); |
| 201 | freeAdjMatrix(); |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 206 | { |
| 207 | int i; |
| 208 | int v, u; |
| 209 | int* Q = new int[numOfRouter]; |
| 210 | int head = 0; |
| 211 | /* Initiate the Parent */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 212 | for (i = 0 ; i < numOfRouter; i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 213 | m_parent[i] = EMPTY_PARENT; |
| 214 | m_distance[i] = INF_DISTANCE; |
| 215 | Q[i] = i; |
| 216 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 217 | if (sourceRouter != NO_MAPPING_NUM) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | m_distance[sourceRouter] = 0; |
| 219 | sortQueueByDistance(Q, m_distance, head, numOfRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 220 | while (head < numOfRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 221 | u = Q[head]; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 222 | if (m_distance[u] == INF_DISTANCE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 223 | break; |
| 224 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 225 | for (v = 0 ; v < numOfRouter; v++) { |
| 226 | if (adjMatrix[u][v] > 0) { |
| 227 | if (isNotExplored(Q, v, head + 1, numOfRouter)) { |
| 228 | if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 229 | m_distance[v] = m_distance[u] + adjMatrix[u][v] ; |
| 230 | m_parent[v] = u; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | head++; |
| 236 | sortQueueByDistance(Q, m_distance, head, numOfRouter); |
| 237 | } |
| 238 | } |
| 239 | delete [] Q; |
| 240 | } |
| 241 | |
| 242 | void |
| 243 | LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, |
| 244 | RoutingTable& rt, Map& pMap, int sourceRouter) |
| 245 | { |
| 246 | std::cout << |
| 247 | "LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called"; |
| 248 | std::cout << std::endl; |
| 249 | int nextHopRouter = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 250 | for (int i = 0; i < numOfRouter ; i++) { |
| 251 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 252 | nextHopRouter = getLsNextHop(i, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 253 | if (nextHopRouter != NO_NEXT_HOP) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 254 | double routeCost = m_distance[i]; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 255 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 256 | std::string nextHopFace = |
| 257 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 258 | std::cout << "Dest Router: " << pMap.getRouterNameByMappingNo(i) << std::endl; |
| 259 | std::cout << "Next hop Router: " << nextHopRouterName << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 260 | std::cout << "Next hop Face: " << nextHopFace << std::endl; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 261 | std::cout << "Route Cost: " << routeCost << std::endl; |
| 262 | std::cout << std::endl; |
| 263 | // Add next hop to routing table |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 264 | NextHop nh(nextHopFace, routeCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 265 | rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | int |
| 272 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 273 | { |
| 274 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 275 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 276 | nextHop = dest; |
| 277 | dest = m_parent[dest]; |
| 278 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 279 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 280 | nextHop = NO_NEXT_HOP; |
| 281 | } |
| 282 | return nextHop; |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | LinkStateRoutingTableCalculator::printAllLsPath(int sourceRouter) |
| 287 | { |
| 288 | std::cout << "LinkStateRoutingTableCalculator::printAllLsPath Called" << |
| 289 | std::endl; |
| 290 | std::cout << "Source Router: " << sourceRouter << std::endl; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 291 | for (int i = 0; i < numOfRouter ; i++) { |
| 292 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 293 | printLsPath(i); |
| 294 | std::cout << std::endl; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | LinkStateRoutingTableCalculator::printLsPath(int destRouter) |
| 301 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 302 | if (m_parent[destRouter] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 303 | printLsPath(m_parent[destRouter]); |
| 304 | } |
| 305 | std:: cout << " " << destRouter; |
| 306 | } |
| 307 | |
| 308 | void |
| 309 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
| 310 | double* dist, int start, int element) |
| 311 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 312 | for (int i = start ; i < element ; i++) { |
| 313 | for (int j = i + 1; j < element; j++) { |
| 314 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 315 | int tempU = Q[j]; |
| 316 | Q[j] = Q[i]; |
| 317 | Q[i] = tempU; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | int |
| 324 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 325 | int u, int start, int element) |
| 326 | { |
| 327 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 328 | for (int i = start; i < element; i++) { |
| 329 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 330 | ret = 1; |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | void |
| 338 | LinkStateRoutingTableCalculator::allocateParent() |
| 339 | { |
| 340 | m_parent = new int[numOfRouter]; |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | LinkStateRoutingTableCalculator::allocateDistance() |
| 345 | { |
| 346 | m_distance = new double[numOfRouter]; |
| 347 | } |
| 348 | |
| 349 | void |
| 350 | LinkStateRoutingTableCalculator::freeParent() |
| 351 | { |
| 352 | delete [] m_parent; |
| 353 | } |
| 354 | |
| 355 | void LinkStateRoutingTableCalculator::freeDistance() |
| 356 | { |
| 357 | delete [] m_distance; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | |
| 362 | void |
| 363 | HypRoutingTableCalculator::calculatePath(Map& pMap, |
| 364 | RoutingTable& rt, Nlsr& pnlsr) |
| 365 | { |
| 366 | makeAdjMatrix(pnlsr, pMap); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 367 | ndn::Name routerName = pnlsr.getConfParameter().getRouterPrefix(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 368 | int sourceRouter = pMap.getMappingNoByRouterName(routerName); |
| 369 | int noLink = getNumOfLinkfromAdjMatrix(sourceRouter); |
| 370 | setNoLink(noLink); |
| 371 | allocateLinks(); |
| 372 | allocateLinkCosts(); |
| 373 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 374 | for (int i = 0 ; i < numOfRouter ; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 375 | int k = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 376 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 377 | allocateLinkFaces(); |
| 378 | allocateDistanceToNeighbor(); |
| 379 | allocateDistFromNbrToDest(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 380 | for (int j = 0; j < vNoLink; j++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 381 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(links[j]); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 382 | std::string nextHopFaceUri = |
| 383 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 384 | double distToNbr = getHyperbolicDistance(pnlsr, pMap, |
| 385 | sourceRouter, links[j]); |
| 386 | double distToDestFromNbr = getHyperbolicDistance(pnlsr, |
| 387 | pMap, links[j], i); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 388 | if (distToDestFromNbr >= 0) { |
| 389 | m_linkFaceUris[k] = nextHopFaceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 390 | m_distanceToNeighbor[k] = distToNbr; |
| 391 | m_distFromNbrToDest[k] = distToDestFromNbr; |
| 392 | k++; |
| 393 | } |
| 394 | } |
| 395 | addHypNextHopsToRoutingTable(pnlsr, pMap, rt, k, i); |
| 396 | freeLinkFaces(); |
| 397 | freeDistanceToNeighbor(); |
| 398 | freeDistFromNbrToDest(); |
| 399 | } |
| 400 | } |
| 401 | freeLinks(); |
| 402 | freeLinksCosts(); |
| 403 | freeAdjMatrix(); |
| 404 | } |
| 405 | |
| 406 | void |
| 407 | HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr, Map& pMap, |
| 408 | RoutingTable& rt, int noFaces, int dest) |
| 409 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 410 | for (int i = 0 ; i < noFaces ; ++i) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 411 | ndn::Name destRouter = pMap.getRouterNameByMappingNo(dest); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 412 | NextHop nh(m_linkFaceUris[i], m_distFromNbrToDest[i]); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 413 | rt.addNextHop(destRouter, nh); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 414 | if (m_isDryRun) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 415 | rt.addNextHopToDryTable(destRouter, nh); |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | double |
| 421 | HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr, |
| 422 | Map& pMap, int src, int dest) |
| 423 | { |
| 424 | double distance = 0.0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 425 | ndn::Name srcRouterKey = pMap.getRouterNameByMappingNo(src); |
| 426 | srcRouterKey.append("coordinate"); |
| 427 | ndn::Name destRouterKey = pMap.getRouterNameByMappingNo(dest); |
| 428 | destRouterKey.append("coordinate"); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 429 | double srcRadius = (pnlsr.getLsdb().findCoordinateLsa( |
| 430 | srcRouterKey))->getCorRadius(); |
| 431 | double srcTheta = (pnlsr.getLsdb().findCoordinateLsa( |
| 432 | srcRouterKey))->getCorTheta(); |
| 433 | double destRadius = (pnlsr.getLsdb().findCoordinateLsa( |
| 434 | destRouterKey))->getCorRadius(); |
| 435 | double destTheta = (pnlsr.getLsdb().findCoordinateLsa( |
| 436 | destRouterKey))->getCorTheta(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 437 | double diffTheta = fabs(srcTheta - destTheta); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 438 | if (diffTheta > MATH_PI) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 439 | diffTheta = 2 * MATH_PI - diffTheta; |
| 440 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 441 | if (srcRadius != -1 && destRadius != -1) { |
| 442 | if (diffTheta == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 443 | distance = fabs(srcRadius - destRadius); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 444 | } |
| 445 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 446 | distance = acosh((cosh(srcRadius) * cosh(destRadius)) - |
| 447 | (sinh(srcRadius) * sinh(destRadius) * cos(diffTheta))); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 448 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 449 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 450 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 451 | distance = -1; |
| 452 | } |
| 453 | return distance; |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | HypRoutingTableCalculator::allocateLinkFaces() |
| 458 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 459 | m_linkFaceUris.reserve(vNoLink); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | void |
| 463 | HypRoutingTableCalculator::allocateDistanceToNeighbor() |
| 464 | { |
| 465 | m_distanceToNeighbor = new double[vNoLink]; |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | HypRoutingTableCalculator::allocateDistFromNbrToDest() |
| 470 | { |
| 471 | m_distFromNbrToDest = new double[vNoLink]; |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | HypRoutingTableCalculator::freeLinkFaces() |
| 476 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 477 | m_linkFaceUris.clear(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void |
| 481 | HypRoutingTableCalculator::freeDistanceToNeighbor() |
| 482 | { |
| 483 | delete [] m_distanceToNeighbor; |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | HypRoutingTableCalculator::freeDistFromNbrToDest() |
| 488 | { |
| 489 | delete [] m_distFromNbrToDest; |
| 490 | } |
| 491 | |
| 492 | }//namespace nlsr |