Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 4 | * |
| 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 Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame^] | 22 | #include "forwarder-status.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 23 | #include "encoding/tlv-nfd.hpp" |
| 24 | #include "encoding/block-helpers.hpp" |
| 25 | #include "util/concepts.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
| 30 | //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ForwarderStatus>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<ForwarderStatus>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<ForwarderStatus>)); |
| 33 | static_assert(std::is_base_of<tlv::Error, ForwarderStatus::Error>::value, |
| 34 | "ForwarderStatus::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | ForwarderStatus::ForwarderStatus() |
Hila Ben Abraham | 23f9e78 | 2014-12-02 02:21:34 -0600 | [diff] [blame] | 37 | : m_startTimestamp(time::system_clock::TimePoint::min()) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 38 | , 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 Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 46 | , m_nInNacks(0) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 47 | , m_nOutInterests(0) |
| 48 | , m_nOutDatas(0) |
Eric Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 49 | , m_nOutNacks(0) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 50 | { |
| 51 | } |
| 52 | |
| 53 | ForwarderStatus::ForwarderStatus(const Block& payload) |
| 54 | { |
| 55 | this->wireDecode(payload); |
| 56 | } |
| 57 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 58 | template<encoding::Tag TAG> |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 59 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 60 | ForwarderStatus::wireEncode(EncodingImpl<TAG>& encoder) const |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 61 | { |
| 62 | size_t totalLength = 0; |
| 63 | |
Eric Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 64 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutNacks, |
| 65 | m_nOutNacks); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 66 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutDatas, |
| 67 | m_nOutDatas); |
| 68 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NOutInterests, |
| 69 | m_nOutInterests); |
Eric Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 70 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, |
| 71 | m_nInNacks); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 72 | 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 Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 90 | totalLength += encoder.prependByteArrayBlock(tlv::nfd::NfdVersion, |
Hila Ben Abraham | 23f9e78 | 2014-12-02 02:21:34 -0600 | [diff] [blame] | 91 | reinterpret_cast<const uint8_t*>(m_nfdVersion.c_str()), |
| 92 | m_nfdVersion.size()); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 93 | |
| 94 | totalLength += encoder.prependVarNumber(totalLength); |
| 95 | totalLength += encoder.prependVarNumber(tlv::Content); |
| 96 | return totalLength; |
| 97 | } |
| 98 | |
| 99 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 100 | ForwarderStatus::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 101 | |
| 102 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 103 | ForwarderStatus::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>&) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 104 | |
| 105 | const Block& |
| 106 | ForwarderStatus::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 | |
| 121 | void |
| 122 | ForwarderStatus::wireDecode(const Block& block) |
| 123 | { |
| 124 | if (block.type() != tlv::Content) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 125 | BOOST_THROW_EXCEPTION(Error("expecting Content block for Status payload")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 126 | } |
| 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 Abraham | 23f9e78 | 2014-12-02 02:21:34 -0600 | [diff] [blame] | 132 | m_nfdVersion.assign(reinterpret_cast<const char*>(val->value()), val->value_size()); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 133 | ++val; |
| 134 | } |
| 135 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 136 | BOOST_THROW_EXCEPTION(Error("missing required NfdVersion field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 137 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 144 | BOOST_THROW_EXCEPTION(Error("missing required StartTimestamp field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 145 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 152 | BOOST_THROW_EXCEPTION(Error("missing required CurrentTimestamp field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 153 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 160 | BOOST_THROW_EXCEPTION(Error("missing required NNameTreeEntries field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 161 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 168 | BOOST_THROW_EXCEPTION(Error("missing required NFibEntries field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 169 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 176 | BOOST_THROW_EXCEPTION(Error("missing required NPitEntries field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 177 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 184 | BOOST_THROW_EXCEPTION(Error("missing required NMeasurementsEntries field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 185 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 192 | BOOST_THROW_EXCEPTION(Error("missing required NCsEntries field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 193 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 200 | BOOST_THROW_EXCEPTION(Error("missing required NInInterests field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 201 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 208 | BOOST_THROW_EXCEPTION(Error("missing required NInDatas field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Eric Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 211 | 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 Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 219 | 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 224 | BOOST_THROW_EXCEPTION(Error("missing required NOutInterests field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 225 | } |
| 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 Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 232 | BOOST_THROW_EXCEPTION(Error("missing required NOutDatas field")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 233 | } |
Eric Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 234 | |
| 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 Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | ForwarderStatus& |
Hila Ben Abraham | 23f9e78 | 2014-12-02 02:21:34 -0600 | [diff] [blame] | 245 | ForwarderStatus::setNfdVersion(const std::string& nfdVersion) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 246 | { |
| 247 | m_wire.reset(); |
| 248 | m_nfdVersion = nfdVersion; |
| 249 | return *this; |
| 250 | } |
| 251 | |
| 252 | ForwarderStatus& |
| 253 | ForwarderStatus::setStartTimestamp(const time::system_clock::TimePoint& startTimestamp) |
| 254 | { |
| 255 | m_wire.reset(); |
| 256 | m_startTimestamp = startTimestamp; |
| 257 | return *this; |
| 258 | } |
| 259 | |
| 260 | ForwarderStatus& |
| 261 | ForwarderStatus::setCurrentTimestamp(const time::system_clock::TimePoint& currentTimestamp) |
| 262 | { |
| 263 | m_wire.reset(); |
| 264 | m_currentTimestamp = currentTimestamp; |
| 265 | return *this; |
| 266 | } |
| 267 | |
| 268 | ForwarderStatus& |
| 269 | ForwarderStatus::setNNameTreeEntries(size_t nNameTreeEntries) |
| 270 | { |
| 271 | m_wire.reset(); |
| 272 | m_nNameTreeEntries = nNameTreeEntries; |
| 273 | return *this; |
| 274 | } |
| 275 | |
| 276 | ForwarderStatus& |
| 277 | ForwarderStatus::setNFibEntries(size_t nFibEntries) |
| 278 | { |
| 279 | m_wire.reset(); |
| 280 | m_nFibEntries = nFibEntries; |
| 281 | return *this; |
| 282 | } |
| 283 | |
| 284 | ForwarderStatus& |
| 285 | ForwarderStatus::setNPitEntries(size_t nPitEntries) |
| 286 | { |
| 287 | m_wire.reset(); |
| 288 | m_nPitEntries = nPitEntries; |
| 289 | return *this; |
| 290 | } |
| 291 | |
| 292 | ForwarderStatus& |
| 293 | ForwarderStatus::setNMeasurementsEntries(size_t nMeasurementsEntries) |
| 294 | { |
| 295 | m_wire.reset(); |
| 296 | m_nMeasurementsEntries = nMeasurementsEntries; |
| 297 | return *this; |
| 298 | } |
| 299 | |
| 300 | ForwarderStatus& |
| 301 | ForwarderStatus::setNCsEntries(size_t nCsEntries) |
| 302 | { |
| 303 | m_wire.reset(); |
| 304 | m_nCsEntries = nCsEntries; |
| 305 | return *this; |
| 306 | } |
| 307 | |
| 308 | ForwarderStatus& |
| 309 | ForwarderStatus::setNInInterests(uint64_t nInInterests) |
| 310 | { |
| 311 | m_wire.reset(); |
| 312 | m_nInInterests = nInInterests; |
| 313 | return *this; |
| 314 | } |
| 315 | |
| 316 | ForwarderStatus& |
| 317 | ForwarderStatus::setNInDatas(uint64_t nInDatas) |
| 318 | { |
| 319 | m_wire.reset(); |
| 320 | m_nInDatas = nInDatas; |
| 321 | return *this; |
| 322 | } |
| 323 | |
| 324 | ForwarderStatus& |
Eric Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 325 | ForwarderStatus::setNInNacks(uint64_t nInNacks) |
| 326 | { |
| 327 | m_wire.reset(); |
| 328 | m_nInNacks = nInNacks; |
| 329 | return *this; |
| 330 | } |
| 331 | |
| 332 | ForwarderStatus& |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 333 | ForwarderStatus::setNOutInterests(uint64_t nOutInterests) |
| 334 | { |
| 335 | m_wire.reset(); |
| 336 | m_nOutInterests = nOutInterests; |
| 337 | return *this; |
| 338 | } |
| 339 | |
| 340 | ForwarderStatus& |
| 341 | ForwarderStatus::setNOutDatas(uint64_t nOutDatas) |
| 342 | { |
| 343 | m_wire.reset(); |
| 344 | m_nOutDatas = nOutDatas; |
| 345 | return *this; |
| 346 | } |
| 347 | |
Eric Newberry | 95bd96a | 2015-09-04 23:34:22 -0700 | [diff] [blame] | 348 | ForwarderStatus& |
| 349 | ForwarderStatus::setNOutNacks(uint64_t nOutNacks) |
| 350 | { |
| 351 | m_wire.reset(); |
| 352 | m_nOutNacks = nOutNacks; |
| 353 | return *this; |
| 354 | } |
| 355 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 356 | } // namespace nfd |
| 357 | } // namespace ndn |