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 | writeAdjMatrixLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 171 | int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 172 | allocateParent(); |
| 173 | allocateDistance(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 174 | if (pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 175 | // Single Path |
| 176 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | // update routing table |
| 178 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 179 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 180 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 181 | // Multi Path |
| 182 | setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter)); |
| 183 | allocateLinks(); |
| 184 | allocateLinkCosts(); |
| 185 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 186 | for (int i = 0 ; i < vNoLink; i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 187 | adjustAdMatrix(sourceRouter, links[i], linkCosts[i]); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 188 | writeAdjMatrixLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 189 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | //update routing table |
| 191 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 192 | } |
| 193 | freeLinks(); |
| 194 | freeLinksCosts(); |
| 195 | } |
| 196 | freeParent(); |
| 197 | freeDistance(); |
| 198 | freeAdjMatrix(); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 203 | { |
| 204 | int i; |
| 205 | int v, u; |
| 206 | int* Q = new int[numOfRouter]; |
| 207 | int head = 0; |
| 208 | /* Initiate the Parent */ |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 209 | for (i = 0 ; i < numOfRouter; i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 210 | m_parent[i] = EMPTY_PARENT; |
| 211 | m_distance[i] = INF_DISTANCE; |
| 212 | Q[i] = i; |
| 213 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 214 | if (sourceRouter != NO_MAPPING_NUM) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 215 | m_distance[sourceRouter] = 0; |
| 216 | sortQueueByDistance(Q, m_distance, head, numOfRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 217 | while (head < numOfRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | u = Q[head]; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 219 | if (m_distance[u] == INF_DISTANCE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 220 | break; |
| 221 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 222 | for (v = 0 ; v < numOfRouter; v++) { |
| 223 | if (adjMatrix[u][v] > 0) { |
| 224 | if (isNotExplored(Q, v, head + 1, numOfRouter)) { |
| 225 | if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 226 | m_distance[v] = m_distance[u] + adjMatrix[u][v] ; |
| 227 | m_parent[v] = u; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | head++; |
| 233 | sortQueueByDistance(Q, m_distance, head, numOfRouter); |
| 234 | } |
| 235 | } |
| 236 | delete [] Q; |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, |
| 241 | RoutingTable& rt, Map& pMap, int sourceRouter) |
| 242 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 243 | _LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 244 | int nextHopRouter = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 245 | for (int i = 0; i < numOfRouter ; i++) { |
| 246 | if (i != sourceRouter) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 247 | nextHopRouter = getLsNextHop(i, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 248 | if (nextHopRouter != NO_NEXT_HOP) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 249 | double routeCost = m_distance[i]; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 250 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 251 | std::string nextHopFace = |
| 252 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 253 | // Add next hop to routing table |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 254 | NextHop nh(nextHopFace, routeCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 255 | rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | int |
| 262 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 263 | { |
| 264 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 265 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 266 | nextHop = dest; |
| 267 | dest = m_parent[dest]; |
| 268 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 269 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 270 | nextHop = NO_NEXT_HOP; |
| 271 | } |
| 272 | return nextHop; |
| 273 | } |
| 274 | |
| 275 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 276 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 277 | double* dist, |
| 278 | int start, int element) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 279 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 280 | for (int i = start ; i < element ; i++) { |
| 281 | for (int j = i + 1; j < element; j++) { |
| 282 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 283 | int tempU = Q[j]; |
| 284 | Q[j] = Q[i]; |
| 285 | Q[i] = tempU; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | int |
| 292 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 293 | int u, int start, int element) |
| 294 | { |
| 295 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 296 | for (int i = start; i < element; i++) { |
| 297 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 298 | ret = 1; |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | LinkStateRoutingTableCalculator::allocateParent() |
| 307 | { |
| 308 | m_parent = new int[numOfRouter]; |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | LinkStateRoutingTableCalculator::allocateDistance() |
| 313 | { |
| 314 | m_distance = new double[numOfRouter]; |
| 315 | } |
| 316 | |
| 317 | void |
| 318 | LinkStateRoutingTableCalculator::freeParent() |
| 319 | { |
| 320 | delete [] m_parent; |
| 321 | } |
| 322 | |
| 323 | void LinkStateRoutingTableCalculator::freeDistance() |
| 324 | { |
| 325 | delete [] m_distance; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | |
| 330 | void |
| 331 | HypRoutingTableCalculator::calculatePath(Map& pMap, |
| 332 | RoutingTable& rt, Nlsr& pnlsr) |
| 333 | { |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 334 | allocateAdjMatrix(); |
| 335 | initMatrix(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 336 | makeAdjMatrix(pnlsr, 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 | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 347 | allocateNexthopRouters(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 348 | allocateDistanceToNeighbor(); |
| 349 | allocateDistFromNbrToDest(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 350 | for (int j = 0; j < vNoLink; j++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 351 | double distToNbr = getHyperbolicDistance(pnlsr, pMap, |
| 352 | sourceRouter, links[j]); |
| 353 | double distToDestFromNbr = getHyperbolicDistance(pnlsr, |
| 354 | pMap, links[j], i); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 355 | if (distToDestFromNbr >= 0) { |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 356 | m_nexthopRouters[k] = links[j]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 357 | m_distanceToNeighbor[k] = distToNbr; |
| 358 | m_distFromNbrToDest[k] = distToDestFromNbr; |
| 359 | k++; |
| 360 | } |
| 361 | } |
| 362 | addHypNextHopsToRoutingTable(pnlsr, pMap, rt, k, i); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 363 | freeNexthopRouters(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 364 | freeDistanceToNeighbor(); |
| 365 | freeDistFromNbrToDest(); |
| 366 | } |
| 367 | } |
| 368 | freeLinks(); |
| 369 | freeLinksCosts(); |
| 370 | freeAdjMatrix(); |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | HypRoutingTableCalculator::addHypNextHopsToRoutingTable(Nlsr& pnlsr, Map& pMap, |
| 375 | RoutingTable& rt, int noFaces, int dest) |
| 376 | { |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 377 | ndn::Name destRouter = pMap.getRouterNameByMappingNo(dest); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 378 | for (int i = 0 ; i < noFaces ; ++i) { |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 379 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(m_nexthopRouters[i]); |
| 380 | std::string nextHopFaceUri = |
| 381 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
| 382 | NextHop nh(nextHopFaceUri, m_distFromNbrToDest[i]); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 383 | if (m_isDryRun) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 384 | rt.addNextHopToDryTable(destRouter, nh); |
| 385 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 386 | else { |
| 387 | rt.addNextHop(destRouter, nh); |
| 388 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | double |
| 393 | HypRoutingTableCalculator::getHyperbolicDistance(Nlsr& pnlsr, |
| 394 | Map& pMap, int src, int dest) |
| 395 | { |
| 396 | double distance = 0.0; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 397 | ndn::Name srcRouterKey = pMap.getRouterNameByMappingNo(src); |
| 398 | srcRouterKey.append("coordinate"); |
| 399 | ndn::Name destRouterKey = pMap.getRouterNameByMappingNo(dest); |
| 400 | destRouterKey.append("coordinate"); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 401 | CoordinateLsa* srcLsa = pnlsr.getLsdb().findCoordinateLsa(srcRouterKey); |
| 402 | CoordinateLsa* destLsa = pnlsr.getLsdb().findCoordinateLsa(destRouterKey); |
| 403 | |
| 404 | if ((srcLsa == 0) || (destLsa == 0)) { |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | double srcRadius = srcLsa->getCorRadius(); |
| 409 | double srcTheta = srcLsa->getCorTheta(); |
| 410 | double destRadius = destLsa->getCorRadius(); |
| 411 | double destTheta = destLsa->getCorTheta(); |
| 412 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 413 | double diffTheta = fabs(srcTheta - destTheta); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 414 | if (diffTheta > MATH_PI) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 415 | diffTheta = 2 * MATH_PI - diffTheta; |
| 416 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 417 | if (srcRadius != -1 && destRadius != -1) { |
| 418 | if (diffTheta == 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 419 | distance = fabs(srcRadius - destRadius); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 420 | } |
| 421 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 422 | distance = acosh((cosh(srcRadius) * cosh(destRadius)) - |
| 423 | (sinh(srcRadius) * sinh(destRadius) * cos(diffTheta))); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 424 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 425 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 426 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 427 | distance = -1; |
| 428 | } |
| 429 | return distance; |
| 430 | } |
| 431 | |
| 432 | void |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 433 | HypRoutingTableCalculator::allocateNexthopRouters() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 434 | { |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 435 | m_nexthopRouters = new uint32_t[vNoLink]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void |
| 439 | HypRoutingTableCalculator::allocateDistanceToNeighbor() |
| 440 | { |
| 441 | m_distanceToNeighbor = new double[vNoLink]; |
| 442 | } |
| 443 | |
| 444 | void |
| 445 | HypRoutingTableCalculator::allocateDistFromNbrToDest() |
| 446 | { |
| 447 | m_distFromNbrToDest = new double[vNoLink]; |
| 448 | } |
| 449 | |
| 450 | void |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 451 | HypRoutingTableCalculator::freeNexthopRouters() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 452 | { |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 453 | delete [] m_nexthopRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | void |
| 457 | HypRoutingTableCalculator::freeDistanceToNeighbor() |
| 458 | { |
| 459 | delete [] m_distanceToNeighbor; |
| 460 | } |
| 461 | |
| 462 | void |
| 463 | HypRoutingTableCalculator::freeDistFromNbrToDest() |
| 464 | { |
| 465 | delete [] m_distFromNbrToDest; |
| 466 | } |
| 467 | |
| 468 | }//namespace nlsr |