blob: 21fee5611d89b6707ab7dbcb75218f00224debe7 [file] [log] [blame]
Junxiao Shi65f1a712014-11-20 14:59:36 -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 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/tlv-nfd.hpp"
24#include "encoding/block-helpers.hpp"
25#include "util/concepts.hpp"
26
27namespace ndn {
28namespace nfd {
29
30//BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ForwarderStatus>));
31BOOST_CONCEPT_ASSERT((WireEncodable<ForwarderStatus>));
32BOOST_CONCEPT_ASSERT((WireDecodable<ForwarderStatus>));
33static_assert(std::is_base_of<tlv::Error, ForwarderStatus::Error>::value,
34 "ForwarderStatus::Error must inherit from tlv::Error");
35
36ForwarderStatus::ForwarderStatus()
Hila Ben Abraham23f9e782014-12-02 02:21:34 -060037 : m_startTimestamp(time::system_clock::TimePoint::min())
Junxiao Shi65f1a712014-11-20 14:59:36 -070038 , m_currentTimestamp(time::system_clock::TimePoint::min())
39 , m_nNameTreeEntries(0)
40 , m_nFibEntries(0)
41 , m_nPitEntries(0)
42 , m_nMeasurementsEntries(0)
43 , m_nCsEntries(0)
44 , m_nInInterests(0)
45 , m_nInDatas(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070046 , m_nInNacks(0)
Junxiao Shi65f1a712014-11-20 14:59:36 -070047 , m_nOutInterests(0)
48 , m_nOutDatas(0)
Eric Newberry95bd96a2015-09-04 23:34:22 -070049 , m_nOutNacks(0)
Junxiao Shi65f1a712014-11-20 14:59:36 -070050{
51}
52
53ForwarderStatus::ForwarderStatus(const Block& payload)
54{
55 this->wireDecode(payload);
56}
57
Alexander Afanasyev74633892015-02-08 18:08:46 -080058template<encoding::Tag TAG>
Junxiao Shi65f1a712014-11-20 14:59:36 -070059size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -080060ForwarderStatus::wireEncode(EncodingImpl<TAG>& encoder) const
Junxiao Shi65f1a712014-11-20 14:59:36 -070061{
62 size_t totalLength = 0;
63
Eric Newberry95bd96a2015-09-04 23:34:22 -070064 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks,
65 m_nOutNacks);
Junxiao Shi65f1a712014-11-20 14:59:36 -070066 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutDatas,
67 m_nOutDatas);
68 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests,
69 m_nOutInterests);
Eric Newberry95bd96a2015-09-04 23:34:22 -070070 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks,
71 m_nInNacks);
Junxiao Shi65f1a712014-11-20 14:59:36 -070072 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInDatas,
73 m_nInDatas);
74 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests,
75 m_nInInterests);
76 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NCsEntries,
77 m_nCsEntries);
78 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NMeasurementsEntries,
79 m_nMeasurementsEntries);
80 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NPitEntries,
81 m_nPitEntries);
82 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NFibEntries,
83 m_nFibEntries);
84 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NNameTreeEntries,
85 m_nNameTreeEntries);
86 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::CurrentTimestamp,
87 time::toUnixTimestamp(m_currentTimestamp).count());
88 totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::StartTimestamp,
89 time::toUnixTimestamp(m_startTimestamp).count());
Alexander Afanasyev74633892015-02-08 18:08:46 -080090 totalLength += encoder.prependByteArrayBlock(tlv::nfd::NfdVersion,
Hila Ben Abraham23f9e782014-12-02 02:21:34 -060091 reinterpret_cast<const uint8_t*>(m_nfdVersion.c_str()),
92 m_nfdVersion.size());
Junxiao Shi65f1a712014-11-20 14:59:36 -070093
94 totalLength += encoder.prependVarNumber(totalLength);
95 totalLength += encoder.prependVarNumber(tlv::Content);
96 return totalLength;
97}
98
99template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800100ForwarderStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700101
102template size_t
Alexander Afanasyev74633892015-02-08 18:08:46 -0800103ForwarderStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const;
Junxiao Shi65f1a712014-11-20 14:59:36 -0700104
105const Block&
106ForwarderStatus::wireEncode() const
107{
108 if (m_wire.hasWire())
109 return m_wire;
110
111 EncodingEstimator estimator;
112 size_t estimatedSize = wireEncode(estimator);
113
114 EncodingBuffer buffer(estimatedSize, 0);
115 wireEncode(buffer);
116
117 m_wire = buffer.block();
118 return m_wire;
119}
120
121void
122ForwarderStatus::wireDecode(const Block& block)
123{
124 if (block.type() != tlv::Content) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700125 BOOST_THROW_EXCEPTION(Error("expecting Content block for Status payload"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700126 }
127 m_wire = block;
128 m_wire.parse();
129 Block::element_const_iterator val = m_wire.elements_begin();
130
131 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NfdVersion) {
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600132 m_nfdVersion.assign(reinterpret_cast<const char*>(val->value()), val->value_size());
Junxiao Shi65f1a712014-11-20 14:59:36 -0700133 ++val;
134 }
135 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700136 BOOST_THROW_EXCEPTION(Error("missing required NfdVersion field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700137 }
138
139 if (val != m_wire.elements_end() && val->type() == tlv::nfd::StartTimestamp) {
140 m_startTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
141 ++val;
142 }
143 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700144 BOOST_THROW_EXCEPTION(Error("missing required StartTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700145 }
146
147 if (val != m_wire.elements_end() && val->type() == tlv::nfd::CurrentTimestamp) {
148 m_currentTimestamp = time::fromUnixTimestamp(time::milliseconds(readNonNegativeInteger(*val)));
149 ++val;
150 }
151 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700152 BOOST_THROW_EXCEPTION(Error("missing required CurrentTimestamp field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700153 }
154
155 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NNameTreeEntries) {
156 m_nNameTreeEntries = 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 NNameTreeEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700161 }
162
163 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NFibEntries) {
164 m_nFibEntries = 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 NFibEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700169 }
170
171 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NPitEntries) {
172 m_nPitEntries = 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 NPitEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700177 }
178
179 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NMeasurementsEntries) {
180 m_nMeasurementsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
181 ++val;
182 }
183 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700184 BOOST_THROW_EXCEPTION(Error("missing required NMeasurementsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700185 }
186
187 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NCsEntries) {
188 m_nCsEntries = static_cast<size_t>(readNonNegativeInteger(*val));
189 ++val;
190 }
191 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700192 BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700193 }
194
195 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
196 m_nInInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
197 ++val;
198 }
199 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700200 BOOST_THROW_EXCEPTION(Error("missing required NInInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700201 }
202
203 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInDatas) {
204 m_nInDatas = 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 NInDatas field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700209 }
210
Eric Newberry95bd96a2015-09-04 23:34:22 -0700211 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInNacks) {
212 m_nInNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
213 ++val;
214 }
215 else {
216 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
217 }
218
Junxiao Shi65f1a712014-11-20 14:59:36 -0700219 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutInterests) {
220 m_nOutInterests = static_cast<uint64_t>(readNonNegativeInteger(*val));
221 ++val;
222 }
223 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700224 BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700225 }
226
227 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutDatas) {
228 m_nOutDatas = static_cast<uint64_t>(readNonNegativeInteger(*val));
229 ++val;
230 }
231 else {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700232 BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field"));
Junxiao Shi65f1a712014-11-20 14:59:36 -0700233 }
Eric Newberry95bd96a2015-09-04 23:34:22 -0700234
235 if (val != m_wire.elements_end() && val->type() == tlv::nfd::NOutNacks) {
236 m_nOutNacks = static_cast<uint64_t>(readNonNegativeInteger(*val));
237 ++val;
238 }
239 else {
240 BOOST_THROW_EXCEPTION(Error("missing required NInNacks field"));
241 }
Junxiao Shi65f1a712014-11-20 14:59:36 -0700242}
243
244ForwarderStatus&
Hila Ben Abraham23f9e782014-12-02 02:21:34 -0600245ForwarderStatus::setNfdVersion(const std::string& nfdVersion)
Junxiao Shi65f1a712014-11-20 14:59:36 -0700246{
247 m_wire.reset();
248 m_nfdVersion = nfdVersion;
249 return *this;
250}
251
252ForwarderStatus&
253ForwarderStatus::setStartTimestamp(const time::system_clock::TimePoint& startTimestamp)
254{
255 m_wire.reset();
256 m_startTimestamp = startTimestamp;
257 return *this;
258}
259
260ForwarderStatus&
261ForwarderStatus::setCurrentTimestamp(const time::system_clock::TimePoint& currentTimestamp)
262{
263 m_wire.reset();
264 m_currentTimestamp = currentTimestamp;
265 return *this;
266}
267
268ForwarderStatus&
269ForwarderStatus::setNNameTreeEntries(size_t nNameTreeEntries)
270{
271 m_wire.reset();
272 m_nNameTreeEntries = nNameTreeEntries;
273 return *this;
274}
275
276ForwarderStatus&
277ForwarderStatus::setNFibEntries(size_t nFibEntries)
278{
279 m_wire.reset();
280 m_nFibEntries = nFibEntries;
281 return *this;
282}
283
284ForwarderStatus&
285ForwarderStatus::setNPitEntries(size_t nPitEntries)
286{
287 m_wire.reset();
288 m_nPitEntries = nPitEntries;
289 return *this;
290}
291
292ForwarderStatus&
293ForwarderStatus::setNMeasurementsEntries(size_t nMeasurementsEntries)
294{
295 m_wire.reset();
296 m_nMeasurementsEntries = nMeasurementsEntries;
297 return *this;
298}
299
300ForwarderStatus&
301ForwarderStatus::setNCsEntries(size_t nCsEntries)
302{
303 m_wire.reset();
304 m_nCsEntries = nCsEntries;
305 return *this;
306}
307
308ForwarderStatus&
309ForwarderStatus::setNInInterests(uint64_t nInInterests)
310{
311 m_wire.reset();
312 m_nInInterests = nInInterests;
313 return *this;
314}
315
316ForwarderStatus&
317ForwarderStatus::setNInDatas(uint64_t nInDatas)
318{
319 m_wire.reset();
320 m_nInDatas = nInDatas;
321 return *this;
322}
323
324ForwarderStatus&
Eric Newberry95bd96a2015-09-04 23:34:22 -0700325ForwarderStatus::setNInNacks(uint64_t nInNacks)
326{
327 m_wire.reset();
328 m_nInNacks = nInNacks;
329 return *this;
330}
331
332ForwarderStatus&
Junxiao Shi65f1a712014-11-20 14:59:36 -0700333ForwarderStatus::setNOutInterests(uint64_t nOutInterests)
334{
335 m_wire.reset();
336 m_nOutInterests = nOutInterests;
337 return *this;
338}
339
340ForwarderStatus&
341ForwarderStatus::setNOutDatas(uint64_t nOutDatas)
342{
343 m_wire.reset();
344 m_nOutDatas = nOutDatas;
345 return *this;
346}
347
Eric Newberry95bd96a2015-09-04 23:34:22 -0700348ForwarderStatus&
349ForwarderStatus::setNOutNacks(uint64_t nOutNacks)
350{
351 m_wire.reset();
352 m_nOutNacks = nOutNacks;
353 return *this;
354}
355
Junxiao Shi65f1a712014-11-20 14:59:36 -0700356} // namespace nfd
357} // namespace ndn