face-status.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "face-status.hpp"
23 #include "encoding/tlv-nfd.hpp"
25 #include "util/concepts.hpp"
26 
27 namespace ndn {
28 namespace nfd {
29 
30 //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceStatus>));
31 BOOST_CONCEPT_ASSERT((WireEncodable<FaceStatus>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<FaceStatus>));
33 static_assert(std::is_base_of<tlv::Error, FaceStatus::Error>::value,
34  "FaceStatus::Error must inherit from tlv::Error");
35 
37  : m_hasExpirationPeriod(false)
38  , m_nInInterests(0)
39  , m_nInDatas(0)
40  , m_nInNacks(0)
41  , m_nOutInterests(0)
42  , m_nOutDatas(0)
43  , m_nOutNacks(0)
44  , m_nInBytes(0)
45  , m_nOutBytes(0)
46  , m_flags(0)
47 {
48 }
49 
51 {
52  this->wireDecode(block);
53 }
54 
55 template<encoding::Tag TAG>
56 size_t
57 FaceStatus::wireEncode(EncodingImpl<TAG>& encoder) const
58 {
59  size_t totalLength = 0;
60 
61  totalLength += prependNonNegativeIntegerBlock(encoder,
62  tlv::nfd::Flags, m_flags);
63  totalLength += prependNonNegativeIntegerBlock(encoder,
64  tlv::nfd::NOutBytes, m_nOutBytes);
65  totalLength += prependNonNegativeIntegerBlock(encoder,
66  tlv::nfd::NInBytes, m_nInBytes);
67  totalLength += prependNonNegativeIntegerBlock(encoder,
68  tlv::nfd::NOutNacks, m_nOutNacks);
69  totalLength += prependNonNegativeIntegerBlock(encoder,
70  tlv::nfd::NOutDatas, m_nOutDatas);
71  totalLength += prependNonNegativeIntegerBlock(encoder,
72  tlv::nfd::NOutInterests, m_nOutInterests);
73  totalLength += prependNonNegativeIntegerBlock(encoder,
74  tlv::nfd::NInNacks, m_nInNacks);
75  totalLength += prependNonNegativeIntegerBlock(encoder,
76  tlv::nfd::NInDatas, m_nInDatas);
77  totalLength += prependNonNegativeIntegerBlock(encoder,
78  tlv::nfd::NInInterests, m_nInInterests);
79  totalLength += prependNonNegativeIntegerBlock(encoder,
81  totalLength += prependNonNegativeIntegerBlock(encoder,
83  totalLength += prependNonNegativeIntegerBlock(encoder,
85  if (m_hasExpirationPeriod) {
86  totalLength += prependNonNegativeIntegerBlock(encoder,
87  tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
88  }
89  totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
90  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
91  totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
92  reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
93  totalLength += prependNonNegativeIntegerBlock(encoder,
95 
96  totalLength += encoder.prependVarNumber(totalLength);
97  totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
98  return totalLength;
99 }
100 
101 template size_t
102 FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
103 
104 template size_t
105 FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
106 
107 const Block&
109 {
110  if (m_wire.hasWire())
111  return m_wire;
112 
113  EncodingEstimator estimator;
114  size_t estimatedSize = wireEncode(estimator);
115 
116  EncodingBuffer buffer(estimatedSize, 0);
117  wireEncode(buffer);
118 
119  m_wire = buffer.block();
120  return m_wire;
121 }
122 
123 void
125 {
126  if (block.type() != tlv::nfd::FaceStatus) {
127  BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
128  }
129  m_wire = block;
130  m_wire.parse();
132 
133  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
135  ++val;
136  }
137  else {
138  BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
139  }
140 
141  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
142  m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
143  ++val;
144  }
145  else {
146  BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
147  }
148 
149  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
150  m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
151  ++val;
152  }
153  else {
154  BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
155  }
156 
157  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
158  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
159  m_hasExpirationPeriod = true;
160  ++val;
161  }
162  else {
163  m_hasExpirationPeriod = false;
164  // ExpirationPeriod is optional
165  }
166 
167  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
168  m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
169  ++val;
170  }
171  else {
172  BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
173  }
174 
175  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
177  ++val;
178  }
179  else {
180  BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
181  }
182 
183  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
184  m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
185  ++val;
186  }
187  else {
188  BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
189  }
190 
191  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
192  m_nInInterests = readNonNegativeInteger(*val);
193  ++val;
194  }
195  else {
196  BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
197  }
198 
199  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
200  m_nInDatas = readNonNegativeInteger(*val);
201  ++val;
202  }
203  else {
204  BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
205  }
206 
207  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
208  m_nInNacks = readNonNegativeInteger(*val);
209  ++val;
210  }
211  else {
212  BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
213  }
214 
215  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
216  m_nOutInterests = readNonNegativeInteger(*val);
217  ++val;
218  }
219  else {
220  BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
221  }
222 
223  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
224  m_nOutDatas = readNonNegativeInteger(*val);
225  ++val;
226  }
227  else {
228  BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
229  }
230 
231  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
232  m_nOutNacks = readNonNegativeInteger(*val);
233  ++val;
234  }
235  else {
236  BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
237  }
238 
239  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
240  m_nInBytes = readNonNegativeInteger(*val);
241  ++val;
242  }
243  else {
244  BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
245  }
246 
247  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
248  m_nOutBytes = readNonNegativeInteger(*val);
249  ++val;
250  }
251  else {
252  BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
253  }
254 
255  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
256  m_flags = readNonNegativeInteger(*val);
257  ++val;
258  }
259  else {
260  BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
261  }
262 }
263 
264 FaceStatus&
265 FaceStatus::setExpirationPeriod(const time::milliseconds& expirationPeriod)
266 {
267  m_wire.reset();
268  m_expirationPeriod = expirationPeriod;
269  m_hasExpirationPeriod = true;
270  return *this;
271 }
272 
273 FaceStatus&
274 FaceStatus::setNInInterests(uint64_t nInInterests)
275 {
276  m_wire.reset();
277  m_nInInterests = nInInterests;
278  return *this;
279 }
280 
281 FaceStatus&
282 FaceStatus::setNInDatas(uint64_t nInDatas)
283 {
284  m_wire.reset();
285  m_nInDatas = nInDatas;
286  return *this;
287 }
288 
289 FaceStatus&
290 FaceStatus::setNInNacks(uint64_t nInNacks)
291 {
292  m_wire.reset();
293  m_nInNacks = nInNacks;
294  return *this;
295 }
296 
297 FaceStatus&
298 FaceStatus::setNOutInterests(uint64_t nOutInterests)
299 {
300  m_wire.reset();
301  m_nOutInterests = nOutInterests;
302  return *this;
303 }
304 
305 FaceStatus&
306 FaceStatus::setNOutDatas(uint64_t nOutDatas)
307 {
308  m_wire.reset();
309  m_nOutDatas = nOutDatas;
310  return *this;
311 }
312 
313 FaceStatus&
314 FaceStatus::setNOutNacks(uint64_t nOutNacks)
315 {
316  m_wire.reset();
317  m_nOutNacks = nOutNacks;
318  return *this;
319 }
320 
321 FaceStatus&
322 FaceStatus::setNInBytes(uint64_t nInBytes)
323 {
324  m_wire.reset();
325  m_nInBytes = nInBytes;
326  return *this;
327 }
328 
329 FaceStatus&
330 FaceStatus::setNOutBytes(uint64_t nOutBytes)
331 {
332  m_wire.reset();
333  m_nOutBytes = nOutBytes;
334  return *this;
335 }
336 
337 FaceStatus&
338 FaceStatus::setFlags(uint64_t flags)
339 {
340  m_wire.reset();
341  m_flags = flags;
342  return *this;
343 }
344 
345 bool
346 FaceStatus::getFlagBit(size_t bit) const
347 {
348  if (bit >= 64) {
349  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
350  }
351 
352  return m_flags & (1 << bit);
353 }
354 
355 FaceStatus&
356 FaceStatus::setFlagBit(size_t bit, bool value)
357 {
358  if (bit >= 64) {
359  BOOST_THROW_EXCEPTION(std::out_of_range("bit must be within range [0, 64)"));
360  }
361 
362  m_wire.reset();
363 
364  if (value) {
365  m_flags |= (1 << bit);
366  }
367  else {
368  m_flags &= ~(1 << bit);
369  }
370 
371  return *this;
372 }
373 
374 void
376 {
377  m_wire.reset();
378 }
379 
380 std::ostream&
381 operator<<(std::ostream& os, const FaceStatus& status)
382 {
383  os << "FaceStatus("
384  << "FaceID: " << status.getFaceId() << ",\n"
385  << "RemoteUri: " << status.getRemoteUri() << ",\n"
386  << "LocalUri: " << status.getLocalUri() << ",\n";
387 
388  if (status.hasExpirationPeriod()) {
389  os << "ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
390  }
391  else {
392  os << "ExpirationPeriod: infinite,\n";
393  }
394 
395  os << "FaceScope: " << status.getFaceScope() << ",\n"
396  << "FacePersistency: " << status.getFacePersistency() << ",\n"
397  << "LinkType: " << status.getLinkType() << ",\n";
398 
399  auto osFlags = os.flags();
400  os << "Flags: " << std::showbase << std::hex << status.getFlags() << ",\n";
401  os.flags(osFlags);
402 
403  os << "Counters: { Interests: {in: " << status.getNInInterests() << ", "
404  << "out: " << status.getNOutInterests() << "},\n"
405  << " Data: {in: " << status.getNInDatas() << ", "
406  << "out: " << status.getNOutDatas() << "},\n"
407  << " Nack: {in: " << status.getNInNacks() << ", "
408  << "out: " << status.getNOutNacks() << "},\n"
409  << " bytes: {in: " << status.getNInBytes() << ", "
410  << "out: " << status.getNOutBytes() << "} }\n"
411  << ")";
412  return os;
413 }
414 
415 } // namespace nfd
416 } // namespace ndn
uint64_t getNInNacks() const
Definition: face-status.hpp:97
void wireDecode(const Block &wire)
decode FaceStatus
LinkType getLinkType() const
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
FaceStatus & setNInDatas(uint64_t nInDatas)
EncodingImpl< EstimatorTag > EncodingEstimator
bool hasExpirationPeriod() const
Definition: face-status.hpp:63
uint64_t getFlags() const
FaceScope getFaceScope() const
void wireReset() const
uint64_t getNInDatas() const
Definition: face-status.hpp:88
FaceStatus & setNOutDatas(uint64_t nOutDatas)
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
FaceStatus & setNOutBytes(uint64_t nOutBytes)
FaceStatus & setNOutInterests(uint64_t nOutInterests)
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
element_const_iterator elements_end() const
Definition: block.cpp:595
FaceStatus & setNInInterests(uint64_t nInInterests)
element_const_iterator elements_begin() const
Definition: block.cpp:589
FaceStatus & setNInBytes(uint64_t nInBytes)
uint64_t getNInInterests() const
Definition: face-status.hpp:79
FaceStatus & setNInNacks(uint64_t nInNacks)
EncodingImpl< EncoderTag > EncodingBuffer
FaceStatus & setExpirationPeriod(const time::milliseconds &expirationPeriod)
bool getFlagBit(size_t bit) const
FaceStatus & setFlagBit(size_t bit, bool value)
represents Face status
Definition: face-status.hpp:37
uint64_t getFaceId() const
Definition: face-traits.hpp:58
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
const std::string & getLocalUri() const
Definition: face-traits.hpp:86
uint64_t getNOutBytes() const
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
const std::string & getRemoteUri() const
Definition: face-traits.hpp:72
uint32_t type() const
Definition: block.hpp:324
uint64_t getNOutInterests() const
FaceStatus & setFlags(uint64_t flags)
FacePersistency getFacePersistency() const
const time::milliseconds & getExpirationPeriod() const
Definition: face-status.hpp:69
FaceStatus & setNOutNacks(uint64_t nOutNacks)
uint64_t getNInBytes() const
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
uint64_t getNOutDatas() const
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
const Block & wireEncode() const
encode FaceStatus
uint64_t getNOutNacks() const