Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | f8503d2 | 2017-02-17 01:19:10 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [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 "rib-entry.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" |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
| 29 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 30 | //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Route>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<Route>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<Route>)); |
| 33 | static_assert(std::is_base_of<tlv::Error, Route::Error>::value, |
| 34 | "Route::Error must inherit from tlv::Error"); |
| 35 | |
| 36 | //BOOST_CONCEPT_ASSERT((boost::EqualityComparable<RibEntry>)); |
| 37 | BOOST_CONCEPT_ASSERT((WireEncodable<RibEntry>)); |
| 38 | BOOST_CONCEPT_ASSERT((WireDecodable<RibEntry>)); |
| 39 | static_assert(std::is_base_of<tlv::Error, RibEntry::Error>::value, |
| 40 | "RibEntry::Error must inherit from tlv::Error"); |
| 41 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 42 | const time::milliseconds Route::INFINITE_EXPIRATION_PERIOD(time::milliseconds::max()); |
| 43 | |
| 44 | Route::Route() |
Davide Pesavento | f8503d2 | 2017-02-17 01:19:10 -0500 | [diff] [blame] | 45 | : m_faceId(INVALID_FACE_ID) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 46 | , m_origin(0) |
| 47 | , m_cost(0) |
| 48 | , m_flags(ROUTE_FLAG_CHILD_INHERIT) |
| 49 | , m_expirationPeriod(INFINITE_EXPIRATION_PERIOD) |
| 50 | , m_hasInfiniteExpirationPeriod(true) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | Route::Route(const Block& block) |
| 55 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 56 | this->wireDecode(block); |
| 57 | } |
| 58 | |
| 59 | Route& |
| 60 | Route::setFaceId(uint64_t faceId) |
| 61 | { |
| 62 | m_faceId = faceId; |
| 63 | m_wire.reset(); |
| 64 | return *this; |
| 65 | } |
| 66 | |
| 67 | Route& |
| 68 | Route::setOrigin(uint64_t origin) |
| 69 | { |
| 70 | m_origin = origin; |
| 71 | m_wire.reset(); |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | Route& |
| 76 | Route::setCost(uint64_t cost) |
| 77 | { |
| 78 | m_cost = cost; |
| 79 | m_wire.reset(); |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | Route& |
| 84 | Route::setFlags(uint64_t flags) |
| 85 | { |
| 86 | m_flags = flags; |
| 87 | m_wire.reset(); |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | Route& |
| 92 | Route::setExpirationPeriod(time::milliseconds expirationPeriod) |
| 93 | { |
| 94 | m_expirationPeriod = expirationPeriod; |
| 95 | m_hasInfiniteExpirationPeriod = m_expirationPeriod == INFINITE_EXPIRATION_PERIOD; |
| 96 | m_wire.reset(); |
| 97 | return *this; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 100 | template<encoding::Tag TAG> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 101 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 102 | Route::wireEncode(EncodingImpl<TAG>& block) const |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 103 | { |
| 104 | size_t totalLength = 0; |
| 105 | |
| 106 | // Absence of an ExpirationPeriod signifies non-expiration |
| 107 | if (!m_hasInfiniteExpirationPeriod) { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 108 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::ExpirationPeriod, |
| 109 | static_cast<uint64_t>(m_expirationPeriod.count())); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 110 | } |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 111 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Flags, m_flags); |
| 112 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost); |
| 113 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Origin, m_origin); |
| 114 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 115 | |
| 116 | totalLength += block.prependVarNumber(totalLength); |
| 117 | totalLength += block.prependVarNumber(ndn::tlv::nfd::Route); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 118 | return totalLength; |
| 119 | } |
| 120 | |
| 121 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 122 | Route::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 123 | |
| 124 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 125 | Route::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 126 | |
| 127 | const Block& |
| 128 | Route::wireEncode() const |
| 129 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 130 | if (m_wire.hasWire()) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 131 | return m_wire; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 132 | |
| 133 | EncodingEstimator estimator; |
| 134 | size_t estimatedSize = wireEncode(estimator); |
| 135 | |
| 136 | EncodingBuffer buffer(estimatedSize, 0); |
| 137 | wireEncode(buffer); |
| 138 | |
| 139 | m_wire = buffer.block(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 140 | return m_wire; |
| 141 | } |
| 142 | |
| 143 | void |
| 144 | Route::wireDecode(const Block& wire) |
| 145 | { |
| 146 | m_faceId = 0; |
| 147 | m_origin = 0; |
| 148 | m_cost = 0; |
| 149 | m_flags = 0; |
| 150 | m_expirationPeriod = time::milliseconds::min(); |
| 151 | |
| 152 | m_wire = wire; |
| 153 | |
| 154 | if (m_wire.type() != tlv::nfd::Route) { |
| 155 | std::stringstream error; |
| 156 | error << "Expected Route Block, but Block is of a different type: #" |
| 157 | << m_wire.type(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 158 | BOOST_THROW_EXCEPTION(Error(error.str())); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | m_wire.parse(); |
| 162 | |
| 163 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 164 | |
| 165 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 166 | m_faceId = readNonNegativeInteger(*val); |
| 167 | ++val; |
| 168 | } |
| 169 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 170 | BOOST_THROW_EXCEPTION(Error("Missing required FaceId field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) { |
| 174 | m_origin = readNonNegativeInteger(*val); |
| 175 | ++val; |
| 176 | } |
| 177 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 178 | BOOST_THROW_EXCEPTION(Error("Missing required Origin field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Cost) { |
| 182 | m_cost = readNonNegativeInteger(*val); |
| 183 | ++val; |
| 184 | } |
| 185 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 186 | BOOST_THROW_EXCEPTION(Error("Missing required Cost field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) { |
| 190 | m_flags = readNonNegativeInteger(*val); |
| 191 | ++val; |
| 192 | } |
| 193 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 194 | BOOST_THROW_EXCEPTION(Error("Missing required Flags field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) { |
| 198 | m_expirationPeriod = time::milliseconds(readNonNegativeInteger(*val)); |
| 199 | m_hasInfiniteExpirationPeriod = false; |
| 200 | } |
| 201 | else { |
| 202 | m_expirationPeriod = INFINITE_EXPIRATION_PERIOD; |
| 203 | m_hasInfiniteExpirationPeriod = true; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | std::ostream& |
| 208 | operator<<(std::ostream& os, const Route& route) |
| 209 | { |
| 210 | os << "Route(" |
| 211 | << "FaceId: " << route.getFaceId() << ", " |
| 212 | << "Origin: " << route.getOrigin() << ", " |
| 213 | << "Cost: " << route.getCost() << ", " |
| 214 | << "Flags: " << route.getFlags() << ", "; |
| 215 | |
| 216 | if (!route.hasInfiniteExpirationPeriod()) { |
| 217 | os << "ExpirationPeriod: " << route.getExpirationPeriod(); |
| 218 | } |
| 219 | else { |
| 220 | os << "ExpirationPeriod: Infinity"; |
| 221 | } |
| 222 | |
| 223 | os << ")"; |
| 224 | |
| 225 | return os; |
| 226 | } |
| 227 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 228 | //////////////////// |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 229 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 230 | RibEntry::RibEntry() = default; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 231 | |
| 232 | RibEntry::RibEntry(const Block& block) |
| 233 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 234 | this->wireDecode(block); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 237 | RibEntry& |
| 238 | RibEntry::setName(const Name& prefix) |
| 239 | { |
| 240 | m_prefix = prefix; |
| 241 | m_wire.reset(); |
| 242 | return *this; |
| 243 | } |
| 244 | |
| 245 | RibEntry& |
| 246 | RibEntry::addRoute(const Route& route) |
| 247 | { |
| 248 | m_routes.push_back(route); |
| 249 | m_wire.reset(); |
| 250 | return *this; |
| 251 | } |
| 252 | |
| 253 | RibEntry& |
| 254 | RibEntry::clearRoutes() |
| 255 | { |
| 256 | m_routes.clear(); |
| 257 | m_wire.reset(); |
| 258 | return *this; |
| 259 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 260 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 261 | template<encoding::Tag TAG> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 262 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 263 | RibEntry::wireEncode(EncodingImpl<TAG>& block) const |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 264 | { |
| 265 | size_t totalLength = 0; |
| 266 | |
| 267 | for (std::list<Route>::const_reverse_iterator it = m_routes.rbegin(); |
| 268 | it != m_routes.rend(); ++it) |
| 269 | { |
| 270 | totalLength += it->wireEncode(block); |
| 271 | } |
| 272 | |
| 273 | totalLength += m_prefix.wireEncode(block); |
| 274 | |
| 275 | totalLength += block.prependVarNumber(totalLength); |
| 276 | totalLength += block.prependVarNumber(tlv::nfd::RibEntry); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 277 | return totalLength; |
| 278 | } |
| 279 | |
| 280 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 281 | RibEntry::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 282 | |
| 283 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 284 | RibEntry::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 285 | |
| 286 | const Block& |
| 287 | RibEntry::wireEncode() const |
| 288 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame^] | 289 | if (m_wire.hasWire()) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 290 | return m_wire; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 291 | |
| 292 | EncodingEstimator estimator; |
| 293 | size_t estimatedSize = wireEncode(estimator); |
| 294 | |
| 295 | EncodingBuffer buffer(estimatedSize, 0); |
| 296 | wireEncode(buffer); |
| 297 | |
| 298 | m_wire = buffer.block(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 299 | return m_wire; |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | RibEntry::wireDecode(const Block& wire) |
| 304 | { |
| 305 | m_prefix.clear(); |
| 306 | m_routes.clear(); |
| 307 | |
| 308 | m_wire = wire; |
| 309 | |
| 310 | if (m_wire.type() != tlv::nfd::RibEntry) { |
| 311 | std::stringstream error; |
| 312 | error << "Expected RibEntry Block, but Block is of a different type: #" |
| 313 | << m_wire.type(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 314 | BOOST_THROW_EXCEPTION(Error(error.str())); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | m_wire.parse(); |
| 318 | |
| 319 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 320 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 321 | if (val != m_wire.elements_end() && val->type() == tlv::Name) { |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 322 | m_prefix.wireDecode(*val); |
| 323 | ++val; |
| 324 | } |
| 325 | else { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 326 | BOOST_THROW_EXCEPTION(Error("Missing required Name field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | for (; val != m_wire.elements_end(); ++val) { |
| 330 | |
| 331 | if (val->type() == tlv::nfd::Route) { |
| 332 | m_routes.push_back(Route(*val)); |
| 333 | } |
| 334 | else { |
| 335 | std::stringstream error; |
| 336 | error << "Expected Route Block, but Block is of a different type: #" |
| 337 | << m_wire.type(); |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 338 | BOOST_THROW_EXCEPTION(Error(error.str())); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | std::ostream& |
| 344 | operator<<(std::ostream& os, const RibEntry& entry) |
| 345 | { |
| 346 | os << "RibEntry{\n" |
| 347 | << " Name: " << entry.getName() << "\n"; |
| 348 | |
| 349 | for (RibEntry::iterator it = entry.begin(); it != entry.end(); ++it) { |
| 350 | os << " " << *it << "\n"; |
| 351 | } |
| 352 | |
| 353 | os << "}"; |
| 354 | |
| 355 | return os; |
| 356 | } |
| 357 | |
| 358 | } // namespace nfd |
| 359 | } // namespace ndn |