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 "utility/name-helper.hpp"
27 #include "logger.hpp"
28 
29 namespace nlsr {
30 
31 INIT_LOGGER("SyncLogicHandler");
32 
33 const std::string NLSR_COMPONENT = "NLSR";
34 const std::string LSA_COMPONENT = "LSA";
35 
36 template<class T>
38 {
39 public:
40  void
42  {
43  }
44 };
45 
46 SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew, ConfParameter& conf)
47  : onNewLsa(ndn::make_unique<OnNewLsa>())
48  , m_syncFace(face)
49  , m_isLsaNew(isLsaNew)
50  , m_confParam(conf)
51 {
52 }
53 
54 void
55 SyncLogicHandler::createSyncSocket(const ndn::Name& syncPrefix)
56 {
57  if (m_syncSocket != nullptr) {
58  NLSR_LOG_WARN("Trying to create Sync socket, but Sync socket already exists");
59  return;
60  }
61 
62  m_syncPrefix = syncPrefix;
63 
64  // Build LSA sync update prefix
65  buildUpdatePrefix();
66 
67  NLSR_LOG_DEBUG("Creating Sync socket. Sync Prefix: " << m_syncPrefix);
68 
69  // The face's lifetime is managed in main.cpp; SyncSocket should not manage the memory
70  // of the object
71  std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>());
72 
73  m_syncSocket = std::make_shared<chronosync::Socket>(m_syncPrefix, m_nameLsaUserPrefix, *facePtr,
74  std::bind(&SyncLogicHandler::onChronoSyncUpdate, this, _1));
75 
76  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
77  m_syncSocket->addSyncNode(m_adjLsaUserPrefix);
78  }
79  else {
80  m_syncSocket->addSyncNode(m_coorLsaUserPrefix);
81  }
82 }
83 
84 void
85 SyncLogicHandler::onChronoSyncUpdate(const std::vector<chronosync::MissingDataInfo>& v)
86 {
87  NLSR_LOG_DEBUG("Received ChronoSync update event");
88 
89  for (size_t i = 0; i < v.size(); i++){
90  ndn::Name updateName = v[i].session.getPrefix(-1);
91 
92  NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << v[i].high);
93 
94  int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT);
95  int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT);
96 
97  if (nlsrPosition < 0 || lsaPosition < 0) {
98  NLSR_LOG_WARN("Received malformed sync update");
99  return;
100  }
101 
102  ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
103  ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
104 
105  ndn::Name originRouter = networkName;
106  originRouter.append(routerName);
107 
108  processUpdateFromSync(originRouter, updateName, v[i].high);
109  }
110 }
111 
112 void
113 SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
114  const ndn::Name& updateName, const uint64_t& seqNo)
115 {
116  NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
117 
118  // A router should not try to fetch its own LSA
119  if (originRouter != m_confParam.getRouterPrefix()) {
120 
121  Lsa::Type lsaType;
122  std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
123 
124  NLSR_LOG_DEBUG("Received sync update with higher " << lsaType
125  << " sequence number than entry in LSDB");
126 
127  if (m_isLsaNew(originRouter, lsaType, seqNo)) {
128  if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
129  m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
130  NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing"
131  << " is enabled. Not going to fetch.");
132  return;
133  }
134 
135  if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
136  m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
137  NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state"
138  << " is enabled. Not going to fetch.");
139  return;
140  }
141  (*onNewLsa)(updateName, seqNo);
142  }
143  }
144 }
145 
146 void
147 SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
148 {
149  if (m_syncSocket == nullptr) {
150  NLSR_LOG_FATAL("Cannot publish routing update; SyncSocket does not exist");
151 
152  BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncSocket does not exist"));
153  }
154 
155 
156  switch (type) {
158  publishSyncUpdate(m_adjLsaUserPrefix, seqNo);
159  break;
161  publishSyncUpdate(m_coorLsaUserPrefix, seqNo);
162  break;
163  case Lsa::Type::NAME:
164  publishSyncUpdate(m_nameLsaUserPrefix, seqNo);
165  break;
166  default:
167  break;
168  }
169 }
170 
171 void
172 SyncLogicHandler::buildUpdatePrefix()
173 {
174  ndn::Name updatePrefix = m_confParam.getLsaPrefix();
175  updatePrefix.append(m_confParam.getSiteName());
176  updatePrefix.append(m_confParam.getRouterName());
177 
178  m_nameLsaUserPrefix = updatePrefix;
179  m_nameLsaUserPrefix.append(std::to_string(Lsa::Type::NAME));
180 
181  m_adjLsaUserPrefix = updatePrefix;
182  m_adjLsaUserPrefix.append(std::to_string(Lsa::Type::ADJACENCY));
183 
184  m_coorLsaUserPrefix = updatePrefix;
185  m_coorLsaUserPrefix.append(std::to_string(Lsa::Type::COORDINATE));
186 }
187 
188 void
189 SyncLogicHandler::publishSyncUpdate(const ndn::Name& updatePrefix, uint64_t seqNo)
190 {
191  NLSR_LOG_DEBUG("Publishing Sync Update. Prefix: " << updatePrefix << " Seq No: " << seqNo);
192 
193  ndn::Name updateName(updatePrefix);
194  std::string data("NoData");
195 
196  m_syncSocket->publishData(reinterpret_cast<const uint8_t*>(data.c_str()), data.size(),
197  ndn::time::milliseconds(1000), seqNo, updateName);
198 }
199 
200 } // namespace nlsr
const std::string NLSR_COMPONENT
#define NLSR_LOG_WARN(x)
Definition: logger.hpp:47
A class to house all the configuration parameters for NLSR.
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Definition: tlv-nlsr.hpp:27
SyncLogicHandler(ndn::Face &face, const IsLsaNew &isLsaNew, ConfParameter &conf)
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:41
ndn::util::Signal< SyncLogicHandler, const ndn::Name &, const uint64_t & > OnNewLsa
Definition: signals.hpp:36
const ndn::Name & getRouterPrefix() const
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
#define INIT_LOGGER(name)
Definition: logger.hpp:35
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
std::function< bool(const ndn::Name &, const Lsa::Type &lsaType, const uint64_t &)> IsLsaNew
const ndn::Name & getLsaPrefix() const
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:50
Copyright (c) 2014-2017, 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
void createSyncSocket(const ndn::Name &syncPrefix)
Create and configure a socket to enable ChronoSync for this NLSR.
const ndn::Name & getSiteName() const
#define NLSR_LOG_FATAL(x)
Definition: logger.hpp:53
void onChronoSyncUpdate(const std::vector< chronosync::MissingDataInfo > &v)
Hook function to call whenever sync detects new data.
void publishRoutingUpdate(const Lsa::Type &type, const uint64_t &seqNo)
Instruct ChronoSync to publish an update.