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; -*- */
2 /*
3  * Copyright (c) 2013-2017 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 
22 #include "face-status.hpp"
25 #include "encoding/tlv-nfd.hpp"
26 #include "util/concepts.hpp"
27 #include "util/string-helper.hpp"
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
53 FaceStatus::wireEncode(EncodingImpl<TAG>& encoder) const
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);
69  if (m_expirationPeriod) {
71  m_expirationPeriod->count());
72  }
73  totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
74  totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
76 
77  totalLength += encoder.prependVarNumber(totalLength);
78  totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
79  return totalLength;
80 }
81 
83 
84 const Block&
86 {
87  if (m_wire.hasWire())
88  return m_wire;
89 
90  EncodingEstimator estimator;
91  size_t estimatedSize = wireEncode(estimator);
92 
93  EncodingBuffer buffer(estimatedSize, 0);
94  wireEncode(buffer);
95 
96  m_wire = buffer.block();
97  return m_wire;
98 }
99 
100 void
102 {
103  if (block.type() != tlv::nfd::FaceStatus) {
104  BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
105  }
106  m_wire = block;
107  m_wire.parse();
109 
110  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
112  ++val;
113  }
114  else {
115  BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
116  }
117 
118  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
119  m_remoteUri = readString(*val);
120  ++val;
121  }
122  else {
123  BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
124  }
125 
126  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
127  m_localUri = readString(*val);
128  ++val;
129  }
130  else {
131  BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
132  }
133 
134  if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
135  m_expirationPeriod.emplace(readNonNegativeInteger(*val));
136  ++val;
137  }
138  else {
139  m_expirationPeriod = nullopt;
140  }
141 
142  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
143  m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
144  ++val;
145  }
146  else {
147  BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
148  }
149 
150  if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
151  m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
152  ++val;
153  }
154  else {
155  BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
156  }
157 
158  if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
159  m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
160  ++val;
161  }
162  else {
163  BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
164  }
165 
166  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
167  m_nInInterests = readNonNegativeInteger(*val);
168  ++val;
169  }
170  else {
171  BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
172  }
173 
174  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
175  m_nInData = readNonNegativeInteger(*val);
176  ++val;
177  }
178  else {
179  BOOST_THROW_EXCEPTION(Error("missing required NInData field"));
180  }
181 
182  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
183  m_nInNacks = readNonNegativeInteger(*val);
184  ++val;
185  }
186  else {
187  BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
188  }
189 
190  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
191  m_nOutInterests = readNonNegativeInteger(*val);
192  ++val;
193  }
194  else {
195  BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
196  }
197 
198  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
199  m_nOutData = readNonNegativeInteger(*val);
200  ++val;
201  }
202  else {
203  BOOST_THROW_EXCEPTION(Error("missing required NOutData field"));
204  }
205 
206  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
207  m_nOutNacks = readNonNegativeInteger(*val);
208  ++val;
209  }
210  else {
211  BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
212  }
213 
214  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
215  m_nInBytes = readNonNegativeInteger(*val);
216  ++val;
217  }
218  else {
219  BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
220  }
221 
222  if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
223  m_nOutBytes = readNonNegativeInteger(*val);
224  ++val;
225  }
226  else {
227  BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
228  }
229 
230  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
232  ++val;
233  }
234  else {
235  BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
236  }
237 }
238 
239 FaceStatus&
241 {
242  m_wire.reset();
243  m_expirationPeriod = expirationPeriod;
244  return *this;
245 }
246 
247 FaceStatus&
249 {
250  m_wire.reset();
251  m_expirationPeriod = nullopt;
252  return *this;
253 }
254 
255 FaceStatus&
256 FaceStatus::setNInInterests(uint64_t nInInterests)
257 {
258  m_wire.reset();
259  m_nInInterests = nInInterests;
260  return *this;
261 }
262 
263 FaceStatus&
264 FaceStatus::setNInData(uint64_t nInData)
265 {
266  m_wire.reset();
267  m_nInData = nInData;
268  return *this;
269 }
270 
271 FaceStatus&
272 FaceStatus::setNInNacks(uint64_t nInNacks)
273 {
274  m_wire.reset();
275  m_nInNacks = nInNacks;
276  return *this;
277 }
278 
279 FaceStatus&
280 FaceStatus::setNOutInterests(uint64_t nOutInterests)
281 {
282  m_wire.reset();
283  m_nOutInterests = nOutInterests;
284  return *this;
285 }
286 
287 FaceStatus&
288 FaceStatus::setNOutData(uint64_t nOutData)
289 {
290  m_wire.reset();
291  m_nOutData = nOutData;
292  return *this;
293 }
294 
295 FaceStatus&
296 FaceStatus::setNOutNacks(uint64_t nOutNacks)
297 {
298  m_wire.reset();
299  m_nOutNacks = nOutNacks;
300  return *this;
301 }
302 
303 FaceStatus&
304 FaceStatus::setNInBytes(uint64_t nInBytes)
305 {
306  m_wire.reset();
307  m_nInBytes = nInBytes;
308  return *this;
309 }
310 
311 FaceStatus&
312 FaceStatus::setNOutBytes(uint64_t nOutBytes)
313 {
314  m_wire.reset();
315  m_nOutBytes = nOutBytes;
316  return *this;
317 }
318 
319 bool
320 operator==(const FaceStatus& a, const FaceStatus& b)
321 {
322  return a.getFaceId() == b.getFaceId() &&
323  a.getRemoteUri() == b.getRemoteUri() &&
324  a.getLocalUri() == b.getLocalUri() &&
325  a.getFaceScope() == b.getFaceScope() &&
327  a.getLinkType() == b.getLinkType() &&
328  a.getFlags() == b.getFlags() &&
331  a.getNInInterests() == b.getNInInterests() &&
332  a.getNInData() == b.getNInData() &&
333  a.getNInNacks() == b.getNInNacks() &&
334  a.getNOutInterests() == b.getNOutInterests() &&
335  a.getNOutData() == b.getNOutData() &&
336  a.getNOutNacks() == b.getNOutNacks() &&
337  a.getNInBytes() == b.getNInBytes() &&
338  a.getNOutBytes() == b.getNOutBytes();
339 }
340 
341 std::ostream&
342 operator<<(std::ostream& os, const FaceStatus& status)
343 {
344  os << "Face(FaceId: " << status.getFaceId() << ",\n"
345  << " RemoteUri: " << status.getRemoteUri() << ",\n"
346  << " LocalUri: " << status.getLocalUri() << ",\n";
347 
348  if (status.hasExpirationPeriod()) {
349  os << " ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
350  }
351  else {
352  os << " ExpirationPeriod: infinite,\n";
353  }
354 
355  os << " FaceScope: " << status.getFaceScope() << ",\n"
356  << " FacePersistency: " << status.getFacePersistency() << ",\n"
357  << " LinkType: " << status.getLinkType() << ",\n"
358  << " Flags: " << AsHex{status.getFlags()} << ",\n"
359  << " Counters: {Interests: {in: " << status.getNInInterests() << ", "
360  << "out: " << status.getNOutInterests() << "},\n"
361  << " Data: {in: " << status.getNInData() << ", "
362  << "out: " << status.getNOutData() << "},\n"
363  << " Nacks: {in: " << status.getNInNacks() << ", "
364  << "out: " << status.getNOutNacks() << "},\n"
365  << " bytes: {in: " << status.getNInBytes() << ", "
366  << "out: " << status.getNOutBytes() << "}}\n";
367 
368  return os << " )";
369 }
370 
371 } // namespace nfd
372 } // namespace ndn
constexpr nullopt_t nullopt
uint64_t getNInNacks() const
Definition: face-status.hpp:99
void wireDecode(const Block &wire)
decode FaceStatus
LinkType getLinkType() const
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.
uint64_t getFlags() const
bool hasExpirationPeriod() const
Definition: face-status.hpp:62
FaceScope getFaceScope() const
Definition: face-traits.hpp:93
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
time::milliseconds getExpirationPeriod() const
Definition: face-status.hpp:68
Helper class to convert a number to hexadecimal format, for use with stream insertion operators...
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ChannelStatus)
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
FaceStatus & setNOutBytes(uint64_t nOutBytes)
FaceStatus & setNOutInterests(uint64_t nOutInterests)
void emplace(Args &&...args)
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
FaceStatus & setExpirationPeriod(time::milliseconds expirationPeriod)
FaceStatus & setNInInterests(uint64_t nInInterests)
FaceStatus & setNInBytes(uint64_t nInBytes)
FaceStatus & unsetExpirationPeriod()
uint64_t getNInInterests() const
Definition: face-status.hpp:81
FaceStatus & setNInNacks(uint64_t nInNacks)
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
FaceStatus & setNOutData(uint64_t nOutData)
bool operator==(const ChannelStatus &a, const ChannelStatus &b)
represents an item in NFD Face dataset
Definition: face-status.hpp:36
uint64_t getNOutData() const
uint64_t getFaceId() const
Definition: face-traits.hpp:51
std::ostream & operator<<(std::ostream &os, FaceScope faceScope)
void reset()
Reset wire buffer of the element.
Definition: block.cpp:256
const std::string & getLocalUri() const
Definition: face-traits.hpp:79
concept check for an item in a Status Dataset
Definition: concepts.hpp:115
uint64_t getNOutBytes() const
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:334
const std::string & getRemoteUri() const
Definition: face-traits.hpp:65
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
uint64_t getNOutInterests() const
FacePersistency getFacePersistency() const
FaceStatus & setNOutNacks(uint64_t nOutNacks)
uint64_t getNInBytes() const
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:250
uint64_t getNInData() const
Definition: face-status.hpp:90
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:363
FaceStatus & setNInData(uint64_t nInData)
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:355
const Block & wireEncode() const
encode FaceStatus
Definition: face-status.cpp:85
uint64_t getNOutNacks() const
EncodingImpl< EncoderTag > EncodingBuffer
EncodingImpl< EstimatorTag > EncodingEstimator