akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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/>. |
| 19 | * |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 21 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 22 | #ifndef NLSR_ROUTING_TABLE_CALCULATOR_HPP |
| 23 | #define NLSR_ROUTING_TABLE_CALCULATOR_HPP |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 24 | |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 25 | #include "common.hpp" |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 26 | #include "lsa.hpp" |
| 27 | #include "conf-parameter.hpp" |
Nick Gordon | e40377d | 2017-08-11 15:10:02 -0500 | [diff] [blame] | 28 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 29 | #include <list> |
| 30 | #include <iostream> |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 31 | #include <boost/cstdint.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 <ndn-cxx/name.hpp> |
| 34 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 35 | namespace nlsr { |
| 36 | |
| 37 | class Map; |
| 38 | class RoutingTable; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | |
| 40 | class RoutingTableCalculator |
| 41 | { |
| 42 | public: |
| 43 | RoutingTableCalculator() |
| 44 | { |
| 45 | } |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 46 | RoutingTableCalculator(size_t nRouters) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 47 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 48 | m_nRouters = nRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 49 | } |
| 50 | protected: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 51 | /*! \brief Allocate the space needed for the adj. matrix. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 52 | void |
| 53 | allocateAdjMatrix(); |
| 54 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 55 | /*! \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] | 56 | void |
| 57 | initMatrix(); |
| 58 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 59 | /*! \brief Constructs an adj. matrix to calculate with. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 60 | \param adjLsaList The Adjacency Lsa list. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 61 | \param pMap The map to populate with the adj. data. |
| 62 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 63 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 64 | makeAdjMatrix(const std::list<AdjLsa>& adjLsaList, Map& pMap); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 65 | |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 66 | void |
Saurab Dulal | 9da6aa7 | 2018-01-12 17:25:51 +0000 | [diff] [blame] | 67 | writeAdjMatrixLog(const Map& map) const; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 68 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 69 | /*! \brief Returns how many links a router in the matrix has. |
| 70 | \param sRouter The router to count the links of. |
| 71 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 72 | int |
| 73 | getNumOfLinkfromAdjMatrix(int sRouter); |
| 74 | |
| 75 | void |
| 76 | freeAdjMatrix(); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 77 | /*! \brief Adjust a link cost in the adj. matrix |
| 78 | \param source The source router whose adjacency to adjust. |
| 79 | \param link The adjacency of the source to adjust. |
| 80 | \param linkCost The cost to change to. |
| 81 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 82 | void |
| 83 | adjustAdMatrix(int source, int link, double linkCost); |
| 84 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 85 | /*! \brief Populates temp. variables with the link costs for some router. |
| 86 | \param source The router whose values are to be adjusted. |
| 87 | \param links An integer pointer array for the link mappingNos. |
| 88 | \param linkCosts A double pointer array that stores the link costs. |
| 89 | |
| 90 | Obtains a sparse list of adjacencies and link costs for some |
| 91 | router. Since this is sparse, that means while generating these |
| 92 | arrays, if there is no adjacency at i in the matrix, these |
| 93 | temporary variables will not contain a 0 at i, but rather will |
| 94 | contain the values for the next valid adjacency. |
| 95 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 96 | void |
| 97 | getLinksFromAdjMatrix(int* links, double* linkCosts, int source); |
| 98 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 99 | /*! Allocates an array large enough to hold multipath calculation temps. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 100 | void |
| 101 | allocateLinks(); |
| 102 | |
| 103 | void |
| 104 | allocateLinkCosts(); |
| 105 | |
| 106 | void |
| 107 | freeLinks(); |
| 108 | |
| 109 | void |
| 110 | freeLinksCosts(); |
| 111 | |
| 112 | void |
| 113 | setNoLink(int nl) |
| 114 | { |
| 115 | vNoLink = nl; |
| 116 | } |
| 117 | |
| 118 | protected: |
| 119 | double** adjMatrix; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 120 | size_t m_nRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 121 | |
| 122 | int vNoLink; |
| 123 | int* links; |
| 124 | double* linkCosts; |
| 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) |
| 132 | , EMPTY_PARENT(-12345) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | , INF_DISTANCE(2147483647) |
| 134 | , NO_MAPPING_NUM(-1) |
| 135 | , NO_NEXT_HOP(-12345) |
| 136 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | } |
| 138 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 139 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 140 | calculatePath(Map& pMap, RoutingTable& rt, ConfParameter& confParam, |
| 141 | const std::list<AdjLsa>& adjLsaList); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 142 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 143 | private: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 144 | /*! \brief Performs a Dijkstra's calculation over the adjacency matrix. |
| 145 | \param sourceRouter The origin router to compute paths from. |
| 146 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 147 | void |
| 148 | doDijkstraPathCalculation(int sourceRouter); |
| 149 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 150 | /*! \brief Sort the elements of a list. |
| 151 | \param Q The array that contains the elements to sort. |
| 152 | \param dist The array that contains the distances. |
| 153 | \param start The first element in the list to sort. |
| 154 | \param element The last element in the list to sort through. |
| 155 | |
| 156 | Sorts the list based on distance. The distances are indexed by |
| 157 | their mappingNo in dist. Currently uses an insertion sort. |
| 158 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 159 | void |
| 160 | sortQueueByDistance(int* Q, double* dist, int start, int element); |
| 161 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 162 | /*! \brief Returns whether an element has been visited yet. |
| 163 | \param Q The list of elements to look through. |
| 164 | \param u The element to check. |
| 165 | \param start The start of list to look through. |
| 166 | \param element The end of the list to look through. |
| 167 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 168 | int |
| 169 | isNotExplored(int* Q, int u, int start, int element); |
| 170 | |
| 171 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 172 | addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies, RoutingTable& rt, |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 173 | Map& pMap, uint32_t sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 174 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 175 | /*! \brief Determines a destination's next hop. |
| 176 | \param dest The router whose next hop we want to determine. |
| 177 | \param source The router to determine a next path to. |
| 178 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 179 | int |
| 180 | getLsNextHop(int dest, int source); |
| 181 | |
| 182 | void |
| 183 | allocateParent(); |
| 184 | |
| 185 | void |
| 186 | allocateDistance(); |
| 187 | |
| 188 | void |
| 189 | freeParent(); |
| 190 | |
| 191 | void |
| 192 | freeDistance(); |
| 193 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 194 | private: |
| 195 | int* m_parent; |
| 196 | double* m_distance; |
| 197 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 198 | const int EMPTY_PARENT; |
| 199 | const double INF_DISTANCE; |
| 200 | const int NO_MAPPING_NUM; |
| 201 | const int NO_NEXT_HOP; |
| 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 |