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 | /* |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | namespace nlsr { |
| 31 | |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 32 | class NameMap; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 33 | class RoutingTable; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 34 | |
| 35 | class RoutingTableCalculator |
| 36 | { |
| 37 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 38 | RoutingTableCalculator(size_t nRouters) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 39 | { |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 40 | m_nRouters = nRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 41 | } |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 42 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 43 | protected: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 44 | /*! \brief Allocate the space needed for the adj. matrix. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | void |
| 46 | allocateAdjMatrix(); |
| 47 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 48 | /*! \brief set NON_ADJACENT_COST i.e. -12345 to every cell of the matrix to |
| 49 | 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] | 50 | void |
| 51 | initMatrix(); |
| 52 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 53 | /*! \brief Constructs an adj. matrix to calculate with. |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 54 | \param lsdb Reference to the Lsdb |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 55 | \param pMap The map to populate with the adj. data. |
| 56 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 57 | void |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 58 | makeAdjMatrix(const Lsdb& lsdb, NameMap& pMap); |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 59 | |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 60 | /*! \brief Writes a formated adjacent matrix to DEBUG log |
| 61 | \param map The map containing adjacent matrix data |
| 62 | */ |
akmhoque | 2f42335 | 2014-06-03 11:49:35 -0500 | [diff] [blame] | 63 | void |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 64 | writeAdjMatrixLog(const NameMap& map) const; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 65 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 66 | /*! \brief Returns how many links a router in the matrix has. |
| 67 | \param sRouter The router to count the links of. |
| 68 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 69 | int |
| 70 | getNumOfLinkfromAdjMatrix(int sRouter); |
| 71 | |
| 72 | void |
| 73 | freeAdjMatrix(); |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 74 | /*! \brief Adjust a link cost in the adj. matrix |
| 75 | \param source The source router whose adjacency to adjust. |
| 76 | \param link The adjacency of the source to adjust. |
| 77 | \param linkCost The cost to change to. |
| 78 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 79 | void |
| 80 | adjustAdMatrix(int source, int link, double linkCost); |
| 81 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 82 | /*! \brief Populates temp. variables with the link costs for some router. |
| 83 | \param source The router whose values are to be adjusted. |
| 84 | \param links An integer pointer array for the link mappingNos. |
| 85 | \param linkCosts A double pointer array that stores the link costs. |
| 86 | |
| 87 | Obtains a sparse list of adjacencies and link costs for some |
| 88 | router. Since this is sparse, that means while generating these |
| 89 | arrays, if there is no adjacency at i in the matrix, these |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 90 | temporary variables will not contain a NON_ADJACENT_COST (-12345) at i, |
| 91 | but rather will contain the values for the next valid adjacency. |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 92 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 93 | void |
| 94 | getLinksFromAdjMatrix(int* links, double* linkCosts, int source); |
| 95 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 96 | /*! Allocates an array large enough to hold multipath calculation temps. */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 97 | void |
| 98 | allocateLinks(); |
| 99 | |
| 100 | void |
| 101 | allocateLinkCosts(); |
| 102 | |
| 103 | void |
| 104 | freeLinks(); |
| 105 | |
| 106 | void |
| 107 | freeLinksCosts(); |
| 108 | |
| 109 | void |
| 110 | setNoLink(int nl) |
| 111 | { |
| 112 | vNoLink = nl; |
| 113 | } |
| 114 | |
| 115 | protected: |
| 116 | double** adjMatrix; |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 117 | size_t m_nRouters; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 118 | |
| 119 | int vNoLink; |
| 120 | int* links; |
| 121 | double* linkCosts; |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 122 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | class LinkStateRoutingTableCalculator: public RoutingTableCalculator |
| 126 | { |
| 127 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 128 | LinkStateRoutingTableCalculator(size_t nRouters) |
| 129 | : RoutingTableCalculator(nRouters) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 130 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 131 | } |
| 132 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 133 | void |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 134 | calculatePath(NameMap& pMap, RoutingTable& rt, ConfParameter& confParam, |
Ashlesh Gawande | 57a8717 | 2020-05-09 19:47:06 -0700 | [diff] [blame] | 135 | const Lsdb& lsdb); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 136 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 137 | private: |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 138 | /*! \brief Performs a Dijkstra's calculation over the adjacency matrix. |
| 139 | \param sourceRouter The origin router to compute paths from. |
| 140 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 141 | void |
| 142 | doDijkstraPathCalculation(int sourceRouter); |
| 143 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 144 | /*! \brief Sort the elements of a list. |
| 145 | \param Q The array that contains the elements to sort. |
| 146 | \param dist The array that contains the distances. |
| 147 | \param start The first element in the list to sort. |
| 148 | \param element The last element in the list to sort through. |
| 149 | |
| 150 | Sorts the list based on distance. The distances are indexed by |
| 151 | their mappingNo in dist. Currently uses an insertion sort. |
dulalsaurab | d0816a3 | 2019-07-26 13:11:24 +0000 | [diff] [blame] | 152 | |
| 153 | The cost between two nodes can be zero or greater than zero. |
| 154 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 155 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 156 | void |
| 157 | sortQueueByDistance(int* Q, double* dist, int start, int element); |
| 158 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 159 | /*! \brief Returns whether an element has been visited yet. |
| 160 | \param Q The list of elements to look through. |
| 161 | \param u The element to check. |
| 162 | \param start The start of list to look through. |
| 163 | \param element The end of the list to look through. |
| 164 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 165 | int |
| 166 | isNotExplored(int* Q, int u, int start, int element); |
| 167 | |
| 168 | void |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 169 | addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies, RoutingTable& rt, |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 170 | NameMap& pMap, uint32_t sourceRouter); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 171 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 172 | /*! \brief Determines a destination's next hop. |
| 173 | \param dest The router whose next hop we want to determine. |
| 174 | \param source The router to determine a next path to. |
| 175 | */ |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 176 | int |
| 177 | getLsNextHop(int dest, int source); |
| 178 | |
| 179 | void |
| 180 | allocateParent(); |
| 181 | |
| 182 | void |
| 183 | allocateDistance(); |
| 184 | |
| 185 | void |
| 186 | freeParent(); |
| 187 | |
| 188 | void |
| 189 | freeDistance(); |
| 190 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 191 | private: |
| 192 | int* m_parent; |
| 193 | double* m_distance; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 194 | }; |
| 195 | |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 196 | class AdjacencyList; |
| 197 | class Lsdb; |
| 198 | |
| 199 | class HyperbolicRoutingCalculator |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 200 | { |
| 201 | public: |
Vince Lehman | 9a70903 | 2014-09-13 16:28:07 -0500 | [diff] [blame] | 202 | HyperbolicRoutingCalculator(size_t nRouters, bool isDryRun, ndn::Name thisRouterName) |
| 203 | : m_nRouters(nRouters) |
| 204 | , m_isDryRun(isDryRun) |
| 205 | , m_thisRouterName(thisRouterName) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 206 | { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void |
Junxiao Shi | 4eb4eae | 2024-02-14 12:36:57 +0000 | [diff] [blame] | 210 | calculatePath(NameMap& map, RoutingTable& rt, Lsdb& lsdb, AdjacencyList& adjacencies); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 211 | |
| 212 | private: |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 213 | double |
Saurab Dulal | 72b2b25 | 2019-01-22 16:58:08 -0600 | [diff] [blame] | 214 | getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, ndn::Name dest); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 215 | |
| 216 | void |
Junxiao Shi | 6593a43 | 2023-08-21 10:50:28 +0000 | [diff] [blame] | 217 | addNextHop(const ndn::Name& destinationRouter, const ndn::FaceUri& faceUri, double cost, RoutingTable& rt); |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 218 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 219 | double |
| 220 | calculateHyperbolicDistance(double rI, double rJ, double deltaTheta); |
| 221 | |
| 222 | double |
| 223 | calculateAngularDistance(std::vector<double> angleVectorI, |
| 224 | std::vector<double> angleVectorJ); |
| 225 | |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 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 | }; |
| 231 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 232 | } // namespace nlsr |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 233 | |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame] | 234 | #endif // NLSR_ROUTING_TABLE_CALCULATOR_HPP |