akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, The University of Memphis, |
| 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 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 | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 23 | #ifndef NLSR_ROUTING_TABLE_CALCULATOR_HPP |
| 24 | #define NLSR_ROUTING_TABLE_CALCULATOR_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | |
| 26 | #include <list> |
| 27 | #include <iostream> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 28 | #include <boost/cstdint.hpp> |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 30 | #include <ndn-cxx/name.hpp> |
| 31 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
| 34 | class Map; |
| 35 | class RoutingTable; |
| 36 | class Nlsr; |
| 37 | |
| 38 | class RoutingTableCalculator |
| 39 | { |
| 40 | public: |
| 41 | RoutingTableCalculator() |
| 42 | { |
| 43 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 44 | RoutingTableCalculator(size_t nRouters) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 46 | m_nRouters = nRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | } |
| 48 | protected: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 49 | /*! \brief Allocate the space needed for the adj. matrix. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 50 | void |
| 51 | allocateAdjMatrix(); |
| 52 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 53 | /*! \brief Zero every cell of the matrix to ensure that the memory is safe. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 54 | void |
| 55 | initMatrix(); |
| 56 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 57 | /*! \brief Constructs an adj. matrix to calculate with. |
| 58 | \param pnlsr The NLSR object that contains the LSAs that we need to iterate |
| 59 | over. |
| 60 | \param pMap The map to populate with the adj. data. |
| 61 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 62 | void |
| 63 | makeAdjMatrix(Nlsr& pnlsr, Map pMap); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 64 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 65 | void |
| 66 | writeAdjMatrixLog(); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 67 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 68 | /*! \brief Returns how many links a router in the matrix has. |
| 69 | \param sRouter The router to count the links of. |
| 70 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 71 | int |
| 72 | getNumOfLinkfromAdjMatrix(int sRouter); |
| 73 | |
| 74 | void |
| 75 | freeAdjMatrix(); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 76 | /*! \brief Adjust a link cost in the adj. matrix |
| 77 | \param source The source router whose adjacency to adjust. |
| 78 | \param link The adjacency of the source to adjust. |
| 79 | \param linkCost The cost to change to. |
| 80 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 81 | void |
| 82 | adjustAdMatrix(int source, int link, double linkCost); |
| 83 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 84 | /*! \brief Populates temp. variables with the link costs for some router. |
| 85 | \param source The router whose values are to be adjusted. |
| 86 | \param links An integer pointer array for the link mappingNos. |
| 87 | \param linkCosts A double pointer array that stores the link costs. |
| 88 | |
| 89 | Obtains a sparse list of adjacencies and link costs for some |
| 90 | router. Since this is sparse, that means while generating these |
| 91 | arrays, if there is no adjacency at i in the matrix, these |
| 92 | temporary variables will not contain a 0 at i, but rather will |
| 93 | contain the values for the next valid adjacency. |
| 94 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 95 | void |
| 96 | getLinksFromAdjMatrix(int* links, double* linkCosts, int source); |
| 97 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 98 | /*! Allocates an array large enough to hold multipath calculation temps. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 99 | void |
| 100 | allocateLinks(); |
| 101 | |
| 102 | void |
| 103 | allocateLinkCosts(); |
| 104 | |
| 105 | void |
| 106 | freeLinks(); |
| 107 | |
| 108 | void |
| 109 | freeLinksCosts(); |
| 110 | |
| 111 | void |
| 112 | setNoLink(int nl) |
| 113 | { |
| 114 | vNoLink = nl; |
| 115 | } |
| 116 | |
| 117 | protected: |
| 118 | double** adjMatrix; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 119 | size_t m_nRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 120 | |
| 121 | int vNoLink; |
| 122 | int* links; |
| 123 | double* linkCosts; |
| 124 | }; |
| 125 | |
| 126 | class LinkStateRoutingTableCalculator: public RoutingTableCalculator |
| 127 | { |
| 128 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 129 | LinkStateRoutingTableCalculator(size_t nRouters) |
| 130 | : RoutingTableCalculator(nRouters) |
| 131 | , EMPTY_PARENT(-12345) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 132 | , INF_DISTANCE(2147483647) |
| 133 | , NO_MAPPING_NUM(-1) |
| 134 | , NO_NEXT_HOP(-12345) |
| 135 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 136 | } |
| 137 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 138 | void |
| 139 | calculatePath(Map& pMap, RoutingTable& rt, Nlsr& pnlsr); |
| 140 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | private: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 142 | /*! \brief Performs a Dijkstra's calculation over the adjacency matrix. |
| 143 | \param sourceRouter The origin router to compute paths from. |
| 144 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 145 | void |
| 146 | doDijkstraPathCalculation(int sourceRouter); |
| 147 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 148 | /*! \brief Sort the elements of a list. |
| 149 | \param Q The array that contains the elements to sort. |
| 150 | \param dist The array that contains the distances. |
| 151 | \param start The first element in the list to sort. |
| 152 | \param element The last element in the list to sort through. |
| 153 | |
| 154 | Sorts the list based on distance. The distances are indexed by |
| 155 | their mappingNo in dist. Currently uses an insertion sort. |
| 156 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 157 | void |
| 158 | sortQueueByDistance(int* Q, double* dist, int start, int element); |
| 159 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 160 | /*! \brief Returns whether an element has been visited yet. |
| 161 | \param Q The list of elements to look through. |
| 162 | \param u The element to check. |
| 163 | \param start The start of list to look through. |
| 164 | \param element The end of the list to look through. |
| 165 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 166 | int |
| 167 | isNotExplored(int* Q, int u, int start, int element); |
| 168 | |
| 169 | void |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 170 | addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, RoutingTable& rt, |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 171 | Map& pMap, uint32_t sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 172 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame^] | 173 | /*! \brief Determines a destination's next hop. |
| 174 | \param dest The router whose next hop we want to determine. |
| 175 | \param source The router to determine a next path to. |
| 176 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 177 | int |
| 178 | getLsNextHop(int dest, int source); |
| 179 | |
| 180 | void |
| 181 | allocateParent(); |
| 182 | |
| 183 | void |
| 184 | allocateDistance(); |
| 185 | |
| 186 | void |
| 187 | freeParent(); |
| 188 | |
| 189 | void |
| 190 | freeDistance(); |
| 191 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 192 | private: |
| 193 | int* m_parent; |
| 194 | double* m_distance; |
| 195 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 196 | const int EMPTY_PARENT; |
| 197 | const double INF_DISTANCE; |
| 198 | const int NO_MAPPING_NUM; |
| 199 | const int NO_NEXT_HOP; |
| 200 | |
| 201 | }; |
| 202 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 203 | class AdjacencyList; |
| 204 | class Lsdb; |
| 205 | |
| 206 | class HyperbolicRoutingCalculator |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 207 | { |
| 208 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 209 | HyperbolicRoutingCalculator(size_t nRouters, bool isDryRun, ndn::Name thisRouterName) |
| 210 | : m_nRouters(nRouters) |
| 211 | , m_isDryRun(isDryRun) |
| 212 | , m_thisRouterName(thisRouterName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 213 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 217 | calculatePaths(Map& map, RoutingTable& rt, Lsdb& lsdb, AdjacencyList& adjacencies); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | |
| 219 | private: |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 220 | double |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 221 | getHyperbolicDistance(Map& map, Lsdb& lsdb, ndn::Name src, ndn::Name dest); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 222 | |
| 223 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 224 | addNextHop(ndn::Name destinationRouter, std::string faceUri, double cost, RoutingTable& rt); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 225 | |
| 226 | private: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 227 | const size_t m_nRouters; |
| 228 | const bool m_isDryRun; |
| 229 | const ndn::Name m_thisRouterName; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 230 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 231 | static const double MATH_PI; |
| 232 | static const double UNKNOWN_DISTANCE; |
| 233 | static const double UNKNOWN_RADIUS; |
| 234 | static const int32_t ROUTER_NOT_FOUND; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | }; |
| 236 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 237 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 238 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 239 | #endif //NLSR_ROUTING_TABLE_CALCULATOR_HPP |