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