sync-logic-handler.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, 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 #include "sync-logic-handler.hpp"
23 #include "hello-protocol.hpp"
24 #include "logger.hpp"
25 #include "utility/name-helper.hpp"
26 
27 namespace nlsr {
28 
29 INIT_LOGGER(SyncLogicHandler);
30 
31 const std::string LSA_COMPONENT{"LSA"};
32 
33 SyncLogicHandler::SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
34  IsLsaNew isLsaNew, const SyncLogicOptions& opts)
35  : m_isLsaNew(std::move(isLsaNew))
36  , m_routerPrefix(opts.routerPrefix)
37  , m_hyperbolicState(opts.hyperbolicState)
38  , m_nameLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::NAME))
39  , m_adjLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::ADJACENCY))
40  , m_coorLsaUserPrefix(makeLsaUserPrefix(opts.userPrefix, Lsa::Type::COORDINATE))
41  , m_syncLogic(face, keyChain, opts.syncProtocol, opts.syncPrefix,
42  m_nameLsaUserPrefix, opts.syncInterestLifetime,
43  std::bind(&SyncLogicHandler::processUpdate, this, _1, _2, _3))
44 {
45  if (m_hyperbolicState != HYPERBOLIC_STATE_ON) {
46  m_syncLogic.addUserNode(m_adjLsaUserPrefix);
47  }
48 
49  if (m_hyperbolicState != HYPERBOLIC_STATE_OFF) {
50  m_syncLogic.addUserNode(m_coorLsaUserPrefix);
51  }
52 }
53 
54 void
55 SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq, uint64_t incomingFaceId)
56 {
57  NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
58 
59  int32_t nlsrPosition = util::getNameComponentPosition(updateName, HelloProtocol::NLSR_COMPONENT);
60  int32_t lsaPosition = util::getNameComponentPosition(updateName, LSA_COMPONENT);
61 
62  if (nlsrPosition < 0 || lsaPosition < 0) {
63  NLSR_LOG_WARN("Received malformed sync update");
64  return;
65  }
66 
67  ndn::Name networkName = updateName.getSubName(1, nlsrPosition - 1);
68  ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
69  ndn::Name originRouter = networkName;
70  originRouter.append(routerName);
71 
72  processUpdateFromSync(originRouter, updateName, highSeq, incomingFaceId);
73 }
74 
75 void
76 SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
77  const ndn::Name& updateName, uint64_t seqNo,
78  uint64_t incomingFaceId)
79 {
80  NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
81 
82  if (originRouter == m_routerPrefix) {
83  // A router should not try to fetch its own LSA
84  return;
85  }
86 
87  auto lsaType = boost::lexical_cast<Lsa::Type>(updateName.get(-1).toUri());
88  NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
89  " sequence number than entry in LSDB");
90 
91  if (m_isLsaNew(originRouter, lsaType, seqNo, incomingFaceId)) {
92  if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
93  m_hyperbolicState == HYPERBOLIC_STATE_ON) {
94  NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing "
95  "is enabled. Not going to fetch.");
96  return;
97  }
98 
99  if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
100  m_hyperbolicState == HYPERBOLIC_STATE_OFF) {
101  NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state "
102  "is enabled. Not going to fetch.");
103  return;
104  }
105 
106  onNewLsa(updateName, seqNo, originRouter, incomingFaceId);
107  }
108 }
109 
110 void
112 {
113  switch (type) {
115  m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
116  break;
118  m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
119  break;
120  case Lsa::Type::NAME:
121  m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
122  break;
123  default:
124  break;
125  }
126 }
127 
128 } // namespace nlsr
static const std::string NLSR_COMPONENT
Represents a Link State Announcement (LSA).
Definition: lsa.hpp:46
NLSR-to-sync interaction point.
void publishRoutingUpdate(Lsa::Type type, uint64_t seqNo)
Instruct ChronoSync to publish an update.
std::function< bool(const ndn::Name &routerName, Lsa::Type lsaType, uint64_t seqNo, uint64_t inFace) > IsLsaNew
SyncLogicHandler(ndn::Face &face, ndn::KeyChain &keyChain, IsLsaNew isLsaNew, const SyncLogicOptions &opts)
void publishUpdate(const ndn::Name &userPrefix, uint64_t seq)
Publish update to Sync.
void addUserNode(const ndn::Name &userPrefix)
Add user node to Sync.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_WARN(x)
Definition: logger.hpp:40
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
int32_t getNameComponentPosition(const ndn::Name &name, const std::string &searchString)
search a name component in ndn::Name and return the position of the component
Definition: name-helper.hpp:38
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
const std::string LSA_COMPONENT
ndn::Name makeLsaUserPrefix(const ndn::Name &userPrefix, Lsa::Type lsaType)
@ HYPERBOLIC_STATE_ON
@ HYPERBOLIC_STATE_OFF