lsa.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "lsa.hpp"
23 #include "nlsr.hpp"
24 #include "name-prefix-list.hpp"
25 #include "adjacent.hpp"
26 #include "logger.hpp"
27 
28 #include <string>
29 #include <iostream>
30 #include <sstream>
31 #include <algorithm>
32 #include <cmath>
33 #include <limits>
34 #include <boost/algorithm/string.hpp>
35 
36 namespace nlsr {
37 
38 INIT_LOGGER("Lsa");
39 
40 std::string
41 Lsa::getData() const
42 {
43  std::ostringstream os;
44  os << m_origRouter << "|" << getType() << "|" << m_lsSeqNo << "|"
45  << ndn::time::toIsoString(m_expirationTimePoint) << "|";
46  return os.str();
47 }
48 
49 const ndn::Name
50 Lsa::getKey() const
51 {
52  return ndn::Name(m_origRouter).append(std::to_string(getType()));
53 }
54 
55 bool
56 Lsa::deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator)
57 {
58  m_origRouter = ndn::Name(*iterator++);
59  if (m_origRouter.size() <= 0)
60  return false;
61  if (*iterator++ != std::to_string(getType()))
62  return false;
63  m_lsSeqNo = boost::lexical_cast<uint32_t>(*iterator++);
64  m_expirationTimePoint = ndn::time::fromIsoString(*iterator++);
65  return true;
66 }
67 
68 NameLsa::NameLsa(const ndn::Name& origR, uint32_t lsn,
69  const ndn::time::system_clock::TimePoint& lt,
70  NamePrefixList& npl)
71 {
72  m_origRouter = origR;
73  m_lsSeqNo = lsn;
75  for (const auto& name : npl.getNames()) {
76  addName(name);
77  }
78 }
79 
80 std::string
82 {
83  std::ostringstream os;
84  os << getData() << m_npl.size();
85  for (const auto& name : m_npl.getNames()) {
86  os << "|" << name;
87  }
88  os << "|";
89  return os.str();
90 }
91 
92 bool
93 NameLsa::deserialize(const std::string& content) noexcept
94 {
95  uint32_t numName = 0;
96  boost::char_separator<char> sep("|");
97  boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
98  boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
99  tokens.begin();
100 
101  try {
102  if (!deserializeCommon(tok_iter))
103  return false;
104  numName = boost::lexical_cast<uint32_t>(*tok_iter++);
105  for (uint32_t i = 0; i < numName; i++) {
106  ndn::Name name(*tok_iter++);
107  addName(name);
108  }
109  }
110  catch (const std::exception& e) {
111  NLSR_LOG_ERROR("Could not deserialize from content: " << e.what());
112  return false;
113  }
114  return true;
115 }
116 
117 bool
118 NameLsa::isEqualContent(const NameLsa& other) const
119 {
120  return m_npl == other.getNpl();
121 }
122 
123 void
125 {
126  NLSR_LOG_DEBUG(*this);
127 }
128 
129 CoordinateLsa::CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
130  const ndn::time::system_clock::TimePoint& lt,
131  double r, std::vector<double> theta)
132 {
133  m_origRouter = origR;
134  m_lsSeqNo = lsn;
136  m_corRad = r;
137  m_angles = theta;
138 }
139 
140 bool
142 {
143  if (clsa.getCorTheta().size() != m_angles.size()) {
144  return false;
145  }
146 
147  std::vector<double> m_angles2 = clsa.getCorTheta();
148  for (unsigned int i = 0; i < clsa.getCorTheta().size(); i++) {
149  if (std::abs(m_angles[i] - m_angles2[i]) > std::numeric_limits<double>::epsilon()) {
150  return false;
151  }
152  }
153 
154  return (std::abs(m_corRad - clsa.getCorRadius()) <
155  std::numeric_limits<double>::epsilon());
156 }
157 
158 std::string
160 {
161  std::ostringstream os;
162  os << getData() << m_corRad << "|" << m_angles.size() << "|";
163  for (const auto& angle: m_angles) {
164  os << angle << "|";
165  }
166  return os.str();
167 }
168 
169 bool
170 CoordinateLsa::deserialize(const std::string& content) noexcept
171 {
172  boost::char_separator<char> sep("|");
173  boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
174  boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
175  tokens.begin();
176 
177  try {
178  if (!deserializeCommon(tok_iter))
179  return false;
180  m_corRad = boost::lexical_cast<double>(*tok_iter++);
181  int numAngles = boost::lexical_cast<uint32_t>(*tok_iter++);
182  for (int i = 0; i < numAngles; i++) {
183  m_angles.push_back(boost::lexical_cast<double>(*tok_iter++));
184  }
185  }
186  catch (const std::exception& e) {
187  NLSR_LOG_ERROR("Could not deserialize from content: " << e.what());
188  return false;
189  }
190  return true;
191 }
192 
193 void
195 {
196  NLSR_LOG_DEBUG(*this);
197 }
198 
199 AdjLsa::AdjLsa(const ndn::Name& origR, uint32_t lsn,
200  const ndn::time::system_clock::TimePoint& lt,
201  uint32_t nl , AdjacencyList& adl)
202 {
203  m_origRouter = origR;
204  m_lsSeqNo = lsn;
206  m_noLink = nl;
207  std::list<Adjacent> al = adl.getAdjList();
208  for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
209  if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
210  addAdjacent((*it));
211  }
212  }
213 }
214 
215 bool
217 {
218  return m_adl == alsa.getAdl();
219 }
220 
221 std::string
223 {
224  std::ostringstream os;
225  os << getData() << m_adl.size();
226  for (const auto& adjacent : m_adl.getAdjList()) {
227  os << "|" << adjacent.getName() << "|" << adjacent.getFaceUri()
228  << "|" << adjacent.getLinkCost();
229  }
230  os << "|";
231  return os.str();
232 }
233 
234 bool
235 AdjLsa::deserialize(const std::string& content) noexcept
236 {
237  uint32_t numLink = 0;
238  boost::char_separator<char> sep("|");
239  boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
240  boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
241  tokens.begin();
242 
243  try {
244  if (!deserializeCommon(tok_iter))
245  return false;
246  numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
247  for (uint32_t i = 0; i < numLink; i++) {
248  ndn::Name adjName(*tok_iter++);
249  std::string connectingFaceUri(*tok_iter++);
250  double linkCost = boost::lexical_cast<double>(*tok_iter++);
251  Adjacent adjacent(adjName, ndn::FaceUri(connectingFaceUri), linkCost,
253  addAdjacent(adjacent);
254  }
255  }
256  catch (const std::exception& e) {
257  NLSR_LOG_ERROR("Could not deserialize from content: " << e.what());
258  return false;
259  }
260  return true;
261 }
262 
263 void
265 {
266  // Only add NPT entries if this is an adj LSA from another router.
267  if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
268  // Pass the originating router as both the name to register and
269  // where it came from.
271  }
272 }
273 
274 
275 void
277 {
278  if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
280  }
281 }
282 
283 void
285 {
286  NLSR_LOG_DEBUG(*this);
287 }
288 
289 std::ostream&
290 operator<<(std::ostream& os, const AdjLsa& lsa)
291 {
292  os << lsa.toString();
293  os << "-Adjacents:";
294 
295  int adjacencyIndex = 1;
296 
297  for (const Adjacent& adjacency : lsa.m_adl) {
298  os << "--Adjacent" << adjacencyIndex++ << ":\n"
299  << "---Adjacent Name: " << adjacency.getName() << "\n"
300  << "---Connecting FaceUri: " << adjacency.getFaceUri() << "\n"
301  << "---Link Cost: " << adjacency.getLinkCost() << "\n";
302  }
303  os << "adj_lsa_end";
304 
305  return os;
306 }
307 
308 std::ostream&
309 operator<<(std::ostream& os, const CoordinateLsa& lsa)
310 {
311  os << lsa.toString();
312  os << "--Hyperbolic Radius: " << lsa.m_corRad << "\n";
313  int i = 0;
314  for (const auto& value : lsa.m_angles) {
315  os << "---Hyperbolic Theta: " << i++ << ": " << value << "\n";
316  }
317  os << "cor_lsa_end";
318 
319  return os;
320 }
321 
322 std::ostream&
323 operator<<(std::ostream& os, const NameLsa& lsa)
324 {
325  os << lsa.toString();
326  os << "--Names:\n";
327  int i = 0;
328  auto names = lsa.m_npl.getNames();
329  for (const auto& name : names) {
330  os << "---Name " << i++ << ": " << name << "\n";
331  }
332  os << "name_lsa_end";
333 
334  return os;
335 }
336 
337 std::ostream&
338 operator<<(std::ostream& os, const Lsa::Type& type)
339 {
340  os << std::to_string(type);
341  return os;
342 }
343 
344 std::istream&
345 operator>>(std::istream& is, Lsa::Type& type)
346 {
347  std::string typeString;
348  is >> typeString;
349  if (typeString == "ADJACENCY") {
350  type = Lsa::Type::ADJACENCY;
351  }
352  else if (typeString == "COORDINATE") {
353  type = Lsa::Type::COORDINATE;
354  }
355  else if (typeString == "NAME") {
356  type = Lsa::Type::NAME;
357  }
358  else {
359  type = Lsa::Type::BASE;
360  }
361  return is;
362 }
363 
364 std::string
366 {
367  std::ostringstream os;
368  os << "LSA of type " << getType() << ":\n-Origin Router: " << getOrigRouter()
369  << "\n-Sequence Number: " << getLsSeqNo() << "\n-Expiration Point: "
370  << getExpirationTimePoint() << "\n";
371  return os.str();
372 }
373 
374 } // namespace nlsr
375 
376 namespace std {
377 std::string
379 {
380  switch (type) {
382  return "ADJACENCY";
384  return "COORDINATE";
386  return "NAME";
388  return "MOCK";
389  default:
390  return "BASE";
391  }
392 }
393 
394 } // namespace std
ndn::time::system_clock::TimePoint m_expirationTimePoint
Definition: lsa.hpp:154
std::string getData() const
Definition: lsa.cpp:41
std::string to_string(const nlsr::Lsa::Type &type)
Definition: lsa.cpp:378
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:83
ConfParameter & getConfParameter()
Definition: nlsr.hpp:133
const ndn::time::system_clock::TimePoint & getExpirationTimePoint() const
Definition: lsa.hpp:88
const ndn::FaceUri & getFaceUri() const
Definition: adjacent.hpp:69
std::string serialize() const override
Returns the data this adjacency LSA has.
Definition: lsa.cpp:222
std::list< ndn::Name > getNames() const
AdjacencyList & getAdl()
Definition: lsa.hpp:252
ndn::Name m_origRouter
Definition: lsa.hpp:152
bool deserialize(const std::string &content) noexceptoverride
Initializes this LSA object with content&#39;s data.
Definition: lsa.cpp:93
void writeLog() const override
Definition: lsa.cpp:284
NamePrefixTable & getNamePrefixTable()
Definition: nlsr.hpp:169
NamePrefixList & getNpl()
Definition: lsa.hpp:176
bool isEqualContent(AdjLsa &alsa)
Definition: lsa.cpp:216
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:41
bool deserializeCommon(boost::tokenizer< boost::char_separator< char >>::iterator &iterator)
Definition: lsa.cpp:56
const ndn::Name & getRouterPrefix() const
STL namespace.
void writeLog() const override
Definition: lsa.cpp:194
bool deserialize(const std::string &content) noexceptoverride
Initializes this adj. LSA from the supplied content.
Definition: lsa.cpp:235
std::string toString() const
Definition: lsa.cpp:365
Copyright (c) 2014-2017, The University of Memphis, Regents of the University of California.
void removeNptEntries(Nlsr &pnlsr)
Definition: lsa.cpp:276
#define INIT_LOGGER(name)
Definition: logger.hpp:35
void removeEntry(const ndn::Name &name, const ndn::Name &destRouter)
Removes a destination from a name prefix table entry.
const ndn::Name & getOrigRouter() const
Definition: lsa.hpp:76
const ndn::Name & getName() const
Definition: adjacent.hpp:57
uint32_t getLsSeqNo() const
Definition: lsa.hpp:70
void writeLog() const override
Definition: lsa.cpp:124
uint32_t m_lsSeqNo
Definition: lsa.hpp:153
bool isEqualContent(const NameLsa &other) const
Definition: lsa.cpp:118
virtual Type getType() const
Definition: lsa.hpp:58
std::string serialize() const override
Returns the data that this name LSA has.
Definition: lsa.cpp:81
A neighbor reachable over a Face.
Definition: adjacent.hpp:38
bool isEqualContent(const CoordinateLsa &clsa)
Definition: lsa.cpp:141
#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 std::vector< double > getCorTheta() const
Definition: lsa.hpp:372
std::istream & operator>>(std::istream &is, Lsa::Type &type)
Definition: lsa.cpp:345
void addNptEntries(Nlsr &pnlsr)
Installs this LSA&#39;s name prefixes into the NPT.
Definition: lsa.cpp:264
const ndn::Name getKey() const
Gets the key for this LSA.
Definition: lsa.cpp:50
bool deserialize(const std::string &content) noexceptoverride
Initializes this coordinate LSA with the data in content.
Definition: lsa.cpp:170
uint64_t getLinkCost() const
Definition: adjacent.hpp:81
std::string serialize() const override
Returns the data that this coordinate LSA represents.
Definition: lsa.cpp:159
std::list< Adjacent > & getAdjList()
double getCorRadius() const
Definition: lsa.hpp:360
void addEntry(const ndn::Name &name, const ndn::Name &destRouter)
Adds a destination to the specified name prefix.