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