blob: 06c8f1fd940d06184037a4169965155f47990707 [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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());
Alexander Afanasyev74633892015-02-08 18:08:46 -080074 totalLength += encoder.prependByteArrayBlock(tlv::nfd::NfdVersion,
Davide Pesavento25e3d8c2017-02-08 22:17:46 -050075 reinterpret_cast<const uint8_t*>(m_nfdVersion.data()),
76 m_nfdVersion.size());
Junxiao Shi65f1a712014-11-20 14:59:36 -070077
78 totalLength += encoder.prependVarNumber(totalLength);
79 totalLength += encoder.prependVarNumber(tlv::Content);
80 return totalLength;
81}
82
83template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080084ForwarderStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070085
86template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080087ForwarderStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070088
89const Block&
90ForwarderStatus::wireEncode() const
91{
92 if (m_wire.hasWire())
93 return m_wire;
94
95 EncodingEstimator estimator;
96 size_t estimatedSize = wireEncode(estimator);
97
98 EncodingBuffer buffer(estimatedSize, 0);
99 wireEncode(buffer);
100
101 m_wire = buffer.block();
102 return m_wire;
103}
104
105void
106ForwarderStatus::wireDecode(const Block& block)
107{
108 if (block.type() != tlv::Content) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700109 BOOST_THROW_EXCEPTION(Error("expecting Content block for Status payload"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700110 }
111 m_wire = block;
112 m_wire.parse();
113 Block::element_const_iterator val = m_wire.elements_begin();
114
115 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600116 m_nfdVersion.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
Junxiao Shi65f1a712014-11-20 14:59:36 -0700117 ++val;
118 }
119 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700120 BOOST_THROW_EXCEPTION(Error("missing required NfdVersion field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700121 }
122
123 if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
124 m_startTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
125 ++val;
126 }
127 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700128 BOOST_THROW_EXCEPTION(Error("missing required StartTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700129 }
130
131 if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
132 m_currentTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
133 ++val;
134 }
135 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700136 BOOST_THROW_EXCEPTION(Error("missing required CurrentTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700137 }
138
139 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
140 m_nNameTreeEntries = static_cast<size_t>(readNonNegativeInteger(*val));
141 ++val;
142 }
143 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700144 BOOST_THROW_EXCEPTION(Error("missing required NNameTreeEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700145 }
146
147 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
148 m_nFibEntries = static_cast<size_t>(readNonNegativeInteger(*val));
149 ++val;
150 }
151 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700152 BOOST_THROW_EXCEPTION(Error("missing required NFibEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700153 }
154
155 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
156 m_nPitEntries = static_cast<size_t>(readNonNegativeInteger(*val));
157 ++val;
158 }
159 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700160 BOOST_THROW_EXCEPTION(Error("missing required NPitEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700161 }
162
163 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
164 m_nMeasurementsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
165 ++val;
166 }
167 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700168 BOOST_THROW_EXCEPTION(Error("missing required NMeasurementsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700169 }
170
171 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
172 m_nCsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
173 ++val;
174 }
175 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700176 BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700177 }
178
179 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
180 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
181 ++val;
182 }
183 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700184 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700185 }
186
Junxiao Shi9a53d782017-04-04 20:09:57 +0000187 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInData) {
188 m_nInData = static_cast<uint64_t>(readNonNegativeInteger(*val));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700189 ++val;
190 }
191 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000192 BOOST_THROW_EXCEPTION(Error("missing required NInData field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700193 }
194
Eric Newberry95bd96a2015-09-04 23:34:22 -0700195 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
196 m_nInNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
197 ++val;
198 }
199 else {
200 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
201 }
202
Junxiao Shi65f1a712014-11-20 14:59:36 -0700203 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
204 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
205 ++val;
206 }
207 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700208 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700209 }
210
Junxiao Shi9a53d782017-04-04 20:09:57 +0000211 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutData) {
212 m_nOutData = static_cast<uint64_t>(readNonNegativeInteger(*val));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700213 ++val;
214 }
215 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000216 BOOST_THROW_EXCEPTION(Error("missing required NOutData field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700217 }
Eric Newberry95bd96a2015-09-04 23:34:22 -0700218
219 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
220 m_nOutNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
221 ++val;
222 }
223 else {
Junxiao Shi9a53d782017-04-04 20:09:57 +0000224 BOOST_THROW_EXCEPTION(Error("missing required NOutNacks field"));
Eric Newberry95bd96a2015-09-04 23:34:22 -0700225 }
Junxiao Shi65f1a712014-11-20 14:59:36 -0700226}
227
228ForwarderStatus&
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600229ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700230{
231 m_wire.reset();
232 m_nfdVersion = nfdVersion;
233 return *this;
234}
235
236ForwarderStatus&
237ForwarderStatus::setStartTimestamp(const time::system_clock::TimePoint& startTimestamp)
238{
239 m_wire.reset();
240 m_startTimestamp = startTimestamp;
241 return *this;
242}
243
244ForwarderStatus&
245ForwarderStatus::setCurrentTimestamp(const time::system_clock::TimePoint& currentTimestamp)
246{
247 m_wire.reset();
248 m_currentTimestamp = currentTimestamp;
249 return *this;
250}
251
252ForwarderStatus&
253ForwarderStatus::setNNameTreeEntries(size_t nNameTreeEntries)
254{
255 m_wire.reset();
256 m_nNameTreeEntries = nNameTreeEntries;
257 return *this;
258}
259
260ForwarderStatus&
261ForwarderStatus::setNFibEntries(size_t nFibEntries)
262{
263 m_wire.reset();
264 m_nFibEntries = nFibEntries;
265 return *this;
266}
267
268ForwarderStatus&
269ForwarderStatus::setNPitEntries(size_t nPitEntries)
270{
271 m_wire.reset();
272 m_nPitEntries = nPitEntries;
273 return *this;
274}
275
276ForwarderStatus&
277ForwarderStatus::setNMeasurementsEntries(size_t nMeasurementsEntries)
278{
279 m_wire.reset();
280 m_nMeasurementsEntries = nMeasurementsEntries;
281 return *this;
282}
283
284ForwarderStatus&
285ForwarderStatus::setNCsEntries(size_t nCsEntries)
286{
287 m_wire.reset();
288 m_nCsEntries = nCsEntries;
289 return *this;
290}
291
292ForwarderStatus&
293ForwarderStatus::setNInInterests(uint64_t nInInterests)
294{
295 m_wire.reset();
296 m_nInInterests = nInInterests;
297 return *this;
298}
299
300ForwarderStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000301ForwarderStatus::setNInData(uint64_t nInData)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700302{
303 m_wire.reset();
Junxiao Shi9a53d782017-04-04 20:09:57 +0000304 m_nInData = nInData;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700305 return *this;
306}
307
308ForwarderStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700309ForwarderStatus::setNInNacks(uint64_t nInNacks)
310{
311 m_wire.reset();
312 m_nInNacks = nInNacks;
313 return *this;
314}
315
316ForwarderStatus&
Junxiao Shi65f1a712014-11-20 14:59:36 -0700317ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
318{
319 m_wire.reset();
320 m_nOutInterests = nOutInterests;
321 return *this;
322}
323
324ForwarderStatus&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000325ForwarderStatus::setNOutData(uint64_t nOutData)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700326{
327 m_wire.reset();
Junxiao Shi9a53d782017-04-04 20:09:57 +0000328 m_nOutData = nOutData;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700329 return *this;
330}
331
Eric Newberry95bd96a2015-09-04 23:34:22 -0700332ForwarderStatus&
333ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
334{
335 m_wire.reset();
336 m_nOutNacks = nOutNacks;
337 return *this;
338}
339
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500340bool
341operator==(const ForwarderStatus& a, const ForwarderStatus& b)
342{
343 return a.getNfdVersion() == b.getNfdVersion() &&
344 a.getStartTimestamp() == b.getStartTimestamp() &&
345 a.getCurrentTimestamp() == b.getCurrentTimestamp() &&
346 a.getNNameTreeEntries() == b.getNNameTreeEntries() &&
347 a.getNFibEntries() == b.getNFibEntries() &&
348 a.getNPitEntries() == b.getNPitEntries() &&
349 a.getNMeasurementsEntries() == b.getNMeasurementsEntries() &&
350 a.getNCsEntries() == b.getNCsEntries() &&
351 a.getNInInterests() == b.getNInInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000352 a.getNInData() == b.getNInData() &&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500353 a.getNInNacks() == b.getNInNacks() &&
354 a.getNOutInterests() == b.getNOutInterests() &&
Junxiao Shi9a53d782017-04-04 20:09:57 +0000355 a.getNOutData() == b.getNOutData() &&
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500356 a.getNOutNacks() == b.getNOutNacks();
357}
358
359std::ostream&
360operator<<(std::ostream& os, const ForwarderStatus& status)
361{
362 os << "GeneralStatus(NfdVersion: " << status.getNfdVersion() << ",\n"
363 << " StartTimestamp: " << status.getStartTimestamp() << ",\n"
364 << " CurrentTimestamp: " << status.getCurrentTimestamp() << ",\n"
365 << " Counters: {NameTreeEntries: " << status.getNNameTreeEntries() << ",\n"
366 << " FibEntries: " << status.getNFibEntries() << ",\n"
367 << " PitEntries: " << status.getNPitEntries() << ",\n"
368 << " MeasurementsEntries: " << status.getNMeasurementsEntries() << ",\n"
369 << " CsEntries: " << status.getNCsEntries() << ",\n"
370 << " Interests: {in: " << status.getNInInterests() << ", "
371 << "out: " << status.getNOutInterests() << "},\n"
Junxiao Shi9a53d782017-04-04 20:09:57 +0000372 << " Data: {in: " << status.getNInData() << ", "
373 << "out: " << status.getNOutData() << "},\n"
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500374 << " Nacks: {in: " << status.getNInNacks() << ", "
375 << "out: " << status.getNOutNacks() << "}}\n"
376 << " )";
377
378 return os;
379}
380
Junxiao Shi65f1a712014-11-20 14:59:36 -0700381} // namespace nfd
382} // namespace ndn