blob: bdc740493cfcf84bbaef09310b5666df6f28cd09 [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)
46{
47}
48
Chengyu Fan36dca992014-09-25 13:42:03 -060049FaceStatus::FaceStatus(const Block& block)
50{
51 this->wireDecode(block);
52}
53
Alexander Afanasyev74633892015-02-08 18:08:46 -080054template<encoding::Tag TAG>
Junxiao Shi13e637f2014-07-16 19:20:40 -070055size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080056FaceStatus::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi13e637f2014-07-16 19:20:40 -070057{
58 size_t totalLength = 0;
59
60 totalLength += prependNonNegativeIntegerBlock(encoder,
61 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,
Junxiao Shi13e637f2014-07-16 19:20:40 -070067 tlv::nfd::NOutDatas, m_nOutDatas);
68 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,
Junxiao Shi13e637f2014-07-16 19:20:40 -070073 tlv::nfd::NInDatas, m_nInDatas);
74 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);
Junxiao Shi13e637f2014-07-16 19:20:40 -070082 if (m_hasExpirationPeriod) {
83 totalLength += prependNonNegativeIntegerBlock(encoder,
84 tlv::nfd::ExpirationPeriod, m_expirationPeriod.count());
85 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080086 totalLength += encoder.prependByteArrayBlock(tlv::nfd::LocalUri,
Junxiao Shi13e637f2014-07-16 19:20:40 -070087 reinterpret_cast<const uint8_t*>(m_localUri.c_str()), m_localUri.size());
Alexander Afanasyev74633892015-02-08 18:08:46 -080088 totalLength += encoder.prependByteArrayBlock(tlv::nfd::Uri,
Junxiao Shi13e637f2014-07-16 19:20:40 -070089 reinterpret_cast<const uint8_t*>(m_remoteUri.c_str()), m_remoteUri.size());
90 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) {
155 m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val));
156 m_hasExpirationPeriod = true;
157 ++val;
158 }
159 else {
160 m_hasExpirationPeriod = false;
161 // ExpirationPeriod is optional
162 }
163
Chengyu Fan36dca992014-09-25 13:42:03 -0600164 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceScope) {
165 m_faceScope = static_cast<FaceScope>(readNonNegativeInteger(*val));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700166 ++val;
167 }
168 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700169 BOOST_THROW_EXCEPTION(Error("missing required FaceScope field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600170 }
171
172 if (val != m_wire.elements_end() && val->type() == tlv::nfd::FacePersistency) {
173 m_facePersistency = static_cast<FacePersistency>(readNonNegativeInteger(*val));
174 ++val;
175 }
176 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700177 BOOST_THROW_EXCEPTION(Error("missing required FacePersistency field"));
Chengyu Fan36dca992014-09-25 13:42:03 -0600178 }
179
180 if (val != m_wire.elements_end() && val->type() == tlv::nfd::LinkType) {
181 m_linkType = static_cast<LinkType>(readNonNegativeInteger(*val));
182 ++val;
183 }
184 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700185 BOOST_THROW_EXCEPTION(Error("missing required LinkType field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700186 }
187
188 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
189 m_nInInterests = readNonNegativeInteger(*val);
190 ++val;
191 }
192 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700193 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700194 }
195
196 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
197 m_nInDatas = readNonNegativeInteger(*val);
198 ++val;
199 }
200 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700201 BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700202 }
203
Eric Newberry95bd96a2015-09-04 23:34:22 -0700204 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
205 m_nInNacks = readNonNegativeInteger(*val);
206 ++val;
207 }
208 else {
209 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
210 }
211
Junxiao Shi13e637f2014-07-16 19:20:40 -0700212 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
213 m_nOutInterests = readNonNegativeInteger(*val);
214 ++val;
215 }
216 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700217 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700218 }
219
220 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
221 m_nOutDatas = readNonNegativeInteger(*val);
222 ++val;
223 }
224 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700225 BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700226 }
227
Eric Newberry95bd96a2015-09-04 23:34:22 -0700228 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
229 m_nOutNacks = readNonNegativeInteger(*val);
230 ++val;
231 }
232 else {
233 BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
234 }
235
Junxiao Shi13e637f2014-07-16 19:20:40 -0700236 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInBytes) {
237 m_nInBytes = readNonNegativeInteger(*val);
238 ++val;
239 }
240 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700241 BOOST_THROW_EXCEPTION(Error("missing required NInBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700242 }
243
244 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutBytes) {
245 m_nOutBytes = readNonNegativeInteger(*val);
246 ++val;
247 }
248 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700249 BOOST_THROW_EXCEPTION(Error("missing required NOutBytes field"));
Junxiao Shi13e637f2014-07-16 19:20:40 -0700250 }
251}
252
Chengyu Fan36dca992014-09-25 13:42:03 -0600253FaceStatus&
254FaceStatus::setExpirationPeriod(const time::milliseconds& expirationPeriod)
255{
256 m_wire.reset();
257 m_expirationPeriod = expirationPeriod;
258 m_hasExpirationPeriod = true;
259 return *this;
260}
261
262FaceStatus&
263FaceStatus::setNInInterests(uint64_t nInInterests)
264{
265 m_wire.reset();
266 m_nInInterests = nInInterests;
267 return *this;
268}
269
270FaceStatus&
271FaceStatus::setNInDatas(uint64_t nInDatas)
272{
273 m_wire.reset();
274 m_nInDatas = nInDatas;
275 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&
295FaceStatus::setNOutDatas(uint64_t nOutDatas)
296{
297 m_wire.reset();
298 m_nOutDatas = nOutDatas;
299 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
326void
327FaceStatus::wireReset() const
328{
329 m_wire.reset();
330}
331
Junxiao Shi13e637f2014-07-16 19:20:40 -0700332std::ostream&
333operator<<(std::ostream& os, const FaceStatus& status)
334{
335 os << "FaceStatus("
336 << "FaceID: " << status.getFaceId() << ",\n"
337 << "RemoteUri: " << status.getRemoteUri() << ",\n"
338 << "LocalUri: " << status.getLocalUri() << ",\n";
339
340 if (status.hasExpirationPeriod()) {
341 os << "ExpirationPeriod: " << status.getExpirationPeriod() << ",\n";
342 }
343 else {
344 os << "ExpirationPeriod: infinite,\n";
345 }
346
Chengyu Fan36dca992014-09-25 13:42:03 -0600347 os << "FaceScope: " << status.getFaceScope() << ",\n"
348 << "FacePersistency: " << status.getFacePersistency() << ",\n"
349 << "LinkType: " << status.getLinkType() << ",\n"
Junxiao Shi13e637f2014-07-16 19:20:40 -0700350 << "Counters: { Interests: {in: " << status.getNInInterests() << ", "
351 << "out: " << status.getNOutInterests() << "},\n"
352 << " Data: {in: " << status.getNInDatas() << ", "
353 << "out: " << status.getNOutDatas() << "},\n"
Eric Newberry95bd96a2015-09-04 23:34:22 -0700354 << " Nack: {in: " << status.getNInNacks() << ", "
355 << "out: " << status.getNOutNacks() << "},\n"
Junxiao Shi13e637f2014-07-16 19:20:40 -0700356 << " bytes: {in: " << status.getNInBytes() << ", "
357 << "out: " << status.getNOutBytes() << "} }\n"
358 << ")";
359 return os;
360}
361
362} // namespace nfd
363} // namespace ndn