blob: fbb6c7339e52b923c4340ab38d31eae1cc858e93 [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)
40 , m_nInDatas(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070041 , m_nInNacks(0)
Junxiao Shi65f1a712014-11-20 14:59:36 -070042 , m_nOutInterests(0)
43 , m_nOutDatas(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
Eric Newberry95bd96a2015-09-04 23:34:22 -070059 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks,
60 m_nOutNacks);
Junxiao Shi65f1a712014-11-20 14:59:36 -070061 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutDatas,
62 m_nOutDatas);
63 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests,
64 m_nOutInterests);
Eric Newberry95bd96a2015-09-04 23:34:22 -070065 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks,
66 m_nInNacks);
Junxiao Shi65f1a712014-11-20 14:59:36 -070067 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInDatas,
68 m_nInDatas);
69 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests,
70 m_nInInterests);
71 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries,
72 m_nCsEntries);
73 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries,
74 m_nMeasurementsEntries);
75 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries,
76 m_nPitEntries);
77 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries,
78 m_nFibEntries);
79 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries,
80 m_nNameTreeEntries);
81 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::CurrentTimestamp,
82 time::toUnixTimestamp(m_currentTimestamp).count());
83 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp,
84 time::toUnixTimestamp(m_startTimestamp).count());
Alexander Afanasyev74633892015-02-08 18:08:46 -080085 totalLength += encoder.prependByteArrayBlock(tlv::nfd::NfdVersion,
Davide Pesavento25e3d8c2017-02-08 22:17:46 -050086 reinterpret_cast<const uint8_t*>(m_nfdVersion.data()),
87 m_nfdVersion.size());
Junxiao Shi65f1a712014-11-20 14:59:36 -070088
89 totalLength += encoder.prependVarNumber(totalLength);
90 totalLength += encoder.prependVarNumber(tlv::Content);
91 return totalLength;
92}
93
94template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080095ForwarderStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070096
97template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080098ForwarderStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -070099
100const Block&
101ForwarderStatus::wireEncode() const
102{
103 if (m_wire.hasWire())
104 return m_wire;
105
106 EncodingEstimator estimator;
107 size_t estimatedSize = wireEncode(estimator);
108
109 EncodingBuffer buffer(estimatedSize, 0);
110 wireEncode(buffer);
111
112 m_wire = buffer.block();
113 return m_wire;
114}
115
116void
117ForwarderStatus::wireDecode(const Block& block)
118{
119 if (block.type() != tlv::Content) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700120 BOOST_THROW_EXCEPTION(Error("expecting Content block for Status payload"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700121 }
122 m_wire = block;
123 m_wire.parse();
124 Block::element_const_iterator val = m_wire.elements_begin();
125
126 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600127 m_nfdVersion.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
Junxiao Shi65f1a712014-11-20 14:59:36 -0700128 ++val;
129 }
130 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700131 BOOST_THROW_EXCEPTION(Error("missing required NfdVersion field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700132 }
133
134 if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
135 m_startTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
136 ++val;
137 }
138 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700139 BOOST_THROW_EXCEPTION(Error("missing required StartTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700140 }
141
142 if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
143 m_currentTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
144 ++val;
145 }
146 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700147 BOOST_THROW_EXCEPTION(Error("missing required CurrentTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700148 }
149
150 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
151 m_nNameTreeEntries = static_cast<size_t>(readNonNegativeInteger(*val));
152 ++val;
153 }
154 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700155 BOOST_THROW_EXCEPTION(Error("missing required NNameTreeEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700156 }
157
158 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
159 m_nFibEntries = static_cast<size_t>(readNonNegativeInteger(*val));
160 ++val;
161 }
162 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700163 BOOST_THROW_EXCEPTION(Error("missing required NFibEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700164 }
165
166 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
167 m_nPitEntries = static_cast<size_t>(readNonNegativeInteger(*val));
168 ++val;
169 }
170 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700171 BOOST_THROW_EXCEPTION(Error("missing required NPitEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700172 }
173
174 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
175 m_nMeasurementsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
176 ++val;
177 }
178 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700179 BOOST_THROW_EXCEPTION(Error("missing required NMeasurementsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700180 }
181
182 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
183 m_nCsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
184 ++val;
185 }
186 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700187 BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700188 }
189
190 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
191 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
192 ++val;
193 }
194 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700195 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700196 }
197
198 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
199 m_nInDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
200 ++val;
201 }
202 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700203 BOOST_THROW_EXCEPTION(Error("missing required NInDatas field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700204 }
205
Eric Newberry95bd96a2015-09-04 23:34:22 -0700206 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
207 m_nInNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
208 ++val;
209 }
210 else {
211 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
212 }
213
Junxiao Shi65f1a712014-11-20 14:59:36 -0700214 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
215 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
216 ++val;
217 }
218 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700219 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700220 }
221
222 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
223 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
224 ++val;
225 }
226 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700227 BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700228 }
Eric Newberry95bd96a2015-09-04 23:34:22 -0700229
230 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
231 m_nOutNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
232 ++val;
233 }
234 else {
235 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
236 }
Junxiao Shi65f1a712014-11-20 14:59:36 -0700237}
238
239ForwarderStatus&
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600240ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700241{
242 m_wire.reset();
243 m_nfdVersion = nfdVersion;
244 return *this;
245}
246
247ForwarderStatus&
248ForwarderStatus::setStartTimestamp(const time::system_clock::TimePoint& startTimestamp)
249{
250 m_wire.reset();
251 m_startTimestamp = startTimestamp;
252 return *this;
253}
254
255ForwarderStatus&
256ForwarderStatus::setCurrentTimestamp(const time::system_clock::TimePoint& currentTimestamp)
257{
258 m_wire.reset();
259 m_currentTimestamp = currentTimestamp;
260 return *this;
261}
262
263ForwarderStatus&
264ForwarderStatus::setNNameTreeEntries(size_t nNameTreeEntries)
265{
266 m_wire.reset();
267 m_nNameTreeEntries = nNameTreeEntries;
268 return *this;
269}
270
271ForwarderStatus&
272ForwarderStatus::setNFibEntries(size_t nFibEntries)
273{
274 m_wire.reset();
275 m_nFibEntries = nFibEntries;
276 return *this;
277}
278
279ForwarderStatus&
280ForwarderStatus::setNPitEntries(size_t nPitEntries)
281{
282 m_wire.reset();
283 m_nPitEntries = nPitEntries;
284 return *this;
285}
286
287ForwarderStatus&
288ForwarderStatus::setNMeasurementsEntries(size_t nMeasurementsEntries)
289{
290 m_wire.reset();
291 m_nMeasurementsEntries = nMeasurementsEntries;
292 return *this;
293}
294
295ForwarderStatus&
296ForwarderStatus::setNCsEntries(size_t nCsEntries)
297{
298 m_wire.reset();
299 m_nCsEntries = nCsEntries;
300 return *this;
301}
302
303ForwarderStatus&
304ForwarderStatus::setNInInterests(uint64_t nInInterests)
305{
306 m_wire.reset();
307 m_nInInterests = nInInterests;
308 return *this;
309}
310
311ForwarderStatus&
312ForwarderStatus::setNInDatas(uint64_t nInDatas)
313{
314 m_wire.reset();
315 m_nInDatas = nInDatas;
316 return *this;
317}
318
319ForwarderStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700320ForwarderStatus::setNInNacks(uint64_t nInNacks)
321{
322 m_wire.reset();
323 m_nInNacks = nInNacks;
324 return *this;
325}
326
327ForwarderStatus&
Junxiao Shi65f1a712014-11-20 14:59:36 -0700328ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
329{
330 m_wire.reset();
331 m_nOutInterests = nOutInterests;
332 return *this;
333}
334
335ForwarderStatus&
336ForwarderStatus::setNOutDatas(uint64_t nOutDatas)
337{
338 m_wire.reset();
339 m_nOutDatas = nOutDatas;
340 return *this;
341}
342
Eric Newberry95bd96a2015-09-04 23:34:22 -0700343ForwarderStatus&
344ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
345{
346 m_wire.reset();
347 m_nOutNacks = nOutNacks;
348 return *this;
349}
350
Davide Pesavento25e3d8c2017-02-08 22:17:46 -0500351bool
352operator==(const ForwarderStatus& a, const ForwarderStatus& b)
353{
354 return a.getNfdVersion() == b.getNfdVersion() &&
355 a.getStartTimestamp() == b.getStartTimestamp() &&
356 a.getCurrentTimestamp() == b.getCurrentTimestamp() &&
357 a.getNNameTreeEntries() == b.getNNameTreeEntries() &&
358 a.getNFibEntries() == b.getNFibEntries() &&
359 a.getNPitEntries() == b.getNPitEntries() &&
360 a.getNMeasurementsEntries() == b.getNMeasurementsEntries() &&
361 a.getNCsEntries() == b.getNCsEntries() &&
362 a.getNInInterests() == b.getNInInterests() &&
363 a.getNInDatas() == b.getNInDatas() &&
364 a.getNInNacks() == b.getNInNacks() &&
365 a.getNOutInterests() == b.getNOutInterests() &&
366 a.getNOutDatas() == b.getNOutDatas() &&
367 a.getNOutNacks() == b.getNOutNacks();
368}
369
370std::ostream&
371operator<<(std::ostream& os, const ForwarderStatus& status)
372{
373 os << "GeneralStatus(NfdVersion: " << status.getNfdVersion() << ",\n"
374 << " StartTimestamp: " << status.getStartTimestamp() << ",\n"
375 << " CurrentTimestamp: " << status.getCurrentTimestamp() << ",\n"
376 << " Counters: {NameTreeEntries: " << status.getNNameTreeEntries() << ",\n"
377 << " FibEntries: " << status.getNFibEntries() << ",\n"
378 << " PitEntries: " << status.getNPitEntries() << ",\n"
379 << " MeasurementsEntries: " << status.getNMeasurementsEntries() << ",\n"
380 << " CsEntries: " << status.getNCsEntries() << ",\n"
381 << " Interests: {in: " << status.getNInInterests() << ", "
382 << "out: " << status.getNOutInterests() << "},\n"
383 << " Data: {in: " << status.getNInDatas() << ", "
384 << "out: " << status.getNOutDatas() << "},\n"
385 << " Nacks: {in: " << status.getNInNacks() << ", "
386 << "out: " << status.getNOutNacks() << "}}\n"
387 << " )";
388
389 return os;
390}
391
Junxiao Shi65f1a712014-11-20 14:59:36 -0700392} // namespace nfd
393} // namespace ndn