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-2019 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 namespace ndn {
29 namespace nfd {
30 
31 BOOST_CONCEPT_ASSERT((StatusDatasetItem<ForwarderStatus>));
32 
34  : m_nNameTreeEntries(0)
35  , m_nFibEntries(0)
36  , m_nPitEntries(0)
37  , m_nMeasurementsEntries(0)
38  , m_nCsEntries(0)
39  , m_nInInterests(0)
40  , m_nInData(0)
41  , m_nInNacks(0)
42  , m_nOutInterests(0)
43  , m_nOutData(0)
44  , m_nOutNacks(0)
45  , m_nSatisfiedInterests(0)
46  , m_nUnsatisfiedInterests(0)
47 {
48 }
49 
51 {
52  this->wireDecode(payload);
53 }
54 
55 template<encoding::Tag TAG>
56 size_t
58 {
59  size_t totalLength = 0;
60 
61  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NUnsatisfiedInterests, m_nUnsatisfiedInterests);
62  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NSatisfiedInterests, m_nSatisfiedInterests);
63  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks, m_nOutNacks);
64  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutData, m_nOutData);
65  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, m_nOutInterests);
66  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, m_nInNacks);
67  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInData, m_nInData);
68  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, m_nInInterests);
69  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries, m_nCsEntries);
70  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries, m_nMeasurementsEntries);
71  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries, m_nPitEntries);
72  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries, m_nFibEntries);
73  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries, m_nNameTreeEntries);
75  time::toUnixTimestamp(m_currentTimestamp).count());
77  time::toUnixTimestamp(m_startTimestamp).count());
78  totalLength += prependStringBlock(encoder, tlv::nfd::NfdVersion, m_nfdVersion);
79 
80  totalLength += encoder.prependVarNumber(totalLength);
81  totalLength += encoder.prependVarNumber(tlv::Content);
82  return totalLength;
83 }
84 
86 
87 const Block&
89 {
90  if (m_wire.hasWire())
91  return m_wire;
92 
93  EncodingEstimator estimator;
94  size_t estimatedSize = wireEncode(estimator);
95 
96  EncodingBuffer buffer(estimatedSize, 0);
97  wireEncode(buffer);
98 
99  m_wire = buffer.block();
100  return m_wire;
101 }
102 
103 void
105 {
106  if (block.type() != tlv::Content) {
107  NDN_THROW(Error("Content", block.type()));
108  }
109 
110  m_wire = block;
111  m_wire.parse();
112  auto val = m_wire.elements_begin();
113 
114  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
115  m_nfdVersion = readString(*val);
116  ++val;
117  }
118  else {
119  NDN_THROW(Error("missing required NfdVersion field"));
120  }
121 
122  if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
124  ++val;
125  }
126  else {
127  NDN_THROW(Error("missing required StartTimestamp field"));
128  }
129 
130  if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
132  ++val;
133  }
134  else {
135  NDN_THROW(Error("missing required CurrentTimestamp field"));
136  }
137 
138  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
139  m_nNameTreeEntries = readNonNegativeInteger(*val);
140  ++val;
141  }
142  else {
143  NDN_THROW(Error("missing required NNameTreeEntries field"));
144  }
145 
146  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
147  m_nFibEntries = readNonNegativeInteger(*val);
148  ++val;
149  }
150  else {
151  NDN_THROW(Error("missing required NFibEntries field"));
152  }
153 
154  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
155  m_nPitEntries = readNonNegativeInteger(*val);
156  ++val;
157  }
158  else {
159  NDN_THROW(Error("missing required NPitEntries field"));
160  }
161 
162  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
163  m_nMeasurementsEntries = readNonNegativeInteger(*val);
164  ++val;
165  }
166  else {
167  NDN_THROW(Error("missing required NMeasurementsEntries field"));
168  }
169 
170  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
171  m_nCsEntries = readNonNegativeInteger(*val);
172  ++val;
173  }
174  else {
175  NDN_THROW(Error("missing required NCsEntries field"));
176  }
177 
178  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
179  m_nInInterests = readNonNegativeInteger(*val);
180  ++val;
181  }
182  else {
183  NDN_THROW(Error("missing required NInInterests field"));
184  }
185 
186  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
187  m_nInData = readNonNegativeInteger(*val);
188  ++val;
189  }
190  else {
191  NDN_THROW(Error("missing required NInData field"));
192  }
193 
194  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
195  m_nInNacks = readNonNegativeInteger(*val);
196  ++val;
197  }
198  else {
199  NDN_THROW(Error("missing required NInNacks field"));
200  }
201 
202  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
203  m_nOutInterests = readNonNegativeInteger(*val);
204  ++val;
205  }
206  else {
207  NDN_THROW(Error("missing required NOutInterests field"));
208  }
209 
210  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
211  m_nOutData = readNonNegativeInteger(*val);
212  ++val;
213  }
214  else {
215  NDN_THROW(Error("missing required NOutData field"));
216  }
217 
218  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
219  m_nOutNacks = readNonNegativeInteger(*val);
220  ++val;
221  }
222  else {
223  NDN_THROW(Error("missing required NOutNacks field"));
224  }
225 
226  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NSatisfiedInterests) {
227  m_nSatisfiedInterests = readNonNegativeInteger(*val);
228  ++val;
229  }
230  else {
231  NDN_THROW(Error("missing required NSatisfiedInterests field"));
232  }
233 
234  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NUnsatisfiedInterests) {
235  m_nUnsatisfiedInterests = readNonNegativeInteger(*val);
236  ++val;
237  }
238  else {
239  NDN_THROW(Error("missing required NUnsatisfiedInterests field"));
240  }
241 }
242 
244 ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
245 {
246  m_wire.reset();
247  m_nfdVersion = nfdVersion;
248  return *this;
249 }
250 
253 {
254  m_wire.reset();
255  m_startTimestamp = startTimestamp;
256  return *this;
257 }
258 
261 {
262  m_wire.reset();
263  m_currentTimestamp = currentTimestamp;
264  return *this;
265 }
266 
268 ForwarderStatus::setNNameTreeEntries(uint64_t nNameTreeEntries)
269 {
270  m_wire.reset();
271  m_nNameTreeEntries = nNameTreeEntries;
272  return *this;
273 }
274 
276 ForwarderStatus::setNFibEntries(uint64_t nFibEntries)
277 {
278  m_wire.reset();
279  m_nFibEntries = nFibEntries;
280  return *this;
281 }
282 
284 ForwarderStatus::setNPitEntries(uint64_t nPitEntries)
285 {
286  m_wire.reset();
287  m_nPitEntries = nPitEntries;
288  return *this;
289 }
290 
292 ForwarderStatus::setNMeasurementsEntries(uint64_t nMeasurementsEntries)
293 {
294  m_wire.reset();
295  m_nMeasurementsEntries = nMeasurementsEntries;
296  return *this;
297 }
298 
300 ForwarderStatus::setNCsEntries(uint64_t nCsEntries)
301 {
302  m_wire.reset();
303  m_nCsEntries = nCsEntries;
304  return *this;
305 }
306 
308 ForwarderStatus::setNInInterests(uint64_t nInInterests)
309 {
310  m_wire.reset();
311  m_nInInterests = nInInterests;
312  return *this;
313 }
314 
317 {
318  m_wire.reset();
319  m_nInData = nInData;
320  return *this;
321 }
322 
325 {
326  m_wire.reset();
327  m_nInNacks = nInNacks;
328  return *this;
329 }
330 
332 ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
333 {
334  m_wire.reset();
335  m_nOutInterests = nOutInterests;
336  return *this;
337 }
338 
341 {
342  m_wire.reset();
343  m_nOutData = nOutData;
344  return *this;
345 }
346 
348 ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
349 {
350  m_wire.reset();
351  m_nOutNacks = nOutNacks;
352  return *this;
353 }
354 
356 ForwarderStatus::setNSatisfiedInterests(uint64_t nSatisfiedInterests)
357 {
358  m_wire.reset();
359  m_nSatisfiedInterests = nSatisfiedInterests;
360  return *this;
361 }
362 
364 ForwarderStatus::setNUnsatisfiedInterests(uint64_t nUnsatisfiedInterests)
365 {
366  m_wire.reset();
367  m_nUnsatisfiedInterests = nUnsatisfiedInterests;
368  return *this;
369 }
370 
371 bool
373 {
374  return a.getNfdVersion() == b.getNfdVersion() &&
378  a.getNFibEntries() == b.getNFibEntries() &&
379  a.getNPitEntries() == b.getNPitEntries() &&
381  a.getNCsEntries() == b.getNCsEntries() &&
382  a.getNInInterests() == b.getNInInterests() &&
383  a.getNInData() == b.getNInData() &&
384  a.getNInNacks() == b.getNInNacks() &&
385  a.getNOutInterests() == b.getNOutInterests() &&
386  a.getNOutData() == b.getNOutData() &&
387  a.getNOutNacks() == b.getNOutNacks() &&
390 }
391 
392 std::ostream&
393 operator<<(std::ostream& os, const ForwarderStatus& status)
394 {
395  os << "GeneralStatus(NfdVersion: " << status.getNfdVersion() << ",\n"
396  << " StartTimestamp: " << status.getStartTimestamp() << ",\n"
397  << " CurrentTimestamp: " << status.getCurrentTimestamp() << ",\n"
398  << " Counters: {NameTreeEntries: " << status.getNNameTreeEntries() << ",\n"
399  << " FibEntries: " << status.getNFibEntries() << ",\n"
400  << " PitEntries: " << status.getNPitEntries() << ",\n"
401  << " MeasurementsEntries: " << status.getNMeasurementsEntries() << ",\n"
402  << " CsEntries: " << status.getNCsEntries() << ",\n"
403  << " Interests: {in: " << status.getNInInterests() << ", "
404  << "out: " << status.getNOutInterests() << "},\n"
405  << " Data: {in: " << status.getNInData() << ", "
406  << "out: " << status.getNOutData() << "},\n"
407  << " Nacks: {in: " << status.getNInNacks() << ", "
408  << "out: " << status.getNOutNacks() << "},\n"
409  << " SatisfiedInterests: " << status.getNSatisfiedInterests() << ",\n"
410  << " UnsatisfiedInterests: " << status.getNUnsatisfiedInterests() << "}\n"
411  << " )";
412 
413  return os;
414 }
415 
416 } // namespace nfd
417 } // namespace ndn
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:435
element_const_iterator elements_end() const noexcept
Equivalent to elements().end().
Definition: block.hpp:444
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:221
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:277
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:254
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
Represents NFD General Status dataset.
ForwarderStatus & setNCsEntries(uint64_t nCsEntries)
ForwarderStatus & setNInData(uint64_t nInData)
const std::string & getNfdVersion() const
ForwarderStatus & setNMeasurementsEntries(uint64_t nMeasurementsEntries)
ForwarderStatus & setCurrentTimestamp(const time::system_clock::TimePoint &currentTimestamp)
ForwarderStatus & setNSatisfiedInterests(uint64_t nSatisfiedInterests)
ForwarderStatus & setNInNacks(uint64_t nInNacks)
uint64_t getNMeasurementsEntries() const
const time::system_clock::TimePoint & getStartTimestamp() 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 & setNInInterests(uint64_t nInInterests)
ForwarderStatus & setNFibEntries(uint64_t nFibEntries)
ForwarderStatus & setStartTimestamp(const time::system_clock::TimePoint &startTimestamp)
uint64_t getNSatisfiedInterests() const
ForwarderStatus & setNUnsatisfiedInterests(uint64_t nUnsatisfiedInterests)
void wireDecode(const Block &wire)
Decode ForwarderStatus from a Content block.
ForwarderStatus & setNOutNacks(uint64_t nOutNacks)
uint64_t getNInInterests() const
const time::system_clock::TimePoint & getCurrentTimestamp() const
ForwarderStatus & setNOutData(uint64_t nOutData)
ForwarderStatus & setNNameTreeEntries(uint64_t nNameTreeEntries)
time_point TimePoint
Definition: time.hpp:203
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
#define NDN_THROW(e)
Definition: exception.hpp:61
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, const std::string &value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
EncodingImpl< EncoderTag > EncodingBuffer
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
milliseconds toUnixTimestamp(const system_clock::time_point &point)
Convert system_clock::time_point to UNIX timestamp.
Definition: time.cpp:113
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48
system_clock::time_point fromUnixTimestamp(milliseconds duration)
Convert UNIX timestamp to system_clock::time_point.
Definition: time.cpp:119
@ 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
Definition: data.cpp:25