blob: 360865a0aa954de9776891b993fd49d30e3568a3 [file] [log] [blame]
Junxiao Shi13e637f2014-07-16 19:20:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5d75fd92017-08-08 18:09:20 +00002/*
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 Shi5d75fd92017-08-08 18:09:20 +000070 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::ExpirationPeriod,
71 m_expirationPeriod->count());
Junxiao Shi13e637f2014-07-16 19:20:40 -070072 }
Junxiao Shi5d75fd92017-08-08 18:09:20 +000073 totalLength += prependStringBlock(encoder, tlv::nfd::LocalUri, m_localUri);
74 totalLength += prependStringBlock(encoder, tlv::nfd::Uri, m_remoteUri);
75 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::FaceId, m_faceId);
Junxiao Shi13e637f2014-07-16 19:20:40 -070076
77 totalLength += encoder.prependVarNumber(totalLength);
78 totalLength += encoder.prependVarNumber(tlv::nfd::FaceStatus);
79 return totalLength;
80}
81
Davide Pesavento88a0d812017-08-19 21:31:42 -040082NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(FaceStatus);
Junxiao Shi13e637f2014-07-16 19:20:40 -070083
84const Block&
85FaceStatus::wireEncode() const
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
100void
101FaceStatus::wireDecode(const Block& block)
102{
103 if (block.type() != tlv::nfd::FaceStatus) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700104 BOOST_THROW_EXCEPTION(Error("expecting FaceStatus block"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700105 }
106 m_wire = block;
107 m_wire.parse();
108 Block::element_const_iterator val = m_wire.elements_begin();
109
110 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) {
111 m_faceId = readNonNegativeInteger(*val);
112 ++val;
113 }
114 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700115 BOOST_THROW_EXCEPTION(Error("missing required FaceId field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700116 }
117
118 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Uri) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000119 m_remoteUri = readString(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700120 ++val;
121 }
122 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700123 BOOST_THROW_EXCEPTION(Error("missing required Uri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700124 }
125
126 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LocalUri) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000127 m_localUri = readString(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700128 ++val;
129 }
130 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700131 BOOST_THROW_EXCEPTION(Error("missing required LocalUri field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700132 }
133
134 if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500135 m_expirationPeriod.emplace(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700136 ++val;
137 }
138 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500139 m_expirationPeriod = nullopt;
Junxiao Shi13e637f2014-07-16 19:20:40 -0700140 }
141
Chengyu Fan36dca992014-09-25 13:42:03 -0600142 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000143 m_faceScope = readNonNegativeIntegerAs<FaceScope>(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700144 ++val;
145 }
146 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700147 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600148 }
149
150 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000151 m_facePersistency = readNonNegativeIntegerAs<FacePersistency>(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -0600152 ++val;
153 }
154 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700155 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600156 }
157
158 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000159 m_linkType = readNonNegativeIntegerAs<LinkType>(*val);
Chengyu Fan36dca992014-09-25 13:42:03 -0600160 ++val;
161 }
162 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700163 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700164 }
165
166 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
167 m_nInInterests = readNonNegativeInteger(*val);
168 ++val;
169 }
170 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700171 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700172 }
173
Junxiao Shi9a53d782017-04-04 20:09:57 +0000174 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500175 m_nInData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700176 ++val;
177 }
178 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000179 BOOST_THROW_EXCEPTION(Error("missing required NInData field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700180 }
181
Eric Newberry95bd96a2015-09-04 23:34:22 -0700182 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
Junxiao Shi13e637f2014-07-16 19:20:40 -0700190 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
191 m_nOutInterests = readNonNegativeInteger(*val);
192 ++val;
193 }
194 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700195 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700196 }
197
Junxiao Shi9a53d782017-04-04 20:09:57 +0000198 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500199 m_nOutData = readNonNegativeInteger(*val);
Junxiao Shi13e637f2014-07-16 19:20:40 -0700200 ++val;
201 }
202 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000203 BOOST_THROW_EXCEPTION(Error("missing required NOutData field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700204 }
205
Eric Newberry95bd96a2015-09-04 23:34:22 -0700206 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
Junxiao Shi13e637f2014-07-16 19:20:40 -0700214 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
215 m_nInBytes = readNonNegativeInteger(*val);
216 ++val;
217 }
218 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700219 BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700220 }
221
222 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
223 m_nOutBytes = readNonNegativeInteger(*val);
224 ++val;
225 }
226 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700227 BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700228 }
Eric Newberry1ce8ab22016-09-24 11:57:21 -0700229
230 if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) {
231 m_flags = readNonNegativeInteger(*val);
232 ++val;
233 }
234 else {
235 BOOST_THROW_EXCEPTION(Error("missing required Flags field"));
236 }
Junxiao Shi13e637f2014-07-16 19:20:40 -0700237}
238
Chengyu Fan36dca992014-09-25 13:42:03 -0600239FaceStatus&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500240FaceStatus::setExpirationPeriod(time::milliseconds expirationPeriod)
Chengyu Fan36dca992014-09-25 13:42:03 -0600241{
242 m_wire.reset();
243 m_expirationPeriod = expirationPeriod;
Chengyu Fan36dca992014-09-25 13:42:03 -0600244 return *this;
245}
246
247FaceStatus&
Davide Pesavento156c1ea2017-03-19 16:09:33 -0400248FaceStatus::unsetExpirationPeriod()
249{
250 m_wire.reset();
251 m_expirationPeriod = nullopt;
252 return *this;
253}
254
255FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600256FaceStatus::setNInInterests(uint64_t nInInterests)
257{
258 m_wire.reset();
259 m_nInInterests = nInInterests;
260 return *this;
261}
262
263FaceStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000264FaceStatus::setNInData(uint64_t nInData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600265{
266 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500267 m_nInData = nInData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600268 return *this;
269}
270
271FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700272FaceStatus::setNInNacks(uint64_t nInNacks)
273{
274 m_wire.reset();
275 m_nInNacks = nInNacks;
276 return *this;
277}
278
279FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600280FaceStatus::setNOutInterests(uint64_t nOutInterests)
281{
282 m_wire.reset();
283 m_nOutInterests = nOutInterests;
284 return *this;
285}
286
287FaceStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000288FaceStatus::setNOutData(uint64_t nOutData)
Chengyu Fan36dca992014-09-25 13:42:03 -0600289{
290 m_wire.reset();
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500291 m_nOutData = nOutData;
Chengyu Fan36dca992014-09-25 13:42:03 -0600292 return *this;
293}
294
295FaceStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700296FaceStatus::setNOutNacks(uint64_t nOutNacks)
297{
298 m_wire.reset();
299 m_nOutNacks = nOutNacks;
300 return *this;
301}
302
303FaceStatus&
Chengyu Fan36dca992014-09-25 13:42:03 -0600304FaceStatus::setNInBytes(uint64_t nInBytes)
305{
306 m_wire.reset();
307 m_nInBytes = nInBytes;
308 return *this;
309}
310
311FaceStatus&
312FaceStatus::setNOutBytes(uint64_t nOutBytes)
313{
314 m_wire.reset();
315 m_nOutBytes = nOutBytes;
316 return *this;
317}
318
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500319bool
320operator==(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() &&
326 a.getFacePersistency() == b.getFacePersistency() &&
327 a.getLinkType() == b.getLinkType() &&
328 a.getFlags() == b.getFlags() &&
329 a.hasExpirationPeriod() == b.hasExpirationPeriod() &&
330 (!a.hasExpirationPeriod() || a.getExpirationPeriod() == b.getExpirationPeriod()) &&
331 a.getNInInterests() == b.getNInInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000332 a.getNInData() == b.getNInData() &&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500333 a.getNInNacks() == b.getNInNacks() &&
334 a.getNOutInterests() == b.getNOutInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000335 a.getNOutData() == b.getNOutData() &&
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500336 a.getNOutNacks() == b.getNOutNacks() &&
337 a.getNInBytes() == b.getNInBytes() &&
338 a.getNOutBytes() == b.getNOutBytes();
339}
340
Junxiao Shi13e637f2014-07-16 19:20:40 -0700341std::ostream&
342operator<<(std::ostream& os, const FaceStatus& status)
343{
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500344 os << "Face(FaceId: " << status.getFaceId() << ",\n"
345 << " RemoteUri: " << status.getRemoteUri() << ",\n"
346 << " LocalUri: " << status.getLocalUri() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700347
348 if (status.hasExpirationPeriod()) {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500349 os << " ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700350 }
351 else {
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500352 os << " ExpirationPeriod: infinite,\n";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700353 }
354
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500355 os << " FaceScope: " << status.getFaceScope() << ",\n"
356 << " FacePersistency: " << status.getFacePersistency() << ",\n"
Davide Pesaventoe78eeca2017-02-23 23:22:32 -0500357 << " LinkType: " << status.getLinkType() << ",\n"
358 << " Flags: " << AsHex{status.getFlags()} << ",\n"
359 << " Counters: {Interests: {in: " << status.getNInInterests() << ", "
Junxiao Shi13e637f2014-07-16 19:20:40 -0700360 << "out: " << status.getNOutInterests() << "},\n"
Junxiao Shi9a53d782017-04-04 20:09:57 +0000361 << " Data: {in: " << status.getNInData() << ", "
362 << "out: " << status.getNOutData() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500363 << " Nacks: {in: " << status.getNInNacks() << ", "
Eric Newberry95bd96a2015-09-04 23:34:22 -0700364 << "out: " << status.getNOutNacks() << "},\n"
Davide Pesavento4ec7a5a2017-02-07 23:13:39 -0500365 << " bytes: {in: " << status.getNInBytes() << ", "
366 << "out: " << status.getNOutBytes() << "}}\n";
367
368 return os << " )";
Junxiao Shi13e637f2014-07-16 19:20:40 -0700369}
370
371} // namespace nfd
372} // namespace ndn