blob: 50eac6304d58bbd8dc6546e1dc13627ffdfc2760 [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5d75fd92017-08-08 18:09:20 +00002/*
Davide Pesavento25e3d8c2017-02-08 22:17:46 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Junxiao Shi65f1a712014-11-20 14:59:36 -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 "forwarder-status.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070023#include "encoding/block-helpers.hpp"
Davide Pesavento25e3d8c2017-02-08 22:17:46 -050024#include "encoding/encoding-buffer.hpp"
25#include "encoding/tlv-nfd.hpp"
Junxiao Shi65f1a712014-11-20 14:59:36 -070026#include "util/concepts.hpp"
27
28namespace ndn {
29namespace nfd {
30
Davide Pesavento25e3d8c2017-02-08 22:17:46 -050031BOOST_CONCEPT_ASSERT((StatusDatasetItem<ForwarderStatus>));
Junxiao Shi65f1a712014-11-20 14:59:36 -070032
33ForwarderStatus::ForwarderStatus()
Davide Pesavento25e3d8c2017-02-08 22:17:46 -050034 : m_nNameTreeEntries(0)
Junxiao Shi65f1a712014-11-20 14:59:36 -070035 , m_nFibEntries(0)
36 , m_nPitEntries(0)
37 , m_nMeasurementsEntries(0)
38 , m_nCsEntries(0)
39 , m_nInInterests(0)
Junxiao Shi9a53d782017-04-04 20:09:57 +000040 , m_nInData(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070041 , m_nInNacks(0)
Junxiao Shi65f1a712014-11-20 14:59:36 -070042 , m_nOutInterests(0)
Junxiao Shi9a53d782017-04-04 20:09:57 +000043 , m_nOutData(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070044 , m_nOutNacks(0)
Junxiao Shi65f1a712014-11-20 14:59:36 -070045{
46}
47
48ForwarderStatus::ForwarderStatus(const Block& payload)
49{
50 this->wireDecode(payload);
51}
52
Alexander Afanasyev74633892015-02-08 18:08:46 -080053template<encoding::Tag TAG>
Junxiao Shi65f1a712014-11-20 14:59:36 -070054size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080055ForwarderStatus::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi65f1a712014-11-20 14:59:36 -070056{
57 size_t totalLength = 0;
58
Junxiao Shi9a53d782017-04-04 20:09:57 +000059 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks, m_nOutNacks);
60 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutData, m_nOutData);
61 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, m_nOutInterests);
62 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, m_nInNacks);
63 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInData, m_nInData);
64 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, m_nInInterests);
65 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries, m_nCsEntries);
66 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries, m_nMeasurementsEntries);
67 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries, m_nPitEntries);
68 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries, m_nFibEntries);
69 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries, m_nNameTreeEntries);
Junxiao Shi65f1a712014-11-20 14:59:36 -070070 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::CurrentTimestamp,
71 time::toUnixTimestamp(m_currentTimestamp).count());
72 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp,
73 time::toUnixTimestamp(m_startTimestamp).count());
Junxiao Shi5d75fd92017-08-08 18:09:20 +000074 totalLength += prependStringBlock(encoder, tlv::nfd::NfdVersion, m_nfdVersion);
Junxiao Shi65f1a712014-11-20 14:59:36 -070075
76 totalLength += encoder.prependVarNumber(totalLength);
77 totalLength += encoder.prependVarNumber(tlv::Content);
78 return totalLength;
79}
80
81template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080082ForwarderStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070083
84template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080085ForwarderStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070086
87const Block&
88ForwarderStatus::wireEncode() const
89{
90 if (m_wire.hasWire())
91 return m_wire;
92
93 EncodingEstimator estimator;
94 size_t estimatedSize = wireEncode(estimator);
95
96 EncodingBuffer buffer(estimatedSize, 0);
97 wireEncode(buffer);
98
99 m_wire = buffer.block();
100 return m_wire;
101}
102
103void
104ForwarderStatus::wireDecode(const Block& block)
105{
106 if (block.type() != tlv::Content) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700107 BOOST_THROW_EXCEPTION(Error("expecting Content block for Status payload"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700108 }
109 m_wire = block;
110 m_wire.parse();
111 Block::element_const_iterator val = m_wire.elements_begin();
112
113 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000114 m_nfdVersion = readString(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700115 ++val;
116 }
117 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700118 BOOST_THROW_EXCEPTION(Error("missing required NfdVersion field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700119 }
120
121 if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
122 m_startTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
123 ++val;
124 }
125 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700126 BOOST_THROW_EXCEPTION(Error("missing required StartTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700127 }
128
129 if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
130 m_currentTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
131 ++val;
132 }
133 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700134 BOOST_THROW_EXCEPTION(Error("missing required CurrentTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700135 }
136
137 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000138 m_nNameTreeEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700139 ++val;
140 }
141 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700142 BOOST_THROW_EXCEPTION(Error("missing required NNameTreeEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700143 }
144
145 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000146 m_nFibEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700147 ++val;
148 }
149 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700150 BOOST_THROW_EXCEPTION(Error("missing required NFibEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700151 }
152
153 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000154 m_nPitEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700155 ++val;
156 }
157 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700158 BOOST_THROW_EXCEPTION(Error("missing required NPitEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700159 }
160
161 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000162 m_nMeasurementsEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700163 ++val;
164 }
165 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700166 BOOST_THROW_EXCEPTION(Error("missing required NMeasurementsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700167 }
168
169 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000170 m_nCsEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700171 ++val;
172 }
173 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700174 BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700175 }
176
177 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000178 m_nInInterests = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700179 ++val;
180 }
181 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700182 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700183 }
184
Junxiao Shi9a53d782017-04-04 20:09:57 +0000185 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000186 m_nInData = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700187 ++val;
188 }
189 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000190 BOOST_THROW_EXCEPTION(Error("missing required NInData field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700191 }
192
Eric Newberry95bd96a2015-09-04 23:34:22 -0700193 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000194 m_nInNacks = readNonNegativeInteger(*val);
Eric Newberry95bd96a2015-09-04 23:34:22 -0700195 ++val;
196 }
197 else {
198 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
199 }
200
Junxiao Shi65f1a712014-11-20 14:59:36 -0700201 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000202 m_nOutInterests = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700203 ++val;
204 }
205 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700206 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700207 }
208
Junxiao Shi9a53d782017-04-04 20:09:57 +0000209 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000210 m_nOutData = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700211 ++val;
212 }
213 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000214 BOOST_THROW_EXCEPTION(Error("missing required NOutData field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700215 }
Eric Newberry95bd96a2015-09-04 23:34:22 -0700216
217 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000218 m_nOutNacks = readNonNegativeInteger(*val);
Eric Newberry95bd96a2015-09-04 23:34:22 -0700219 ++val;
220 }
221 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000222 BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
Eric Newberry95bd96a2015-09-04 23:34:22 -0700223 }
Junxiao Shi65f1a712014-11-20 14:59:36 -0700224}
225
226ForwarderStatus&
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600227ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700228{
229 m_wire.reset();
230 m_nfdVersion = nfdVersion;
231 return *this;
232}
233
234ForwarderStatus&
235ForwarderStatus::setStartTimestamp(const time::system_clock::TimePoint& startTimestamp)
236{
237 m_wire.reset();
238 m_startTimestamp = startTimestamp;
239 return *this;
240}
241
242ForwarderStatus&
243ForwarderStatus::setCurrentTimestamp(const time::system_clock::TimePoint& currentTimestamp)
244{
245 m_wire.reset();
246 m_currentTimestamp = currentTimestamp;
247 return *this;
248}
249
250ForwarderStatus&
251ForwarderStatus::setNNameTreeEntries(size_t nNameTreeEntries)
252{
253 m_wire.reset();
254 m_nNameTreeEntries = nNameTreeEntries;
255 return *this;
256}
257
258ForwarderStatus&
259ForwarderStatus::setNFibEntries(size_t nFibEntries)
260{
261 m_wire.reset();
262 m_nFibEntries = nFibEntries;
263 return *this;
264}
265
266ForwarderStatus&
267ForwarderStatus::setNPitEntries(size_t nPitEntries)
268{
269 m_wire.reset();
270 m_nPitEntries = nPitEntries;
271 return *this;
272}
273
274ForwarderStatus&
275ForwarderStatus::setNMeasurementsEntries(size_t nMeasurementsEntries)
276{
277 m_wire.reset();
278 m_nMeasurementsEntries = nMeasurementsEntries;
279 return *this;
280}
281
282ForwarderStatus&
283ForwarderStatus::setNCsEntries(size_t nCsEntries)
284{
285 m_wire.reset();
286 m_nCsEntries = nCsEntries;
287 return *this;
288}
289
290ForwarderStatus&
291ForwarderStatus::setNInInterests(uint64_t nInInterests)
292{
293 m_wire.reset();
294 m_nInInterests = nInInterests;
295 return *this;
296}
297
298ForwarderStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000299ForwarderStatus::setNInData(uint64_t nInData)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700300{
301 m_wire.reset();
Junxiao Shi9a53d782017-04-04 20:09:57 +0000302 m_nInData = nInData;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700303 return *this;
304}
305
306ForwarderStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700307ForwarderStatus::setNInNacks(uint64_t nInNacks)
308{
309 m_wire.reset();
310 m_nInNacks = nInNacks;
311 return *this;
312}
313
314ForwarderStatus&
Junxiao Shi65f1a712014-11-20 14:59:36 -0700315ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
316{
317 m_wire.reset();
318 m_nOutInterests = nOutInterests;
319 return *this;
320}
321
322ForwarderStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000323ForwarderStatus::setNOutData(uint64_t nOutData)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700324{
325 m_wire.reset();
Junxiao Shi9a53d782017-04-04 20:09:57 +0000326 m_nOutData = nOutData;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700327 return *this;
328}
329
Eric Newberry95bd96a2015-09-04 23:34:22 -0700330ForwarderStatus&
331ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
332{
333 m_wire.reset();
334 m_nOutNacks = nOutNacks;
335 return *this;
336}
337
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500338bool
339operator==(const ForwarderStatus& a, const ForwarderStatus& b)
340{
341 return a.getNfdVersion() == b.getNfdVersion() &&
342 a.getStartTimestamp() == b.getStartTimestamp() &&
343 a.getCurrentTimestamp() == b.getCurrentTimestamp() &&
344 a.getNNameTreeEntries() == b.getNNameTreeEntries() &&
345 a.getNFibEntries() == b.getNFibEntries() &&
346 a.getNPitEntries() == b.getNPitEntries() &&
347 a.getNMeasurementsEntries() == b.getNMeasurementsEntries() &&
348 a.getNCsEntries() == b.getNCsEntries() &&
349 a.getNInInterests() == b.getNInInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000350 a.getNInData() == b.getNInData() &&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500351 a.getNInNacks() == b.getNInNacks() &&
352 a.getNOutInterests() == b.getNOutInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000353 a.getNOutData() == b.getNOutData() &&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500354 a.getNOutNacks() == b.getNOutNacks();
355}
356
357std::ostream&
358operator<<(std::ostream& os, const ForwarderStatus& status)
359{
360 os << "GeneralStatus(NfdVersion: " << status.getNfdVersion() << ",\n"
361 << " StartTimestamp: " << status.getStartTimestamp() << ",\n"
362 << " CurrentTimestamp: " << status.getCurrentTimestamp() << ",\n"
363 << " Counters: {NameTreeEntries: " << status.getNNameTreeEntries() << ",\n"
364 << " FibEntries: " << status.getNFibEntries() << ",\n"
365 << " PitEntries: " << status.getNPitEntries() << ",\n"
366 << " MeasurementsEntries: " << status.getNMeasurementsEntries() << ",\n"
367 << " CsEntries: " << status.getNCsEntries() << ",\n"
368 << " Interests: {in: " << status.getNInInterests() << ", "
369 << "out: " << status.getNOutInterests() << "},\n"
Junxiao Shi9a53d782017-04-04 20:09:57 +0000370 << " Data: {in: " << status.getNInData() << ", "
371 << "out: " << status.getNOutData() << "},\n"
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500372 << " Nacks: {in: " << status.getNInNacks() << ", "
373 << "out: " << status.getNOutNacks() << "}}\n"
374 << " )";
375
376 return os;
377}
378
Junxiao Shi65f1a712014-11-20 14:59:36 -0700379} // namespace nfd
380} // namespace ndn