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