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-2022, 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 #include <boost/lexical_cast.hpp>
28 
29 namespace nlsr {
30 
31 INIT_LOGGER(SyncLogicHandler);
32 
33 const std::string LSA_COMPONENT{"LSA"};
34 
35 SyncLogicHandler::SyncLogicHandler(ndn::Face& face, ndn::KeyChain& keyChain,
36  IsLsaNew isLsaNew, const ConfParameter& conf)
37  : m_isLsaNew(std::move(isLsaNew))
38  , m_confParam(conf)
39  , m_nameLsaUserPrefix(ndn::Name(m_confParam.getSyncUserPrefix()).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)))
40  , m_syncLogic(face, keyChain, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
41  m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
42  std::bind(&SyncLogicHandler::processUpdate, this, _1, _2, _3))
43 {
44  m_adjLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
45  .append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY));
46  m_coorLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
47  .append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE));
48 
49  if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_ON) {
50  m_syncLogic.addUserNode(m_adjLsaUserPrefix);
51  }
52 
53  if (m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
54  m_syncLogic.addUserNode(m_coorLsaUserPrefix);
55  }
56 }
57 
58 void
59 SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq, uint64_t incomingFaceId)
60 {
61  NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
62 
63  int32_t nlsrPosition = util::getNameComponentPosition(updateName, HelloProtocol::NLSR_COMPONENT);
64  int32_t lsaPosition = util::getNameComponentPosition(updateName, LSA_COMPONENT);
65 
66  if (nlsrPosition < 0 || lsaPosition < 0) {
67  NLSR_LOG_WARN("Received malformed sync update");
68  return;
69  }
70 
71  ndn::Name networkName = updateName.getSubName(1, nlsrPosition - 1);
72  ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
73  ndn::Name originRouter = networkName;
74  originRouter.append(routerName);
75 
76  processUpdateFromSync(originRouter, updateName, highSeq, incomingFaceId);
77 }
78 
79 void
80 SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
81  const ndn::Name& updateName, uint64_t seqNo,
82  uint64_t incomingFaceId)
83 {
84  NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
85 
86  // A router should not try to fetch its own LSA
87  if (originRouter != m_confParam.getRouterPrefix()) {
88 
89  Lsa::Type lsaType;
90  std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
91 
92  NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
93  " sequence number than entry in LSDB");
94 
95  if (m_isLsaNew(originRouter, lsaType, seqNo, incomingFaceId)) {
96  if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
97  m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
98  NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
99  "is enabled. Not going to fetch.");
100  return;
101  }
102 
103  if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
104  m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
105  NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
106  "is enabled. Not going to fetch.");
107  return;
108  }
109 
110  onNewLsa(updateName, seqNo, originRouter, incomingFaceId);
111  }
112  }
113 }
114 
115 void
116 SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
117 {
118  switch (type) {
120  m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
121  break;
123  m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
124  break;
125  case Lsa::Type::NAME:
126  m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
127  break;
128  default:
129  break;
130  }
131 }
132 
133 } // namespace nlsr
A class to house all the configuration parameters for NLSR.
int32_t getHyperbolicState() const
const ndn::Name & getSyncUserPrefix() const
const ndn::Name & getRouterPrefix() const
static const std::string NLSR_COMPONENT
Data abstraction for Lsa Lsa := LSA-TYPE TLV-LENGTH Name SequenceNumber ExpirationTimePoint.
Definition: lsa.hpp:42
NLSR-to-ChronoSync interaction point.
void publishRoutingUpdate(const Lsa::Type &type, const uint64_t &seqNo)
Instruct ChronoSync to publish an update.
SyncLogicHandler(ndn::Face &face, ndn::KeyChain &keyChain, IsLsaNew isLsaNew, const ConfParameter &conf)
std::function< bool(const ndn::Name &, const Lsa::Type &lsaType, const uint64_t &, uint64_t)> IsLsaNew
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
@ HYPERBOLIC_STATE_ON
@ HYPERBOLIC_STATE_OFF