blob: 27f506e3f079cfb0642739bf62476f62d0d44d2f [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
Davide Pesavento88a0d812017-08-19 21:31:42 -040081NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ForwarderStatus);
Junxiao Shi65f1a712014-11-20 14:59:36 -070082
83const Block&
84ForwarderStatus::wireEncode() const
85{
86 if (m_wire.hasWire())
87 return m_wire;
88
89 EncodingEstimator estimator;
90 size_t estimatedSize = wireEncode(estimator);
91
92 EncodingBuffer buffer(estimatedSize, 0);
93 wireEncode(buffer);
94
95 m_wire = buffer.block();
96 return m_wire;
97}
98
99void
100ForwarderStatus::wireDecode(const Block& block)
101{
102 if (block.type() != tlv::Content) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700103 BOOST_THROW_EXCEPTION(Error("expecting Content block for Status payload"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700104 }
105 m_wire = block;
106 m_wire.parse();
107 Block::element_const_iterator val = m_wire.elements_begin();
108
109 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000110 m_nfdVersion = readString(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700111 ++val;
112 }
113 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700114 BOOST_THROW_EXCEPTION(Error("missing required NfdVersion field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700115 }
116
117 if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
118 m_startTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
119 ++val;
120 }
121 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700122 BOOST_THROW_EXCEPTION(Error("missing required StartTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700123 }
124
125 if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
126 m_currentTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
127 ++val;
128 }
129 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700130 BOOST_THROW_EXCEPTION(Error("missing required CurrentTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700131 }
132
133 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000134 m_nNameTreeEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700135 ++val;
136 }
137 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700138 BOOST_THROW_EXCEPTION(Error("missing required NNameTreeEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700139 }
140
141 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000142 m_nFibEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700143 ++val;
144 }
145 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700146 BOOST_THROW_EXCEPTION(Error("missing required NFibEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700147 }
148
149 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000150 m_nPitEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700151 ++val;
152 }
153 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700154 BOOST_THROW_EXCEPTION(Error("missing required NPitEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700155 }
156
157 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000158 m_nMeasurementsEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700159 ++val;
160 }
161 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700162 BOOST_THROW_EXCEPTION(Error("missing required NMeasurementsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700163 }
164
165 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000166 m_nCsEntries = readNonNegativeIntegerAs<size_t>(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700167 ++val;
168 }
169 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700170 BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700171 }
172
173 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000174 m_nInInterests = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700175 ++val;
176 }
177 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700178 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700179 }
180
Junxiao Shi9a53d782017-04-04 20:09:57 +0000181 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000182 m_nInData = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700183 ++val;
184 }
185 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000186 BOOST_THROW_EXCEPTION(Error("missing required NInData field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700187 }
188
Eric Newberry95bd96a2015-09-04 23:34:22 -0700189 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000190 m_nInNacks = readNonNegativeInteger(*val);
Eric Newberry95bd96a2015-09-04 23:34:22 -0700191 ++val;
192 }
193 else {
194 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
195 }
196
Junxiao Shi65f1a712014-11-20 14:59:36 -0700197 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000198 m_nOutInterests = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700199 ++val;
200 }
201 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700202 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700203 }
204
Junxiao Shi9a53d782017-04-04 20:09:57 +0000205 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000206 m_nOutData = readNonNegativeInteger(*val);
Junxiao Shi65f1a712014-11-20 14:59:36 -0700207 ++val;
208 }
209 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000210 BOOST_THROW_EXCEPTION(Error("missing required NOutData field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700211 }
Eric Newberry95bd96a2015-09-04 23:34:22 -0700212
213 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
Junxiao Shi5d75fd92017-08-08 18:09:20 +0000214 m_nOutNacks = readNonNegativeInteger(*val);
Eric Newberry95bd96a2015-09-04 23:34:22 -0700215 ++val;
216 }
217 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000218 BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
Eric Newberry95bd96a2015-09-04 23:34:22 -0700219 }
Junxiao Shi65f1a712014-11-20 14:59:36 -0700220}
221
222ForwarderStatus&
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600223ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700224{
225 m_wire.reset();
226 m_nfdVersion = nfdVersion;
227 return *this;
228}
229
230ForwarderStatus&
231ForwarderStatus::setStartTimestamp(const time::system_clock::TimePoint& startTimestamp)
232{
233 m_wire.reset();
234 m_startTimestamp = startTimestamp;
235 return *this;
236}
237
238ForwarderStatus&
239ForwarderStatus::setCurrentTimestamp(const time::system_clock::TimePoint& currentTimestamp)
240{
241 m_wire.reset();
242 m_currentTimestamp = currentTimestamp;
243 return *this;
244}
245
246ForwarderStatus&
247ForwarderStatus::setNNameTreeEntries(size_t nNameTreeEntries)
248{
249 m_wire.reset();
250 m_nNameTreeEntries = nNameTreeEntries;
251 return *this;
252}
253
254ForwarderStatus&
255ForwarderStatus::setNFibEntries(size_t nFibEntries)
256{
257 m_wire.reset();
258 m_nFibEntries = nFibEntries;
259 return *this;
260}
261
262ForwarderStatus&
263ForwarderStatus::setNPitEntries(size_t nPitEntries)
264{
265 m_wire.reset();
266 m_nPitEntries = nPitEntries;
267 return *this;
268}
269
270ForwarderStatus&
271ForwarderStatus::setNMeasurementsEntries(size_t nMeasurementsEntries)
272{
273 m_wire.reset();
274 m_nMeasurementsEntries = nMeasurementsEntries;
275 return *this;
276}
277
278ForwarderStatus&
279ForwarderStatus::setNCsEntries(size_t nCsEntries)
280{
281 m_wire.reset();
282 m_nCsEntries = nCsEntries;
283 return *this;
284}
285
286ForwarderStatus&
287ForwarderStatus::setNInInterests(uint64_t nInInterests)
288{
289 m_wire.reset();
290 m_nInInterests = nInInterests;
291 return *this;
292}
293
294ForwarderStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000295ForwarderStatus::setNInData(uint64_t nInData)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700296{
297 m_wire.reset();
Junxiao Shi9a53d782017-04-04 20:09:57 +0000298 m_nInData = nInData;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700299 return *this;
300}
301
302ForwarderStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700303ForwarderStatus::setNInNacks(uint64_t nInNacks)
304{
305 m_wire.reset();
306 m_nInNacks = nInNacks;
307 return *this;
308}
309
310ForwarderStatus&
Junxiao Shi65f1a712014-11-20 14:59:36 -0700311ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
312{
313 m_wire.reset();
314 m_nOutInterests = nOutInterests;
315 return *this;
316}
317
318ForwarderStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000319ForwarderStatus::setNOutData(uint64_t nOutData)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700320{
321 m_wire.reset();
Junxiao Shi9a53d782017-04-04 20:09:57 +0000322 m_nOutData = nOutData;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700323 return *this;
324}
325
Eric Newberry95bd96a2015-09-04 23:34:22 -0700326ForwarderStatus&
327ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
328{
329 m_wire.reset();
330 m_nOutNacks = nOutNacks;
331 return *this;
332}
333
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500334bool
335operator==(const ForwarderStatus& a, const ForwarderStatus& b)
336{
337 return a.getNfdVersion() == b.getNfdVersion() &&
338 a.getStartTimestamp() == b.getStartTimestamp() &&
339 a.getCurrentTimestamp() == b.getCurrentTimestamp() &&
340 a.getNNameTreeEntries() == b.getNNameTreeEntries() &&
341 a.getNFibEntries() == b.getNFibEntries() &&
342 a.getNPitEntries() == b.getNPitEntries() &&
343 a.getNMeasurementsEntries() == b.getNMeasurementsEntries() &&
344 a.getNCsEntries() == b.getNCsEntries() &&
345 a.getNInInterests() == b.getNInInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000346 a.getNInData() == b.getNInData() &&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500347 a.getNInNacks() == b.getNInNacks() &&
348 a.getNOutInterests() == b.getNOutInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000349 a.getNOutData() == b.getNOutData() &&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500350 a.getNOutNacks() == b.getNOutNacks();
351}
352
353std::ostream&
354operator<<(std::ostream& os, const ForwarderStatus& status)
355{
356 os << "GeneralStatus(NfdVersion: " << status.getNfdVersion() << ",\n"
357 << " StartTimestamp: " << status.getStartTimestamp() << ",\n"
358 << " CurrentTimestamp: " << status.getCurrentTimestamp() << ",\n"
359 << " Counters: {NameTreeEntries: " << status.getNNameTreeEntries() << ",\n"
360 << " FibEntries: " << status.getNFibEntries() << ",\n"
361 << " PitEntries: " << status.getNPitEntries() << ",\n"
362 << " MeasurementsEntries: " << status.getNMeasurementsEntries() << ",\n"
363 << " CsEntries: " << status.getNCsEntries() << ",\n"
364 << " Interests: {in: " << status.getNInInterests() << ", "
365 << "out: " << status.getNOutInterests() << "},\n"
Junxiao Shi9a53d782017-04-04 20:09:57 +0000366 << " Data: {in: " << status.getNInData() << ", "
367 << "out: " << status.getNOutData() << "},\n"
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500368 << " Nacks: {in: " << status.getNInNacks() << ", "
369 << "out: " << status.getNOutNacks() << "}}\n"
370 << " )";
371
372 return os;
373}
374
Junxiao Shi65f1a712014-11-20 14:59:36 -0700375} // namespace nfd
376} // namespace ndn