blob: 9c0c6b56c31c09c072ddba9f196b100766ad1e31 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande57a87172020-05-09 19:47:06 -07002/*
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08003 * Copyright (c) 2014-2020, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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 Gawande57a87172020-05-09 19:47:06 -070019 */
Nick Gordone40377d2017-08-11 15:10:02 -050020
akmhoquefdbddb12014-05-02 18:35:19 -050021#ifndef NLSR_ROUTING_TABLE_CALCULATOR_HPP
22#define NLSR_ROUTING_TABLE_CALCULATOR_HPP
akmhoque53353462014-04-22 08:43:45 -050023
Nick Gordone40377d2017-08-11 15:10:02 -050024#include "common.hpp"
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -080025#include "lsa/lsa.hpp"
26#include "lsa/adj-lsa.hpp"
Ashlesh Gawande57a87172020-05-09 19:47:06 -070027#include "lsdb.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060028#include "conf-parameter.hpp"
Nick Gordone40377d2017-08-11 15:10:02 -050029
akmhoque53353462014-04-22 08:43:45 -050030#include <list>
akmhoquefdbddb12014-05-02 18:35:19 -050031#include <boost/cstdint.hpp>
akmhoque53353462014-04-22 08:43:45 -050032
Vince Lehman9a709032014-09-13 16:28:07 -050033#include <ndn-cxx/name.hpp>
34
akmhoque53353462014-04-22 08:43:45 -050035namespace nlsr {
36
37class Map;
38class RoutingTable;
akmhoque53353462014-04-22 08:43:45 -050039
40class RoutingTableCalculator
41{
42public:
Vince Lehman9a709032014-09-13 16:28:07 -050043 RoutingTableCalculator(size_t nRouters)
akmhoque53353462014-04-22 08:43:45 -050044 {
Vince Lehman9a709032014-09-13 16:28:07 -050045 m_nRouters = nRouters;
akmhoque53353462014-04-22 08:43:45 -050046 }
dulalsaurabd0816a32019-07-26 13:11:24 +000047
akmhoque53353462014-04-22 08:43:45 -050048protected:
Nick G97e34942016-07-11 14:46:27 -050049 /*! \brief Allocate the space needed for the adj. matrix. */
akmhoque53353462014-04-22 08:43:45 -050050 void
51 allocateAdjMatrix();
52
dulalsaurabd0816a32019-07-26 13:11:24 +000053 /*! \brief set NON_ADJACENT_COST i.e. -12345 to every cell of the matrix to
54 ensure that the memory is safe. This is also to incorporate zero cost links */
akmhoque53353462014-04-22 08:43:45 -050055 void
56 initMatrix();
57
Nick G97e34942016-07-11 14:46:27 -050058 /*! \brief Constructs an adj. matrix to calculate with.
Ashlesh Gawande57a87172020-05-09 19:47:06 -070059 \param lsdb Reference to the Lsdb
Nick G97e34942016-07-11 14:46:27 -050060 \param pMap The map to populate with the adj. data.
61 */
akmhoque53353462014-04-22 08:43:45 -050062 void
Ashlesh Gawande57a87172020-05-09 19:47:06 -070063 makeAdjMatrix(const Lsdb& lsdb, Map& pMap);
Vince Lehman9a709032014-09-13 16:28:07 -050064
dulalsaurabd0816a32019-07-26 13:11:24 +000065 /*! \brief Writes a formated adjacent matrix to DEBUG log
66 \param map The map containing adjacent matrix data
67 */
akmhoque2f423352014-06-03 11:49:35 -050068 void
Saurab Dulal9da6aa72018-01-12 17:25:51 +000069 writeAdjMatrixLog(const Map& map) const;
akmhoque53353462014-04-22 08:43:45 -050070
Nick G97e34942016-07-11 14:46:27 -050071 /*! \brief Returns how many links a router in the matrix has.
72 \param sRouter The router to count the links of.
73 */
akmhoque53353462014-04-22 08:43:45 -050074 int
75 getNumOfLinkfromAdjMatrix(int sRouter);
76
77 void
78 freeAdjMatrix();
Nick G97e34942016-07-11 14:46:27 -050079 /*! \brief Adjust a link cost in the adj. matrix
80 \param source The source router whose adjacency to adjust.
81 \param link The adjacency of the source to adjust.
82 \param linkCost The cost to change to.
83 */
akmhoque53353462014-04-22 08:43:45 -050084 void
85 adjustAdMatrix(int source, int link, double linkCost);
86
Nick G97e34942016-07-11 14:46:27 -050087 /*! \brief Populates temp. variables with the link costs for some router.
88 \param source The router whose values are to be adjusted.
89 \param links An integer pointer array for the link mappingNos.
90 \param linkCosts A double pointer array that stores the link costs.
91
92 Obtains a sparse list of adjacencies and link costs for some
93 router. Since this is sparse, that means while generating these
94 arrays, if there is no adjacency at i in the matrix, these
dulalsaurabd0816a32019-07-26 13:11:24 +000095 temporary variables will not contain a NON_ADJACENT_COST (-12345) at i,
96 but rather will contain the values for the next valid adjacency.
Nick G97e34942016-07-11 14:46:27 -050097 */
akmhoque53353462014-04-22 08:43:45 -050098 void
99 getLinksFromAdjMatrix(int* links, double* linkCosts, int source);
100
Nick G97e34942016-07-11 14:46:27 -0500101 /*! Allocates an array large enough to hold multipath calculation temps. */
akmhoque53353462014-04-22 08:43:45 -0500102 void
103 allocateLinks();
104
105 void
106 allocateLinkCosts();
107
108 void
109 freeLinks();
110
111 void
112 freeLinksCosts();
113
114 void
115 setNoLink(int nl)
116 {
117 vNoLink = nl;
118 }
119
120protected:
121 double** adjMatrix;
Vince Lehman9a709032014-09-13 16:28:07 -0500122 size_t m_nRouters;
akmhoque53353462014-04-22 08:43:45 -0500123
124 int vNoLink;
125 int* links;
126 double* linkCosts;
dulalsaurabd0816a32019-07-26 13:11:24 +0000127
akmhoque53353462014-04-22 08:43:45 -0500128};
129
130class LinkStateRoutingTableCalculator: public RoutingTableCalculator
131{
132public:
Vince Lehman9a709032014-09-13 16:28:07 -0500133 LinkStateRoutingTableCalculator(size_t nRouters)
134 : RoutingTableCalculator(nRouters)
akmhoque53353462014-04-22 08:43:45 -0500135 {
akmhoque53353462014-04-22 08:43:45 -0500136 }
137
akmhoque53353462014-04-22 08:43:45 -0500138 void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600139 calculatePath(Map& pMap, RoutingTable& rt, ConfParameter& confParam,
Ashlesh Gawande57a87172020-05-09 19:47:06 -0700140 const Lsdb& lsdb);
akmhoque53353462014-04-22 08:43:45 -0500141
akmhoque53353462014-04-22 08:43:45 -0500142private:
Nick G97e34942016-07-11 14:46:27 -0500143 /*! \brief Performs a Dijkstra's calculation over the adjacency matrix.
144 \param sourceRouter The origin router to compute paths from.
145 */
akmhoque53353462014-04-22 08:43:45 -0500146 void
147 doDijkstraPathCalculation(int sourceRouter);
148
Nick G97e34942016-07-11 14:46:27 -0500149 /*! \brief Sort the elements of a list.
150 \param Q The array that contains the elements to sort.
151 \param dist The array that contains the distances.
152 \param start The first element in the list to sort.
153 \param element The last element in the list to sort through.
154
155 Sorts the list based on distance. The distances are indexed by
156 their mappingNo in dist. Currently uses an insertion sort.
dulalsaurabd0816a32019-07-26 13:11:24 +0000157
158 The cost between two nodes can be zero or greater than zero.
159
Nick G97e34942016-07-11 14:46:27 -0500160 */
akmhoque53353462014-04-22 08:43:45 -0500161 void
162 sortQueueByDistance(int* Q, double* dist, int start, int element);
163
Nick G97e34942016-07-11 14:46:27 -0500164 /*! \brief Returns whether an element has been visited yet.
165 \param Q The list of elements to look through.
166 \param u The element to check.
167 \param start The start of list to look through.
168 \param element The end of the list to look through.
169 */
akmhoque53353462014-04-22 08:43:45 -0500170 int
171 isNotExplored(int* Q, int u, int start, int element);
172
173 void
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174 addAllLsNextHopsToRoutingTable(AdjacencyList& adjacencies, RoutingTable& rt,
Vince Lehman9a709032014-09-13 16:28:07 -0500175 Map& pMap, uint32_t sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500176
Nick G97e34942016-07-11 14:46:27 -0500177 /*! \brief Determines a destination's next hop.
178 \param dest The router whose next hop we want to determine.
179 \param source The router to determine a next path to.
180 */
akmhoque53353462014-04-22 08:43:45 -0500181 int
182 getLsNextHop(int dest, int source);
183
184 void
185 allocateParent();
186
187 void
188 allocateDistance();
189
190 void
191 freeParent();
192
193 void
194 freeDistance();
195
akmhoque53353462014-04-22 08:43:45 -0500196private:
197 int* m_parent;
198 double* m_distance;
199
dulalsaurabd0816a32019-07-26 13:11:24 +0000200 static const int EMPTY_PARENT;
201 static const double INF_DISTANCE;
202 static const int NO_MAPPING_NUM;
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500203public:
dulalsaurabd0816a32019-07-26 13:11:24 +0000204 static const int NO_NEXT_HOP;
akmhoque53353462014-04-22 08:43:45 -0500205
206};
207
Vince Lehman9a709032014-09-13 16:28:07 -0500208class AdjacencyList;
209class Lsdb;
210
211class HyperbolicRoutingCalculator
akmhoque53353462014-04-22 08:43:45 -0500212{
213public:
Vince Lehman9a709032014-09-13 16:28:07 -0500214 HyperbolicRoutingCalculator(size_t nRouters, bool isDryRun, ndn::Name thisRouterName)
215 : m_nRouters(nRouters)
216 , m_isDryRun(isDryRun)
217 , m_thisRouterName(thisRouterName)
akmhoque53353462014-04-22 08:43:45 -0500218 {
akmhoque53353462014-04-22 08:43:45 -0500219 }
220
221 void
Saurab Dulal72b2b252019-01-22 16:58:08 -0600222 calculatePath(Map& map, RoutingTable& rt, Lsdb& lsdb, AdjacencyList& adjacencies);
akmhoque53353462014-04-22 08:43:45 -0500223
224private:
akmhoque53353462014-04-22 08:43:45 -0500225 double
Saurab Dulal72b2b252019-01-22 16:58:08 -0600226 getHyperbolicDistance(Lsdb& lsdb, ndn::Name src, ndn::Name dest);
akmhoque53353462014-04-22 08:43:45 -0500227
228 void
Vince Lehman9a709032014-09-13 16:28:07 -0500229 addNextHop(ndn::Name destinationRouter, std::string faceUri, double cost, RoutingTable& rt);
akmhoque53353462014-04-22 08:43:45 -0500230
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600231 double
232 calculateHyperbolicDistance(double rI, double rJ, double deltaTheta);
233
234 double
235 calculateAngularDistance(std::vector<double> angleVectorI,
236 std::vector<double> angleVectorJ);
237
akmhoque53353462014-04-22 08:43:45 -0500238private:
Vince Lehman9a709032014-09-13 16:28:07 -0500239 const size_t m_nRouters;
240 const bool m_isDryRun;
241 const ndn::Name m_thisRouterName;
akmhoque53353462014-04-22 08:43:45 -0500242
Vince Lehman9a709032014-09-13 16:28:07 -0500243 static const double MATH_PI;
244 static const double UNKNOWN_DISTANCE;
245 static const double UNKNOWN_RADIUS;
akmhoque53353462014-04-22 08:43:45 -0500246};
247
Nick Gordonfad8e252016-08-11 14:21:38 -0500248} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500249
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600250#endif // NLSR_ROUTING_TABLE_CALCULATOR_HPP