forwarder-status.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 
26 
27 namespace ndn::nfd {
28 
30 
32 {
33  this->wireDecode(payload);
34 }
35 
36 template<encoding::Tag TAG>
37 size_t
39 {
40  size_t totalLength = 0;
41 
42  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NUnsatisfiedInterests, m_nUnsatisfiedInterests);
43  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NSatisfiedInterests, m_nSatisfiedInterests);
44  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks, m_nOutNacks);
45  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutData, m_nOutData);
46  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, m_nOutInterests);
47  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, m_nInNacks);
48  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInData, m_nInData);
49  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, m_nInInterests);
50  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries, m_nCsEntries);
51  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries, m_nMeasurementsEntries);
52  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries, m_nPitEntries);
53  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries, m_nFibEntries);
54  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries, m_nNameTreeEntries);
56  time::toUnixTimestamp(m_currentTimestamp).count());
58  time::toUnixTimestamp(m_startTimestamp).count());
59  totalLength += prependStringBlock(encoder, tlv::nfd::NfdVersion, m_nfdVersion);
60 
61  totalLength += encoder.prependVarNumber(totalLength);
62  totalLength += encoder.prependVarNumber(tlv::Content);
63  return totalLength;
64 }
65 
67 
68 const Block&
70 {
71  if (m_wire.hasWire())
72  return m_wire;
73 
74  EncodingEstimator estimator;
75  size_t estimatedSize = wireEncode(estimator);
76 
77  EncodingBuffer buffer(estimatedSize, 0);
78  wireEncode(buffer);
79 
80  m_wire = buffer.block();
81  return m_wire;
82 }
83 
84 void
86 {
87  if (block.type() != tlv::Content) {
88  NDN_THROW(Error("Content", block.type()));
89  }
90 
91  m_wire = block;
92  m_wire.parse();
93  auto val = m_wire.elements_begin();
94 
95  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
96  m_nfdVersion = readString(*val);
97  ++val;
98  }
99  else {
100  NDN_THROW(Error("missing required NfdVersion field"));
101  }
102 
103  if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
105  ++val;
106  }
107  else {
108  NDN_THROW(Error("missing required StartTimestamp field"));
109  }
110 
111  if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
113  ++val;
114  }
115  else {
116  NDN_THROW(Error("missing required CurrentTimestamp field"));
117  }
118 
119  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
120  m_nNameTreeEntries = readNonNegativeInteger(*val);
121  ++val;
122  }
123  else {
124  NDN_THROW(Error("missing required NNameTreeEntries field"));
125  }
126 
127  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
128  m_nFibEntries = readNonNegativeInteger(*val);
129  ++val;
130  }
131  else {
132  NDN_THROW(Error("missing required NFibEntries field"));
133  }
134 
135  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
136  m_nPitEntries = readNonNegativeInteger(*val);
137  ++val;
138  }
139  else {
140  NDN_THROW(Error("missing required NPitEntries field"));
141  }
142 
143  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
144  m_nMeasurementsEntries = readNonNegativeInteger(*val);
145  ++val;
146  }
147  else {
148  NDN_THROW(Error("missing required NMeasurementsEntries field"));
149  }
150 
151  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
152  m_nCsEntries = readNonNegativeInteger(*val);
153  ++val;
154  }
155  else {
156  NDN_THROW(Error("missing required NCsEntries field"));
157  }
158 
159  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
160  m_nInInterests = readNonNegativeInteger(*val);
161  ++val;
162  }
163  else {
164  NDN_THROW(Error("missing required NInInterests field"));
165  }
166 
167  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
168  m_nInData = readNonNegativeInteger(*val);
169  ++val;
170  }
171  else {
172  NDN_THROW(Error("missing required NInData field"));
173  }
174 
175  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
176  m_nInNacks = readNonNegativeInteger(*val);
177  ++val;
178  }
179  else {
180  NDN_THROW(Error("missing required NInNacks field"));
181  }
182 
183  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
184  m_nOutInterests = readNonNegativeInteger(*val);
185  ++val;
186  }
187  else {
188  NDN_THROW(Error("missing required NOutInterests field"));
189  }
190 
191  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
192  m_nOutData = readNonNegativeInteger(*val);
193  ++val;
194  }
195  else {
196  NDN_THROW(Error("missing required NOutData field"));
197  }
198 
199  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
200  m_nOutNacks = readNonNegativeInteger(*val);
201  ++val;
202  }
203  else {
204  NDN_THROW(Error("missing required NOutNacks field"));
205  }
206 
207  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NSatisfiedInterests) {
208  m_nSatisfiedInterests = readNonNegativeInteger(*val);
209  ++val;
210  }
211  else {
212  NDN_THROW(Error("missing required NSatisfiedInterests field"));
213  }
214 
215  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NUnsatisfiedInterests) {
216  m_nUnsatisfiedInterests = readNonNegativeInteger(*val);
217  ++val;
218  }
219  else {
220  NDN_THROW(Error("missing required NUnsatisfiedInterests field"));
221  }
222 }
223 
225 ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
226 {
227  m_wire.reset();
228  m_nfdVersion = nfdVersion;
229  return *this;
230 }
231 
234 {
235  m_wire.reset();
236  m_startTimestamp = startTimestamp;
237  return *this;
238 }
239 
242 {
243  m_wire.reset();
244  m_currentTimestamp = currentTimestamp;
245  return *this;
246 }
247 
249 ForwarderStatus::setNNameTreeEntries(uint64_t nNameTreeEntries)
250 {
251  m_wire.reset();
252  m_nNameTreeEntries = nNameTreeEntries;
253  return *this;
254 }
255 
257 ForwarderStatus::setNFibEntries(uint64_t nFibEntries)
258 {
259  m_wire.reset();
260  m_nFibEntries = nFibEntries;
261  return *this;
262 }
263 
265 ForwarderStatus::setNPitEntries(uint64_t nPitEntries)
266 {
267  m_wire.reset();
268  m_nPitEntries = nPitEntries;
269  return *this;
270 }
271 
273 ForwarderStatus::setNMeasurementsEntries(uint64_t nMeasurementsEntries)
274 {
275  m_wire.reset();
276  m_nMeasurementsEntries = nMeasurementsEntries;
277  return *this;
278 }
279 
281 ForwarderStatus::setNCsEntries(uint64_t nCsEntries)
282 {
283  m_wire.reset();
284  m_nCsEntries = nCsEntries;
285  return *this;
286 }
287 
289 ForwarderStatus::setNInInterests(uint64_t nInInterests)
290 {
291  m_wire.reset();
292  m_nInInterests = nInInterests;
293  return *this;
294 }
295 
298 {
299  m_wire.reset();
300  m_nInData = nInData;
301  return *this;
302 }
303 
306 {
307  m_wire.reset();
308  m_nInNacks = nInNacks;
309  return *this;
310 }
311 
313 ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
314 {
315  m_wire.reset();
316  m_nOutInterests = nOutInterests;
317  return *this;
318 }
319 
322 {
323  m_wire.reset();
324  m_nOutData = nOutData;
325  return *this;
326 }
327 
329 ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
330 {
331  m_wire.reset();
332  m_nOutNacks = nOutNacks;
333  return *this;
334 }
335 
337 ForwarderStatus::setNSatisfiedInterests(uint64_t nSatisfiedInterests)
338 {
339  m_wire.reset();
340  m_nSatisfiedInterests = nSatisfiedInterests;
341  return *this;
342 }
343 
345 ForwarderStatus::setNUnsatisfiedInterests(uint64_t nUnsatisfiedInterests)
346 {
347  m_wire.reset();
348  m_nUnsatisfiedInterests = nUnsatisfiedInterests;
349  return *this;
350 }
351 
352 bool
354 {
355  return a.getNfdVersion() == b.getNfdVersion() &&
359  a.getNFibEntries() == b.getNFibEntries() &&
360  a.getNPitEntries() == b.getNPitEntries() &&
362  a.getNCsEntries() == b.getNCsEntries() &&
363  a.getNInInterests() == b.getNInInterests() &&
364  a.getNInData() == b.getNInData() &&
365  a.getNInNacks() == b.getNInNacks() &&
366  a.getNOutInterests() == b.getNOutInterests() &&
367  a.getNOutData() == b.getNOutData() &&
368  a.getNOutNacks() == b.getNOutNacks() &&
371 }
372 
373 std::ostream&
374 operator<<(std::ostream& os, const ForwarderStatus& status)
375 {
376  os << "GeneralStatus(NfdVersion: " << status.getNfdVersion() << ",\n"
377  << " StartTimestamp: " << status.getStartTimestamp() << ",\n"
378  << " CurrentTimestamp: " << status.getCurrentTimestamp() << ",\n"
379  << " Counters: {NameTreeEntries: " << status.getNNameTreeEntries() << ",\n"
380  << " FibEntries: " << status.getNFibEntries() << ",\n"
381  << " PitEntries: " << status.getNPitEntries() << ",\n"
382  << " MeasurementsEntries: " << status.getNMeasurementsEntries() << ",\n"
383  << " CsEntries: " << status.getNCsEntries() << ",\n"
384  << " Interests: {in: " << status.getNInInterests() << ", "
385  << "out: " << status.getNOutInterests() << "},\n"
386  << " Data: {in: " << status.getNInData() << ", "
387  << "out: " << status.getNOutData() << "},\n"
388  << " Nacks: {in: " << status.getNInNacks() << ", "
389  << "out: " << status.getNOutNacks() << "},\n"
390  << " SatisfiedInterests: " << status.getNSatisfiedInterests() << ",\n"
391  << " UnsatisfiedInterests: " << status.getNUnsatisfiedInterests() << "}\n"
392  << " )";
393 
394  return os;
395 }
396 
397 } // 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 NFD General Status dataset.
ForwarderStatus & setNCsEntries(uint64_t nCsEntries)
ForwarderStatus & setStartTimestamp(const time::system_clock::time_point &startTimestamp)
ForwarderStatus & setNInData(uint64_t nInData)
const std::string & getNfdVersion() const
ForwarderStatus & setNMeasurementsEntries(uint64_t nMeasurementsEntries)
ForwarderStatus & setNSatisfiedInterests(uint64_t nSatisfiedInterests)
ForwarderStatus & setNInNacks(uint64_t nInNacks)
uint64_t getNMeasurementsEntries() const
uint64_t getNNameTreeEntries() const
ForwarderStatus & setNfdVersion(const std::string &nfdVersion)
uint64_t getNOutInterests() const
ForwarderStatus & setNPitEntries(uint64_t nPitEntries)
uint64_t getNUnsatisfiedInterests() const
ForwarderStatus & setNOutInterests(uint64_t nOutInterests)
const Block & wireEncode() const
Encode ForwarderStatus as a Content block.
ForwarderStatus & setCurrentTimestamp(const time::system_clock::time_point &currentTimestamp)
ForwarderStatus & setNInInterests(uint64_t nInInterests)
ForwarderStatus & setNFibEntries(uint64_t nFibEntries)
uint64_t getNSatisfiedInterests() const
ForwarderStatus & setNUnsatisfiedInterests(uint64_t nUnsatisfiedInterests)
void wireDecode(const Block &wire)
Decode ForwarderStatus from a Content block.
const time::system_clock::time_point & getStartTimestamp() const
const time::system_clock::time_point & getCurrentTimestamp() const
ForwarderStatus & setNOutNacks(uint64_t nOutNacks)
uint64_t getNInInterests() const
ForwarderStatus & setNOutData(uint64_t nOutData)
ForwarderStatus & setNNameTreeEntries(uint64_t nNameTreeEntries)
::boost::chrono::time_point< system_clock > time_point
Definition: time.hpp:205
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:56
EncodingImpl< EstimatorTag > EncodingEstimator
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, std::string_view value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read the TLV-VALUE of a TLV element as a string.
EncodingImpl< EncoderTag > EncodingBuffer
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)
constexpr system_clock::time_point fromUnixTimestamp(system_clock::duration d)
Convert UNIX timestamp to system_clock::time_point.
Definition: time.hpp:274
constexpr Duration toUnixTimestamp(const system_clock::time_point &tp)
Convert system_clock::time_point to UNIX timestamp.
Definition: time.hpp:265
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
@ NNameTreeEntries
Definition: tlv-nfd.hpp:56
@ NUnsatisfiedInterests
Definition: tlv-nfd.hpp:87
@ NSatisfiedInterests
Definition: tlv-nfd.hpp:86
@ NMeasurementsEntries
Definition: tlv-nfd.hpp:59
@ CurrentTimestamp
Definition: tlv-nfd.hpp:55
@ Content
Definition: tlv.hpp:93