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/block-helpers.hpp" |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 24 | #include "encoding/encoding-buffer.hpp" |
| 25 | #include "encoding/tlv-nfd.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 26 | #include "util/concepts.hpp" |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 27 | #include "util/string-helper.hpp" |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 28 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 29 | #include <boost/range/adaptor/reversed.hpp> |
| 30 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 31 | namespace ndn { |
| 32 | namespace nfd { |
| 33 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 34 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<Route>)); |
| 35 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<RibEntry>)); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 36 | |
| 37 | Route::Route() |
Davide Pesavento | f8503d2 | 2017-02-17 01:19:10 -0500 | [diff] [blame] | 38 | : m_faceId(INVALID_FACE_ID) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 39 | , m_origin(0) |
| 40 | , m_cost(0) |
| 41 | , m_flags(ROUTE_FLAG_CHILD_INHERIT) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | Route::Route(const Block& block) |
| 46 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 47 | this->wireDecode(block); |
| 48 | } |
| 49 | |
| 50 | Route& |
| 51 | Route::setFaceId(uint64_t faceId) |
| 52 | { |
| 53 | m_faceId = faceId; |
| 54 | m_wire.reset(); |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | Route& |
| 59 | Route::setOrigin(uint64_t origin) |
| 60 | { |
| 61 | m_origin = origin; |
| 62 | m_wire.reset(); |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | Route& |
| 67 | Route::setCost(uint64_t cost) |
| 68 | { |
| 69 | m_cost = cost; |
| 70 | m_wire.reset(); |
| 71 | return *this; |
| 72 | } |
| 73 | |
| 74 | Route& |
| 75 | Route::setFlags(uint64_t flags) |
| 76 | { |
| 77 | m_flags = flags; |
| 78 | m_wire.reset(); |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | Route& |
| 83 | Route::setExpirationPeriod(time::milliseconds expirationPeriod) |
| 84 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 85 | if (expirationPeriod == time::milliseconds::max()) |
| 86 | return unsetExpirationPeriod(); |
| 87 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 88 | m_expirationPeriod = expirationPeriod; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 89 | m_wire.reset(); |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | Route& |
| 94 | Route::unsetExpirationPeriod() |
| 95 | { |
| 96 | m_expirationPeriod = nullopt; |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 97 | m_wire.reset(); |
| 98 | return *this; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 101 | template<encoding::Tag TAG> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 102 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 103 | Route::wireEncode(EncodingImpl<TAG>& block) const |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 104 | { |
| 105 | size_t totalLength = 0; |
| 106 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 107 | if (m_expirationPeriod) { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 108 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::ExpirationPeriod, |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 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 |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 144 | Route::wireDecode(const Block& block) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 145 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 146 | if (block.type() != tlv::nfd::Route) { |
| 147 | BOOST_THROW_EXCEPTION(Error("expecting Route, but Block has type " + to_string(block.type()))); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 148 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 149 | m_wire = block; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 150 | m_wire.parse(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 151 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 152 | |
| 153 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 154 | m_faceId = readNonNegativeInteger(*val); |
| 155 | ++val; |
| 156 | } |
| 157 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 158 | BOOST_THROW_EXCEPTION(Error("missing required FaceId field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) { |
| 162 | m_origin = readNonNegativeInteger(*val); |
| 163 | ++val; |
| 164 | } |
| 165 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 166 | BOOST_THROW_EXCEPTION(Error("missing required Origin field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Cost) { |
| 170 | m_cost = readNonNegativeInteger(*val); |
| 171 | ++val; |
| 172 | } |
| 173 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 174 | BOOST_THROW_EXCEPTION(Error("missing required Cost field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Flags) { |
| 178 | m_flags = readNonNegativeInteger(*val); |
| 179 | ++val; |
| 180 | } |
| 181 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 182 | BOOST_THROW_EXCEPTION(Error("missing required Flags field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::ExpirationPeriod) { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 186 | m_expirationPeriod.emplace(readNonNegativeInteger(*val)); |
| 187 | ++val; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 188 | } |
| 189 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 190 | m_expirationPeriod = nullopt; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 194 | bool |
| 195 | operator==(const Route& a, const Route& b) |
| 196 | { |
| 197 | return a.getFaceId() == b.getFaceId() && |
| 198 | a.getOrigin() == b.getOrigin() && |
| 199 | a.getCost() == b.getCost() && |
| 200 | a.getFlags() == b.getFlags() && |
| 201 | a.getExpirationPeriod() == b.getExpirationPeriod(); |
| 202 | } |
| 203 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 204 | std::ostream& |
| 205 | operator<<(std::ostream& os, const Route& route) |
| 206 | { |
| 207 | os << "Route(" |
| 208 | << "FaceId: " << route.getFaceId() << ", " |
| 209 | << "Origin: " << route.getOrigin() << ", " |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 210 | << "Cost: " << route.getCost() << ", " |
| 211 | << "Flags: " << AsHex{route.getFlags()} << ", "; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 212 | |
| 213 | if (route.hasExpirationPeriod()) { |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 214 | os << "ExpirationPeriod: " << route.getExpirationPeriod(); |
| 215 | } |
| 216 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 217 | os << "ExpirationPeriod: infinite"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 220 | return os << ")"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 223 | //////////////////// |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 224 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 225 | RibEntry::RibEntry() = default; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 226 | |
| 227 | RibEntry::RibEntry(const Block& block) |
| 228 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 229 | this->wireDecode(block); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 232 | RibEntry& |
| 233 | RibEntry::setName(const Name& prefix) |
| 234 | { |
| 235 | m_prefix = prefix; |
| 236 | m_wire.reset(); |
| 237 | return *this; |
| 238 | } |
| 239 | |
| 240 | RibEntry& |
| 241 | RibEntry::addRoute(const Route& route) |
| 242 | { |
| 243 | m_routes.push_back(route); |
| 244 | m_wire.reset(); |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | RibEntry& |
| 249 | RibEntry::clearRoutes() |
| 250 | { |
| 251 | m_routes.clear(); |
| 252 | m_wire.reset(); |
| 253 | return *this; |
| 254 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 255 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 256 | template<encoding::Tag TAG> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 257 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 258 | RibEntry::wireEncode(EncodingImpl<TAG>& block) const |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 259 | { |
| 260 | size_t totalLength = 0; |
| 261 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 262 | for (const auto& route : m_routes | boost::adaptors::reversed) { |
| 263 | totalLength += route.wireEncode(block); |
| 264 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 265 | totalLength += m_prefix.wireEncode(block); |
| 266 | |
| 267 | totalLength += block.prependVarNumber(totalLength); |
| 268 | totalLength += block.prependVarNumber(tlv::nfd::RibEntry); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 269 | return totalLength; |
| 270 | } |
| 271 | |
| 272 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 273 | RibEntry::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 274 | |
| 275 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 276 | RibEntry::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 277 | |
| 278 | const Block& |
| 279 | RibEntry::wireEncode() const |
| 280 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 281 | if (m_wire.hasWire()) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 282 | return m_wire; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 283 | |
| 284 | EncodingEstimator estimator; |
| 285 | size_t estimatedSize = wireEncode(estimator); |
| 286 | |
| 287 | EncodingBuffer buffer(estimatedSize, 0); |
| 288 | wireEncode(buffer); |
| 289 | |
| 290 | m_wire = buffer.block(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 291 | return m_wire; |
| 292 | } |
| 293 | |
| 294 | void |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 295 | RibEntry::wireDecode(const Block& block) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 296 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 297 | if (block.type() != tlv::nfd::RibEntry) { |
| 298 | BOOST_THROW_EXCEPTION(Error("expecting RibEntry, but Block has type " + to_string(block.type()))); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 299 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 300 | m_wire = block; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 301 | m_wire.parse(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 302 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 303 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 304 | if (val == m_wire.elements_end()) { |
| 305 | BOOST_THROW_EXCEPTION(Error("unexpected end of RibEntry")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 306 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 307 | else if (val->type() != tlv::Name) { |
| 308 | BOOST_THROW_EXCEPTION(Error("expecting Name, but Block has type " + to_string(val->type()))); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 309 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 310 | m_prefix.wireDecode(*val); |
| 311 | ++val; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 312 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 313 | m_routes.clear(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 314 | for (; val != m_wire.elements_end(); ++val) { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 315 | if (val->type() != tlv::nfd::Route) { |
| 316 | BOOST_THROW_EXCEPTION(Error("expecting Route, but Block has type " + to_string(val->type()))); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 317 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 318 | m_routes.emplace_back(*val); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 322 | bool |
| 323 | operator==(const RibEntry& a, const RibEntry& b) |
| 324 | { |
| 325 | const auto& aRoutes = a.getRoutes(); |
| 326 | const auto& bRoutes = b.getRoutes(); |
| 327 | |
| 328 | if (a.getName() != b.getName() || |
| 329 | aRoutes.size() != bRoutes.size()) |
| 330 | return false; |
| 331 | |
| 332 | std::vector<bool> matched(bRoutes.size(), false); |
| 333 | return std::all_of(aRoutes.begin(), aRoutes.end(), |
| 334 | [&] (const Route& route) { |
| 335 | for (size_t i = 0; i < bRoutes.size(); ++i) { |
| 336 | if (!matched[i] && bRoutes[i] == route) { |
| 337 | matched[i] = true; |
| 338 | return true; |
| 339 | } |
| 340 | } |
| 341 | return false; |
| 342 | }); |
| 343 | } |
| 344 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 345 | std::ostream& |
| 346 | operator<<(std::ostream& os, const RibEntry& entry) |
| 347 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 348 | os << "RibEntry(Prefix: " << entry.getName() << ",\n" |
| 349 | << " Routes: ["; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 350 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 351 | bool isFirst = true; |
| 352 | for (const auto& route : entry.getRoutes()) { |
| 353 | if (!isFirst) |
| 354 | os << ",\n "; |
| 355 | isFirst = false; |
| 356 | os << route; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 357 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 358 | os << "]\n"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 359 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 360 | return os << " )"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | } // namespace nfd |
| 364 | } // namespace ndn |