blob: e2d0a2b5a4ba7b7e9863532a2272d45ca4b26eb5 [file] [log] [blame]
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2020, 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/tlv-nlsr.hpp"
24
25namespace nlsr {
26
27AdjLsa::AdjLsa(const ndn::Name& originRouter, uint32_t seqNo,
28 const ndn::time::system_clock::TimePoint& 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
40AdjLsa::AdjLsa(const ndn::Block& block)
41{
42 wireDecode(block);
43}
44
45bool
46AdjLsa::isEqualContent(const AdjLsa& alsa) const
47{
48 return m_adl == alsa.getAdl();
49}
50
51template<ndn::encoding::Tag TAG>
52size_t
53AdjLsa::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(ndn::tlv::nlsr::AdjacencyLsa);
66
67 return totalLength;
68}
69
70NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(AdjLsa);
71
72const ndn::Block&
73AdjLsa::wireEncode() const
74{
75 if (m_wire.hasWire() && m_baseWire.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
90void
91AdjLsa::wireDecode(const ndn::Block& wire)
92{
93 m_wire = wire;
94
95 if (m_wire.type() != ndn::tlv::nlsr::AdjacencyLsa) {
96 BOOST_THROW_EXCEPTION(Error("Expected AdjacencyLsa Block, but Block is of a different type: #" +
97 ndn::to_string(m_wire.type())));
98 }
99
100 m_wire.parse();
101
102 ndn::Block::element_const_iterator val = m_wire.elements_begin();
103
104 if (val != m_wire.elements_end() && val->type() == ndn::tlv::nlsr::Lsa) {
105 Lsa::wireDecode(*val);
106 ++val;
107 }
108 else {
109 BOOST_THROW_EXCEPTION(Error("Missing required Lsa field"));
110 }
111
112 AdjacencyList adl;
113 for (; val != m_wire.elements_end(); ++val) {
114 if (val->type() == ndn::tlv::nlsr::Adjacency) {
115 Adjacent adj = Adjacent(*val);
116 adl.insert(adj);
117 }
118 else {
119 BOOST_THROW_EXCEPTION(Error("Expected Adjacency Block, but Block is of a different type: #" +
120 ndn::to_string(m_wire.type())));
121 }
122 }
123 m_adl = adl;
124}
125
126std::ostream&
127operator<<(std::ostream& os, const AdjLsa& lsa)
128{
129 os << lsa.toString();
130 os << " Adjacents:\n";
131
132 int adjacencyIndex = 0;
133
134 for (const Adjacent& adjacency : lsa.getAdl()) {
135 os << " Adjacent " << adjacencyIndex++
136 << ": (name=" << adjacency.getName()
137 << ", uri=" << adjacency.getFaceUri()
138 << ", cost=" << adjacency.getLinkCost() << ")\n";
139 }
140
141 return os;
142}
143
144} // namespace nlsr