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-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 namespace ndn::nfd {
29 
30 FaceStatus::FaceStatus() = default;
31 
33 {
34  this->wireDecode(block);
35 }
36 
37 template<encoding::Tag TAG>
38 size_t
40 {
41  size_t totalLength = 0;
42 
43  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Flags, m_flags);
44  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutBytes, m_nOutBytes);
45  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInBytes, m_nInBytes);
46  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks, m_nOutNacks);
47  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutData, m_nOutData);
48  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, m_nOutInterests);
49  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, m_nInNacks);
50  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInData, m_nInData);
51  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, m_nInInterests);
52  if (m_mtu) {
53  totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mtu, *m_mtu);
54  }
55  if (m_defaultCongestionThreshold) {
57  *m_defaultCongestionThreshold);
58  }
59  if (m_baseCongestionMarkingInterval) {
61  m_baseCongestionMarkingInterval->count());
62  }
66  if (m_expirationPeriod) {
68  m_expirationPeriod->count());
69  }
70  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
71  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
73 
74  totalLength += encoder.prependVarNumber(totalLength);
75  totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
76  return totalLength;
77 }
78 
80 
81 const Block&
83 {
84  if (m_wire.hasWire())
85  return m_wire;
86 
87  EncodingEstimator estimator;
88  size_t estimatedSize = wireEncode(estimator);
89 
90  EncodingBuffer buffer(estimatedSize, 0);
91  wireEncode(buffer);
92 
93  m_wire = buffer.block();
94  return m_wire;
95 }
96 
97 void
99 {
100  if (block.type() != tlv::nfd::FaceStatus) {
101  NDN_THROW(Error("FaceStatus", block.type()));
102  }
103 
104  m_wire = block;
105  m_wire.parse();
106  auto val = m_wire.elements_begin();
107 
108  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
110  ++val;
111  }
112  else {
113  NDN_THROW(Error("missing required FaceId field"));
114  }
115 
116  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
117  m_remoteUri = readString(*val);
118  ++val;
119  }
120  else {
121  NDN_THROW(Error("missing required Uri field"));
122  }
123 
124  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
125  m_localUri = readString(*val);
126  ++val;
127  }
128  else {
129  NDN_THROW(Error("missing required LocalUri field"));
130  }
131 
132  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
133  m_expirationPeriod.emplace(readNonNegativeInteger(*val));
134  ++val;
135  }
136  else {
137  m_expirationPeriod = std::nullopt;
138  }
139 
140  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
141  m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
142  ++val;
143  }
144  else {
145  NDN_THROW(Error("missing required FaceScope field"));
146  }
147 
148  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
149  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
150  ++val;
151  }
152  else {
153  NDN_THROW(Error("missing required FacePersistency field"));
154  }
155 
156  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
157  m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
158  ++val;
159  }
160  else {
161  NDN_THROW(Error("missing required LinkType field"));
162  }
163 
164  if (val != m_wire.elements_end() && val->type() == tlv::nfd::BaseCongestionMarkingInterval) {
165  m_baseCongestionMarkingInterval.emplace(readNonNegativeInteger(*val));
166  ++val;
167  }
168  else {
169  m_baseCongestionMarkingInterval = std::nullopt;
170  }
171 
172  if (val != m_wire.elements_end() && val->type() == tlv::nfd::DefaultCongestionThreshold) {
173  m_defaultCongestionThreshold = readNonNegativeInteger(*val);
174  ++val;
175  }
176  else {
177  m_defaultCongestionThreshold = std::nullopt;
178  }
179 
180  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Mtu) {
181  m_mtu = readNonNegativeInteger(*val);
182  ++val;
183  }
184  else {
185  m_mtu = std::nullopt;
186  }
187 
188  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
189  m_nInInterests = readNonNegativeInteger(*val);
190  ++val;
191  }
192  else {
193  NDN_THROW(Error("missing required NInInterests field"));
194  }
195 
196  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
197  m_nInData = readNonNegativeInteger(*val);
198  ++val;
199  }
200  else {
201  NDN_THROW(Error("missing required NInData field"));
202  }
203 
204  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
205  m_nInNacks = readNonNegativeInteger(*val);
206  ++val;
207  }
208  else {
209  NDN_THROW(Error("missing required NInNacks field"));
210  }
211 
212  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
213  m_nOutInterests = readNonNegativeInteger(*val);
214  ++val;
215  }
216  else {
217  NDN_THROW(Error("missing required NOutInterests field"));
218  }
219 
220  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
221  m_nOutData = readNonNegativeInteger(*val);
222  ++val;
223  }
224  else {
225  NDN_THROW(Error("missing required NOutData field"));
226  }
227 
228  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
229  m_nOutNacks = readNonNegativeInteger(*val);
230  ++val;
231  }
232  else {
233  NDN_THROW(Error("missing required NOutNacks field"));
234  }
235 
236  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
237  m_nInBytes = readNonNegativeInteger(*val);
238  ++val;
239  }
240  else {
241  NDN_THROW(Error("missing required NInBytes field"));
242  }
243 
244  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
245  m_nOutBytes = readNonNegativeInteger(*val);
246  ++val;
247  }
248  else {
249  NDN_THROW(Error("missing required NOutBytes field"));
250  }
251 
252  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
254  ++val;
255  }
256  else {
257  NDN_THROW(Error("missing required Flags field"));
258  }
259 }
260 
261 FaceStatus&
263 {
264  m_wire.reset();
265  m_expirationPeriod = expirationPeriod;
266  return *this;
267 }
268 
269 FaceStatus&
271 {
272  m_wire.reset();
273  m_expirationPeriod = std::nullopt;
274  return *this;
275 }
276 
277 FaceStatus&
279 {
280  m_wire.reset();
281  m_baseCongestionMarkingInterval = interval;
282  return *this;
283 }
284 
285 FaceStatus&
287 {
288  m_wire.reset();
289  m_baseCongestionMarkingInterval = std::nullopt;
290  return *this;
291 }
292 
293 FaceStatus&
295 {
296  m_wire.reset();
297  m_defaultCongestionThreshold = threshold;
298  return *this;
299 }
300 
301 FaceStatus&
303 {
304  m_wire.reset();
305  m_defaultCongestionThreshold = std::nullopt;
306  return *this;
307 }
308 
309 FaceStatus&
310 FaceStatus::setMtu(uint64_t mtu)
311 {
312  m_wire.reset();
313  m_mtu = mtu;
314  return *this;
315 }
316 
317 FaceStatus&
319 {
320  m_wire.reset();
321  m_mtu = std::nullopt;
322  return *this;
323 }
324 
325 FaceStatus&
326 FaceStatus::setNInInterests(uint64_t nInInterests)
327 {
328  m_wire.reset();
329  m_nInInterests = nInInterests;
330  return *this;
331 }
332 
333 FaceStatus&
334 FaceStatus::setNInData(uint64_t nInData)
335 {
336  m_wire.reset();
337  m_nInData = nInData;
338  return *this;
339 }
340 
341 FaceStatus&
342 FaceStatus::setNInNacks(uint64_t nInNacks)
343 {
344  m_wire.reset();
345  m_nInNacks = nInNacks;
346  return *this;
347 }
348 
349 FaceStatus&
350 FaceStatus::setNOutInterests(uint64_t nOutInterests)
351 {
352  m_wire.reset();
353  m_nOutInterests = nOutInterests;
354  return *this;
355 }
356 
357 FaceStatus&
358 FaceStatus::setNOutData(uint64_t nOutData)
359 {
360  m_wire.reset();
361  m_nOutData = nOutData;
362  return *this;
363 }
364 
365 FaceStatus&
366 FaceStatus::setNOutNacks(uint64_t nOutNacks)
367 {
368  m_wire.reset();
369  m_nOutNacks = nOutNacks;
370  return *this;
371 }
372 
373 FaceStatus&
374 FaceStatus::setNInBytes(uint64_t nInBytes)
375 {
376  m_wire.reset();
377  m_nInBytes = nInBytes;
378  return *this;
379 }
380 
381 FaceStatus&
382 FaceStatus::setNOutBytes(uint64_t nOutBytes)
383 {
384  m_wire.reset();
385  m_nOutBytes = nOutBytes;
386  return *this;
387 }
388 
389 bool
390 operator==(const FaceStatus& a, const FaceStatus& b)
391 {
392  return a.getFaceId() == b.getFaceId() &&
393  a.getRemoteUri() == b.getRemoteUri() &&
394  a.getLocalUri() == b.getLocalUri() &&
395  a.getFaceScope() == b.getFaceScope() &&
397  a.getLinkType() == b.getLinkType() &&
398  a.getFlags() == b.getFlags() &&
407  a.hasMtu() == b.hasMtu() &&
408  (!a.hasMtu() || a.getMtu() == b.getMtu()) &&
409  a.getNInInterests() == b.getNInInterests() &&
410  a.getNInData() == b.getNInData() &&
411  a.getNInNacks() == b.getNInNacks() &&
412  a.getNOutInterests() == b.getNOutInterests() &&
413  a.getNOutData() == b.getNOutData() &&
414  a.getNOutNacks() == b.getNOutNacks() &&
415  a.getNInBytes() == b.getNInBytes() &&
416  a.getNOutBytes() == b.getNOutBytes();
417 }
418 
419 std::ostream&
420 operator<<(std::ostream& os, const FaceStatus& status)
421 {
422  os << "Face(FaceId: " << status.getFaceId() << ",\n"
423  << " RemoteUri: " << status.getRemoteUri() << ",\n"
424  << " LocalUri: " << status.getLocalUri() << ",\n";
425 
426  if (status.hasExpirationPeriod()) {
427  os << " ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
428  }
429  else {
430  os << " ExpirationPeriod: infinite,\n";
431  }
432 
433  os << " FaceScope: " << status.getFaceScope() << ",\n"
434  << " FacePersistency: " << status.getFacePersistency() << ",\n"
435  << " LinkType: " << status.getLinkType() << ",\n";
436 
437  if (status.hasBaseCongestionMarkingInterval()) {
438  os << " BaseCongestionMarkingInterval: " << status.getBaseCongestionMarkingInterval() << ",\n";
439  }
440 
441  if (status.hasDefaultCongestionThreshold()) {
442  os << " DefaultCongestionThreshold: " << status.getDefaultCongestionThreshold() << " bytes,\n";
443  }
444 
445  if (status.hasMtu()) {
446  os << " Mtu: " << status.getMtu() << " bytes,\n";
447  }
448 
449  os << " Flags: " << AsHex{status.getFlags()} << ",\n"
450  << " Counters: {Interests: {in: " << status.getNInInterests() << ", "
451  << "out: " << status.getNOutInterests() << "},\n"
452  << " Data: {in: " << status.getNInData() << ", "
453  << "out: " << status.getNOutData() << "},\n"
454  << " Nacks: {in: " << status.getNInNacks() << ", "
455  << "out: " << status.getNOutNacks() << "},\n"
456  << " bytes: {in: " << status.getNInBytes() << ", "
457  << "out: " << status.getNOutBytes() << "}}\n";
458 
459  return os << " )";
460 }
461 
462 } // namespace ndn::nfd
Helper class to convert a number to hexadecimal format, for use with stream insertion operators.
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 item in NFD Face dataset.
Definition: face-status.hpp:38
FaceStatus & setNOutInterests(uint64_t nOutInterests)
const Block & wireEncode() const
Encode FaceStatus.
Definition: face-status.cpp:82
FaceStatus & unsetExpirationPeriod()
FaceStatus & setNOutData(uint64_t nOutData)
FaceStatus & unsetDefaultCongestionThreshold()
FaceStatus & setNOutBytes(uint64_t nOutBytes)
uint64_t getNOutInterests() const
FaceStatus & setNInInterests(uint64_t nInInterests)
FaceStatus & setNOutNacks(uint64_t nOutNacks)
uint64_t getNInData() const
uint64_t getNInBytes() const
void wireDecode(const Block &wire)
Decode FaceStatus.
Definition: face-status.cpp:98
uint64_t getNOutData() const
FaceStatus & setNInData(uint64_t nInData)
FaceStatus & setExpirationPeriod(time::milliseconds expirationPeriod)
FaceStatus & unsetMtu()
uint64_t getDefaultCongestionThreshold() const
Get default congestion threshold (measured in bytes).
bool hasBaseCongestionMarkingInterval() const
Definition: face-status.hpp:82
time::nanoseconds getBaseCongestionMarkingInterval() const
Definition: face-status.hpp:88
uint64_t getNInInterests() const
uint64_t getNInNacks() const
FaceStatus & setNInNacks(uint64_t nInNacks)
FaceStatus & setNInBytes(uint64_t nInBytes)
FaceStatus & setMtu(uint64_t mtu)
Set MTU (measured in bytes).
bool hasExpirationPeriod() const
Definition: face-status.hpp:63
uint64_t getMtu() const
Get MTU (measured in bytes).
FaceStatus & setDefaultCongestionThreshold(uint64_t threshold)
Set default congestion threshold (measured in bytes).
bool hasDefaultCongestionThreshold() const
FaceStatus & setBaseCongestionMarkingInterval(time::nanoseconds interval)
uint64_t getNOutBytes() const
FaceStatus & unsetBaseCongestionMarkingInterval()
time::milliseconds getExpirationPeriod() const
Definition: face-status.hpp:69
uint64_t getNOutNacks() const
uint64_t getFlags() const
const std::string & getLocalUri() const
Definition: face-traits.hpp:74
const std::string & getRemoteUri() const
Definition: face-traits.hpp:60
FacePersistency getFacePersistency() const
uint64_t getFaceId() const
Definition: face-traits.hpp:46
FaceScope getFaceScope() const
Definition: face-traits.hpp:88
LinkType getLinkType() const
#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)
::boost::chrono::milliseconds milliseconds
Definition: time.hpp:52
::boost::chrono::nanoseconds nanoseconds
Definition: time.hpp:54
@ BaseCongestionMarkingInterval
Definition: tlv-nfd.hpp:70
@ DefaultCongestionThreshold
Definition: tlv-nfd.hpp:71
@ ExpirationPeriod
Definition: tlv-nfd.hpp:45