blob: 6024fbdb564cb82a496e0dd3291345a0cb3354b9 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Saurab Dulal9da6aa72018-01-12 17:25:51 +00003 * Copyright (c) 2014-2018, 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/>.
19 *
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Nick Gordone40377d2017-08-11 15:10:02 -050021
akmhoquefdbddb12014-05-02 18:35:19 -050022#ifndef NLSR_ROUTING_TABLE_CALCULATOR_HPP
23#define NLSR_ROUTING_TABLE_CALCULATOR_HPP
akmhoque53353462014-04-22 08:43:45 -050024
Nick Gordone40377d2017-08-11 15:10:02 -050025#include "common.hpp"
26
akmhoque53353462014-04-22 08:43:45 -050027#include <list>
28#include <iostream>
akmhoquefdbddb12014-05-02 18:35:19 -050029#include <boost/cstdint.hpp>
akmhoque53353462014-04-22 08:43:45 -050030
Vince Lehman9a709032014-09-13 16:28:07 -050031#include <ndn-cxx/name.hpp>
32
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
34
35class Map;
36class RoutingTable;
37class Nlsr;
38
39class RoutingTableCalculator
40{
41public:
42 RoutingTableCalculator()
43 {
44 }
Vince Lehman9a709032014-09-13 16:28:07 -050045 RoutingTableCalculator(size_t nRouters)
akmhoque53353462014-04-22 08:43:45 -050046 {
Vince Lehman9a709032014-09-13 16:28:07 -050047 m_nRouters = nRouters;
akmhoque53353462014-04-22 08:43:45 -050048 }
49protected:
Nick G97e34942016-07-11 14:46:27 -050050 /*! \brief Allocate the space needed for the adj. matrix. */
akmhoque53353462014-04-22 08:43:45 -050051 void
52 allocateAdjMatrix();
53
Nick G97e34942016-07-11 14:46:27 -050054 /*! \brief Zero every cell of the matrix to ensure that the memory is safe. */
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.
59 \param pnlsr The NLSR object that contains the LSAs that we need to iterate
60 over.
61 \param pMap The map to populate with the adj. data.
62 */
akmhoque53353462014-04-22 08:43:45 -050063 void
Saurab Dulal9da6aa72018-01-12 17:25:51 +000064 makeAdjMatrix(Nlsr& pnlsr, Map& pMap);
Vince Lehman9a709032014-09-13 16:28:07 -050065
akmhoque2f423352014-06-03 11:49:35 -050066 void
Saurab Dulal9da6aa72018-01-12 17:25:51 +000067 writeAdjMatrixLog(const Map& map) const;
akmhoque53353462014-04-22 08:43:45 -050068
Nick G97e34942016-07-11 14:46:27 -050069 /*! \brief Returns how many links a router in the matrix has.
70 \param sRouter The router to count the links of.
71 */
akmhoque53353462014-04-22 08:43:45 -050072 int
73 getNumOfLinkfromAdjMatrix(int sRouter);
74
75 void
76 freeAdjMatrix();
Nick G97e34942016-07-11 14:46:27 -050077 /*! \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 */
akmhoque53353462014-04-22 08:43:45 -050082 void
83 adjustAdMatrix(int source, int link, double linkCost);
84
Nick G97e34942016-07-11 14:46:27 -050085 /*! \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 */
akmhoque53353462014-04-22 08:43:45 -050096 void
97 getLinksFromAdjMatrix(int* links, double* linkCosts, int source);
98
Nick G97e34942016-07-11 14:46:27 -050099 /*! Allocates an array large enough to hold multipath calculation temps. */
akmhoque53353462014-04-22 08:43:45 -0500100 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
118protected:
119 double** adjMatrix;
Vince Lehman9a709032014-09-13 16:28:07 -0500120 size_t m_nRouters;
akmhoque53353462014-04-22 08:43:45 -0500121
122 int vNoLink;
123 int* links;
124 double* linkCosts;
125};
126
127class LinkStateRoutingTableCalculator: public RoutingTableCalculator
128{
129public:
Vince Lehman9a709032014-09-13 16:28:07 -0500130 LinkStateRoutingTableCalculator(size_t nRouters)
131 : RoutingTableCalculator(nRouters)
132 , EMPTY_PARENT(-12345)
akmhoque53353462014-04-22 08:43:45 -0500133 , INF_DISTANCE(2147483647)
134 , NO_MAPPING_NUM(-1)
135 , NO_NEXT_HOP(-12345)
136 {
akmhoque53353462014-04-22 08:43:45 -0500137 }
138
akmhoque53353462014-04-22 08:43:45 -0500139 void
140 calculatePath(Map& pMap, RoutingTable& rt, Nlsr& pnlsr);
141
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.
157 */
akmhoque53353462014-04-22 08:43:45 -0500158 void
159 sortQueueByDistance(int* Q, double* dist, int start, int element);
160
Nick G97e34942016-07-11 14:46:27 -0500161 /*! \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 */
akmhoque53353462014-04-22 08:43:45 -0500167 int
168 isNotExplored(int* Q, int u, int start, int element);
169
170 void
akmhoque53353462014-04-22 08:43:45 -0500171 addAllLsNextHopsToRoutingTable(Nlsr& pnlsr, RoutingTable& rt,
Vince Lehman9a709032014-09-13 16:28:07 -0500172 Map& pMap, uint32_t sourceRouter);
akmhoque53353462014-04-22 08:43:45 -0500173
Nick G97e34942016-07-11 14:46:27 -0500174 /*! \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 */
akmhoque53353462014-04-22 08:43:45 -0500178 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
akmhoque53353462014-04-22 08:43:45 -0500193private:
194 int* m_parent;
195 double* m_distance;
196
akmhoque53353462014-04-22 08:43:45 -0500197 const int EMPTY_PARENT;
198 const double INF_DISTANCE;
199 const int NO_MAPPING_NUM;
200 const int NO_NEXT_HOP;
201
202};
203
Vince Lehman9a709032014-09-13 16:28:07 -0500204class AdjacencyList;
205class Lsdb;
206
207class HyperbolicRoutingCalculator
akmhoque53353462014-04-22 08:43:45 -0500208{
209public:
Vince Lehman9a709032014-09-13 16:28:07 -0500210 HyperbolicRoutingCalculator(size_t nRouters, bool isDryRun, ndn::Name thisRouterName)
211 : m_nRouters(nRouters)
212 , m_isDryRun(isDryRun)
213 , m_thisRouterName(thisRouterName)
akmhoque53353462014-04-22 08:43:45 -0500214 {
akmhoque53353462014-04-22 08:43:45 -0500215 }
216
217 void
Vince Lehman9a709032014-09-13 16:28:07 -0500218 calculatePaths(Map& map, RoutingTable& rt, Lsdb& lsdb, AdjacencyList& adjacencies);
akmhoque53353462014-04-22 08:43:45 -0500219
220private:
akmhoque53353462014-04-22 08:43:45 -0500221 double
Vince Lehman9a709032014-09-13 16:28:07 -0500222 getHyperbolicDistance(Map& map, Lsdb& lsdb, ndn::Name src, ndn::Name dest);
akmhoque53353462014-04-22 08:43:45 -0500223
224 void
Vince Lehman9a709032014-09-13 16:28:07 -0500225 addNextHop(ndn::Name destinationRouter, std::string faceUri, double cost, RoutingTable& rt);
akmhoque53353462014-04-22 08:43:45 -0500226
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600227 double
228 calculateHyperbolicDistance(double rI, double rJ, double deltaTheta);
229
230 double
231 calculateAngularDistance(std::vector<double> angleVectorI,
232 std::vector<double> angleVectorJ);
233
akmhoque53353462014-04-22 08:43:45 -0500234private:
Vince Lehman9a709032014-09-13 16:28:07 -0500235 const size_t m_nRouters;
236 const bool m_isDryRun;
237 const ndn::Name m_thisRouterName;
akmhoque53353462014-04-22 08:43:45 -0500238
Vince Lehman9a709032014-09-13 16:28:07 -0500239 static const double MATH_PI;
240 static const double UNKNOWN_DISTANCE;
241 static const double UNKNOWN_RADIUS;
akmhoque53353462014-04-22 08:43:45 -0500242};
243
Nick Gordonfad8e252016-08-11 14:21:38 -0500244} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500245
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600246#endif // NLSR_ROUTING_TABLE_CALCULATOR_HPP