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