NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
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-2017 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 
22 #include "forwarder-status.hpp"
25 #include "encoding/tlv-nfd.hpp"
26 #include "util/concepts.hpp"
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 {
46 }
47 
49 {
50  this->wireDecode(payload);
51 }
52 
53 template<encoding::Tag TAG>
54 size_t
55 ForwarderStatus::wireEncode(EncodingImpl<TAG>& encoder) const
56 {
57  size_t totalLength = 0;
58 
59  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks, m_nOutNacks);
60  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutData, m_nOutData);
61  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, m_nOutInterests);
62  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, m_nInNacks);
63  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInData, m_nInData);
64  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, m_nInInterests);
65  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries, m_nCsEntries);
66  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries, m_nMeasurementsEntries);
67  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries, m_nPitEntries);
68  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries, m_nFibEntries);
69  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries, m_nNameTreeEntries);
71  time::toUnixTimestamp(m_currentTimestamp).count());
73  time::toUnixTimestamp(m_startTimestamp).count());
74  totalLength += prependStringBlock(encoder, tlv::nfd::NfdVersion, m_nfdVersion);
75 
76  totalLength += encoder.prependVarNumber(totalLength);
77  totalLength += encoder.prependVarNumber(tlv::Content);
78  return totalLength;
79 }
80 
82 
83 const Block&
85 {
86  if (m_wire.hasWire())
87  return m_wire;
88 
89  EncodingEstimator estimator;
90  size_t estimatedSize = wireEncode(estimator);
91 
92  EncodingBuffer buffer(estimatedSize, 0);
93  wireEncode(buffer);
94 
95  m_wire = buffer.block();
96  return m_wire;
97 }
98 
99 void
101 {
102  if (block.type() != tlv::Content) {
103  BOOST_THROW_EXCEPTION(Error("expecting Content block for Status payload"));
104  }
105  m_wire = block;
106  m_wire.parse();
108 
109  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
110  m_nfdVersion = readString(*val);
111  ++val;
112  }
113  else {
114  BOOST_THROW_EXCEPTION(Error("missing required NfdVersion field"));
115  }
116 
117  if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
119  ++val;
120  }
121  else {
122  BOOST_THROW_EXCEPTION(Error("missing required StartTimestamp field"));
123  }
124 
125  if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
127  ++val;
128  }
129  else {
130  BOOST_THROW_EXCEPTION(Error("missing required CurrentTimestamp field"));
131  }
132 
133  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
134  m_nNameTreeEntries = readNonNegativeIntegerAs<size_t>(*val);
135  ++val;
136  }
137  else {
138  BOOST_THROW_EXCEPTION(Error("missing required NNameTreeEntries field"));
139  }
140 
141  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
142  m_nFibEntries = readNonNegativeIntegerAs<size_t>(*val);
143  ++val;
144  }
145  else {
146  BOOST_THROW_EXCEPTION(Error("missing required NFibEntries field"));
147  }
148 
149  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
150  m_nPitEntries = readNonNegativeIntegerAs<size_t>(*val);
151  ++val;
152  }
153  else {
154  BOOST_THROW_EXCEPTION(Error("missing required NPitEntries field"));
155  }
156 
157  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
158  m_nMeasurementsEntries = readNonNegativeIntegerAs<size_t>(*val);
159  ++val;
160  }
161  else {
162  BOOST_THROW_EXCEPTION(Error("missing required NMeasurementsEntries field"));
163  }
164 
165  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
166  m_nCsEntries = readNonNegativeIntegerAs<size_t>(*val);
167  ++val;
168  }
169  else {
170  BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field"));
171  }
172 
173  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
174  m_nInInterests = readNonNegativeInteger(*val);
175  ++val;
176  }
177  else {
178  BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
179  }
180 
181  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
182  m_nInData = readNonNegativeInteger(*val);
183  ++val;
184  }
185  else {
186  BOOST_THROW_EXCEPTION(Error("missing required NInData field"));
187  }
188 
189  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
190  m_nInNacks = readNonNegativeInteger(*val);
191  ++val;
192  }
193  else {
194  BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
195  }
196 
197  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
198  m_nOutInterests = readNonNegativeInteger(*val);
199  ++val;
200  }
201  else {
202  BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
203  }
204 
205  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
206  m_nOutData = readNonNegativeInteger(*val);
207  ++val;
208  }
209  else {
210  BOOST_THROW_EXCEPTION(Error("missing required NOutData field"));
211  }
212 
213  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
214  m_nOutNacks = readNonNegativeInteger(*val);
215  ++val;
216  }
217  else {
218  BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
219  }
220 }
221 
223 ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
224 {
225  m_wire.reset();
226  m_nfdVersion = nfdVersion;
227  return *this;
228 }
229 
232 {
233  m_wire.reset();
234  m_startTimestamp = startTimestamp;
235  return *this;
236 }
237 
240 {
241  m_wire.reset();
242  m_currentTimestamp = currentTimestamp;
243  return *this;
244 }
245 
247 ForwarderStatus::setNNameTreeEntries(size_t nNameTreeEntries)
248 {
249  m_wire.reset();
250  m_nNameTreeEntries = nNameTreeEntries;
251  return *this;
252 }
253 
256 {
257  m_wire.reset();
258  m_nFibEntries = nFibEntries;
259  return *this;
260 }
261 
264 {
265  m_wire.reset();
266  m_nPitEntries = nPitEntries;
267  return *this;
268 }
269 
271 ForwarderStatus::setNMeasurementsEntries(size_t nMeasurementsEntries)
272 {
273  m_wire.reset();
274  m_nMeasurementsEntries = nMeasurementsEntries;
275  return *this;
276 }
277 
280 {
281  m_wire.reset();
282  m_nCsEntries = nCsEntries;
283  return *this;
284 }
285 
287 ForwarderStatus::setNInInterests(uint64_t nInInterests)
288 {
289  m_wire.reset();
290  m_nInInterests = nInInterests;
291  return *this;
292 }
293 
296 {
297  m_wire.reset();
298  m_nInData = nInData;
299  return *this;
300 }
301 
304 {
305  m_wire.reset();
306  m_nInNacks = nInNacks;
307  return *this;
308 }
309 
311 ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
312 {
313  m_wire.reset();
314  m_nOutInterests = nOutInterests;
315  return *this;
316 }
317 
320 {
321  m_wire.reset();
322  m_nOutData = nOutData;
323  return *this;
324 }
325 
327 ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
328 {
329  m_wire.reset();
330  m_nOutNacks = nOutNacks;
331  return *this;
332 }
333 
334 bool
336 {
337  return a.getNfdVersion() == b.getNfdVersion() &&
341  a.getNFibEntries() == b.getNFibEntries() &&
342  a.getNPitEntries() == b.getNPitEntries() &&
344  a.getNCsEntries() == b.getNCsEntries() &&
345  a.getNInInterests() == b.getNInInterests() &&
346  a.getNInData() == b.getNInData() &&
347  a.getNInNacks() == b.getNInNacks() &&
348  a.getNOutInterests() == b.getNOutInterests() &&
349  a.getNOutData() == b.getNOutData() &&
350  a.getNOutNacks() == b.getNOutNacks();
351 }
352 
353 std::ostream&
354 operator<<(std::ostream& os, const ForwarderStatus& status)
355 {
356  os << "GeneralStatus(NfdVersion: " << status.getNfdVersion() << ",\n"
357  << " StartTimestamp: " << status.getStartTimestamp() << ",\n"
358  << " CurrentTimestamp: " << status.getCurrentTimestamp() << ",\n"
359  << " Counters: {NameTreeEntries: " << status.getNNameTreeEntries() << ",\n"
360  << " FibEntries: " << status.getNFibEntries() << ",\n"
361  << " PitEntries: " << status.getNPitEntries() << ",\n"
362  << " MeasurementsEntries: " << status.getNMeasurementsEntries() << ",\n"
363  << " CsEntries: " << status.getNCsEntries() << ",\n"
364  << " Interests: {in: " << status.getNInInterests() << ", "
365  << "out: " << status.getNOutInterests() << "},\n"
366  << " Data: {in: " << status.getNInData() << ", "
367  << "out: " << status.getNOutData() << "},\n"
368  << " Nacks: {in: " << status.getNInNacks() << ", "
369  << "out: " << status.getNOutNacks() << "}}\n"
370  << " )";
371 
372  return os;
373 }
374 
375 } // namespace nfd
376 } // namespace ndn
represents NFD General Status dataset
Copyright (c) 2011-2015 Regents of the University of California.
size_t getNMeasurementsEntries() const
ForwarderStatus & setNFibEntries(size_t nFibEntries)
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
ForwarderStatus & setNOutNacks(uint64_t nOutNacks)
const time::system_clock::TimePoint & getStartTimestamp() const
const std::string & getNfdVersion() const
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
ForwarderStatus & setNInData(uint64_t nInData)
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
ForwarderStatus & setNOutData(uint64_t nOutData)
ForwarderStatus & setNInNacks(uint64_t nInNacks)
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus)
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
ForwarderStatus & setNCsEntries(size_t nCsEntries)
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
uint64_t getNInInterests() const
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
uint64_t getNOutNacks() const
ForwarderStatus & setNNameTreeEntries(size_t nNameTreeEntries)
ForwarderStatus & setNInInterests(uint64_t nInInterests)
ForwarderStatus & setStartTimestamp(const time::system_clock::TimePoint &startTimestamp)
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
uint64_t getNOutInterests() const
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
ForwarderStatus & setNfdVersion(const std::string &nfdVersion)
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
ForwarderStatus & setNOutInterests(uint64_t nOutInterests)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:256
ForwarderStatus & setNPitEntries(size_t nPitEntries)
concept check for an item in a Status Dataset
Definition: concepts.hpp:115
const Block & wireEncode() const
encode ForwarderStatus as a Content block
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:334
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
time_point TimePoint
Definition: time.hpp:90
system_clock::TimePoint fromUnixTimestamp(const milliseconds &duration)
Convert UNIX timestamp to system_clock::TimePoint.
Definition: time.cpp:125
milliseconds toUnixTimestamp(const system_clock::TimePoint &point)
Convert system_clock::TimePoint to UNIX timestamp.
Definition: time.cpp:119
void wireDecode(const Block &wire)
decode ForwarderStatus from a Content block
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:250
size_t getNNameTreeEntries() const
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
const time::system_clock::TimePoint & getCurrentTimestamp() const
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
ForwarderStatus & setNMeasurementsEntries(size_t nMeasurementsEntries)
ForwarderStatus & setCurrentTimestamp(const time::system_clock::TimePoint &currentTimestamp)
EncodingImpl< EncoderTag > EncodingBuffer
EncodingImpl< EstimatorTag > EncodingEstimator