adj-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 "adj-lsa.hpp"
23 #include "tlv-nlsr.hpp"
24 
25 namespace nlsr {
26 
27 AdjLsa::AdjLsa(const ndn::Name& originRouter, uint64_t seqNo,
28  const ndn::time::system_clock::time_point& timepoint,
29  uint32_t noLink, AdjacencyList& adl)
30  : Lsa(originRouter, seqNo, timepoint)
31  , m_noLink(noLink)
32 {
33  for (const auto& adjacent : adl.getAdjList()) {
34  if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
35  addAdjacent(adjacent);
36  }
37  }
38 }
39 
40 AdjLsa::AdjLsa(const ndn::Block& block)
41 {
42  wireDecode(block);
43 }
44 
45 bool
46 AdjLsa::isEqualContent(const AdjLsa& alsa) const
47 {
48  return m_adl == alsa.getAdl();
49 }
50 
51 template<ndn::encoding::Tag TAG>
52 size_t
53 AdjLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
54 {
55  size_t totalLength = 0;
56 
57  auto list = m_adl.getAdjList();
58  for (auto it = list.rbegin(); it != list.rend(); ++it) {
59  totalLength += it->wireEncode(block);
60  }
61 
62  totalLength += Lsa::wireEncode(block);
63 
64  totalLength += block.prependVarNumber(totalLength);
65  totalLength += block.prependVarNumber(nlsr::tlv::AdjacencyLsa);
66 
67  return totalLength;
68 }
69 
71 
72 const ndn::Block&
74 {
75  if (m_wire.hasWire()) {
76  return m_wire;
77  }
78 
79  ndn::EncodingEstimator estimator;
80  size_t estimatedSize = wireEncode(estimator);
81 
82  ndn::EncodingBuffer buffer(estimatedSize, 0);
83  wireEncode(buffer);
84 
85  m_wire = buffer.block();
86 
87  return m_wire;
88 }
89 
90 void
91 AdjLsa::wireDecode(const ndn::Block& wire)
92 {
93  m_wire = wire;
94 
95  if (m_wire.type() != nlsr::tlv::AdjacencyLsa) {
96  NDN_THROW(Error("AdjacencyLsa", m_wire.type()));
97  }
98 
99  m_wire.parse();
100 
101  auto val = m_wire.elements_begin();
102 
103  if (val != m_wire.elements_end() && val->type() == nlsr::tlv::Lsa) {
104  Lsa::wireDecode(*val);
105  ++val;
106  }
107  else {
108  NDN_THROW(Error("Missing required Lsa field"));
109  }
110 
111  AdjacencyList adl;
112  for (; val != m_wire.elements_end(); ++val) {
113  if (val->type() == nlsr::tlv::Adjacency) {
114  adl.insert(Adjacent(*val));
115  }
116  else {
117  NDN_THROW(Error("Adjacency", val->type()));
118  }
119  }
120  m_adl = adl;
121 }
122 
123 std::string
125 {
126  std::ostringstream os;
127  os << getString();
128  os << " Adjacent(s):\n";
129 
130  int adjacencyIndex = 0;
131 
132  for (const auto& adjacency : m_adl) {
133  os << " Adjacent " << adjacencyIndex++
134  << ": (name=" << adjacency.getName()
135  << ", uri=" << adjacency.getFaceUri()
136  << ", cost=" << adjacency.getLinkCost() << ")\n";
137  }
138 
139  return os.str();
140 }
141 
142 std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
143 AdjLsa::update(const std::shared_ptr<Lsa>& lsa)
144 {
145  auto alsa = std::static_pointer_cast<AdjLsa>(lsa);
146  if (!isEqualContent(*alsa)) {
147  resetAdl();
148  for (const auto& adjacent : alsa->getAdl()) {
149  addAdjacent(adjacent);
150  }
151  return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
152  }
153  return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
154 }
155 
156 std::ostream&
157 operator<<(std::ostream& os, const AdjLsa& lsa)
158 {
159  return os << lsa.toString();
160 }
161 
162 } // namespace nlsr
Data abstraction for AdjLsa AdjacencyLsa := ADJACENCY-LSA-TYPE TLV-LENGTH Lsa Adjacency*.
Definition: adj-lsa.hpp:38
std::tuple< bool, std::list< ndn::Name >, std::list< ndn::Name > > update(const std::shared_ptr< Lsa > &lsa) override
Definition: adj-lsa.cpp:143
AdjLsa()=default
const ndn::Block & wireEncode() const override
Definition: adj-lsa.cpp:73
void wireDecode(const ndn::Block &wire)
Definition: adj-lsa.cpp:91
bool isEqualContent(const AdjLsa &alsa) const
Definition: adj-lsa.cpp:46
void addAdjacent(Adjacent adj)
Definition: adj-lsa.hpp:76
void resetAdl()
Definition: adj-lsa.hpp:69
std::string toString() const override
Definition: adj-lsa.cpp:124
const AdjacencyList & getAdl() const
Definition: adj-lsa.hpp:63
bool insert(const Adjacent &adjacent)
std::list< Adjacent > & getAdjList()
A neighbor reachable over a Face.
Definition: adjacent.hpp:47
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
@ AdjacencyLsa
Definition: tlv-nlsr.hpp:35
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)