NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
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 {
47 }
48 
50 {
51  this->wireDecode(block);
52 }
53 
54 template<encoding::Tag TAG>
55 size_t
57 {
58  size_t totalLength = 0;
59 
60  totalLength += prependNonNegativeIntegerBlock(encoder,
62  totalLength += prependNonNegativeIntegerBlock(encoder,
63  tlv::nfd::NOutBytes, m_nOutBytes);
64  totalLength += prependNonNegativeIntegerBlock(encoder,
65  tlv::nfd::NInBytes, m_nInBytes);
66  totalLength += prependNonNegativeIntegerBlock(encoder,
67  tlv::nfd::NOutNacks, m_nOutNacks);
68  totalLength += prependNonNegativeIntegerBlock(encoder,
69  tlv::nfd::NOutDatas, m_nOutDatas);
70  totalLength += prependNonNegativeIntegerBlock(encoder,
71  tlv::nfd::NOutInterests, m_nOutInterests);
72  totalLength += prependNonNegativeIntegerBlock(encoder,
73  tlv::nfd::NInNacks, m_nInNacks);
74  totalLength += prependNonNegativeIntegerBlock(encoder,
75  tlv::nfd::NInDatas, m_nInDatas);
76  totalLength += prependNonNegativeIntegerBlock(encoder,
77  tlv::nfd::NInInterests, m_nInInterests);
78  totalLength += prependNonNegativeIntegerBlock(encoder,
80  totalLength += prependNonNegativeIntegerBlock(encoder,
82  totalLength += prependNonNegativeIntegerBlock(encoder,
84  if (m_hasExpirationPeriod) {
85  totalLength += prependNonNegativeIntegerBlock(encoder,
86  tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
87  }
88  totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
89  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
90  totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
91  reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
92  totalLength += prependNonNegativeIntegerBlock(encoder,
94 
95  totalLength += encoder.prependVarNumber(totalLength);
96  totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
97  return totalLength;
98 }
99 
100 template size_t
101 FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
102 
103 template size_t
104 FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
105 
106 const Block&
108 {
109  if (m_wire.hasWire())
110  return m_wire;
111 
112  EncodingEstimator estimator;
113  size_t estimatedSize = wireEncode(estimator);
114 
115  EncodingBuffer buffer(estimatedSize, 0);
116  wireEncode(buffer);
117 
118  m_wire = buffer.block();
119  return m_wire;
120 }
121 
122 void
124 {
125  if (block.type() != tlv::nfd::FaceStatus) {
126  BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
127  }
128  m_wire = block;
129  m_wire.parse();
131 
132  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
134  ++val;
135  }
136  else {
137  BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
138  }
139 
140  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
141  m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
142  ++val;
143  }
144  else {
145  BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
146  }
147 
148  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
149  m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
150  ++val;
151  }
152  else {
153  BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
154  }
155 
156  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
157  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
158  m_hasExpirationPeriod = true;
159  ++val;
160  }
161  else {
162  m_hasExpirationPeriod = false;
163  // ExpirationPeriod is optional
164  }
165 
166  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
167  m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
168  ++val;
169  }
170  else {
171  BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
172  }
173 
174  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
176  ++val;
177  }
178  else {
179  BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
180  }
181 
182  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
183  m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
184  ++val;
185  }
186  else {
187  BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
188  }
189 
190  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
191  m_nInInterests = readNonNegativeInteger(*val);
192  ++val;
193  }
194  else {
195  BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
196  }
197 
198  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
199  m_nInDatas = readNonNegativeInteger(*val);
200  ++val;
201  }
202  else {
203  BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
204  }
205 
206  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
207  m_nInNacks = readNonNegativeInteger(*val);
208  ++val;
209  }
210  else {
211  BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
212  }
213 
214  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
215  m_nOutInterests = readNonNegativeInteger(*val);
216  ++val;
217  }
218  else {
219  BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
220  }
221 
222  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
223  m_nOutDatas = readNonNegativeInteger(*val);
224  ++val;
225  }
226  else {
227  BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
228  }
229 
230  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
231  m_nOutNacks = readNonNegativeInteger(*val);
232  ++val;
233  }
234  else {
235  BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
236  }
237 
238  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
239  m_nInBytes = readNonNegativeInteger(*val);
240  ++val;
241  }
242  else {
243  BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
244  }
245 
246  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
247  m_nOutBytes = readNonNegativeInteger(*val);
248  ++val;
249  }
250  else {
251  BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
252  }
253 
254  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
256  ++val;
257  }
258  else {
259  BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
260  }
261 }
262 
263 FaceStatus&
264 FaceStatus::setExpirationPeriod(const time::milliseconds& expirationPeriod)
265 {
266  m_wire.reset();
267  m_expirationPeriod = expirationPeriod;
268  m_hasExpirationPeriod = true;
269  return *this;
270 }
271 
272 FaceStatus&
273 FaceStatus::setNInInterests(uint64_t nInInterests)
274 {
275  m_wire.reset();
276  m_nInInterests = nInInterests;
277  return *this;
278 }
279 
280 FaceStatus&
281 FaceStatus::setNInDatas(uint64_t nInDatas)
282 {
283  m_wire.reset();
284  m_nInDatas = nInDatas;
285  return *this;
286 }
287 
288 FaceStatus&
289 FaceStatus::setNInNacks(uint64_t nInNacks)
290 {
291  m_wire.reset();
292  m_nInNacks = nInNacks;
293  return *this;
294 }
295 
296 FaceStatus&
297 FaceStatus::setNOutInterests(uint64_t nOutInterests)
298 {
299  m_wire.reset();
300  m_nOutInterests = nOutInterests;
301  return *this;
302 }
303 
304 FaceStatus&
305 FaceStatus::setNOutDatas(uint64_t nOutDatas)
306 {
307  m_wire.reset();
308  m_nOutDatas = nOutDatas;
309  return *this;
310 }
311 
312 FaceStatus&
313 FaceStatus::setNOutNacks(uint64_t nOutNacks)
314 {
315  m_wire.reset();
316  m_nOutNacks = nOutNacks;
317  return *this;
318 }
319 
320 FaceStatus&
321 FaceStatus::setNInBytes(uint64_t nInBytes)
322 {
323  m_wire.reset();
324  m_nInBytes = nInBytes;
325  return *this;
326 }
327 
328 FaceStatus&
329 FaceStatus::setNOutBytes(uint64_t nOutBytes)
330 {
331  m_wire.reset();
332  m_nOutBytes = nOutBytes;
333  return *this;
334 }
335 
336 void
338 {
339  m_wire.reset();
340 }
341 
342 std::ostream&
343 operator<<(std::ostream& os, const FaceStatus& status)
344 {
345  os << "FaceStatus("
346  << "FaceID: " << status.getFaceId() << ",\n"
347  << "RemoteUri: " << status.getRemoteUri() << ",\n"
348  << "LocalUri: " << status.getLocalUri() << ",\n";
349 
350  if (status.hasExpirationPeriod()) {
351  os << "ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
352  }
353  else {
354  os << "ExpirationPeriod: infinite,\n";
355  }
356 
357  os << "FaceScope: " << status.getFaceScope() << ",\n"
358  << "FacePersistency: " << status.getFacePersistency() << ",\n"
359  << "LinkType: " << status.getLinkType() << ",\n";
360 
361  auto osFlags = os.flags();
362  os << "Flags: " << std::showbase << std::hex << status.getFlags() << ",\n";
363  os.flags(osFlags);
364 
365  os << "Counters: { Interests: {in: " << status.getNInInterests() << ", "
366  << "out: " << status.getNOutInterests() << "},\n"
367  << " Data: {in: " << status.getNInDatas() << ", "
368  << "out: " << status.getNOutDatas() << "},\n"
369  << " Nack: {in: " << status.getNInNacks() << ", "
370  << "out: " << status.getNOutNacks() << "},\n"
371  << " bytes: {in: " << status.getNInBytes() << ", "
372  << "out: " << status.getNOutBytes() << "} }\n"
373  << ")";
374  return os;
375 }
376 
377 } // namespace nfd
378 } // namespace ndn
const std::string & getLocalUri() const
Definition: face-traits.hpp:87
void wireDecode(const Block &wire)
decode FaceStatus
uint64_t getNOutInterests() const
element_const_iterator elements_begin() const
Definition: block.cpp:589
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
FacePersistency getFacePersistency() const
const time::milliseconds & getExpirationPeriod() const
Definition: face-status.hpp:69
FaceStatus & setNInDatas(uint64_t nInDatas)
EncodingImpl< EstimatorTag > EncodingEstimator
bool hasExpirationPeriod() const
Definition: face-status.hpp:63
uint64_t getNOutNacks() const
uint64_t getNOutBytes() const
uint64_t getNInInterests() const
Definition: face-status.hpp:79
element_const_iterator elements_end() const
Definition: block.cpp:595
FaceStatus & setNOutDatas(uint64_t nOutDatas)
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
FaceScope getFaceScope() const
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.
const std::string & getRemoteUri() const
Definition: face-traits.hpp:73
FaceStatus & setNInInterests(uint64_t nInInterests)
uint64_t getNInBytes() const
FaceStatus & setNInBytes(uint64_t nInBytes)
FaceStatus & setNInNacks(uint64_t nInNacks)
EncodingImpl< EncoderTag > EncodingBuffer
void wireReset() const
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
FaceStatus & setExpirationPeriod(const time::milliseconds &expirationPeriod)
represents Face status
Definition: face-status.hpp:37
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
uint64_t getNOutDatas() const
uint64_t getNInDatas() const
Definition: face-status.hpp:88
uint64_t getFlags() const
FaceStatus & setNOutNacks(uint64_t nOutNacks)
const Block & wireEncode() const
encode FaceStatus
uint64_t getFaceId() const
Definition: face-traits.hpp:59
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
uint32_t type() const
Definition: block.hpp:324
LinkType getLinkType() const
uint64_t getNInNacks() const
Definition: face-status.hpp:97