blob: 9417e28194849e9a9e3c345da873f1b326690f8b [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"
Junxiao Shi13e637f2014-07-16 19:20:40 -070027
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050028#include <iomanip>
29
Junxiao Shi13e637f2014-07-16 19:20:40 -070030namespace ndn {
31namespace nfd {
32
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050033BOOST_CONCEPT_ASSERT((StatusDatasetItem<FaceStatus>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070034
Junxiao Shi13e637f2014-07-16 19:20:40 -070035FaceStatus::FaceStatus()
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050036 : m_nInInterests(0)
37 , m_nInData(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070038 , m_nInNacks(0)
Junxiao Shi13e637f2014-07-16 19:20:40 -070039 , m_nOutInterests(0)
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050040 , m_nOutData(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070041 , m_nOutNacks(0)
Junxiao Shi13e637f2014-07-16 19:20:40 -070042 , m_nInBytes(0)
43 , m_nOutBytes(0)
44{
45}
46
Chengyu Fan36dca992014-09-25 13:42:03 -060047FaceStatus::FaceStatus(const Block& block)
48{
49 this->wireDecode(block);
50}
51
Alexander Afanasyev74633892015-02-08 18:08:46 -080052template<encoding::Tag TAG>
Junxiao Shi13e637f2014-07-16 19:20:40 -070053size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080054FaceStatus::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi13e637f2014-07-16 19:20:40 -070055{
56 size_t totalLength = 0;
57
58 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry1ce8ab22016-09-24 11:57:21 -070059 tlv::nfd::Flags, m_flags);
60 totalLength += prependNonNegativeIntegerBlock(encoder,
Junxiao Shi13e637f2014-07-16 19:20:40 -070061 tlv::nfd::NOutBytes, m_nOutBytes);
62 totalLength += prependNonNegativeIntegerBlock(encoder,
63 tlv::nfd::NInBytes, m_nInBytes);
64 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry95bd96a2015-09-04 23:34:22 -070065 tlv::nfd::NOutNacks, m_nOutNacks);
66 totalLength += prependNonNegativeIntegerBlock(encoder,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050067 tlv::nfd::NOutDatas, m_nOutData);
Junxiao Shi13e637f2014-07-16 19:20:40 -070068 totalLength += prependNonNegativeIntegerBlock(encoder,
69 tlv::nfd::NOutInterests, m_nOutInterests);
70 totalLength += prependNonNegativeIntegerBlock(encoder,
Eric Newberry95bd96a2015-09-04 23:34:22 -070071 tlv::nfd::NInNacks, m_nInNacks);
72 totalLength += prependNonNegativeIntegerBlock(encoder,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050073 tlv::nfd::NInDatas, m_nInData);
Junxiao Shi13e637f2014-07-16 19:20:40 -070074 totalLength += prependNonNegativeIntegerBlock(encoder,
75 tlv::nfd::NInInterests, m_nInInterests);
76 totalLength += prependNonNegativeIntegerBlock(encoder,
Chengyu Fan36dca992014-09-25 13:42:03 -060077 tlv::nfd::LinkType, m_linkType);
78 totalLength += prependNonNegativeIntegerBlock(encoder,
79 tlv::nfd::FacePersistency, m_facePersistency);
80 totalLength += prependNonNegativeIntegerBlock(encoder,
81 tlv::nfd::FaceScope, m_faceScope);
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050082 if (m_expirationPeriod) {
Junxiao Shi13e637f2014-07-16 19:20:40 -070083 totalLength += prependNonNegativeIntegerBlock(encoder,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050084 tlv::nfd::ExpirationPeriod, static_cast<uint64_t>(m_expirationPeriod->count()));
Junxiao Shi13e637f2014-07-16 19:20:40 -070085 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080086 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050087 reinterpret_cast<const uint8_t*>(m_localUri.data()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080088 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -050089 reinterpret_cast<const uint8_t*>(m_remoteUri.data()), m_remoteUri.size());
Junxiao Shi13e637f2014-07-16 19:20:40 -070090 totalLength += prependNonNegativeIntegerBlock(encoder,
91 tlv::nfd::FaceId, m_faceId);
92
93 totalLength += encoder.prependVarNumber(totalLength);
94 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
95 return totalLength;
96}
97
98template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080099FaceStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700100
101template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800102FaceStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700103
104const Block&
105FaceStatus::wireEncode() const
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
120void
121FaceStatus::wireDecode(const Block& block)
122{
123 if (block.type() != tlv::nfd::FaceStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700124 BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700125 }
126 m_wire = block;
127 m_wire.parse();
128 Block::element_const_iterator val = m_wire.elements_begin();
129
130 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
131 m_faceId = readNonNegativeInteger(*val);
132 ++val;
133 }
134 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700135 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700136 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700143 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700144 }
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 {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700151 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700152 }
153
154 if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500155 m_expirationPeriod.emplace(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700156 ++val;
157 }
158 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500159 m_expirationPeriod = nullopt;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700160 }
161
Chengyu Fan36dca992014-09-25 13:42:03 -0600162 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
163 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700164 ++val;
165 }
166 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700167 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600168 }
169
170 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
171 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
172 ++val;
173 }
174 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700175 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600176 }
177
178 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
179 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
180 ++val;
181 }
182 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700183 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700184 }
185
186 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
187 m_nInInterests = readNonNegativeInteger(*val);
188 ++val;
189 }
190 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700191 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700192 }
193
194 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500195 m_nInData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700196 ++val;
197 }
198 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700199 BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700200 }
201
Eric Newberry95bd96a2015-09-04 23:34:22 -0700202 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
203 m_nInNacks = readNonNegativeInteger(*val);
204 ++val;
205 }
206 else {
207 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
208 }
209
Junxiao Shi13e637f2014-07-16 19:20:40 -0700210 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
211 m_nOutInterests = readNonNegativeInteger(*val);
212 ++val;
213 }
214 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700215 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700216 }
217
218 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500219 m_nOutData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700220 ++val;
221 }
222 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700223 BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700224 }
225
Eric Newberry95bd96a2015-09-04 23:34:22 -0700226 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
227 m_nOutNacks = readNonNegativeInteger(*val);
228 ++val;
229 }
230 else {
231 BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
232 }
233
Junxiao Shi13e637f2014-07-16 19:20:40 -0700234 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
235 m_nInBytes = readNonNegativeInteger(*val);
236 ++val;
237 }
238 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700239 BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700240 }
241
242 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
243 m_nOutBytes = readNonNegativeInteger(*val);
244 ++val;
245 }
246 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700247 BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700248 }
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700249
250 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
251 m_flags = readNonNegativeInteger(*val);
252 ++val;
253 }
254 else {
255 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
256 }
Junxiao Shi13e637f2014-07-16 19:20:40 -0700257}
258
Chengyu Fan36dca992014-09-25 13:42:03 -0600259FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500260FaceStatus::setExpirationPeriod(time::milliseconds expirationPeriod)
Chengyu Fan36dca992014-09-25 13:42:03 -0600261{
262 m_wire.reset();
263 m_expirationPeriod = expirationPeriod;
Chengyu Fan36dca992014-09-25 13:42:03 -0600264 return *this;
265}
266
267FaceStatus&
268FaceStatus::setNInInterests(uint64_t nInInterests)
269{
270 m_wire.reset();
271 m_nInInterests = nInInterests;
272 return *this;
273}
274
275FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500276FaceStatus::setNInDatas(uint64_t nInData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600277{
278 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500279 m_nInData = nInData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600280 return *this;
281}
282
283FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700284FaceStatus::setNInNacks(uint64_t nInNacks)
285{
286 m_wire.reset();
287 m_nInNacks = nInNacks;
288 return *this;
289}
290
291FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600292FaceStatus::setNOutInterests(uint64_t nOutInterests)
293{
294 m_wire.reset();
295 m_nOutInterests = nOutInterests;
296 return *this;
297}
298
299FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500300FaceStatus::setNOutDatas(uint64_t nOutData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600301{
302 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500303 m_nOutData = nOutData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600304 return *this;
305}
306
307FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700308FaceStatus::setNOutNacks(uint64_t nOutNacks)
309{
310 m_wire.reset();
311 m_nOutNacks = nOutNacks;
312 return *this;
313}
314
315FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600316FaceStatus::setNInBytes(uint64_t nInBytes)
317{
318 m_wire.reset();
319 m_nInBytes = nInBytes;
320 return *this;
321}
322
323FaceStatus&
324FaceStatus::setNOutBytes(uint64_t nOutBytes)
325{
326 m_wire.reset();
327 m_nOutBytes = nOutBytes;
328 return *this;
329}
330
331void
332FaceStatus::wireReset() const
333{
334 m_wire.reset();
335}
336
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500337bool
338operator==(const FaceStatus& a, const FaceStatus& b)
339{
340 return a.getFaceId() == b.getFaceId() &&
341 a.getRemoteUri() == b.getRemoteUri() &&
342 a.getLocalUri() == b.getLocalUri() &&
343 a.getFaceScope() == b.getFaceScope() &&
344 a.getFacePersistency() == b.getFacePersistency() &&
345 a.getLinkType() == b.getLinkType() &&
346 a.getFlags() == b.getFlags() &&
347 a.hasExpirationPeriod() == b.hasExpirationPeriod() &&
348 (!a.hasExpirationPeriod() || a.getExpirationPeriod() == b.getExpirationPeriod()) &&
349 a.getNInInterests() == b.getNInInterests() &&
350 a.getNInDatas() == b.getNInDatas() &&
351 a.getNInNacks() == b.getNInNacks() &&
352 a.getNOutInterests() == b.getNOutInterests() &&
353 a.getNOutDatas() == b.getNOutDatas() &&
354 a.getNOutNacks() == b.getNOutNacks() &&
355 a.getNInBytes() == b.getNInBytes() &&
356 a.getNOutBytes() == b.getNOutBytes();
357}
358
Junxiao Shi13e637f2014-07-16 19:20:40 -0700359std::ostream&
360operator<<(std::ostream& os, const FaceStatus& status)
361{
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500362 os << "Face(FaceId: " << status.getFaceId() << ",\n"
363 << " RemoteUri: " << status.getRemoteUri() << ",\n"
364 << " LocalUri: " << status.getLocalUri() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700365
366 if (status.hasExpirationPeriod()) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500367 os << " ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700368 }
369 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500370 os << " ExpirationPeriod: infinite,\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700371 }
372
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500373 os << " FaceScope: " << status.getFaceScope() << ",\n"
374 << " FacePersistency: " << status.getFacePersistency() << ",\n"
375 << " LinkType: " << status.getLinkType() << ",\n";
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700376
377 auto osFlags = os.flags();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500378 // std::showbase doesn't work with number 0
379 os << " Flags: 0x" << std::noshowbase << std::noshowpos << std::nouppercase
380 << std::hex << status.getFlags() << ",\n";
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700381 os.flags(osFlags);
382
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500383 os << " Counters: {Interests: {in: " << status.getNInInterests() << ", "
Junxiao Shi13e637f2014-07-16 19:20:40 -0700384 << "out: " << status.getNOutInterests() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500385 << " Data: {in: " << status.getNInDatas() << ", "
Junxiao Shi13e637f2014-07-16 19:20:40 -0700386 << "out: " << status.getNOutDatas() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500387 << " Nacks: {in: " << status.getNInNacks() << ", "
Eric Newberry95bd96a2015-09-04 23:34:22 -0700388 << "out: " << status.getNOutNacks() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500389 << " bytes: {in: " << status.getNInBytes() << ", "
390 << "out: " << status.getNOutBytes() << "}}\n";
391
392 return os << " )";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700393}
394
395} // namespace nfd
396} // namespace ndn