blob: 3361d9927205f448917d9a2e4cb43ffbeffaced7 [file] [log] [blame]
Junxiao Shi13e637f2014-07-16 19:20:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -05003 * Copyright (c) 2013-2017 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/block-helpers.hpp"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050024#include "encoding/encoding-buffer.hpp"
25#include "encoding/tlv-nfd.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070026#include "util/concepts.hpp"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -050027#include "util/string-helper.hpp"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050028
Junxiao Shi13e637f2014-07-16 19:20:40 -070029namespace ndn {
30namespace nfd {
31
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050032BOOST_CONCEPT_ASSERT((StatusDatasetItem<FaceStatus>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070033
Junxiao Shi13e637f2014-07-16 19:20:40 -070034FaceStatus::FaceStatus()
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050035 : m_nInInterests(0)
36 , m_nInData(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070037 , m_nInNacks(0)
Junxiao Shi13e637f2014-07-16 19:20:40 -070038 , m_nOutInterests(0)
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050039 , m_nOutData(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070040 , m_nOutNacks(0)
Junxiao Shi13e637f2014-07-16 19:20:40 -070041 , m_nInBytes(0)
42 , m_nOutBytes(0)
43{
44}
45
Chengyu Fan36dca992014-09-25 13:42:03 -060046FaceStatus::FaceStatus(const Block& block)
47{
48 this->wireDecode(block);
49}
50
Alexander Afanasyev74633892015-02-08 18:08:46 -080051template<encoding::Tag TAG>
Junxiao Shi13e637f2014-07-16 19:20:40 -070052size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080053FaceStatus::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi13e637f2014-07-16 19:20:40 -070054{
55 size_t totalLength = 0;
56
Junxiao Shi9a53d782017-04-04 20:09:57 +000057 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);
66 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::LinkType, m_linkType);
67 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FacePersistency, m_facePersistency);
68 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceScope, m_faceScope);
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050069 if (m_expirationPeriod) {
Junxiao Shi13e637f2014-07-16 19:20:40 -070070 totalLength += prependNonNegativeIntegerBlock(encoder,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050071 tlv::nfd::ExpirationPeriod, static_cast<uint64_t>(m_expirationPeriod->count()));
Junxiao Shi13e637f2014-07-16 19:20:40 -070072 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080073 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050074 reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080075 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050076 reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
Junxiao Shi13e637f2014-07-16 19:20:40 -070077 totalLength += prependNonNegativeIntegerBlock(encoder,
78 tlv::nfd::FaceId, m_faceId);
79
80 totalLength += encoder.prependVarNumber(totalLength);
81 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
82 return totalLength;
83}
84
85template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080086FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -070087
88template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080089FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -070090
91const Block&
92FaceStatus::wireEncode() const
93{
94 if (m_wire.hasWire())
95 return m_wire;
96
97 EncodingEstimator estimator;
98 size_t estimatedSize = wireEncode(estimator);
99
100 EncodingBuffer buffer(estimatedSize, 0);
101 wireEncode(buffer);
102
103 m_wire = buffer.block();
104 return m_wire;
105}
106
107void
108FaceStatus::wireDecode(const Block& block)
109{
110 if (block.type() != tlv::nfd::FaceStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700111 BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700112 }
113 m_wire = block;
114 m_wire.parse();
115 Block::element_const_iterator val = m_wire.elements_begin();
116
117 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
118 m_faceId = readNonNegativeInteger(*val);
119 ++val;
120 }
121 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700122 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700123 }
124
125 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
126 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
127 ++val;
128 }
129 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700130 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700131 }
132
133 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
134 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
135 ++val;
136 }
137 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700138 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700139 }
140
141 if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500142 m_expirationPeriod.emplace(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700143 ++val;
144 }
145 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500146 m_expirationPeriod = nullopt;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700147 }
148
Chengyu Fan36dca992014-09-25 13:42:03 -0600149 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
150 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700151 ++val;
152 }
153 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700154 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600155 }
156
157 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
158 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
159 ++val;
160 }
161 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700162 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600163 }
164
165 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
166 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
167 ++val;
168 }
169 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700170 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700171 }
172
173 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
174 m_nInInterests = readNonNegativeInteger(*val);
175 ++val;
176 }
177 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700178 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700179 }
180
Junxiao Shi9a53d782017-04-04 20:09:57 +0000181 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500182 m_nInData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700183 ++val;
184 }
185 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000186 BOOST_THROW_EXCEPTION(Error("missing required NInData field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700187 }
188
Eric Newberry95bd96a2015-09-04 23:34:22 -0700189 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
190 m_nInNacks = readNonNegativeInteger(*val);
191 ++val;
192 }
193 else {
194 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
195 }
196
Junxiao Shi13e637f2014-07-16 19:20:40 -0700197 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
198 m_nOutInterests = readNonNegativeInteger(*val);
199 ++val;
200 }
201 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700202 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700203 }
204
Junxiao Shi9a53d782017-04-04 20:09:57 +0000205 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500206 m_nOutData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700207 ++val;
208 }
209 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000210 BOOST_THROW_EXCEPTION(Error("missing required NOutData field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700211 }
212
Eric Newberry95bd96a2015-09-04 23:34:22 -0700213 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
214 m_nOutNacks = readNonNegativeInteger(*val);
215 ++val;
216 }
217 else {
218 BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
219 }
220
Junxiao Shi13e637f2014-07-16 19:20:40 -0700221 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
222 m_nInBytes = readNonNegativeInteger(*val);
223 ++val;
224 }
225 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700226 BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700227 }
228
229 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
230 m_nOutBytes = readNonNegativeInteger(*val);
231 ++val;
232 }
233 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700234 BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700235 }
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700236
237 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
238 m_flags = readNonNegativeInteger(*val);
239 ++val;
240 }
241 else {
242 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
243 }
Junxiao Shi13e637f2014-07-16 19:20:40 -0700244}
245
Chengyu Fan36dca992014-09-25 13:42:03 -0600246FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500247FaceStatus::setExpirationPeriod(time::milliseconds expirationPeriod)
Chengyu Fan36dca992014-09-25 13:42:03 -0600248{
249 m_wire.reset();
250 m_expirationPeriod = expirationPeriod;
Chengyu Fan36dca992014-09-25 13:42:03 -0600251 return *this;
252}
253
254FaceStatus&
Davide Pesavento156c1ea2017-03-19 16:09:33 -0400255FaceStatus::unsetExpirationPeriod()
256{
257 m_wire.reset();
258 m_expirationPeriod = nullopt;
259 return *this;
260}
261
262FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600263FaceStatus::setNInInterests(uint64_t nInInterests)
264{
265 m_wire.reset();
266 m_nInInterests = nInInterests;
267 return *this;
268}
269
270FaceStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000271FaceStatus::setNInData(uint64_t nInData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600272{
273 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500274 m_nInData = nInData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600275 return *this;
276}
277
278FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700279FaceStatus::setNInNacks(uint64_t nInNacks)
280{
281 m_wire.reset();
282 m_nInNacks = nInNacks;
283 return *this;
284}
285
286FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600287FaceStatus::setNOutInterests(uint64_t nOutInterests)
288{
289 m_wire.reset();
290 m_nOutInterests = nOutInterests;
291 return *this;
292}
293
294FaceStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000295FaceStatus::setNOutData(uint64_t nOutData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600296{
297 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500298 m_nOutData = nOutData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600299 return *this;
300}
301
302FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700303FaceStatus::setNOutNacks(uint64_t nOutNacks)
304{
305 m_wire.reset();
306 m_nOutNacks = nOutNacks;
307 return *this;
308}
309
310FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600311FaceStatus::setNInBytes(uint64_t nInBytes)
312{
313 m_wire.reset();
314 m_nInBytes = nInBytes;
315 return *this;
316}
317
318FaceStatus&
319FaceStatus::setNOutBytes(uint64_t nOutBytes)
320{
321 m_wire.reset();
322 m_nOutBytes = nOutBytes;
323 return *this;
324}
325
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500326bool
327operator==(const FaceStatus& a, const FaceStatus& b)
328{
329 return a.getFaceId() == b.getFaceId() &&
330 a.getRemoteUri() == b.getRemoteUri() &&
331 a.getLocalUri() == b.getLocalUri() &&
332 a.getFaceScope() == b.getFaceScope() &&
333 a.getFacePersistency() == b.getFacePersistency() &&
334 a.getLinkType() == b.getLinkType() &&
335 a.getFlags() == b.getFlags() &&
336 a.hasExpirationPeriod() == b.hasExpirationPeriod() &&
337 (!a.hasExpirationPeriod() || a.getExpirationPeriod() == b.getExpirationPeriod()) &&
338 a.getNInInterests() == b.getNInInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000339 a.getNInData() == b.getNInData() &&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500340 a.getNInNacks() == b.getNInNacks() &&
341 a.getNOutInterests() == b.getNOutInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000342 a.getNOutData() == b.getNOutData() &&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500343 a.getNOutNacks() == b.getNOutNacks() &&
344 a.getNInBytes() == b.getNInBytes() &&
345 a.getNOutBytes() == b.getNOutBytes();
346}
347
Junxiao Shi13e637f2014-07-16 19:20:40 -0700348std::ostream&
349operator<<(std::ostream& os, const FaceStatus& status)
350{
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500351 os << "Face(FaceId: " << status.getFaceId() << ",\n"
352 << " RemoteUri: " << status.getRemoteUri() << ",\n"
353 << " LocalUri: " << status.getLocalUri() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700354
355 if (status.hasExpirationPeriod()) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500356 os << " ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700357 }
358 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500359 os << " ExpirationPeriod: infinite,\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700360 }
361
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500362 os << " FaceScope: " << status.getFaceScope() << ",\n"
363 << " FacePersistency: " << status.getFacePersistency() << ",\n"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500364 << " LinkType: " << status.getLinkType() << ",\n"
365 << " Flags: " << AsHex{status.getFlags()} << ",\n"
366 << " Counters: {Interests: {in: " << status.getNInInterests() << ", "
Junxiao Shi13e637f2014-07-16 19:20:40 -0700367 << "out: " << status.getNOutInterests() << "},\n"
Junxiao Shi9a53d782017-04-04 20:09:57 +0000368 << " Data: {in: " << status.getNInData() << ", "
369 << "out: " << status.getNOutData() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500370 << " Nacks: {in: " << status.getNInNacks() << ", "
Eric Newberry95bd96a2015-09-04 23:34:22 -0700371 << "out: " << status.getNOutNacks() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500372 << " bytes: {in: " << status.getNInBytes() << ", "
373 << "out: " << status.getNOutBytes() << "}}\n";
374
375 return os << " )";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700376}
377
378} // namespace nfd
379} // namespace ndn