akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 2 | /* |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 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/>. |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 19 | */ |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 20 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 21 | #ifndef NLSR_ROUTING_TABLE_CALCULATOR_HPP |
| 22 | #define NLSR_ROUTING_TABLE_CALCULATOR_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 23 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 24 | #include "common.hpp" |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 25 | #include "lsa/lsa.hpp" |
| 26 | #include "lsa/adj-lsa.hpp" |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 27 | #include "lsdb.hpp" |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 28 | #include "conf-parameter.hpp" |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 29 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 30 | #include <list> |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 31 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 32 | namespace nlsr { |
| 33 | |
| 34 | class Map; |
| 35 | class RoutingTable; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 36 | |
| 37 | class RoutingTableCalculator |
| 38 | { |
| 39 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 40 | RoutingTableCalculator(size_t nRouters) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 42 | m_nRouters = nRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | } |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 44 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | protected: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 46 | /*! \brief Allocate the space needed for the adj. matrix. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | void |
| 48 | allocateAdjMatrix(); |
| 49 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 50 | /*! \brief set NON_ADJACENT_COST i.e. -12345 to every cell of the matrix to |
| 51 | ensure that the memory is safe. This is also to incorporate zero cost links */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | void |
| 53 | initMatrix(); |
| 54 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 55 | /*! \brief Constructs an adj. matrix to calculate with. |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 56 | \param lsdb Reference to the Lsdb |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 57 | \param pMap The map to populate with the adj. data. |
| 58 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 59 | void |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 60 | makeAdjMatrix(const Lsdb& lsdb, Map& pMap); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 61 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 62 | /*! \brief Writes a formated adjacent matrix to DEBUG log |
| 63 | \param map The map containing adjacent matrix data |
| 64 | */ |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 65 | void |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 66 | writeAdjMatrixLog(const Map& map) const; |
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 |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 92 | temporary variables will not contain a NON_ADJACENT_COST (-12345) at i, |
| 93 | but rather will contain the values for the next valid adjacency. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 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; |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 124 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | class LinkStateRoutingTableCalculator: public RoutingTableCalculator |
| 128 | { |
| 129 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 130 | LinkStateRoutingTableCalculator(size_t nRouters) |
| 131 | : RoutingTableCalculator(nRouters) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 132 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | } |
| 134 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 135 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 136 | calculatePath(Map& pMap, RoutingTable& rt, ConfParameter& confParam, |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 137 | const Lsdb& lsdb); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 138 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | private: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 140 | /*! \brief Performs a Dijkstra's calculation over the adjacency matrix. |
| 141 | \param sourceRouter The origin router to compute paths from. |
| 142 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | void |
| 144 | doDijkstraPathCalculation(int sourceRouter); |
| 145 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 146 | /*! \brief Sort the elements of a list. |
| 147 | \param Q The array that contains the elements to sort. |
| 148 | \param dist The array that contains the distances. |
| 149 | \param start The first element in the list to sort. |
| 150 | \param element The last element in the list to sort through. |
| 151 | |
| 152 | Sorts the list based on distance. The distances are indexed by |
| 153 | their mappingNo in dist. Currently uses an insertion sort. |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 154 | |
| 155 | The cost between two nodes can be zero or greater than zero. |
| 156 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 157 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 158 | void |
| 159 | sortQueueByDistance(int* Q, double* dist, int start, int element); |
| 160 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 161 | /*! \brief Returns whether an element has been visited yet. |
| 162 | \param Q The list of elements to look through. |
| 163 | \param u The element to check. |
| 164 | \param start The start of list to look through. |
| 165 | \param element The end of the list to look through. |
| 166 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 167 | int |
| 168 | isNotExplored(int* Q, int u, int start, int element); |
| 169 | |
| 170 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 171 | addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies, RoutingTable& rt, |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 172 | Map& pMap, uint32_t sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 173 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 174 | /*! \brief Determines a destination's next hop. |
| 175 | \param dest The router whose next hop we want to determine. |
| 176 | \param source The router to determine a next path to. |
| 177 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 178 | int |
| 179 | getLsNextHop(int dest, int source); |
| 180 | |
| 181 | void |
| 182 | allocateParent(); |
| 183 | |
| 184 | void |
| 185 | allocateDistance(); |
| 186 | |
| 187 | void |
| 188 | freeParent(); |
| 189 | |
| 190 | void |
| 191 | freeDistance(); |
| 192 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 193 | private: |
| 194 | int* m_parent; |
| 195 | double* m_distance; |
| 196 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 197 | static const int EMPTY_PARENT; |
| 198 | static const double INF_DISTANCE; |
| 199 | static const int NO_MAPPING_NUM; |
Ashlesh Gawande | 6b388fc | 2019-09-30 10:14:41 -0500 | [diff] [blame] | 200 | public: |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 201 | static const int NO_NEXT_HOP; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 202 | |
| 203 | }; |
| 204 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 205 | class AdjacencyList; |
| 206 | class Lsdb; |
| 207 | |
| 208 | class HyperbolicRoutingCalculator |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 209 | { |
| 210 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 211 | HyperbolicRoutingCalculator(size_t nRouters, bool isDryRun, ndn::Name thisRouterName) |
| 212 | : m_nRouters(nRouters) |
| 213 | , m_isDryRun(isDryRun) |
| 214 | , m_thisRouterName(thisRouterName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 215 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | void |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 219 | calculatePath(Map& map, RoutingTable& rt, Lsdb& lsdb, AdjacencyList& adjacencies); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 220 | |
| 221 | private: |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 222 | double |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 223 | getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, ndn::Name dest); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 224 | |
| 225 | void |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 226 | addNextHop(ndn::Name destinationRouter, std::string faceUri, double cost, RoutingTable& rt); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 227 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 228 | double |
| 229 | calculateHyperbolicDistance(double rI, double rJ, double deltaTheta); |
| 230 | |
| 231 | double |
| 232 | calculateAngularDistance(std::vector<double> angleVectorI, |
| 233 | std::vector<double> angleVectorJ); |
| 234 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 235 | private: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 236 | const size_t m_nRouters; |
| 237 | const bool m_isDryRun; |
| 238 | const ndn::Name m_thisRouterName; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 239 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 240 | static const double MATH_PI; |
| 241 | static const double UNKNOWN_DISTANCE; |
| 242 | static const double UNKNOWN_RADIUS; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 243 | }; |
| 244 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 245 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 246 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 247 | #endif // NLSR_ROUTING_TABLE_CALCULATOR_HPP |