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 template<class T>
38 {
39 public:
40  void
42  {
43  }
44 };
45 
46 SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew,
47  const ConfParameter& conf)
48  : onNewLsa(std::make_unique<OnNewLsa>())
49  , m_syncFace(face)
50  , m_isLsaNew(isLsaNew)
51  , m_confParam(conf)
52 {
53  createSyncLogic(conf.getSyncPrefix());
54 }
55 
56 void
57 SyncLogicHandler::createSyncLogic(const ndn::Name& syncPrefix, const ndn::time::milliseconds& syncInterestLifetime)
58 {
59  if (m_syncLogic != nullptr) {
60  NLSR_LOG_WARN("Trying to create Sync Logic object, but Sync Logic object already exists");
61  return;
62  }
63 
64  // Build LSA sync update prefix
65  buildUpdatePrefix();
66 
67  NLSR_LOG_DEBUG("Creating Sync Logic object. Sync Prefix: " << syncPrefix);
68 
69  // The face's lifetime is managed in main.cpp; Logic should not manage the memory
70  // of the object
71  std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>());
72 
73  m_syncLogic = std::make_shared<SyncProtocolAdapter>(*facePtr,
74  m_confParam.getSyncProtocol(),
75  syncPrefix,
76  m_nameLsaUserPrefix,
77  syncInterestLifetime,
78  std::bind(&SyncLogicHandler::processUpdate, this, _1, _2));
79 
80  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
81  m_syncLogic->addUserNode(m_adjLsaUserPrefix);
82  }
83  else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
84  m_syncLogic->addUserNode(m_coorLsaUserPrefix);
85  }
86  else {
87  m_syncLogic->addUserNode(m_adjLsaUserPrefix);
88  m_syncLogic->addUserNode(m_coorLsaUserPrefix);
89  }
90 }
91 
92 void
93 SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq)
94 {
95  NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
96 
97  int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT);
98  int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT);
99 
100  if (nlsrPosition < 0 || lsaPosition < 0) {
101  NLSR_LOG_WARN("Received malformed sync update");
102  return;
103  }
104 
105  ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
106  ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
107 
108  ndn::Name originRouter = networkName;
109  originRouter.append(routerName);
110 
111  processUpdateFromSync(originRouter, updateName, highSeq);
112 }
113 
114 void
115 SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
116  const ndn::Name& updateName, uint64_t seqNo)
117 {
118  NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
119 
120  // A router should not try to fetch its own LSA
121  if (originRouter != m_confParam.getRouterPrefix()) {
122 
123  Lsa::Type lsaType;
124  std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
125 
126  NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
127  " sequence number than entry in LSDB");
128 
129  if (m_isLsaNew(originRouter, lsaType, seqNo)) {
130  if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
131  m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
132  NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
133  "is enabled. Not going to fetch.");
134  return;
135  }
136 
137  if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
138  m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
139  NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
140  "is enabled. Not going to fetch.");
141  return;
142  }
143  (*onNewLsa)(updateName, seqNo);
144  }
145  }
146 }
147 
148 void
149 SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
150 {
151  if (m_syncLogic == nullptr) {
152  NLSR_LOG_FATAL("Cannot publish routing update; SyncLogic does not exist");
153 
154  BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncLogic does not exist"));
155  }
156 
157  switch (type) {
159  m_syncLogic->publishUpdate(m_adjLsaUserPrefix, seqNo);
160  break;
162  m_syncLogic->publishUpdate(m_coorLsaUserPrefix, seqNo);
163  break;
164  case Lsa::Type::NAME:
165  m_syncLogic->publishUpdate(m_nameLsaUserPrefix, seqNo);
166  break;
167  default:
168  break;
169  }
170 }
171 
172 void
173 SyncLogicHandler::buildUpdatePrefix()
174 {
175  ndn::Name updatePrefix = m_confParam.getLsaPrefix();
176  updatePrefix.append(m_confParam.getSiteName());
177  updatePrefix.append(m_confParam.getRouterName());
178 
179  m_nameLsaUserPrefix = updatePrefix;
180  m_nameLsaUserPrefix.append(std::to_string(Lsa::Type::NAME));
181 
182  m_adjLsaUserPrefix = updatePrefix;
183  m_adjLsaUserPrefix.append(std::to_string(Lsa::Type::ADJACENCY));
184 
185  m_coorLsaUserPrefix = updatePrefix;
186  m_coorLsaUserPrefix.append(std::to_string(Lsa::Type::COORDINATE));
187 }
188 
189 } // 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.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
ndn::util::Signal< SyncLogicHandler, const ndn::Name &, const uint64_t & > OnNewLsa
Definition: signals.hpp:36
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.
const ndn::Name & getSyncPrefix() const
std::function< bool(const ndn::Name &, const Lsa::Type &lsaType, const uint64_t &)> IsLsaNew
const ndn::Name & getLsaPrefix() const
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
uint32_t getSyncProtocol() const
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
const ndn::Name & getRouterName() const
int32_t getHyperbolicState() const
const std::string LSA_COMPONENT
SyncLogicHandler(ndn::Face &face, const IsLsaNew &isLsaNew, const ConfParameter &conf)
const ndn::Name & getSiteName() const
#define NLSR_LOG_FATAL(x)
Definition: logger.hpp:42
void publishRoutingUpdate(const Lsa::Type &type, const uint64_t &seqNo)
Instruct ChronoSync to publish an update.