lsdb.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2020, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
6  *
7  * This file is part of NLSR (Named-data Link State Routing).
8  * See AUTHORS.md for complete list of NLSR authors and contributors.
9  *
10  * NLSR is free software: you can redistribute it and/or modify it under the terms
11  * of the GNU General Public License as published by the Free Software Foundation,
12  * either version 3 of the License, or (at your option) any later version.
13  *
14  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE. See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20  **/
21 
22 #ifndef NLSR_LSDB_HPP
23 #define NLSR_LSDB_HPP
24 
25 #include "conf-parameter.hpp"
26 #include "lsa.hpp"
27 #include "sequencing-manager.hpp"
28 #include "test-access-control.hpp"
30 #include "statistics.hpp"
32 
33 #include <ndn-cxx/security/key-chain.hpp>
34 #include <ndn-cxx/util/signal.hpp>
35 #include <ndn-cxx/util/time.hpp>
36 #include <ndn-cxx/util/segment-fetcher.hpp>
37 #include <ndn-cxx/ims/in-memory-storage-persistent.hpp>
38 
39 #include <PSync/segment-publisher.hpp>
40 
41 #include <utility>
42 #include <boost/cstdint.hpp>
43 
44 namespace nlsr {
45 
46 class Lsdb
47 {
48 public:
49  Lsdb(ndn::Face& face, ndn::KeyChain& keyChain, ConfParameter& confParam,
50  NamePrefixTable& namePrefixTable, RoutingTable& routingTable);
51 
52  ~Lsdb();
53 
54  bool
55  isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType, const uint64_t& sequenceNumber);
56 
57  bool
58  doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType);
59 
63  bool
65 
69  NameLsa*
70  findNameLsa(const ndn::Name& key);
71 
75  bool
76  installNameLsa(NameLsa& nlsa);
77 
85  bool
86  removeNameLsa(const ndn::Name& key);
87 
92  bool
93  isNameLsaNew(const ndn::Name& key, uint64_t seqNo);
94 
95  void
97 
98  const std::list<NameLsa>&
99  getNameLsdb() const;
100 
102  bool
104 
109  findCoordinateLsa(const ndn::Name& key);
110 
114  bool
116 
124  bool
125  removeCoordinateLsa(const ndn::Name& key);
126 
131  bool
132  isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo);
133 
134  void
135  writeCorLsdbLog();
136 
137  const std::list<CoordinateLsa>&
138  getCoordinateLsdb() const;
139 
140  //function related to Adj LSDB
141 
143  void
145 
147  bool
149 
153  bool
154  removeAdjLsa(const ndn::Name& key);
155 
163  bool
164  isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
165 
169  bool
170  installAdjLsa(AdjLsa& alsa);
171 
175  AdjLsa*
176  findAdjLsa(const ndn::Name& key);
177 
178  const std::list<AdjLsa>&
179  getAdjLsdb() const;
180 
181  void
182  setAdjLsaBuildInterval(uint32_t interval)
183  {
184  m_adjLsaBuildInterval = ndn::time::seconds(interval);
185  }
186 
187  void
188  writeAdjLsdbLog();
189 
190  void
191  expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
192  ndn::time::steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
193 
194  /* \brief Process interest which can be either:
195  * 1) Discovery interest from segment fetcher:
196  * /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>
197  * 2) Interest containing segment number:
198  * /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>/<version>/<segmentNo>
199  */
200  void
201  processInterest(const ndn::Name& name, const ndn::Interest& interest);
202 
203  bool
205  {
206  return m_isBuildAdjLsaSheduled;
207  }
208 
211  return m_sync;
212  }
213 
214 private:
215  /* \brief Add a name LSA to the LSDB if it isn't already there.
216  \param nlsa The candidade name LSA.
217  */
218  bool
219  addNameLsa(NameLsa& nlsa);
220 
224  bool
225  doesNameLsaExist(const ndn::Name& key);
226 
230  bool
231  addCoordinateLsa(CoordinateLsa& clsa);
232 
236  bool
237  doesCoordinateLsaExist(const ndn::Name& key);
238 
246  void
247  buildAdjLsa();
248 
252  bool
253  addAdjLsa(AdjLsa& alsa);
254 
258  bool
259  doesAdjLsaExist(const ndn::Name& key);
260 
266  ndn::scheduler::EventId
267  scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
268  const ndn::time::seconds& expTime);
269 
274  void
275  expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo);
276 
283  ndn::scheduler::EventId
284  scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
285  const ndn::time::seconds& expTime);
286 
287 private:
288  void
289  expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
290 
291  ndn::scheduler::EventId
292  scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
293  const ndn::time::seconds& expTime);
294 
295  void
296  expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
297  uint64_t seqNo);
298 
299  void
300  processInterestForNameLsa(const ndn::Interest& interest,
301  const ndn::Name& lsaKey,
302  uint64_t seqNo);
303 
304  void
305  processInterestForAdjacencyLsa(const ndn::Interest& interest,
306  const ndn::Name& lsaKey,
307  uint64_t seqNo);
308 
309  void
310  processInterestForCoordinateLsa(const ndn::Interest& interest,
311  const ndn::Name& lsaKey,
312  uint64_t seqNo);
313 
314  void
315  onContentValidated(const std::shared_ptr<const ndn::Data>& data);
316 
317  void
318  processContentNameLsa(const ndn::Name& lsaKey,
319  uint64_t lsSeqNo, std::string& dataContent);
320 
321  void
322  processContentAdjacencyLsa(const ndn::Name& lsaKey,
323  uint64_t lsSeqNo, std::string& dataContent);
324 
325  void
326  processContentCoordinateLsa(const ndn::Name& lsaKey,
327  uint64_t lsSeqNo, std::string& dataContent);
328 
344  void
345  onFetchLsaError(uint32_t errorCode,
346  const std::string& msg,
347  const ndn::Name& interestName,
348  uint32_t retransmitNo,
349  const ndn::time::steady_clock::TimePoint& deadline,
350  ndn::Name lsaName,
351  uint64_t seqNo);
352 
359  void
360  afterFetchLsa(const ndn::ConstBufferPtr& data, const ndn::Name& interestName);
361 
362  void
363  emitSegmentValidatedSignal(const ndn::Data& data)
364  {
366  }
367 
368 private:
369  ndn::time::system_clock::TimePoint
370  getLsaExpirationTimePoint();
371 
372 public:
373  static const ndn::Name::Component NAME_COMPONENT;
374 
375  ndn::util::signal::Signal<Lsdb, Statistics::PacketType> lsaIncrementSignal;
376  ndn::util::signal::Signal<Lsdb, const ndn::Data&> afterSegmentValidatedSignal;
377 
378 private:
379  ndn::Face& m_face;
380  ndn::Scheduler m_scheduler;
381 
382  ConfParameter& m_confParam;
383  NamePrefixTable& m_namePrefixTable;
384  RoutingTable& m_routingTable;
385 
387  SyncLogicHandler m_sync;
388 
389 private:
390  std::list<NameLsa> m_nameLsdb;
391  std::list<AdjLsa> m_adjLsdb;
392  std::list<CoordinateLsa> m_corLsdb;
393 
394  ndn::time::seconds m_lsaRefreshTime;
395  std::string m_thisRouterPrefix;
396 
397  typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
398 
399  // Maps the name of an LSA to its highest known sequence number from sync;
400  // Used to stop NLSR from trying to fetch outdated LSAs
401  SequenceNumberMap m_highestSeqNo;
402 
403  static const ndn::time::seconds GRACE_PERIOD;
404  static const ndn::time::steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
405 
407  ndn::time::seconds m_adjLsaBuildInterval;
408 
409  SequencingManager m_sequencingManager;
410 
411 private:
412  ndn::util::signal::ScopedConnection m_onNewLsaConnection;
413 
414  std::set<std::shared_ptr<ndn::util::SegmentFetcher>> m_fetchers;
415  psync::SegmentPublisher m_segmentPublisher;
416 
417  bool m_isBuildAdjLsaSheduled;
418  int64_t m_adjBuildCount;
419  ndn::scheduler::ScopedEventId m_scheduledAdjLsaBuild;
420 
422  ndn::InMemoryStoragePersistent m_lsaStorage;
423 };
424 
425 } // namespace nlsr
426 
427 #endif // NLSR_LSDB_HPP
bool installAdjLsa(AdjLsa &alsa)
Installs an adj. LSA into the LSDB.
Definition: lsdb.cpp:683
A class to house all the configuration parameters for NLSR.
void scheduleAdjLsaBuild()
Schedules a build of this router&#39;s LSA.
Definition: lsdb.cpp:559
bool removeNameLsa(const ndn::Name &key)
Remove a name LSA from the LSDB.
Definition: lsdb.cpp:306
void writeNameLsdbLog()
Definition: lsdb.cpp:339
#define PUBLIC_WITH_TESTS_ELSE_PRIVATE
void setAdjLsaBuildInterval(uint32_t interval)
Definition: lsdb.hpp:182
const std::list< NameLsa > & getNameLsdb() const
Definition: lsdb.cpp:348
bool isLsaNew(const ndn::Name &routerName, const Lsa::Type &lsaType, const uint64_t &sequenceNumber)
Definition: lsdb.cpp:1304
const std::list< CoordinateLsa > & getCoordinateLsdb() const
Definition: lsdb.cpp:541
void writeAdjLsdbLog()
Definition: lsdb.cpp:1275
Lsdb(ndn::Face &face, ndn::KeyChain &keyChain, ConfParameter &confParam, NamePrefixTable &namePrefixTable, RoutingTable &routingTable)
Definition: lsdb.cpp:39
void writeCorLsdbLog()
Definition: lsdb.cpp:528
bool getIsBuildAdjLsaSheduled()
Definition: lsdb.hpp:204
bool isCoordinateLsaNew(const ndn::Name &key, uint64_t seqNo)
Returns whether a cor. LSA from a router is new or not.
Definition: lsdb.cpp:398
CoordinateLsa * findCoordinateLsa(const ndn::Name &key)
Finds a cor. LSA in the LSDB.
Definition: lsdb.cpp:387
ndn::util::signal::Signal< Lsdb, const ndn::Data & > afterSegmentValidatedSignal
Definition: lsdb.hpp:376
bool isNameLsaNew(const ndn::Name &key, uint64_t seqNo)
Definition: lsdb.cpp:175
bool installCoordinateLsa(CoordinateLsa &clsa)
Installs a cor. LSA into the LSDB.
Definition: lsdb.cpp:427
NLSR-to-ChronoSync interaction point.
NameLsa * findNameLsa(const ndn::Name &key)
Returns the name LSA with the given key.
Definition: lsdb.cpp:164
void processInterest(const ndn::Name &name, const ndn::Interest &interest)
Definition: lsdb.cpp:1008
void expressInterest(const ndn::Name &interestName, uint32_t timeoutCount, ndn::time::steady_clock::TimePoint deadline=DEFAULT_LSA_RETRIEVAL_DEADLINE)
Definition: lsdb.cpp:928
bool doesLsaExist(const ndn::Name &key, const Lsa::Type &lsType)
Definition: lsdb.cpp:1289
bool isAdjLsaNew(const ndn::Name &key, uint64_t seqNo)
Returns whether an LSA is new.
Definition: lsdb.cpp:658
bool removeAdjLsa(const ndn::Name &key)
Removes an adj. LSA from the LSDB.
Definition: lsdb.cpp:753
~Lsdb()
Definition: lsdb.cpp:68
static const ndn::Name::Component NAME_COMPONENT
Definition: lsdb.hpp:373
Copyright (c) 2014-2019, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
ndn::util::signal::Signal< Lsdb, Statistics::PacketType > lsaIncrementSignal
Definition: lsdb.hpp:375
bool installNameLsa(NameLsa &nlsa)
Installs a name LSA into the LSDB.
Definition: lsdb.cpp:200
const std::list< AdjLsa > & getAdjLsdb() const
Definition: lsdb.cpp:778
SyncLogicHandler & getSync()
Definition: lsdb.hpp:210
bool buildAndInstallOwnNameLsa()
Builds a name LSA for this router and then installs it into the LSDB.
Definition: lsdb.cpp:149
bool buildAndInstallOwnCoordinateLsa()
Builds a cor. LSA for this router and installs it into the LSDB.
Definition: lsdb.cpp:366
AdjLsa * findAdjLsa(const ndn::Name &key)
Finds an adj. LSA in the LSDB.
Definition: lsdb.cpp:647
bool buildAndInstallOwnAdjLsa()
Wrapper event to build and install an adj. LSA for this router.
Definition: lsdb.cpp:734
bool removeCoordinateLsa(const ndn::Name &key)
Removes a cor. LSA from the LSDB.
Definition: lsdb.cpp:500