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 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 33 | #include <boost/math/constants/constants.hpp> |
| 34 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | namespace nlsr { |
| 36 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 37 | INIT_LOGGER("RoutingTableCalculator"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 38 | using namespace std; |
| 39 | |
| 40 | void |
| 41 | RoutingTableCalculator::allocateAdjMatrix() |
| 42 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 43 | adjMatrix = new double*[m_nRouters]; |
| 44 | |
| 45 | for (size_t i = 0; i < m_nRouters; ++i) { |
| 46 | adjMatrix[i] = new double[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | RoutingTableCalculator::initMatrix() |
| 52 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 53 | for (size_t i = 0; i < m_nRouters; i++) { |
| 54 | for (size_t j = 0; j < m_nRouters; j++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 55 | adjMatrix[i][j] = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 56 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | RoutingTableCalculator::makeAdjMatrix(Nlsr& pnlsr, Map pMap) |
| 62 | { |
| 63 | std::list<AdjLsa> adjLsdb = pnlsr.getLsdb().getAdjLsdb(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 64 | for (std::list<AdjLsa>::iterator it = adjLsdb.begin(); it != adjLsdb.end() ; it++) { |
| 65 | |
| 66 | int32_t row = pMap.getMappingNoByRouterName((*it).getOrigRouter()); |
| 67 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | std::list<Adjacent> adl = (*it).getAdl().getAdjList(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 69 | for (std::list<Adjacent>::iterator itAdl = adl.begin(); itAdl != adl.end() ; itAdl++) { |
| 70 | |
| 71 | int32_t col = pMap.getMappingNoByRouterName((*itAdl).getName()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | double cost = (*itAdl).getLinkCost(); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 73 | |
| 74 | if ((row >= 0 && row < static_cast<int32_t>(m_nRouters)) && |
| 75 | (col >= 0 && col < static_cast<int32_t>(m_nRouters))) |
| 76 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 77 | adjMatrix[row][col] = cost; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 84 | RoutingTableCalculator::writeAdjMatrixLog() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 85 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 86 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 87 | string line=""; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 88 | for (size_t j = 0; j < m_nRouters; j++) { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 89 | line += boost::lexical_cast<std::string>(adjMatrix[i][j]); |
| 90 | line += " "; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 91 | } |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 92 | _LOG_DEBUG(line); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | void |
| 97 | RoutingTableCalculator::adjustAdMatrix(int source, int link, double linkCost) |
| 98 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 99 | for (int i = 0; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 100 | if (i == link) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 101 | adjMatrix[source][i] = linkCost; |
| 102 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 103 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 104 | adjMatrix[source][i] = 0; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | int |
| 110 | RoutingTableCalculator::getNumOfLinkfromAdjMatrix(int sRouter) |
| 111 | { |
| 112 | int noLink = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 113 | |
| 114 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 115 | if (adjMatrix[sRouter][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 116 | noLink++; |
| 117 | } |
| 118 | } |
| 119 | return noLink; |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | RoutingTableCalculator::getLinksFromAdjMatrix(int* links, |
| 124 | double* linkCosts, int source) |
| 125 | { |
| 126 | int j = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 127 | |
| 128 | for (size_t i = 0; i < m_nRouters; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 129 | if (adjMatrix[source][i] > 0) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | links[j] = i; |
| 131 | linkCosts[j] = adjMatrix[source][i]; |
| 132 | j++; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | RoutingTableCalculator::freeAdjMatrix() |
| 139 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 140 | for (size_t i = 0; i < m_nRouters; ++i) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | delete [] adjMatrix[i]; |
| 142 | } |
| 143 | delete [] adjMatrix; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void |
| 148 | RoutingTableCalculator::allocateLinks() |
| 149 | { |
| 150 | links = new int[vNoLink]; |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | RoutingTableCalculator::allocateLinkCosts() |
| 155 | { |
| 156 | linkCosts = new double[vNoLink]; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void |
| 161 | RoutingTableCalculator::freeLinks() |
| 162 | { |
| 163 | delete [] links; |
| 164 | } |
| 165 | void |
| 166 | RoutingTableCalculator::freeLinksCosts() |
| 167 | { |
| 168 | delete [] linkCosts; |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | LinkStateRoutingTableCalculator::calculatePath(Map& pMap, |
| 173 | RoutingTable& rt, Nlsr& pnlsr) |
| 174 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 175 | _LOG_DEBUG("LinkStateRoutingTableCalculator::calculatePath Called"); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 176 | allocateAdjMatrix(); |
| 177 | initMatrix(); |
| 178 | makeAdjMatrix(pnlsr, pMap); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 179 | writeAdjMatrixLog(); |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 180 | int sourceRouter = pMap.getMappingNoByRouterName(pnlsr.getConfParameter().getRouterPrefix()); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 181 | allocateParent(); |
| 182 | allocateDistance(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 183 | if (pnlsr.getConfParameter().getMaxFacesPerPrefix() == 1) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 184 | // Single Path |
| 185 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 186 | // update routing table |
| 187 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 188 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 189 | else { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 190 | // Multi Path |
| 191 | setNoLink(getNumOfLinkfromAdjMatrix(sourceRouter)); |
| 192 | allocateLinks(); |
| 193 | allocateLinkCosts(); |
| 194 | getLinksFromAdjMatrix(links, linkCosts, sourceRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 195 | for (int i = 0 ; i < vNoLink; i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 196 | adjustAdMatrix(sourceRouter, links[i], linkCosts[i]); |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 197 | writeAdjMatrixLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 198 | doDijkstraPathCalculation(sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 199 | //update routing table |
| 200 | addAllLsNextHopsToRoutingTable(pnlsr, rt, pMap, sourceRouter); |
| 201 | } |
| 202 | freeLinks(); |
| 203 | freeLinksCosts(); |
| 204 | } |
| 205 | freeParent(); |
| 206 | freeDistance(); |
| 207 | freeAdjMatrix(); |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | LinkStateRoutingTableCalculator::doDijkstraPathCalculation(int sourceRouter) |
| 212 | { |
| 213 | int i; |
| 214 | int v, u; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 215 | int* Q = new int[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | int head = 0; |
| 217 | /* Initiate the Parent */ |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 218 | for (i = 0 ; i < static_cast<int>(m_nRouters); i++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 219 | m_parent[i] = EMPTY_PARENT; |
| 220 | m_distance[i] = INF_DISTANCE; |
| 221 | Q[i] = i; |
| 222 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 223 | if (sourceRouter != NO_MAPPING_NUM) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | m_distance[sourceRouter] = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 225 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
| 226 | while (head < static_cast<int>(m_nRouters)) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | u = Q[head]; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 228 | if (m_distance[u] == INF_DISTANCE) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 229 | break; |
| 230 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 231 | for (v = 0 ; v < static_cast<int>(m_nRouters); v++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 232 | if (adjMatrix[u][v] > 0) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 233 | if (isNotExplored(Q, v, head + 1, m_nRouters)) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 234 | if (m_distance[u] + adjMatrix[u][v] < m_distance[v]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | m_distance[v] = m_distance[u] + adjMatrix[u][v] ; |
| 236 | m_parent[v] = u; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | head++; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 242 | sortQueueByDistance(Q, m_distance, head, m_nRouters); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | delete [] Q; |
| 246 | } |
| 247 | |
| 248 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 249 | LinkStateRoutingTableCalculator::addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, RoutingTable& rt, |
| 250 | Map& pMap, uint32_t sourceRouter) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 251 | { |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 252 | _LOG_DEBUG("LinkStateRoutingTableCalculator::addAllNextHopsToRoutingTable Called"); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 253 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 254 | int nextHopRouter = 0; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 255 | |
| 256 | for (size_t i = 0; i < m_nRouters ; i++) { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 257 | if (i != sourceRouter) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 258 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 259 | nextHopRouter = getLsNextHop(i, sourceRouter); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 260 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 261 | if (nextHopRouter != NO_NEXT_HOP) { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 262 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 263 | double routeCost = m_distance[i]; |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 264 | ndn::Name nextHopRouterName = pMap.getRouterNameByMappingNo(nextHopRouter); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 265 | std::string nextHopFace = |
| 266 | pnlsr.getAdjacencyList().getAdjacent(nextHopRouterName).getConnectingFaceUri(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 267 | // Add next hop to routing table |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 268 | NextHop nh(nextHopFace, routeCost); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 269 | rt.addNextHop(pMap.getRouterNameByMappingNo(i), nh); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | int |
| 276 | LinkStateRoutingTableCalculator::getLsNextHop(int dest, int source) |
| 277 | { |
| 278 | int nextHop = NO_NEXT_HOP; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 279 | while (m_parent[dest] != EMPTY_PARENT) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 280 | nextHop = dest; |
| 281 | dest = m_parent[dest]; |
| 282 | } |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 283 | if (dest != source) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 284 | nextHop = NO_NEXT_HOP; |
| 285 | } |
| 286 | return nextHop; |
| 287 | } |
| 288 | |
| 289 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 290 | LinkStateRoutingTableCalculator::sortQueueByDistance(int* Q, |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 291 | double* dist, |
| 292 | int start, int element) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 293 | { |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 294 | for (int i = start ; i < element ; i++) { |
| 295 | for (int j = i + 1; j < element; j++) { |
| 296 | if (dist[Q[j]] < dist[Q[i]]) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 297 | int tempU = Q[j]; |
| 298 | Q[j] = Q[i]; |
| 299 | Q[i] = tempU; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | int |
| 306 | LinkStateRoutingTableCalculator::isNotExplored(int* Q, |
| 307 | int u, int start, int element) |
| 308 | { |
| 309 | int ret = 0; |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 310 | for (int i = start; i < element; i++) { |
| 311 | if (Q[i] == u) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 312 | ret = 1; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | LinkStateRoutingTableCalculator::allocateParent() |
| 321 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 322 | m_parent = new int[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void |
| 326 | LinkStateRoutingTableCalculator::allocateDistance() |
| 327 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 328 | m_distance = new double[m_nRouters]; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void |
| 332 | LinkStateRoutingTableCalculator::freeParent() |
| 333 | { |
| 334 | delete [] m_parent; |
| 335 | } |
| 336 | |
| 337 | void LinkStateRoutingTableCalculator::freeDistance() |
| 338 | { |
| 339 | delete [] m_distance; |
| 340 | } |
| 341 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 342 | const double HyperbolicRoutingCalculator::MATH_PI = boost::math::constants::pi<double>(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 343 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 344 | const double HyperbolicRoutingCalculator::UNKNOWN_DISTANCE = -1.0; |
| 345 | const double HyperbolicRoutingCalculator::UNKNOWN_RADIUS = -1.0; |
| 346 | |
| 347 | const int32_t HyperbolicRoutingCalculator::ROUTER_NOT_FOUND = -1.0; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 348 | |
| 349 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 350 | HyperbolicRoutingCalculator::calculatePaths(Map& map, RoutingTable& rt, |
| 351 | Lsdb& lsdb, AdjacencyList& adjacencies) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 352 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 353 | _LOG_TRACE("Calculating hyperbolic paths"); |
| 354 | |
| 355 | int thisRouter = map.getMappingNoByRouterName(m_thisRouterName); |
| 356 | |
| 357 | // Iterate over directly connected neighbors |
| 358 | std::list<Adjacent> neighbors = adjacencies.getAdjList(); |
| 359 | for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) { |
| 360 | |
| 361 | // Don't calculate nexthops using an inactive router |
| 362 | if (adj->getStatus() == Adjacent::STATUS_INACTIVE) { |
| 363 | _LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop"); |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | ndn::Name srcRouterName = adj->getName(); |
| 368 | |
| 369 | // Don't calculate nexthops for this router to other routers |
| 370 | if (srcRouterName == m_thisRouterName) { |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | std::string srcFaceUri = adj->getConnectingFaceUri(); |
| 375 | |
| 376 | // Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link |
| 377 | addNextHop(srcRouterName, srcFaceUri, 0, rt); |
| 378 | |
| 379 | int src = map.getMappingNoByRouterName(srcRouterName); |
| 380 | |
| 381 | if (src == ROUTER_NOT_FOUND) { |
| 382 | _LOG_WARN(adj->getName() << " does not exist in the router map!"); |
| 383 | continue; |
| 384 | } |
| 385 | |
| 386 | // Get hyperbolic distance from direct neighbor to every other router |
| 387 | for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) { |
| 388 | // Don't calculate nexthops to this router or from a router to itself |
| 389 | if (dest != thisRouter && dest != src) { |
| 390 | |
| 391 | ndn::Name destRouterName = map.getRouterNameByMappingNo(dest); |
| 392 | |
| 393 | double distance = getHyperbolicDistance(map, lsdb, srcRouterName, destRouterName); |
| 394 | |
| 395 | // Could not compute distance |
| 396 | if (distance == UNKNOWN_DISTANCE) { |
| 397 | _LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName << " to " << |
| 398 | destRouterName); |
| 399 | continue; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 400 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 401 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 402 | addNextHop(destRouterName, srcFaceUri, distance, rt); |
| 403 | } |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 404 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | double |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 409 | HyperbolicRoutingCalculator::getHyperbolicDistance(Map& map, Lsdb& lsdb, |
| 410 | ndn::Name src, ndn::Name dest) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 411 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 412 | _LOG_TRACE("Calculating hyperbolic distance from " << src << " to " << dest); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 413 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 414 | double distance = UNKNOWN_DISTANCE; |
| 415 | |
| 416 | ndn::Name srcLsaKey = src; |
| 417 | srcLsaKey.append("coordinate"); |
| 418 | |
| 419 | CoordinateLsa* srcLsa = lsdb.findCoordinateLsa(srcLsaKey); |
| 420 | |
| 421 | ndn::Name destLsaKey = dest; |
| 422 | destLsaKey.append("coordinate"); |
| 423 | |
| 424 | CoordinateLsa* destLsa = lsdb.findCoordinateLsa(destLsaKey); |
| 425 | |
| 426 | // Coordinate LSAs do not exist for these routers |
| 427 | if (srcLsa == NULL || destLsa == NULL) { |
| 428 | return UNKNOWN_DISTANCE; |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 429 | } |
| 430 | |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 431 | double srcTheta = srcLsa->getCorTheta(); |
akmhoque | dcee936 | 2014-08-05 22:58:01 -0500 | [diff] [blame] | 432 | double destTheta = destLsa->getCorTheta(); |
| 433 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 434 | double diffTheta = fabs(srcTheta - destTheta); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 435 | |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 436 | if (diffTheta > MATH_PI) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 437 | diffTheta = 2 * MATH_PI - diffTheta; |
| 438 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 439 | |
| 440 | double srcRadius = srcLsa->getCorRadius(); |
| 441 | double destRadius = destLsa->getCorRadius(); |
| 442 | |
| 443 | if (srcRadius == UNKNOWN_RADIUS && destRadius == UNKNOWN_RADIUS) { |
| 444 | return UNKNOWN_DISTANCE; |
| 445 | } |
| 446 | |
| 447 | if (diffTheta == 0) { |
| 448 | distance = fabs(srcRadius - destRadius); |
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 { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 451 | distance = acosh((cosh(srcRadius) * cosh(destRadius)) - |
| 452 | (sinh(srcRadius) * sinh(destRadius) * cos(diffTheta))); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 453 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 454 | |
| 455 | _LOG_TRACE("Distance from " << src << " to " << dest << " is " << distance); |
| 456 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 457 | return distance; |
| 458 | } |
| 459 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 460 | void HyperbolicRoutingCalculator::addNextHop(ndn::Name dest, std::string faceUri, |
| 461 | double cost, RoutingTable& rt) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 462 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 463 | NextHop hop(faceUri, cost); |
Vince Lehman | 20fe4a9 | 2014-09-09 15:57:59 -0500 | [diff] [blame^] | 464 | hop.setHyperbolic(true); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 465 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 466 | _LOG_TRACE("Calculated " << hop << " for destination: " << dest); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 467 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 468 | if (m_isDryRun) { |
| 469 | rt.addNextHopToDryTable(dest, hop); |
| 470 | } |
| 471 | else { |
| 472 | rt.addNextHop(dest, hop); |
| 473 | } |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | }//namespace nlsr |