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