NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
face-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 
28 
29 namespace ndn {
30 namespace nfd {
31 
32 BOOST_CONCEPT_ASSERT((StatusDatasetItem<FaceStatus>));
33 
35  : m_nInInterests(0)
36  , m_nInData(0)
37  , m_nInNacks(0)
38  , m_nOutInterests(0)
39  , m_nOutData(0)
40  , m_nOutNacks(0)
41  , m_nInBytes(0)
42  , m_nOutBytes(0)
43 {
44 }
45 
47 {
48  this->wireDecode(block);
49 }
50 
51 template<encoding::Tag TAG>
52 size_t
54 {
55  size_t totalLength = 0;
56 
57  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
58  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutBytes, m_nOutBytes);
59  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInBytes, m_nInBytes);
60  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks, m_nOutNacks);
61  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutData, m_nOutData);
62  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, m_nOutInterests);
63  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, m_nInNacks);
64  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInData, m_nInData);
65  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, m_nInInterests);
66  if (m_mtu) {
67  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mtu, *m_mtu);
68  }
69  if (m_defaultCongestionThreshold) {
71  *m_defaultCongestionThreshold);
72  }
73  if (m_baseCongestionMarkingInterval) {
75  m_baseCongestionMarkingInterval->count());
76  }
80  if (m_expirationPeriod) {
82  m_expirationPeriod->count());
83  }
84  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
85  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
87 
88  totalLength += encoder.prependVarNumber(totalLength);
89  totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
90  return totalLength;
91 }
92 
94 
95 const Block&
97 {
98  if (m_wire.hasWire())
99  return m_wire;
100 
101  EncodingEstimator estimator;
102  size_t estimatedSize = wireEncode(estimator);
103 
104  EncodingBuffer buffer(estimatedSize, 0);
105  wireEncode(buffer);
106 
107  m_wire = buffer.block();
108  return m_wire;
109 }
110 
111 void
113 {
114  if (block.type() != tlv::nfd::FaceStatus) {
115  NDN_THROW(Error("FaceStatus", block.type()));
116  }
117 
118  m_wire = block;
119  m_wire.parse();
120  auto val = m_wire.elements_begin();
121 
122  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
124  ++val;
125  }
126  else {
127  NDN_THROW(Error("missing required FaceId field"));
128  }
129 
130  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
131  m_remoteUri = readString(*val);
132  ++val;
133  }
134  else {
135  NDN_THROW(Error("missing required Uri field"));
136  }
137 
138  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
139  m_localUri = readString(*val);
140  ++val;
141  }
142  else {
143  NDN_THROW(Error("missing required LocalUri field"));
144  }
145 
146  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
147  m_expirationPeriod.emplace(readNonNegativeInteger(*val));
148  ++val;
149  }
150  else {
151  m_expirationPeriod = nullopt;
152  }
153 
154  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
155  m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
156  ++val;
157  }
158  else {
159  NDN_THROW(Error("missing required FaceScope field"));
160  }
161 
162  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
163  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
164  ++val;
165  }
166  else {
167  NDN_THROW(Error("missing required FacePersistency field"));
168  }
169 
170  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
171  m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
172  ++val;
173  }
174  else {
175  NDN_THROW(Error("missing required LinkType field"));
176  }
177 
178  if (val != m_wire.elements_end() && val->type() == tlv::nfd::BaseCongestionMarkingInterval) {
179  m_baseCongestionMarkingInterval.emplace(readNonNegativeInteger(*val));
180  ++val;
181  }
182  else {
183  m_baseCongestionMarkingInterval = nullopt;
184  }
185 
186  if (val != m_wire.elements_end() && val->type() == tlv::nfd::DefaultCongestionThreshold) {
187  m_defaultCongestionThreshold = readNonNegativeInteger(*val);
188  ++val;
189  }
190  else {
191  m_defaultCongestionThreshold = nullopt;
192  }
193 
194  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Mtu) {
195  m_mtu = readNonNegativeInteger(*val);
196  ++val;
197  }
198  else {
199  m_mtu = nullopt;
200  }
201 
202  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
203  m_nInInterests = readNonNegativeInteger(*val);
204  ++val;
205  }
206  else {
207  NDN_THROW(Error("missing required NInInterests field"));
208  }
209 
210  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
211  m_nInData = readNonNegativeInteger(*val);
212  ++val;
213  }
214  else {
215  NDN_THROW(Error("missing required NInData field"));
216  }
217 
218  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
219  m_nInNacks = readNonNegativeInteger(*val);
220  ++val;
221  }
222  else {
223  NDN_THROW(Error("missing required NInNacks field"));
224  }
225 
226  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
227  m_nOutInterests = readNonNegativeInteger(*val);
228  ++val;
229  }
230  else {
231  NDN_THROW(Error("missing required NOutInterests field"));
232  }
233 
234  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
235  m_nOutData = readNonNegativeInteger(*val);
236  ++val;
237  }
238  else {
239  NDN_THROW(Error("missing required NOutData field"));
240  }
241 
242  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
243  m_nOutNacks = readNonNegativeInteger(*val);
244  ++val;
245  }
246  else {
247  NDN_THROW(Error("missing required NOutNacks field"));
248  }
249 
250  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
251  m_nInBytes = readNonNegativeInteger(*val);
252  ++val;
253  }
254  else {
255  NDN_THROW(Error("missing required NInBytes field"));
256  }
257 
258  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
259  m_nOutBytes = readNonNegativeInteger(*val);
260  ++val;
261  }
262  else {
263  NDN_THROW(Error("missing required NOutBytes field"));
264  }
265 
266  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
268  ++val;
269  }
270  else {
271  NDN_THROW(Error("missing required Flags field"));
272  }
273 }
274 
275 FaceStatus&
277 {
278  m_wire.reset();
279  m_expirationPeriod = expirationPeriod;
280  return *this;
281 }
282 
283 FaceStatus&
285 {
286  m_wire.reset();
287  m_expirationPeriod = nullopt;
288  return *this;
289 }
290 
291 FaceStatus&
293 {
294  m_wire.reset();
295  m_baseCongestionMarkingInterval = interval;
296  return *this;
297 }
298 
299 FaceStatus&
301 {
302  m_wire.reset();
303  m_baseCongestionMarkingInterval = nullopt;
304  return *this;
305 }
306 
307 FaceStatus&
309 {
310  m_wire.reset();
311  m_defaultCongestionThreshold = threshold;
312  return *this;
313 }
314 
315 FaceStatus&
317 {
318  m_wire.reset();
319  m_defaultCongestionThreshold = nullopt;
320  return *this;
321 }
322 
323 FaceStatus&
324 FaceStatus::setMtu(uint64_t mtu)
325 {
326  m_wire.reset();
327  m_mtu = mtu;
328  return *this;
329 }
330 
331 FaceStatus&
333 {
334  m_wire.reset();
335  m_mtu = nullopt;
336  return *this;
337 }
338 
339 FaceStatus&
340 FaceStatus::setNInInterests(uint64_t nInInterests)
341 {
342  m_wire.reset();
343  m_nInInterests = nInInterests;
344  return *this;
345 }
346 
347 FaceStatus&
348 FaceStatus::setNInData(uint64_t nInData)
349 {
350  m_wire.reset();
351  m_nInData = nInData;
352  return *this;
353 }
354 
355 FaceStatus&
356 FaceStatus::setNInNacks(uint64_t nInNacks)
357 {
358  m_wire.reset();
359  m_nInNacks = nInNacks;
360  return *this;
361 }
362 
363 FaceStatus&
364 FaceStatus::setNOutInterests(uint64_t nOutInterests)
365 {
366  m_wire.reset();
367  m_nOutInterests = nOutInterests;
368  return *this;
369 }
370 
371 FaceStatus&
372 FaceStatus::setNOutData(uint64_t nOutData)
373 {
374  m_wire.reset();
375  m_nOutData = nOutData;
376  return *this;
377 }
378 
379 FaceStatus&
380 FaceStatus::setNOutNacks(uint64_t nOutNacks)
381 {
382  m_wire.reset();
383  m_nOutNacks = nOutNacks;
384  return *this;
385 }
386 
387 FaceStatus&
388 FaceStatus::setNInBytes(uint64_t nInBytes)
389 {
390  m_wire.reset();
391  m_nInBytes = nInBytes;
392  return *this;
393 }
394 
395 FaceStatus&
396 FaceStatus::setNOutBytes(uint64_t nOutBytes)
397 {
398  m_wire.reset();
399  m_nOutBytes = nOutBytes;
400  return *this;
401 }
402 
403 bool
404 operator==(const FaceStatus& a, const FaceStatus& b)
405 {
406  return a.getFaceId() == b.getFaceId() &&
407  a.getRemoteUri() == b.getRemoteUri() &&
408  a.getLocalUri() == b.getLocalUri() &&
409  a.getFaceScope() == b.getFaceScope() &&
411  a.getLinkType() == b.getLinkType() &&
412  a.getFlags() == b.getFlags() &&
421  a.hasMtu() == b.hasMtu() &&
422  (!a.hasMtu() || a.getMtu() == b.getMtu()) &&
423  a.getNInInterests() == b.getNInInterests() &&
424  a.getNInData() == b.getNInData() &&
425  a.getNInNacks() == b.getNInNacks() &&
426  a.getNOutInterests() == b.getNOutInterests() &&
427  a.getNOutData() == b.getNOutData() &&
428  a.getNOutNacks() == b.getNOutNacks() &&
429  a.getNInBytes() == b.getNInBytes() &&
430  a.getNOutBytes() == b.getNOutBytes();
431 }
432 
433 std::ostream&
434 operator<<(std::ostream& os, const FaceStatus& status)
435 {
436  os << "Face(FaceId: " << status.getFaceId() << ",\n"
437  << " RemoteUri: " << status.getRemoteUri() << ",\n"
438  << " LocalUri: " << status.getLocalUri() << ",\n";
439 
440  if (status.hasExpirationPeriod()) {
441  os << " ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
442  }
443  else {
444  os << " ExpirationPeriod: infinite,\n";
445  }
446 
447  os << " FaceScope: " << status.getFaceScope() << ",\n"
448  << " FacePersistency: " << status.getFacePersistency() << ",\n"
449  << " LinkType: " << status.getLinkType() << ",\n";
450 
451  if (status.hasBaseCongestionMarkingInterval()) {
452  os << " BaseCongestionMarkingInterval: " << status.getBaseCongestionMarkingInterval() << ",\n";
453  }
454 
455  if (status.hasDefaultCongestionThreshold()) {
456  os << " DefaultCongestionThreshold: " << status.getDefaultCongestionThreshold() << " bytes,\n";
457  }
458 
459  if (status.hasMtu()) {
460  os << " Mtu: " << status.getMtu() << " bytes,\n";
461  }
462 
463  os << " Flags: " << AsHex{status.getFlags()} << ",\n"
464  << " Counters: {Interests: {in: " << status.getNInInterests() << ", "
465  << "out: " << status.getNOutInterests() << "},\n"
466  << " Data: {in: " << status.getNInData() << ", "
467  << "out: " << status.getNOutData() << "},\n"
468  << " Nacks: {in: " << status.getNInNacks() << ", "
469  << "out: " << status.getNOutNacks() << "},\n"
470  << " bytes: {in: " << status.getNInBytes() << ", "
471  << "out: " << status.getNOutBytes() << "}}\n";
472 
473  return os << " )";
474 }
475 
476 } // namespace nfd
477 } // namespace ndn
const std::string & getLocalUri() const
Definition: face-traits.hpp:75
void wireDecode(const Block &wire)
decode FaceStatus
uint64_t getNOutInterests() const
time::milliseconds getExpirationPeriod() const
Definition: face-status.hpp:69
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
FacePersistency getFacePersistency() const
bool hasExpirationPeriod() const
Definition: face-status.hpp:63
uint64_t getNOutNacks() const
bool hasBaseCongestionMarkingInterval() const
Definition: face-status.hpp:82
uint64_t getNOutBytes() const
uint64_t getNInInterests() const
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
FaceStatus & unsetDefaultCongestionThreshold()
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:324
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus)
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:221
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
FaceScope getFaceScope() const
Definition: face-traits.hpp:89
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:433
FaceStatus & setNOutBytes(uint64_t nOutBytes)
FaceStatus & setNOutInterests(uint64_t nOutInterests)
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
const std::string & getRemoteUri() const
Definition: face-traits.hpp:61
uint64_t getNOutData() const
#define NDN_THROW(e)
Definition: exception.hpp:61
FaceStatus & setExpirationPeriod(time::milliseconds expirationPeriod)
FaceStatus & setNInInterests(uint64_t nInInterests)
uint64_t getNInBytes() const
FaceStatus & setNInBytes(uint64_t nInBytes)
FaceStatus & unsetBaseCongestionMarkingInterval()
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:441
FaceStatus & unsetExpirationPeriod()
FaceStatus & setNInNacks(uint64_t nInNacks)
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:39
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:254
FaceStatus & setNOutData(uint64_t nOutData)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
represents an item in NFD Face dataset
Definition: face-status.hpp:37
time::nanoseconds getBaseCongestionMarkingInterval() const
Definition: face-status.hpp:88
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
FaceStatus & setMtu(uint64_t mtu)
set MTU (measured in bytes)
uint64_t getDefaultCongestionThreshold() const
get default congestion threshold (measured in bytes)
concept check for an item in a Status Dataset
Definition: concepts.hpp:115
FaceStatus & setBaseCongestionMarkingInterval(time::nanoseconds interval)
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:277
uint64_t getFlags() const
uint64_t getNInData() const
FaceStatus & setNOutNacks(uint64_t nOutNacks)
uint64_t getMtu() const
get MTU (measured in bytes)
const Block & wireEncode() const
encode FaceStatus
Definition: face-status.cpp:96
FaceStatus & setNInData(uint64_t nInData)
FaceStatus & unsetMtu()
uint64_t getFaceId() const
Definition: face-traits.hpp:47
bool hasDefaultCongestionThreshold() const
EncodingImpl< EncoderTag > EncodingBuffer
const nullopt_t nullopt((nullopt_t::init()))
boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:50
LinkType getLinkType() const
EncodingImpl< EstimatorTag > EncodingEstimator
FaceStatus & setDefaultCongestionThreshold(uint64_t threshold)
set default congestion threshold (measured in bytes)
uint64_t getNInNacks() const
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48