NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
nfd-face-status.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "nfd-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,
61  tlv::nfd::NOutBytes, m_nOutBytes);
62  totalLength += prependNonNegativeIntegerBlock(encoder,
63  tlv::nfd::NInBytes, m_nInBytes);
64  totalLength += prependNonNegativeIntegerBlock(encoder,
65  tlv::nfd::NOutNacks, m_nOutNacks);
66  totalLength += prependNonNegativeIntegerBlock(encoder,
67  tlv::nfd::NOutDatas, m_nOutDatas);
68  totalLength += prependNonNegativeIntegerBlock(encoder,
69  tlv::nfd::NOutInterests, m_nOutInterests);
70  totalLength += prependNonNegativeIntegerBlock(encoder,
71  tlv::nfd::NInNacks, m_nInNacks);
72  totalLength += prependNonNegativeIntegerBlock(encoder,
73  tlv::nfd::NInDatas, m_nInDatas);
74  totalLength += prependNonNegativeIntegerBlock(encoder,
75  tlv::nfd::NInInterests, m_nInInterests);
76  totalLength += prependNonNegativeIntegerBlock(encoder,
78  totalLength += prependNonNegativeIntegerBlock(encoder,
80  totalLength += prependNonNegativeIntegerBlock(encoder,
82  if (m_hasExpirationPeriod) {
83  totalLength += prependNonNegativeIntegerBlock(encoder,
84  tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
85  }
86  totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
87  reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
88  totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
89  reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
90  totalLength += prependNonNegativeIntegerBlock(encoder,
92 
93  totalLength += encoder.prependVarNumber(totalLength);
94  totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
95  return totalLength;
96 }
97 
98 template size_t
99 FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
100 
101 template size_t
102 FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
103 
104 const Block&
106 {
107  if (m_wire.hasWire())
108  return m_wire;
109 
110  EncodingEstimator estimator;
111  size_t estimatedSize = wireEncode(estimator);
112 
113  EncodingBuffer buffer(estimatedSize, 0);
114  wireEncode(buffer);
115 
116  m_wire = buffer.block();
117  return m_wire;
118 }
119 
120 void
122 {
123  if (block.type() != tlv::nfd::FaceStatus) {
124  BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
125  }
126  m_wire = block;
127  m_wire.parse();
129 
130  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
132  ++val;
133  }
134  else {
135  BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
136  }
137 
138  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
139  m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
140  ++val;
141  }
142  else {
143  BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
144  }
145 
146  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
147  m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
148  ++val;
149  }
150  else {
151  BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
152  }
153 
154  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
155  m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
156  m_hasExpirationPeriod = true;
157  ++val;
158  }
159  else {
160  m_hasExpirationPeriod = false;
161  // ExpirationPeriod is optional
162  }
163 
164  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
165  m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
166  ++val;
167  }
168  else {
169  BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
170  }
171 
172  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
174  ++val;
175  }
176  else {
177  BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
178  }
179 
180  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
181  m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
182  ++val;
183  }
184  else {
185  BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
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  BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
194  }
195 
196  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
197  m_nInDatas = readNonNegativeInteger(*val);
198  ++val;
199  }
200  else {
201  BOOST_THROW_EXCEPTION(Error("missing required NInDatas 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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
218  }
219 
220  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
221  m_nOutDatas = readNonNegativeInteger(*val);
222  ++val;
223  }
224  else {
225  BOOST_THROW_EXCEPTION(Error("missing required NOutDatas 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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(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  BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
250  }
251 }
252 
253 FaceStatus&
254 FaceStatus::setExpirationPeriod(const time::milliseconds& expirationPeriod)
255 {
256  m_wire.reset();
257  m_expirationPeriod = expirationPeriod;
258  m_hasExpirationPeriod = true;
259  return *this;
260 }
261 
262 FaceStatus&
263 FaceStatus::setNInInterests(uint64_t nInInterests)
264 {
265  m_wire.reset();
266  m_nInInterests = nInInterests;
267  return *this;
268 }
269 
270 FaceStatus&
271 FaceStatus::setNInDatas(uint64_t nInDatas)
272 {
273  m_wire.reset();
274  m_nInDatas = nInDatas;
275  return *this;
276 }
277 
278 FaceStatus&
279 FaceStatus::setNInNacks(uint64_t nInNacks)
280 {
281  m_wire.reset();
282  m_nInNacks = nInNacks;
283  return *this;
284 }
285 
286 FaceStatus&
287 FaceStatus::setNOutInterests(uint64_t nOutInterests)
288 {
289  m_wire.reset();
290  m_nOutInterests = nOutInterests;
291  return *this;
292 }
293 
294 FaceStatus&
295 FaceStatus::setNOutDatas(uint64_t nOutDatas)
296 {
297  m_wire.reset();
298  m_nOutDatas = nOutDatas;
299  return *this;
300 }
301 
302 FaceStatus&
303 FaceStatus::setNOutNacks(uint64_t nOutNacks)
304 {
305  m_wire.reset();
306  m_nOutNacks = nOutNacks;
307  return *this;
308 }
309 
310 FaceStatus&
311 FaceStatus::setNInBytes(uint64_t nInBytes)
312 {
313  m_wire.reset();
314  m_nInBytes = nInBytes;
315  return *this;
316 }
317 
318 FaceStatus&
319 FaceStatus::setNOutBytes(uint64_t nOutBytes)
320 {
321  m_wire.reset();
322  m_nOutBytes = nOutBytes;
323  return *this;
324 }
325 
326 void
328 {
329  m_wire.reset();
330 }
331 
332 std::ostream&
333 operator<<(std::ostream& os, const FaceStatus& status)
334 {
335  os << "FaceStatus("
336  << "FaceID: " << status.getFaceId() << ",\n"
337  << "RemoteUri: " << status.getRemoteUri() << ",\n"
338  << "LocalUri: " << status.getLocalUri() << ",\n";
339 
340  if (status.hasExpirationPeriod()) {
341  os << "ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
342  }
343  else {
344  os << "ExpirationPeriod: infinite,\n";
345  }
346 
347  os << "FaceScope: " << status.getFaceScope() << ",\n"
348  << "FacePersistency: " << status.getFacePersistency() << ",\n"
349  << "LinkType: " << status.getLinkType() << ",\n"
350  << "Counters: { Interests: {in: " << status.getNInInterests() << ", "
351  << "out: " << status.getNOutInterests() << "},\n"
352  << " Data: {in: " << status.getNInDatas() << ", "
353  << "out: " << status.getNOutDatas() << "},\n"
354  << " Nack: {in: " << status.getNInNacks() << ", "
355  << "out: " << status.getNOutNacks() << "},\n"
356  << " bytes: {in: " << status.getNInBytes() << ", "
357  << "out: " << status.getNOutBytes() << "} }\n"
358  << ")";
359  return os;
360 }
361 
362 } // namespace nfd
363 } // namespace ndn
const std::string & getLocalUri() const
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
FaceStatus & setNInDatas(uint64_t nInDatas)
EncodingImpl< EstimatorTag > EncodingEstimator
bool hasExpirationPeriod() const
uint64_t getNOutNacks() const
uint64_t getNOutBytes() const
uint64_t getNInInterests() const
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
FaceStatus & setNInInterests(uint64_t nInInterests)
uint64_t getNInBytes() const
FaceStatus & setNInBytes(uint64_t nInBytes)
FaceStatus & setNInNacks(uint64_t nInNacks)
EncodingImpl< EncoderTag > EncodingBuffer
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
FaceStatus & setExpirationPeriod(const time::milliseconds &expirationPeriod)
represents Face status
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
FaceStatus & setNOutNacks(uint64_t nOutNacks)
const Block & wireEncode() const
encode FaceStatus
uint64_t getFaceId() 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
uint32_t type() const
Definition: block.hpp:346
LinkType getLinkType() const
uint64_t getNInNacks() const