ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
fib-entry.cpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2023 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
27
28#include <boost/range/adaptor/reversed.hpp>
29
30namespace ndn::nfd {
31
33
35{
36 this->wireDecode(block);
37}
38
41{
42 m_faceId = faceId;
43 m_wire.reset();
44 return *this;
45}
46
49{
50 m_cost = cost;
51 m_wire.reset();
52 return *this;
53}
54
55template<encoding::Tag TAG>
56size_t
58{
59 size_t totalLength = 0;
60
61 totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost);
62 totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId);
63
64 totalLength += block.prependVarNumber(totalLength);
65 totalLength += block.prependVarNumber(ndn::tlv::nfd::NextHopRecord);
66 return totalLength;
67}
68
70
71const Block&
73{
74 if (m_wire.hasWire())
75 return m_wire;
76
77 EncodingEstimator estimator;
78 size_t estimatedSize = wireEncode(estimator);
79
80 EncodingBuffer buffer(estimatedSize, 0);
81 wireEncode(buffer);
82
83 m_wire = buffer.block();
84 return m_wire;
85}
86
87void
89{
90 if (block.type() != tlv::nfd::NextHopRecord) {
91 NDN_THROW(Error("NextHopRecord", block.type()));
92 }
93
94 m_wire = block;
95 m_wire.parse();
96 auto val = m_wire.elements_begin();
97
98 if (val == m_wire.elements_end()) {
99 NDN_THROW(Error("unexpected end of NextHopRecord"));
100 }
101 else if (val->type() != tlv::nfd::FaceId) {
102 NDN_THROW(Error("FaceId", val->type()));
103 }
104 m_faceId = readNonNegativeInteger(*val);
105 ++val;
106
107 if (val == m_wire.elements_end()) {
108 NDN_THROW(Error("unexpected end of NextHopRecord"));
109 }
110 else if (val->type() != tlv::nfd::Cost) {
111 NDN_THROW(Error("Cost", val->type()));
112 }
113 m_cost = readNonNegativeInteger(*val);
114 ++val;
115}
116
117bool
119{
120 return a.getFaceId() == b.getFaceId() &&
121 a.getCost() == b.getCost();
122}
123
124std::ostream&
125operator<<(std::ostream& os, const NextHopRecord& nh)
126{
127 return os << "NextHopRecord("
128 << "FaceId: " << nh.getFaceId() << ", "
129 << "Cost: " << nh.getCost()
130 << ")";
131}
132
134
135FibEntry::FibEntry() = default;
136
138{
139 this->wireDecode(block);
140}
141
144{
145 m_prefix = prefix;
146 m_wire.reset();
147 return *this;
148}
149
152{
153 m_nextHopRecords.push_back(nh);
154 m_wire.reset();
155 return *this;
156}
157
160{
161 m_nextHopRecords.clear();
162 m_wire.reset();
163 return *this;
164}
165
166template<encoding::Tag TAG>
167size_t
169{
170 size_t totalLength = 0;
171
172 for (const auto& nh : m_nextHopRecords | boost::adaptors::reversed) {
173 totalLength += nh.wireEncode(block);
174 }
175 totalLength += m_prefix.wireEncode(block);
176
177 totalLength += block.prependVarNumber(totalLength);
178 totalLength += block.prependVarNumber(tlv::nfd::FibEntry);
179 return totalLength;
180}
181
183
184const Block&
186{
187 if (m_wire.hasWire())
188 return m_wire;
189
190 EncodingEstimator estimator;
191 size_t estimatedSize = wireEncode(estimator);
192
193 EncodingBuffer buffer(estimatedSize, 0);
194 wireEncode(buffer);
195
196 m_wire = buffer.block();
197 return m_wire;
198}
199
200void
202{
203 if (block.type() != tlv::nfd::FibEntry) {
204 NDN_THROW(Error("FibEntry", block.type()));
205 }
206
207 m_wire = block;
208 m_wire.parse();
209 auto val = m_wire.elements_begin();
210
211 if (val == m_wire.elements_end()) {
212 NDN_THROW(Error("unexpected end of FibEntry"));
213 }
214 else if (val->type() != tlv::Name) {
215 NDN_THROW(Error("Name", val->type()));
216 }
217 m_prefix.wireDecode(*val);
218 ++val;
219
220 m_nextHopRecords.clear();
221 for (; val != m_wire.elements_end(); ++val) {
222 if (val->type() != tlv::nfd::NextHopRecord) {
223 NDN_THROW(Error("NextHopRecord", val->type()));
224 }
225 m_nextHopRecords.emplace_back(*val);
226 }
227}
228
229bool
230operator==(const FibEntry& a, const FibEntry& b)
231{
232 const auto& aNextHops = a.getNextHopRecords();
233 const auto& bNextHops = b.getNextHopRecords();
234
235 if (a.getPrefix() != b.getPrefix() ||
236 aNextHops.size() != bNextHops.size())
237 return false;
238
239 std::vector<bool> matched(bNextHops.size(), false);
240 return std::all_of(aNextHops.begin(), aNextHops.end(),
241 [&] (const NextHopRecord& nh) {
242 for (size_t i = 0; i < bNextHops.size(); ++i) {
243 if (!matched[i] && bNextHops[i] == nh) {
244 matched[i] = true;
245 return true;
246 }
247 }
248 return false;
249 });
250}
251
252std::ostream&
253operator<<(std::ostream& os, const FibEntry& entry)
254{
255 os << "FibEntry(Prefix: " << entry.getPrefix() << ",\n"
256 << " NextHops: [";
257
258 std::copy(entry.getNextHopRecords().begin(), entry.getNextHopRecords().end(),
259 make_ostream_joiner(os, ",\n "));
260
261 os << "]\n";
262
263 return os << " )";
264}
265
266} // namespace ndn::nfd
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
element_const_iterator elements_begin() const noexcept
Equivalent to elements().begin().
Definition block.hpp:433
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition block.hpp:442
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition block.hpp:205
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition block.hpp:275
void reset() noexcept
Reset the Block to a default-constructed state.
Definition block.cpp:293
void parse() const
Parse TLV-VALUE into sub-elements.
Definition block.cpp:326
Represents an absolute name.
Definition name.hpp:45
size_t size() const noexcept
Returns the number of components.
Definition name.hpp:180
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Prepend wire encoding to encoder.
Definition name.cpp:92
void wireDecode(const Block &wire)
Decode name from wire encoding.
Definition name.cpp:125
FibEntry & clearNextHopRecords()
const Name & getPrefix() const
const Block & wireEncode() const
FibEntry & setPrefix(const Name &prefix)
void wireDecode(const Block &block)
const std::vector< NextHopRecord > & getNextHopRecords() const
FibEntry & addNextHopRecord(const NextHopRecord &nh)
NextHopRecord & setFaceId(uint64_t faceId)
Definition fib-entry.cpp:40
void wireDecode(const Block &block)
Definition fib-entry.cpp:88
NextHopRecord & setCost(uint64_t cost)
Definition fib-entry.cpp:48
uint64_t getFaceId() const
Definition fib-entry.hpp:48
uint64_t getCost() const
Definition fib-entry.hpp:57
const Block & wireEncode() const
Definition fib-entry.cpp:72
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition exception.hpp:56
Contains classes and functions related to the NFD Management protocol.
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
@ Name
Definition tlv.hpp:71
ostream_joiner< std::decay_t< DelimT >, CharT, Traits > make_ostream_joiner(std::basic_ostream< CharT, Traits > &os, DelimT &&delimiter)
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition data.cpp:377
Backport of ostream_joiner from the Library Fundamentals v2 TS.