routing-table.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
20 #include "routing-table.hpp"
21 #include "nlsr.hpp"
22 #include "map.hpp"
23 #include "conf-parameter.hpp"
25 #include "routing-table-entry.hpp"
26 #include "name-prefix-table.hpp"
27 #include "logger.hpp"
28 
29 #include <iostream>
30 #include <list>
31 #include <string>
32 
33 namespace nlsr {
34 
35 INIT_LOGGER("RoutingTable");
36 
37 RoutingTable::RoutingTable(ndn::Scheduler& scheduler)
38  : afterRoutingChange{ndn::make_unique<AfterRoutingChange>()}
39  , m_scheduler(scheduler)
40  , m_NO_NEXT_HOP{-12345}
41  , m_routingCalcInterval{static_cast<uint32_t>(ROUTING_CALC_INTERVAL_DEFAULT)}
42 {
43 }
44 
45 void
47 {
48  pnlsr.getLsdb().writeCorLsdbLog();
49  pnlsr.getLsdb().writeNameLsdbLog();
50  pnlsr.getLsdb().writeAdjLsdbLog();
51  pnlsr.getNamePrefixTable().writeLog();
52  if (pnlsr.getIsRoutingTableCalculating() == false) {
53  //setting routing table calculation
54  pnlsr.setIsRoutingTableCalculating(true);
55 
56  bool isHrEnabled = pnlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF;
57 
58  if ((!isHrEnabled
59  &&
60  pnlsr.getLsdb()
61  .doesLsaExist(ndn::Name{pnlsr.getConfParameter().getRouterPrefix()}
62  .append(std::to_string(Lsa::Type::ADJACENCY)), Lsa::Type::ADJACENCY))
63  ||
64  (isHrEnabled
65  &&
66  pnlsr.getLsdb()
67  .doesLsaExist(ndn::Name{pnlsr.getConfParameter().getRouterPrefix()}
68  .append(std::to_string(Lsa::Type::COORDINATE)), Lsa::Type::COORDINATE))) {
69  if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
70  NLSR_LOG_TRACE("Clearing old routing table");
71  clearRoutingTable();
72  // for dry run options
73  clearDryRoutingTable();
74 
75  NLSR_LOG_DEBUG("Calculating routing table");
76 
77  // calculate Link State routing
80  calculateLsRoutingTable(pnlsr);
81  }
82  //calculate hyperbolic routing
84  calculateHypRoutingTable(pnlsr);
85  }
86  //calculate dry hyperbolic routing
88  calculateHypDryRoutingTable(pnlsr);
89  }
90  // Inform the NPT that updates have been made
91  NLSR_LOG_DEBUG("Calling Update NPT With new Route");
92  (*afterRoutingChange)(m_rTable);
93  writeLog(pnlsr.getConfParameter().getHyperbolicState());
94  pnlsr.getNamePrefixTable().writeLog();
95  pnlsr.getFib().writeLog();
96  }
97  else {
98  NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
99  " routing table can not be calculated :(");
100  }
101  }
102  else {
103  NLSR_LOG_DEBUG("No Adj LSA of router itself,"
104  " so Routing table can not be calculated :(");
105  clearRoutingTable();
106  clearDryRoutingTable(); // for dry run options
107  // need to update NPT here
108  NLSR_LOG_DEBUG("Calling Update NPT With new Route");
109  (*afterRoutingChange)(m_rTable);
110  writeLog(pnlsr.getConfParameter().getHyperbolicState());
111  pnlsr.getNamePrefixTable().writeLog();
112  pnlsr.getFib().writeLog();
113  //debugging purpose end
114  }
115  pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
116  pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
117  }
118  else {
120  }
121 }
122 
123 void
124 RoutingTable::calculateLsRoutingTable(Nlsr& nlsr)
125 {
126  NLSR_LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
127 
128  Map map;
129  map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
130  map.writeLog();
131 
132  size_t nRouters = map.getMapSize();
133 
134  LinkStateRoutingTableCalculator calculator(nRouters);
135 
136  calculator.calculatePath(map, std::ref(*this), nlsr);
137 }
138 
139 void
140 RoutingTable::calculateHypRoutingTable(Nlsr& nlsr)
141 {
142  Map map;
144  nlsr.getLsdb().getCoordinateLsdb().end());
145  map.writeLog();
146 
147  size_t nRouters = map.getMapSize();
148 
149  HyperbolicRoutingCalculator calculator(nRouters, false,
151 
152  calculator.calculatePaths(map, std::ref(*this),
153  nlsr.getLsdb(), nlsr.getAdjacencyList());
154 }
155 
156 void
157 RoutingTable::calculateHypDryRoutingTable(Nlsr& nlsr)
158 {
159  Map map;
160  map.createFromAdjLsdb(nlsr.getLsdb().getAdjLsdb().begin(), nlsr.getLsdb().getAdjLsdb().end());
161  map.writeLog();
162 
163  size_t nRouters = map.getMapSize();
164 
165  HyperbolicRoutingCalculator calculator(nRouters, true,
167 
168  calculator.calculatePaths(map, std::ref(*this),
169  nlsr.getLsdb(), nlsr.getAdjacencyList());
170 }
171 
172 void
174 {
175  if (pnlsr.getIsRouteCalculationScheduled() != true) {
176  NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
177 
178  m_scheduler.scheduleEvent(m_routingCalcInterval,
179  std::bind(&RoutingTable::calculate, this, std::ref(pnlsr)));
180 
181  pnlsr.setIsRouteCalculationScheduled(true);
182  }
183 }
184 
185 static bool
186 routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
187 {
188  return rte.getDestination() == destRouter;
189 }
190 
191 void
192 RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
193 {
194  NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
195 
196  RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
197  if (rteChk == 0) {
198  RoutingTableEntry rte(destRouter);
199  rte.getNexthopList().addNextHop(nh);
200  m_rTable.push_back(rte);
201  }
202  else {
203  rteChk->getNexthopList().addNextHop(nh);
204  }
205 }
206 
208 RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
209 {
210  std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
211  m_rTable.end(),
212  std::bind(&routingTableEntryCompare,
213  _1, destRouter));
214  if (it != m_rTable.end()) {
215  return &(*it);
216  }
217  return 0;
218 }
219 
220 void
221 RoutingTable::writeLog(int hyperbolicState)
222 {
223  NLSR_LOG_DEBUG("---------------Routing Table------------------");
224  for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
225  it != m_rTable.end(); ++it) {
226  NLSR_LOG_DEBUG("Destination: " << (*it).getDestination());
227  NLSR_LOG_DEBUG("Nexthops: ");
228  (*it).getNexthopList().writeLog();
229  }
230 
231  if (hyperbolicState == HYPERBOLIC_STATE_DRY_RUN) {
232  NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
233  for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
234  it != m_dryTable.end(); ++it) {
235  NLSR_LOG_DEBUG("Destination: " << (*it).getDestination());
236  NLSR_LOG_DEBUG("Nexthops: ");
237  (*it).getNexthopList().writeLog();
238  }
239  }
240 }
241 
242 void
243 RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
244 {
245  NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
246 
247  std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
248  m_dryTable.end(),
249  std::bind(&routingTableEntryCompare,
250  _1, destRouter));
251  if (it == m_dryTable.end()) {
252  RoutingTableEntry rte(destRouter);
253  rte.getNexthopList().addNextHop(nh);
254  m_dryTable.push_back(rte);
255  }
256  else {
257  (*it).getNexthopList().addNextHop(nh);
258  }
259 }
260 
261 void
262 RoutingTable::clearRoutingTable()
263 {
264  if (m_rTable.size() > 0) {
265  m_rTable.clear();
266  }
267 }
268 
269 void
270 RoutingTable::clearDryRoutingTable()
271 {
272  if (m_dryTable.size() > 0) {
273  m_dryTable.clear();
274  }
275 }
276 
277 } // namespace nlsr
void writeLog()
Definition: fib.cpp:398
bool getIsBuildAdjLsaSheduled()
Definition: nlsr.hpp:199
void writeNameLsdbLog()
Definition: lsdb.cpp:360
ConfParameter & getConfParameter()
Definition: nlsr.hpp:133
size_t getMapSize() const
Definition: map.hpp:117
void createFromCoordinateLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:99
void writeAdjLsdbLog()
Definition: lsdb.cpp:1287
NamePrefixTable & getNamePrefixTable()
Definition: nlsr.hpp:169
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:41
const ndn::Name & getRouterPrefix() const
void writeCorLsdbLog()
Definition: lsdb.cpp:561
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
void calculate(Nlsr &pnlsr)
Calculates a list of next hops for each router in the network.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
const std::list< CoordinateLsa > & getCoordinateLsdb() const
Definition: lsdb.cpp:571
AdjacencyList & getAdjacencyList()
Definition: nlsr.hpp:139
static bool routingTableEntryCompare(RoutingTableEntry &rte, ndn::Name &destRouter)
void setIsRouteCalculationScheduled(bool ircs)
Definition: nlsr.hpp:229
bool getIsRoutingTableCalculating()
Definition: nlsr.hpp:211
const ndn::Name & getDestination() const
RoutingTable(ndn::Scheduler &scheduler)
void scheduleRoutingTableCalculation(Nlsr &pnlsr)
Schedules a calculation event in the event scheduler only if one isn&#39;t already scheduled.
void writeLog()
Definition: map.cpp:84
bool doesLsaExist(const ndn::Name &key, const Lsa::Type &lsType)
Definition: lsdb.cpp:1298
bool getIsRouteCalculationScheduled()
Definition: nlsr.hpp:223
void createFromAdjLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:82
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Lsdb & getLsdb()
Definition: nlsr.hpp:157
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
void addNextHopToDryTable(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry in a dry run scenario.
void setIsRoutingTableCalculating(bool irtc)
Definition: nlsr.hpp:217
RoutingTableEntry * findRoutingTableEntry(const ndn::Name &destRouter)
int32_t getHyperbolicState() const
void calculatePath(Map &pMap, RoutingTable &rt, Nlsr &pnlsr)
void calculatePaths(Map &map, RoutingTable &rt, Lsdb &lsdb, AdjacencyList &adjacencies)
void addNextHop(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry.
const std::list< AdjLsa > & getAdjLsdb() const
Definition: lsdb.cpp:809
Fib & getFib()
Definition: nlsr.hpp:175
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:38