blob: 584208c78a14c5d0ee0afacaf406cd4ab132e8fa [file] [log] [blame]
Junxiao Shi13e637f2014-07-16 19:20:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi7357ef22016-09-07 02:39:37 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Junxiao Shi13e637f2014-07-16 19:20:40 -07004 *
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
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "face-status.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070023#include "encoding/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
25#include "util/concepts.hpp"
Junxiao Shi13e637f2014-07-16 19:20:40 -070026
27namespace ndn {
28namespace nfd {
29
Junxiao Shi65f1a712014-11-20 14:59:36 -070030//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<FaceStatus>));
31BOOST_CONCEPT_ASSERT((WireEncodable<FaceStatus>));
32BOOST_CONCEPT_ASSERT((WireDecodable<FaceStatus>));
33static_assert(std::is_base_of<tlv::Error, FaceStatus::Error>::value,
34 "FaceStatus::Error must inherit from tlv::Error");
35
Junxiao Shi13e637f2014-07-16 19:20:40 -070036FaceStatus::FaceStatus()
Junxiao Shi65f1a712014-11-20 14:59:36 -070037 : m_hasExpirationPeriod(false)
Junxiao Shi13e637f2014-07-16 19:20:40 -070038 , m_nInInterests(0)
39 , m_nInDatas(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070040 , m_nInNacks(0)
Junxiao Shi13e637f2014-07-16 19:20:40 -070041 , m_nOutInterests(0)
42 , m_nOutDatas(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070043 , m_nOutNacks(0)
Junxiao Shi13e637f2014-07-16 19:20:40 -070044 , m_nInBytes(0)
45 , m_nOutBytes(0)
Eric Newberry1ce8ab22016-09-24 11:57:21 -070046 , m_flags(0)
Junxiao Shi13e637f2014-07-16 19:20:40 -070047{
48}
49
Chengyu Fan36dca992014-09-25 13:42:03 -060050FaceStatus::FaceStatus(const Block& block)
51{
52 this->wireDecode(block);
53}
54
Alexander Afanasyev74633892015-02-08 18:08:46 -080055template<encoding::Tag TAG>
Junxiao Shi13e637f2014-07-16 19:20:40 -070056size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080057FaceStatus::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi13e637f2014-07-16 19:20:40 -070058{
59 size_t totalLength = 0;
60
61 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry1ce8ab22016-09-24 11:57:21 -070062 tlv::nfd::Flags, m_flags);
63 totalLength += prependNonNegativeIntegerBlock(encoder,
Junxiao Shi13e637f2014-07-16 19:20:40 -070064 tlv::nfd::NOutBytes, m_nOutBytes);
65 totalLength += prependNonNegativeIntegerBlock(encoder,
66 tlv::nfd::NInBytes, m_nInBytes);
67 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry95bd96a2015-09-04 23:34:22 -070068 tlv::nfd::NOutNacks, m_nOutNacks);
69 totalLength += prependNonNegativeIntegerBlock(encoder,
Junxiao Shi13e637f2014-07-16 19:20:40 -070070 tlv::nfd::NOutDatas, m_nOutDatas);
71 totalLength += prependNonNegativeIntegerBlock(encoder,
72 tlv::nfd::NOutInterests, m_nOutInterests);
73 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry95bd96a2015-09-04 23:34:22 -070074 tlv::nfd::NInNacks, m_nInNacks);
75 totalLength += prependNonNegativeIntegerBlock(encoder,
Junxiao Shi13e637f2014-07-16 19:20:40 -070076 tlv::nfd::NInDatas, m_nInDatas);
77 totalLength += prependNonNegativeIntegerBlock(encoder,
78 tlv::nfd::NInInterests, m_nInInterests);
79 totalLength += prependNonNegativeIntegerBlock(encoder,
Chengyu Fan36dca992014-09-25 13:42:03 -060080 tlv::nfd::LinkType, m_linkType);
81 totalLength += prependNonNegativeIntegerBlock(encoder,
82 tlv::nfd::FacePersistency, m_facePersistency);
83 totalLength += prependNonNegativeIntegerBlock(encoder,
84 tlv::nfd::FaceScope, m_faceScope);
Junxiao Shi13e637f2014-07-16 19:20:40 -070085 if (m_hasExpirationPeriod) {
86 totalLength += prependNonNegativeIntegerBlock(encoder,
87 tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
88 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080089 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Junxiao Shi13e637f2014-07-16 19:20:40 -070090 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080091 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Junxiao Shi13e637f2014-07-16 19:20:40 -070092 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
93 totalLength += prependNonNegativeIntegerBlock(encoder,
94 tlv::nfd::FaceId, m_faceId);
95
96 totalLength += encoder.prependVarNumber(totalLength);
97 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
98 return totalLength;
99}
100
101template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800102FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700103
104template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800105FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700106
107const Block&
108FaceStatus::wireEncode() const
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
123void
124FaceStatus::wireDecode(const Block& block)
125{
126 if (block.type() != tlv::nfd::FaceStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700127 BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700128 }
129 m_wire = block;
130 m_wire.parse();
131 Block::element_const_iterator val = m_wire.elements_begin();
132
133 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
134 m_faceId = readNonNegativeInteger(*val);
135 ++val;
136 }
137 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700138 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700139 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700146 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700147 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700154 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700155 }
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
Chengyu Fan36dca992014-09-25 13:42:03 -0600167 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
168 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700169 ++val;
170 }
171 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700172 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600173 }
174
175 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
176 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
177 ++val;
178 }
179 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700180 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600181 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700188 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700189 }
190
191 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
192 m_nInInterests = readNonNegativeInteger(*val);
193 ++val;
194 }
195 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700196 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700197 }
198
199 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
200 m_nInDatas = readNonNegativeInteger(*val);
201 ++val;
202 }
203 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700204 BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700205 }
206
Eric Newberry95bd96a2015-09-04 23:34:22 -0700207 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
Junxiao Shi13e637f2014-07-16 19:20:40 -0700215 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
216 m_nOutInterests = readNonNegativeInteger(*val);
217 ++val;
218 }
219 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700220 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700221 }
222
223 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
224 m_nOutDatas = readNonNegativeInteger(*val);
225 ++val;
226 }
227 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700228 BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700229 }
230
Eric Newberry95bd96a2015-09-04 23:34:22 -0700231 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
Junxiao Shi13e637f2014-07-16 19:20:40 -0700239 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
240 m_nInBytes = readNonNegativeInteger(*val);
241 ++val;
242 }
243 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700244 BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700245 }
246
247 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
248 m_nOutBytes = readNonNegativeInteger(*val);
249 ++val;
250 }
251 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700252 BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700253 }
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700254
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 }
Junxiao Shi13e637f2014-07-16 19:20:40 -0700262}
263
Chengyu Fan36dca992014-09-25 13:42:03 -0600264FaceStatus&
265FaceStatus::setExpirationPeriod(const time::milliseconds& expirationPeriod)
266{
267 m_wire.reset();
268 m_expirationPeriod = expirationPeriod;
269 m_hasExpirationPeriod = true;
270 return *this;
271}
272
273FaceStatus&
274FaceStatus::setNInInterests(uint64_t nInInterests)
275{
276 m_wire.reset();
277 m_nInInterests = nInInterests;
278 return *this;
279}
280
281FaceStatus&
282FaceStatus::setNInDatas(uint64_t nInDatas)
283{
284 m_wire.reset();
285 m_nInDatas = nInDatas;
286 return *this;
287}
288
289FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700290FaceStatus::setNInNacks(uint64_t nInNacks)
291{
292 m_wire.reset();
293 m_nInNacks = nInNacks;
294 return *this;
295}
296
297FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600298FaceStatus::setNOutInterests(uint64_t nOutInterests)
299{
300 m_wire.reset();
301 m_nOutInterests = nOutInterests;
302 return *this;
303}
304
305FaceStatus&
306FaceStatus::setNOutDatas(uint64_t nOutDatas)
307{
308 m_wire.reset();
309 m_nOutDatas = nOutDatas;
310 return *this;
311}
312
313FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700314FaceStatus::setNOutNacks(uint64_t nOutNacks)
315{
316 m_wire.reset();
317 m_nOutNacks = nOutNacks;
318 return *this;
319}
320
321FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600322FaceStatus::setNInBytes(uint64_t nInBytes)
323{
324 m_wire.reset();
325 m_nInBytes = nInBytes;
326 return *this;
327}
328
329FaceStatus&
330FaceStatus::setNOutBytes(uint64_t nOutBytes)
331{
332 m_wire.reset();
333 m_nOutBytes = nOutBytes;
334 return *this;
335}
336
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700337FaceStatus&
338FaceStatus::setFlags(uint64_t flags)
339{
340 m_wire.reset();
341 m_flags = flags;
342 return *this;
343}
344
345bool
346FaceStatus::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
355FaceStatus&
356FaceStatus::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
Chengyu Fan36dca992014-09-25 13:42:03 -0600374void
375FaceStatus::wireReset() const
376{
377 m_wire.reset();
378}
379
Junxiao Shi13e637f2014-07-16 19:20:40 -0700380std::ostream&
381operator<<(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
Chengyu Fan36dca992014-09-25 13:42:03 -0600395 os << "FaceScope: " << status.getFaceScope() << ",\n"
396 << "FacePersistency: " << status.getFacePersistency() << ",\n"
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700397 << "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() << ", "
Junxiao Shi13e637f2014-07-16 19:20:40 -0700404 << "out: " << status.getNOutInterests() << "},\n"
405 << " Data: {in: " << status.getNInDatas() << ", "
406 << "out: " << status.getNOutDatas() << "},\n"
Eric Newberry95bd96a2015-09-04 23:34:22 -0700407 << " Nack: {in: " << status.getNInNacks() << ", "
408 << "out: " << status.getNOutNacks() << "},\n"
Junxiao Shi13e637f2014-07-16 19:20:40 -0700409 << " bytes: {in: " << status.getNInBytes() << ", "
410 << "out: " << status.getNOutBytes() << "} }\n"
411 << ")";
412 return os;
413}
414
415} // namespace nfd
416} // namespace ndn