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-2024, 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, AdjacencyList& adl)
29  : Lsa(originRouter, seqNo, timepoint)
30 {
31  for (const auto& adjacent : adl.getAdjList()) {
32  if (adjacent.getStatus() == Adjacent::STATUS_ACTIVE) {
33  addAdjacent(adjacent);
34  }
35  }
36 }
37 
38 AdjLsa::AdjLsa(const ndn::Block& block)
39 {
40  wireDecode(block);
41 }
42 
43 template<ndn::encoding::Tag TAG>
44 size_t
45 AdjLsa::wireEncode(ndn::EncodingImpl<TAG>& block) const
46 {
47  size_t totalLength = 0;
48 
49  auto list = m_adl.getAdjList();
50  for (auto it = list.rbegin(); it != list.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::AdjacencyLsa);
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 AdjLsa::wireDecode(const ndn::Block& wire)
84 {
85  m_wire = wire;
86 
87  if (m_wire.type() != nlsr::tlv::AdjacencyLsa) {
88  NDN_THROW(Error("AdjacencyLsa", 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  AdjacencyList adl;
104  for (; val != m_wire.elements_end(); ++val) {
105  if (val->type() == nlsr::tlv::Adjacency) {
106  adl.insert(Adjacent(*val));
107  }
108  else {
109  NDN_THROW(Error("Adjacency", val->type()));
110  }
111  }
112  m_adl = adl;
113 }
114 
115 void
116 AdjLsa::print(std::ostream& os) const
117 {
118  os << " Adjacent(s):\n";
119 
120  int adjacencyIndex = 0;
121  for (const auto& adjacency : m_adl) {
122  os << " Adjacent " << adjacencyIndex++
123  << ": (name=" << adjacency.getName()
124  << ", uri=" << adjacency.getFaceUri()
125  << ", cost=" << adjacency.getLinkCost() << ")\n";
126  }
127 }
128 
129 std::tuple<bool, std::list<ndn::Name>, std::list<ndn::Name>>
130 AdjLsa::update(const std::shared_ptr<Lsa>& lsa)
131 {
132  auto alsa = std::static_pointer_cast<AdjLsa>(lsa);
133  if (*this != *alsa) {
134  resetAdl();
135  for (const auto& adjacent : alsa->getAdl()) {
136  addAdjacent(adjacent);
137  }
138  return {true, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
139  }
140  return {false, std::list<ndn::Name>{}, std::list<ndn::Name>{}};
141 }
142 
143 } // namespace nlsr
Represents an LSA of adjacencies of the origin router in link-state mode.
Definition: adj-lsa.hpp:44
std::tuple< bool, std::list< ndn::Name >, std::list< ndn::Name > > update(const std::shared_ptr< Lsa > &lsa) override
Definition: adj-lsa.cpp:130
AdjLsa()=default
const ndn::Block & wireEncode() const override
Definition: adj-lsa.cpp:65
void wireDecode(const ndn::Block &wire)
Definition: adj-lsa.cpp:83
void resetAdl()
Definition: adj-lsa.hpp:75
void addAdjacent(const Adjacent &adj)
Definition: adj-lsa.hpp:82
bool insert(const Adjacent &adjacent)
std::list< Adjacent > & getAdjList()
A neighbor reachable over a Face.
Definition: adjacent.hpp:47
Represents a Link State Announcement (LSA).
Definition: lsa.hpp:46
virtual const ndn::Block & wireEncode() const =0
ndn::Block m_wire
Definition: lsa.hpp:142
void wireDecode(const ndn::Block &wire)
Definition: lsa.cpp:65
@ AdjacencyLsa
Definition: tlv-nlsr.hpp:35
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Adjacent)