name-lsa.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2023, The University of Memphis,
4  * Regents of the University of California,
5  * Arizona Board of Regents.
6  *
7  * This file is part of NLSR (Named-data Link State Routing).
8  * See AUTHORS.md for complete list of NLSR authors and contributors.
9  *
10  * NLSR is free software: you can redistribute it and/or modify it under the terms
11  * of the GNU General Public License as published by the Free Software Foundation,
12  * either version 3 of the License, or (at your option) any later version.
13  *
14  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  * PURPOSE. See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "name-lsa.hpp"
23 #include "tlv-nlsr.hpp"
24 
25 namespace nlsr {
26 
27 NameLsa::NameLsa(const ndn::Name& originRouter, uint64_t seqNo,
28  const ndn::time::system_clock::time_point& timepoint,
29  const NamePrefixList& npl)
30  : Lsa(originRouter, seqNo, timepoint)
31 {
32  for (const auto& name : npl.getNames()) {
33  addName(name);
34  }
35 }
36 
37 NameLsa::NameLsa(const ndn::Block& block)
38 {
39  wireDecode(block);
40 }
41 
42 template<ndn::encoding::Tag TAG>
43 size_t
44 NameLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
45 {
46  size_t totalLength = 0;
47 
48  auto names = m_npl.getNames();
49 
50  for (auto it = names.rbegin(); it != names.rend(); ++it) {
51  totalLength += it->wireEncode(block);
52  }
53 
54  totalLength += Lsa::wireEncode(block);
55 
56  totalLength += block.prependVarNumber(totalLength);
57  totalLength += block.prependVarNumber(nlsr::tlv::NameLsa);
58 
59  return totalLength;
60 }
61 
63 
64 const ndn::Block&
66 {
67  if (m_wire.hasWire()) {
68  return m_wire;
69  }
70 
71  ndn::EncodingEstimator estimator;
72  size_t estimatedSize = wireEncode(estimator);
73 
74  ndn::EncodingBuffer buffer(estimatedSize, 0);
75  wireEncode(buffer);
76 
77  m_wire = buffer.block();
78 
79  return m_wire;
80 }
81 
82 void
83 NameLsa::wireDecode(const ndn::Block& wire)
84 {
85  m_wire = wire;
86 
87  if (m_wire.type() != nlsr::tlv::NameLsa) {
88  NDN_THROW(Error("NameLsa", m_wire.type()));
89  }
90 
91  m_wire.parse();
92 
93  auto val = m_wire.elements_begin();
94 
95  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
96  Lsa::wireDecode(*val);
97  ++val;
98  }
99  else {
100  NDN_THROW(Error("Missing required Lsa field"));
101  }
102 
103  NamePrefixList npl;
104  for (; val != m_wire.elements_end(); ++val) {
105  if (val->type() == ndn::tlv::Name) {
106  npl.insert(ndn::Name(*val));
107  }
108  else {
109  NDN_THROW(Error("Name", val->type()));
110  }
111  }
112  m_npl = npl;
113 }
114 
115 bool
116 NameLsa::isEqualContent(const NameLsa& other) const
117 {
118  return m_npl == other.getNpl();
119 }
120 
121 std::string
123 {
124  std::ostringstream os;
125  os << getString();
126  os << " Names:\n";
127  int i = 0;
128  for (const auto& name : m_npl.getNames()) {
129  os << " Name " << i++ << ": " << name << "\n";
130  }
131 
132  return os.str();
133 }
134 
135 std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
136 NameLsa::update(const std::shared_ptr<Lsa>& lsa)
137 {
138  auto nlsa = std::static_pointer_cast<NameLsa>(lsa);
139  bool updated = false;
140 
141  m_npl.sort();
142  nlsa->getNpl().sort();
143 
144  // Obtain the set difference of the current and the incoming
145  // name prefix sets, and add those.
146  std::list<ndn::Name> newNames = nlsa->getNpl().getNames();
147  std::list<ndn::Name> oldNames = m_npl.getNames();
148  std::list<ndn::Name> namesToAdd;
149  std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
150  std::inserter(namesToAdd, namesToAdd.begin()));
151  for (const auto& name : namesToAdd) {
152  addName(name);
153  updated = true;
154  }
155 
156  m_npl.sort();
157 
158  // Also remove any names that are no longer being advertised.
159  std::list<ndn::Name> namesToRemove;
160  std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
161  std::inserter(namesToRemove, namesToRemove.begin()));
162  for (const auto& name : namesToRemove) {
163  removeName(name);
164  updated = true;
165  }
166 
167  return {updated, namesToAdd, namesToRemove};
168 }
169 
170 std::ostream&
171 operator<<(std::ostream& os, const NameLsa& lsa)
172 {
173  return os << lsa.toString();
174 }
175 
176 } // namespace nlsr
Data abstraction for Lsa Lsa := LSA-TYPE TLV-LENGTH Name SequenceNumber ExpirationTimePoint.
Definition: lsa.hpp:42
virtual const ndn::Block & wireEncode() const =0
std::string getString() const
Definition: lsa.cpp:144
ndn::Block m_wire
Definition: lsa.hpp:144
void wireDecode(const ndn::Block &wire)
Definition: lsa.cpp:68
Data abstraction for NameLsa NameLsa := NAME-LSA-TYPE TLV-LENGTH Lsa Name+.
Definition: name-lsa.hpp:36
NamePrefixList & getNpl()
Definition: name-lsa.hpp:59
NameLsa()=default
std::string toString() const override
Definition: name-lsa.cpp:122
void addName(const ndn::Name &name)
Definition: name-lsa.hpp:71
std::tuple< bool, std::list< ndn::Name >, std::list< ndn::Name > > update(const std::shared_ptr< Lsa > &lsa) override
Definition: name-lsa.cpp:136
void removeName(const ndn::Name &name)
Definition: name-lsa.hpp:78
void wireDecode(const ndn::Block &wire)
Definition: name-lsa.cpp:83
const ndn::Block & wireEncode() const override
Definition: name-lsa.cpp:65
bool isEqualContent(const NameLsa &other) const
Definition: name-lsa.cpp:116
bool insert(const ndn::Name &name, const std::string &source="")
inserts name into NamePrefixList
std::list< ndn::Name > getNames() const
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
std::ostream & operator<<(std::ostream &os, const Adjacent &adjacent)
Definition: adjacent.cpp:176
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)