blob: df0521f262e02f85a61dff59b908bea2d5a3c0b4 [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
57 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry1ce8ab22016-09-24 11:57:21 -070058 tlv::nfd::Flags, m_flags);
59 totalLength += prependNonNegativeIntegerBlock(encoder,
Junxiao Shi13e637f2014-07-16 19:20:40 -070060 tlv::nfd::NOutBytes, m_nOutBytes);
61 totalLength += prependNonNegativeIntegerBlock(encoder,
62 tlv::nfd::NInBytes, m_nInBytes);
63 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry95bd96a2015-09-04 23:34:22 -070064 tlv::nfd::NOutNacks, m_nOutNacks);
65 totalLength += prependNonNegativeIntegerBlock(encoder,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050066 tlv::nfd::NOutDatas, m_nOutData);
Junxiao Shi13e637f2014-07-16 19:20:40 -070067 totalLength += prependNonNegativeIntegerBlock(encoder,
68 tlv::nfd::NOutInterests, m_nOutInterests);
69 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry95bd96a2015-09-04 23:34:22 -070070 tlv::nfd::NInNacks, m_nInNacks);
71 totalLength += prependNonNegativeIntegerBlock(encoder,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050072 tlv::nfd::NInDatas, m_nInData);
Junxiao Shi13e637f2014-07-16 19:20:40 -070073 totalLength += prependNonNegativeIntegerBlock(encoder,
74 tlv::nfd::NInInterests, m_nInInterests);
75 totalLength += prependNonNegativeIntegerBlock(encoder,
Chengyu Fan36dca992014-09-25 13:42:03 -060076 tlv::nfd::LinkType, m_linkType);
77 totalLength += prependNonNegativeIntegerBlock(encoder,
78 tlv::nfd::FacePersistency, m_facePersistency);
79 totalLength += prependNonNegativeIntegerBlock(encoder,
80 tlv::nfd::FaceScope, m_faceScope);
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050081 if (m_expirationPeriod) {
Junxiao Shi13e637f2014-07-16 19:20:40 -070082 totalLength += prependNonNegativeIntegerBlock(encoder,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050083 tlv::nfd::ExpirationPeriod, static_cast<uint64_t>(m_expirationPeriod->count()));
Junxiao Shi13e637f2014-07-16 19:20:40 -070084 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080085 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050086 reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080087 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050088 reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
Junxiao Shi13e637f2014-07-16 19:20:40 -070089 totalLength += prependNonNegativeIntegerBlock(encoder,
90 tlv::nfd::FaceId, m_faceId);
91
92 totalLength += encoder.prependVarNumber(totalLength);
93 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
94 return totalLength;
95}
96
97template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080098FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -070099
100template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800101FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700102
103const Block&
104FaceStatus::wireEncode() const
105{
106 if (m_wire.hasWire())
107 return m_wire;
108
109 EncodingEstimator estimator;
110 size_t estimatedSize = wireEncode(estimator);
111
112 EncodingBuffer buffer(estimatedSize, 0);
113 wireEncode(buffer);
114
115 m_wire = buffer.block();
116 return m_wire;
117}
118
119void
120FaceStatus::wireDecode(const Block& block)
121{
122 if (block.type() != tlv::nfd::FaceStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700123 BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700124 }
125 m_wire = block;
126 m_wire.parse();
127 Block::element_const_iterator val = m_wire.elements_begin();
128
129 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
130 m_faceId = readNonNegativeInteger(*val);
131 ++val;
132 }
133 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700134 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700135 }
136
137 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
138 m_remoteUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
139 ++val;
140 }
141 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700142 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700143 }
144
145 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
146 m_localUri.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
147 ++val;
148 }
149 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700150 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700151 }
152
153 if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500154 m_expirationPeriod.emplace(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700155 ++val;
156 }
157 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500158 m_expirationPeriod = nullopt;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700159 }
160
Chengyu Fan36dca992014-09-25 13:42:03 -0600161 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
162 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700163 ++val;
164 }
165 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700166 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600167 }
168
169 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
170 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
171 ++val;
172 }
173 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700174 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600175 }
176
177 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
178 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
179 ++val;
180 }
181 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700182 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700183 }
184
185 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
186 m_nInInterests = readNonNegativeInteger(*val);
187 ++val;
188 }
189 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700190 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700191 }
192
193 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500194 m_nInData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700195 ++val;
196 }
197 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700198 BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700199 }
200
Eric Newberry95bd96a2015-09-04 23:34:22 -0700201 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
202 m_nInNacks = readNonNegativeInteger(*val);
203 ++val;
204 }
205 else {
206 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
207 }
208
Junxiao Shi13e637f2014-07-16 19:20:40 -0700209 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
210 m_nOutInterests = readNonNegativeInteger(*val);
211 ++val;
212 }
213 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700214 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700215 }
216
217 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500218 m_nOutData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700219 ++val;
220 }
221 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700222 BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700223 }
224
Eric Newberry95bd96a2015-09-04 23:34:22 -0700225 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
226 m_nOutNacks = readNonNegativeInteger(*val);
227 ++val;
228 }
229 else {
230 BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
231 }
232
Junxiao Shi13e637f2014-07-16 19:20:40 -0700233 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
234 m_nInBytes = readNonNegativeInteger(*val);
235 ++val;
236 }
237 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700238 BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700239 }
240
241 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
242 m_nOutBytes = readNonNegativeInteger(*val);
243 ++val;
244 }
245 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700246 BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700247 }
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700248
249 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
250 m_flags = readNonNegativeInteger(*val);
251 ++val;
252 }
253 else {
254 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
255 }
Junxiao Shi13e637f2014-07-16 19:20:40 -0700256}
257
Chengyu Fan36dca992014-09-25 13:42:03 -0600258FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500259FaceStatus::setExpirationPeriod(time::milliseconds expirationPeriod)
Chengyu Fan36dca992014-09-25 13:42:03 -0600260{
261 m_wire.reset();
262 m_expirationPeriod = expirationPeriod;
Chengyu Fan36dca992014-09-25 13:42:03 -0600263 return *this;
264}
265
266FaceStatus&
267FaceStatus::setNInInterests(uint64_t nInInterests)
268{
269 m_wire.reset();
270 m_nInInterests = nInInterests;
271 return *this;
272}
273
274FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500275FaceStatus::setNInDatas(uint64_t nInData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600276{
277 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500278 m_nInData = nInData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600279 return *this;
280}
281
282FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700283FaceStatus::setNInNacks(uint64_t nInNacks)
284{
285 m_wire.reset();
286 m_nInNacks = nInNacks;
287 return *this;
288}
289
290FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600291FaceStatus::setNOutInterests(uint64_t nOutInterests)
292{
293 m_wire.reset();
294 m_nOutInterests = nOutInterests;
295 return *this;
296}
297
298FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500299FaceStatus::setNOutDatas(uint64_t nOutData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600300{
301 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500302 m_nOutData = nOutData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600303 return *this;
304}
305
306FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700307FaceStatus::setNOutNacks(uint64_t nOutNacks)
308{
309 m_wire.reset();
310 m_nOutNacks = nOutNacks;
311 return *this;
312}
313
314FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600315FaceStatus::setNInBytes(uint64_t nInBytes)
316{
317 m_wire.reset();
318 m_nInBytes = nInBytes;
319 return *this;
320}
321
322FaceStatus&
323FaceStatus::setNOutBytes(uint64_t nOutBytes)
324{
325 m_wire.reset();
326 m_nOutBytes = nOutBytes;
327 return *this;
328}
329
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500330bool
331operator==(const FaceStatus& a, const FaceStatus& b)
332{
333 return a.getFaceId() == b.getFaceId() &&
334 a.getRemoteUri() == b.getRemoteUri() &&
335 a.getLocalUri() == b.getLocalUri() &&
336 a.getFaceScope() == b.getFaceScope() &&
337 a.getFacePersistency() == b.getFacePersistency() &&
338 a.getLinkType() == b.getLinkType() &&
339 a.getFlags() == b.getFlags() &&
340 a.hasExpirationPeriod() == b.hasExpirationPeriod() &&
341 (!a.hasExpirationPeriod() || a.getExpirationPeriod() == b.getExpirationPeriod()) &&
342 a.getNInInterests() == b.getNInInterests() &&
343 a.getNInDatas() == b.getNInDatas() &&
344 a.getNInNacks() == b.getNInNacks() &&
345 a.getNOutInterests() == b.getNOutInterests() &&
346 a.getNOutDatas() == b.getNOutDatas() &&
347 a.getNOutNacks() == b.getNOutNacks() &&
348 a.getNInBytes() == b.getNInBytes() &&
349 a.getNOutBytes() == b.getNOutBytes();
350}
351
Junxiao Shi13e637f2014-07-16 19:20:40 -0700352std::ostream&
353operator<<(std::ostream& os, const FaceStatus& status)
354{
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500355 os << "Face(FaceId: " << status.getFaceId() << ",\n"
356 << " RemoteUri: " << status.getRemoteUri() << ",\n"
357 << " LocalUri: " << status.getLocalUri() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700358
359 if (status.hasExpirationPeriod()) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500360 os << " ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700361 }
362 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500363 os << " ExpirationPeriod: infinite,\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700364 }
365
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500366 os << " FaceScope: " << status.getFaceScope() << ",\n"
367 << " FacePersistency: " << status.getFacePersistency() << ",\n"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500368 << " LinkType: " << status.getLinkType() << ",\n"
369 << " Flags: " << AsHex{status.getFlags()} << ",\n"
370 << " Counters: {Interests: {in: " << status.getNInInterests() << ", "
Junxiao Shi13e637f2014-07-16 19:20:40 -0700371 << "out: " << status.getNOutInterests() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500372 << " Data: {in: " << status.getNInDatas() << ", "
Junxiao Shi13e637f2014-07-16 19:20:40 -0700373 << "out: " << status.getNOutDatas() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500374 << " Nacks: {in: " << status.getNInNacks() << ", "
Eric Newberry95bd96a2015-09-04 23:34:22 -0700375 << "out: " << status.getNOutNacks() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500376 << " bytes: {in: " << status.getNInBytes() << ", "
377 << "out: " << status.getNOutBytes() << "}}\n";
378
379 return os << " )";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700380}
381
382} // namespace nfd
383} // namespace ndn