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-2019, 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,
50  ndn::security::SigningInfo& signingInfo, ConfParameter& confParam,
51  NamePrefixTable& namePrefixTable, RoutingTable& routingTable);
52 
53  ~Lsdb();
54 
55  bool
56  isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType, const uint64_t& sequenceNumber);
57 
58  bool
59  doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType);
60 
64  bool
66 
70  NameLsa*
71  findNameLsa(const ndn::Name& key);
72 
76  bool
77  installNameLsa(NameLsa& nlsa);
78 
86  bool
87  removeNameLsa(const ndn::Name& key);
88 
93  bool
94  isNameLsaNew(const ndn::Name& key, uint64_t seqNo);
95 
96  void
98 
99  const std::list<NameLsa>&
100  getNameLsdb() const;
101 
103  bool
105 
110  findCoordinateLsa(const ndn::Name& key);
111 
115  bool
117 
125  bool
126  removeCoordinateLsa(const ndn::Name& key);
127 
132  bool
133  isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo);
134 
135  void
136  writeCorLsdbLog();
137 
138  const std::list<CoordinateLsa>&
139  getCoordinateLsdb() const;
140 
141  //function related to Adj LSDB
142 
144  void
146 
148  bool
150 
154  bool
155  removeAdjLsa(const ndn::Name& key);
156 
164  bool
165  isAdjLsaNew(const ndn::Name& key, uint64_t seqNo);
166 
170  bool
171  installAdjLsa(AdjLsa& alsa);
172 
176  AdjLsa*
177  findAdjLsa(const ndn::Name& key);
178 
179  const std::list<AdjLsa>&
180  getAdjLsdb() const;
181 
182  void
183  setAdjLsaBuildInterval(uint32_t interval)
184  {
185  m_adjLsaBuildInterval = ndn::time::seconds(interval);
186  }
187 
188  void
189  writeAdjLsdbLog();
190 
191  void
192  expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
193  ndn::time::steady_clock::TimePoint deadline = DEFAULT_LSA_RETRIEVAL_DEADLINE);
194 
195  /* \brief Process interest which can be either:
196  * 1) Discovery interest from segment fetcher:
197  * /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>
198  * 2) Interest containing segment number:
199  * /localhop/<network>/nlsr/LSA/<site>/<router>/<lsaType>/<seqNo>/<version>/<segmentNo>
200  */
201  void
202  processInterest(const ndn::Name& name, const ndn::Interest& interest);
203 
204  bool
206  {
207  return m_isBuildAdjLsaSheduled;
208  }
209 
210  const SyncLogicHandler&
212  return m_sync;
213  }
214 
215 private:
216  /* \brief Add a name LSA to the LSDB if it isn't already there.
217  \param nlsa The candidade name LSA.
218  */
219  bool
220  addNameLsa(NameLsa& nlsa);
221 
225  bool
226  doesNameLsaExist(const ndn::Name& key);
227 
231  bool
232  addCoordinateLsa(CoordinateLsa& clsa);
233 
237  bool
238  doesCoordinateLsaExist(const ndn::Name& key);
239 
247  void
248  buildAdjLsa();
249 
253  bool
254  addAdjLsa(AdjLsa& alsa);
255 
259  bool
260  doesAdjLsaExist(const ndn::Name& key);
261 
267  ndn::scheduler::EventId
268  scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
269  const ndn::time::seconds& expTime);
270 
275  void
276  expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo);
277 
284  ndn::scheduler::EventId
285  scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
286  const ndn::time::seconds& expTime);
287 
288 private:
289  void
290  expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo);
291 
292  ndn::scheduler::EventId
293  scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
294  const ndn::time::seconds& expTime);
295 
296  void
297  expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
298  uint64_t seqNo);
299 
300  void
301  processInterestForNameLsa(const ndn::Interest& interest,
302  const ndn::Name& lsaKey,
303  uint64_t seqNo);
304 
305  void
306  processInterestForAdjacencyLsa(const ndn::Interest& interest,
307  const ndn::Name& lsaKey,
308  uint64_t seqNo);
309 
310  void
311  processInterestForCoordinateLsa(const ndn::Interest& interest,
312  const ndn::Name& lsaKey,
313  uint64_t seqNo);
314 
315  void
316  onContentValidated(const std::shared_ptr<const ndn::Data>& data);
317 
318  void
319  processContentNameLsa(const ndn::Name& lsaKey,
320  uint64_t lsSeqNo, std::string& dataContent);
321 
322  void
323  processContentAdjacencyLsa(const ndn::Name& lsaKey,
324  uint64_t lsSeqNo, std::string& dataContent);
325 
326  void
327  processContentCoordinateLsa(const ndn::Name& lsaKey,
328  uint64_t lsSeqNo, std::string& dataContent);
329 
345  void
346  onFetchLsaError(uint32_t errorCode,
347  const std::string& msg,
348  const ndn::Name& interestName,
349  uint32_t retransmitNo,
350  const ndn::time::steady_clock::TimePoint& deadline,
351  ndn::Name lsaName,
352  uint64_t seqNo);
353 
360  void
361  afterFetchLsa(const ndn::ConstBufferPtr& data, const ndn::Name& interestName);
362 
363 private:
364  ndn::time::system_clock::TimePoint
365  getLsaExpirationTimePoint();
366 
367 public:
368  static const ndn::Name::Component NAME_COMPONENT;
369 
370  ndn::util::signal::Signal<Lsdb, Statistics::PacketType> lsaIncrementSignal;
371  ndn::util::signal::Signal<Lsdb, const ndn::Data&> afterSegmentValidatedSignal;
372 
373 private:
374  ndn::Face& m_face;
375  ndn::Scheduler m_scheduler;
376  ndn::security::SigningInfo& m_signingInfo;
377 
378  ConfParameter& m_confParam;
379  NamePrefixTable& m_namePrefixTable;
380  RoutingTable& m_routingTable;
381 
383  SyncLogicHandler m_sync;
384 
385 private:
386  std::list<NameLsa> m_nameLsdb;
387  std::list<AdjLsa> m_adjLsdb;
388  std::list<CoordinateLsa> m_corLsdb;
389 
390  ndn::time::seconds m_lsaRefreshTime;
391  std::string m_thisRouterPrefix;
392 
393  typedef std::map<ndn::Name, uint64_t> SequenceNumberMap;
394 
395  // Maps the name of an LSA to its highest known sequence number from sync;
396  // Used to stop NLSR from trying to fetch outdated LSAs
397  SequenceNumberMap m_highestSeqNo;
398 
399  static const ndn::time::seconds GRACE_PERIOD;
400  static const ndn::time::steady_clock::TimePoint DEFAULT_LSA_RETRIEVAL_DEADLINE;
401 
403  ndn::time::seconds m_adjLsaBuildInterval;
404 
405  SequencingManager m_sequencingManager;
406 
407 private:
408  ndn::util::signal::ScopedConnection m_onNewLsaConnection;
409 
410  std::set<std::shared_ptr<ndn::util::SegmentFetcher>> m_fetchers;
411  psync::SegmentPublisher m_segmentPublisher;
412 
413  bool m_isBuildAdjLsaSheduled;
414  int64_t m_adjBuildCount;
415 
417  ndn::InMemoryStoragePersistent m_lsaStorage;
418 };
419 
420 } // namespace nlsr
421 
422 #endif // NLSR_LSDB_HPP
bool installAdjLsa(AdjLsa &alsa)
Installs an adj. LSA into the LSDB.
Definition: lsdb.cpp:675
const std::list< NameLsa > & getNameLsdb() const
Definition: lsdb.cpp:346
A class to house all the configuration parameters for NLSR.
void scheduleAdjLsaBuild()
Schedules a build of this router&#39;s LSA.
Definition: lsdb.cpp:553
bool removeNameLsa(const ndn::Name &key)
Remove a name LSA from the LSDB.
Definition: lsdb.cpp:304
void writeNameLsdbLog()
Definition: lsdb.cpp:337
#define PUBLIC_WITH_TESTS_ELSE_PRIVATE
void setAdjLsaBuildInterval(uint32_t interval)
Definition: lsdb.hpp:183
bool isLsaNew(const ndn::Name &routerName, const Lsa::Type &lsaType, const uint64_t &sequenceNumber)
Definition: lsdb.cpp:1292
void writeAdjLsdbLog()
Definition: lsdb.cpp:1267
void writeCorLsdbLog()
Definition: lsdb.cpp:526
bool getIsBuildAdjLsaSheduled()
Definition: lsdb.hpp:205
const std::list< CoordinateLsa > & getCoordinateLsdb() const
Definition: lsdb.cpp:535
bool isCoordinateLsaNew(const ndn::Name &key, uint64_t seqNo)
Returns whether a cor. LSA from a router is new or not.
Definition: lsdb.cpp:396
CoordinateLsa * findCoordinateLsa(const ndn::Name &key)
Finds a cor. LSA in the LSDB.
Definition: lsdb.cpp:385
ndn::util::signal::Signal< Lsdb, const ndn::Data & > afterSegmentValidatedSignal
Definition: lsdb.hpp:371
bool isNameLsaNew(const ndn::Name &key, uint64_t seqNo)
Definition: lsdb.cpp:171
Lsdb(ndn::Face &face, ndn::KeyChain &keyChain, ndn::security::SigningInfo &signingInfo, ConfParameter &confParam, NamePrefixTable &namePrefixTable, RoutingTable &routingTable)
Definition: lsdb.cpp:39
bool installCoordinateLsa(CoordinateLsa &clsa)
Installs a cor. LSA into the LSDB.
Definition: lsdb.cpp:425
const SyncLogicHandler & getSync()
Definition: lsdb.hpp:211
NLSR-to-ChronoSync interaction point.
NameLsa * findNameLsa(const ndn::Name &key)
Returns the name LSA with the given key.
Definition: lsdb.cpp:160
void processInterest(const ndn::Name &name, const ndn::Interest &interest)
Definition: lsdb.cpp:1000
void expressInterest(const ndn::Name &interestName, uint32_t timeoutCount, ndn::time::steady_clock::TimePoint deadline=DEFAULT_LSA_RETRIEVAL_DEADLINE)
Definition: lsdb.cpp:920
bool doesLsaExist(const ndn::Name &key, const Lsa::Type &lsType)
Definition: lsdb.cpp:1277
bool isAdjLsaNew(const ndn::Name &key, uint64_t seqNo)
Returns whether an LSA is new.
Definition: lsdb.cpp:650
bool removeAdjLsa(const ndn::Name &key)
Removes an adj. LSA from the LSDB.
Definition: lsdb.cpp:745
~Lsdb()
Definition: lsdb.cpp:70
static const ndn::Name::Component NAME_COMPONENT
Definition: lsdb.hpp:368
Copyright (c) 2014-2018, 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:370
bool installNameLsa(NameLsa &nlsa)
Installs a name LSA into the LSDB.
Definition: lsdb.cpp:196
bool buildAndInstallOwnNameLsa()
Builds a name LSA for this router and then installs it into the LSDB.
Definition: lsdb.cpp:145
bool buildAndInstallOwnCoordinateLsa()
Builds a cor. LSA for this router and installs it into the LSDB.
Definition: lsdb.cpp:364
const std::list< AdjLsa > & getAdjLsdb() const
Definition: lsdb.cpp:770
AdjLsa * findAdjLsa(const ndn::Name &key)
Finds an adj. LSA in the LSDB.
Definition: lsdb.cpp:639
bool buildAndInstallOwnAdjLsa()
Wrapper event to build and install an adj. LSA for this router.
Definition: lsdb.cpp:726
bool removeCoordinateLsa(const ndn::Name &key)
Removes a cor. LSA from the LSDB.
Definition: lsdb.cpp:498