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 | std::cout << std::endl; |
| 246 | int nextHopRouter = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 247 | for (int i = 0; i < numOfRouter ; i++) { |
| 248 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 249 | nextHopRouter = getLsNextHop(i, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 250 | if (nextHopRouter != NO_NEXT_HOP) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 251 | double routeCost = m_distance[i]; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 252 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 253 | std::string nextHopFace = |
| 254 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 255 | // Add next hop to routing table |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 256 | NextHop nh(nextHopFace, routeCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 257 | rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | int |
| 264 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 265 | { |
| 266 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 267 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 268 | nextHop = dest; |
| 269 | dest = m_parent[dest]; |
| 270 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 271 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 272 | nextHop = NO_NEXT_HOP; |
| 273 | } |
| 274 | return nextHop; |
| 275 | } |
| 276 | |
| 277 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 278 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame^] | 279 | double* dist, |
| 280 | int start, int element) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 281 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 282 | for (int i = start ; i < element ; i++) { |
| 283 | for (int j = i + 1; j < element; j++) { |
| 284 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 285 | int tempU = Q[j]; |
| 286 | Q[j] = Q[i]; |
| 287 | Q[i] = tempU; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | int |
| 294 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 295 | int u, int start, int element) |
| 296 | { |
| 297 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 298 | for (int i = start; i < element; i++) { |
| 299 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 300 | ret = 1; |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | LinkStateRoutingTableCalculator::allocateParent() |
| 309 | { |
| 310 | m_parent = new int[numOfRouter]; |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | LinkStateRoutingTableCalculator::allocateDistance() |
| 315 | { |
| 316 | m_distance = new double[numOfRouter]; |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | LinkStateRoutingTableCalculator::freeParent() |
| 321 | { |
| 322 | delete [] m_parent; |
| 323 | } |
| 324 | |
| 325 | void LinkStateRoutingTableCalculator::freeDistance() |
| 326 | { |
| 327 | delete [] m_distance; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | |
| 332 | void |
| 333 | HypRoutingTableCalculator::calculatePath(Map& pMap, |
| 334 | RoutingTable& rt, Nlsr& pnlsr) |
| 335 | { |
| 336 | makeAdjMatrix(pnlsr, pMap); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame^] | 337 | //std::cout << pMap; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 338 | ndn::Name routerName = pnlsr.getConfParameter().getRouterPrefix(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 339 | int sourceRouter = pMap.getMappingNoByRouterName(routerName); |
| 340 | int noLink = getNumOfLinkfromAdjMatrix(sourceRouter); |
| 341 | setNoLink(noLink); |
| 342 | allocateLinks(); |
| 343 | allocateLinkCosts(); |
| 344 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 345 | for (int i = 0 ; i < numOfRouter ; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 346 | int k = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 347 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 348 | allocateLinkFaces(); |
| 349 | allocateDistanceToNeighbor(); |
| 350 | allocateDistFromNbrToDest(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 351 | for (int j = 0; j < vNoLink; j++) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 352 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(links[j]); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 353 | std::string nextHopFaceUri = |
| 354 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 355 | double distToNbr = getHyperbolicDistance(pnlsr, pMap, |
| 356 | sourceRouter, links[j]); |
| 357 | double distToDestFromNbr = getHyperbolicDistance(pnlsr, |
| 358 | pMap, links[j], i); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 359 | if (distToDestFromNbr >= 0) { |
| 360 | m_linkFaceUris[k] = nextHopFaceUri; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 361 | m_distanceToNeighbor[k] = distToNbr; |
| 362 | m_distFromNbrToDest[k] = distToDestFromNbr; |
| 363 | k++; |
| 364 | } |
| 365 | } |
| 366 | addHypNextHopsToRoutingTable(pnlsr, pMap, rt, k, i); |
| 367 | freeLinkFaces(); |
| 368 | freeDistanceToNeighbor(); |
| 369 | freeDistFromNbrToDest(); |
| 370 | } |
| 371 | } |
| 372 | freeLinks(); |
| 373 | freeLinksCosts(); |
| 374 | freeAdjMatrix(); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr, Map& pMap, |
| 379 | RoutingTable& rt, int noFaces, int dest) |
| 380 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 381 | for (int i = 0 ; i < noFaces ; ++i) { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 382 | ndn::Name destRouter = pMap.getRouterNameByMappingNo(dest); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 383 | NextHop nh(m_linkFaceUris[i], m_distFromNbrToDest[i]); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 384 | rt.addNextHop(destRouter, nh); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 385 | if (m_isDryRun) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 386 | rt.addNextHopToDryTable(destRouter, nh); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | double |
| 392 | HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr, |
| 393 | Map& pMap, int src, int dest) |
| 394 | { |
| 395 | double distance = 0.0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 396 | ndn::Name srcRouterKey = pMap.getRouterNameByMappingNo(src); |
| 397 | srcRouterKey.append("coordinate"); |
| 398 | ndn::Name destRouterKey = pMap.getRouterNameByMappingNo(dest); |
| 399 | destRouterKey.append("coordinate"); |
akmhoque | b6450b1 | 2014-04-24 00:01:03 -0500 | [diff] [blame] | 400 | double srcRadius = (pnlsr.getLsdb().findCoordinateLsa( |
| 401 | srcRouterKey))->getCorRadius(); |
| 402 | double srcTheta = (pnlsr.getLsdb().findCoordinateLsa( |
| 403 | srcRouterKey))->getCorTheta(); |
| 404 | double destRadius = (pnlsr.getLsdb().findCoordinateLsa( |
| 405 | destRouterKey))->getCorRadius(); |
| 406 | double destTheta = (pnlsr.getLsdb().findCoordinateLsa( |
| 407 | destRouterKey))->getCorTheta(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 408 | double diffTheta = fabs(srcTheta - destTheta); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 409 | if (diffTheta > MATH_PI) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 410 | diffTheta = 2 * MATH_PI - diffTheta; |
| 411 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 412 | if (srcRadius != -1 && destRadius != -1) { |
| 413 | if (diffTheta == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 414 | distance = fabs(srcRadius - destRadius); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 415 | } |
| 416 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 417 | distance = acosh((cosh(srcRadius) * cosh(destRadius)) - |
| 418 | (sinh(srcRadius) * sinh(destRadius) * cos(diffTheta))); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 419 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 420 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 421 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 422 | distance = -1; |
| 423 | } |
| 424 | return distance; |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | HypRoutingTableCalculator::allocateLinkFaces() |
| 429 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 430 | m_linkFaceUris.reserve(vNoLink); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | void |
| 434 | HypRoutingTableCalculator::allocateDistanceToNeighbor() |
| 435 | { |
| 436 | m_distanceToNeighbor = new double[vNoLink]; |
| 437 | } |
| 438 | |
| 439 | void |
| 440 | HypRoutingTableCalculator::allocateDistFromNbrToDest() |
| 441 | { |
| 442 | m_distFromNbrToDest = new double[vNoLink]; |
| 443 | } |
| 444 | |
| 445 | void |
| 446 | HypRoutingTableCalculator::freeLinkFaces() |
| 447 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 448 | m_linkFaceUris.clear(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void |
| 452 | HypRoutingTableCalculator::freeDistanceToNeighbor() |
| 453 | { |
| 454 | delete [] m_distanceToNeighbor; |
| 455 | } |
| 456 | |
| 457 | void |
| 458 | HypRoutingTableCalculator::freeDistFromNbrToDest() |
| 459 | { |
| 460 | delete [] m_distFromNbrToDest; |
| 461 | } |
| 462 | |
| 463 | }//namespace nlsr |