Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 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) |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 39 | , m_origin(ROUTE_ORIGIN_APP) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 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& |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 59 | Route::setOrigin(RouteOrigin origin) |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 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 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame^] | 121 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Route); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 122 | |
| 123 | const Block& |
| 124 | Route::wireEncode() const |
| 125 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 126 | if (m_wire.hasWire()) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 127 | return m_wire; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 128 | |
| 129 | EncodingEstimator estimator; |
| 130 | size_t estimatedSize = wireEncode(estimator); |
| 131 | |
| 132 | EncodingBuffer buffer(estimatedSize, 0); |
| 133 | wireEncode(buffer); |
| 134 | |
| 135 | m_wire = buffer.block(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 136 | return m_wire; |
| 137 | } |
| 138 | |
| 139 | void |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 140 | Route::wireDecode(const Block& block) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 141 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 142 | if (block.type() != tlv::nfd::Route) { |
| 143 | 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] | 144 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 145 | m_wire = block; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 146 | m_wire.parse(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 147 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 148 | |
| 149 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::FaceId) { |
| 150 | m_faceId = readNonNegativeInteger(*val); |
| 151 | ++val; |
| 152 | } |
| 153 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 154 | BOOST_THROW_EXCEPTION(Error("missing required FaceId field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Origin) { |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 158 | m_origin = readNonNegativeIntegerAs<RouteOrigin>(*val); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 159 | ++val; |
| 160 | } |
| 161 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 162 | BOOST_THROW_EXCEPTION(Error("missing required Origin field")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | if (val != m_wire.elements_end() && val->type() == tlv::nfd::Cost) { |
| 166 | m_cost = readNonNegativeInteger(*val); |
| 167 | ++val; |
| 168 | } |
| 169 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 170 | BOOST_THROW_EXCEPTION(Error("missing required Cost 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::Flags) { |
| 174 | m_flags = readNonNegativeInteger(*val); |
| 175 | ++val; |
| 176 | } |
| 177 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 178 | BOOST_THROW_EXCEPTION(Error("missing required Flags 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::ExpirationPeriod) { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 182 | m_expirationPeriod.emplace(readNonNegativeInteger(*val)); |
| 183 | ++val; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 184 | } |
| 185 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 186 | m_expirationPeriod = nullopt; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 190 | bool |
| 191 | operator==(const Route& a, const Route& b) |
| 192 | { |
| 193 | return a.getFaceId() == b.getFaceId() && |
| 194 | a.getOrigin() == b.getOrigin() && |
| 195 | a.getCost() == b.getCost() && |
| 196 | a.getFlags() == b.getFlags() && |
| 197 | a.getExpirationPeriod() == b.getExpirationPeriod(); |
| 198 | } |
| 199 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 200 | std::ostream& |
| 201 | operator<<(std::ostream& os, const Route& route) |
| 202 | { |
| 203 | os << "Route(" |
| 204 | << "FaceId: " << route.getFaceId() << ", " |
| 205 | << "Origin: " << route.getOrigin() << ", " |
Davide Pesavento | e78eeca | 2017-02-23 23:22:32 -0500 | [diff] [blame] | 206 | << "Cost: " << route.getCost() << ", " |
| 207 | << "Flags: " << AsHex{route.getFlags()} << ", "; |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 208 | |
| 209 | if (route.hasExpirationPeriod()) { |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 210 | os << "ExpirationPeriod: " << route.getExpirationPeriod(); |
| 211 | } |
| 212 | else { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 213 | os << "ExpirationPeriod: infinite"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 216 | return os << ")"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 217 | } |
| 218 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 219 | //////////////////// |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 220 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 221 | RibEntry::RibEntry() = default; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 222 | |
| 223 | RibEntry::RibEntry(const Block& block) |
| 224 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 225 | this->wireDecode(block); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 228 | RibEntry& |
| 229 | RibEntry::setName(const Name& prefix) |
| 230 | { |
| 231 | m_prefix = prefix; |
| 232 | m_wire.reset(); |
| 233 | return *this; |
| 234 | } |
| 235 | |
| 236 | RibEntry& |
| 237 | RibEntry::addRoute(const Route& route) |
| 238 | { |
| 239 | m_routes.push_back(route); |
| 240 | m_wire.reset(); |
| 241 | return *this; |
| 242 | } |
| 243 | |
| 244 | RibEntry& |
| 245 | RibEntry::clearRoutes() |
| 246 | { |
| 247 | m_routes.clear(); |
| 248 | m_wire.reset(); |
| 249 | return *this; |
| 250 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 251 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 252 | template<encoding::Tag TAG> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 253 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 254 | RibEntry::wireEncode(EncodingImpl<TAG>& block) const |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 255 | { |
| 256 | size_t totalLength = 0; |
| 257 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 258 | for (const auto& route : m_routes | boost::adaptors::reversed) { |
| 259 | totalLength += route.wireEncode(block); |
| 260 | } |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 261 | totalLength += m_prefix.wireEncode(block); |
| 262 | |
| 263 | totalLength += block.prependVarNumber(totalLength); |
| 264 | totalLength += block.prependVarNumber(tlv::nfd::RibEntry); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 265 | return totalLength; |
| 266 | } |
| 267 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame^] | 268 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(RibEntry); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 269 | |
| 270 | const Block& |
| 271 | RibEntry::wireEncode() const |
| 272 | { |
Davide Pesavento | 6ad3d53 | 2017-02-17 01:43:57 -0500 | [diff] [blame] | 273 | if (m_wire.hasWire()) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 274 | return m_wire; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 275 | |
| 276 | EncodingEstimator estimator; |
| 277 | size_t estimatedSize = wireEncode(estimator); |
| 278 | |
| 279 | EncodingBuffer buffer(estimatedSize, 0); |
| 280 | wireEncode(buffer); |
| 281 | |
| 282 | m_wire = buffer.block(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 283 | return m_wire; |
| 284 | } |
| 285 | |
| 286 | void |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 287 | RibEntry::wireDecode(const Block& block) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 288 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 289 | if (block.type() != tlv::nfd::RibEntry) { |
| 290 | 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] | 291 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 292 | m_wire = block; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 293 | m_wire.parse(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 294 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 295 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 296 | if (val == m_wire.elements_end()) { |
| 297 | BOOST_THROW_EXCEPTION(Error("unexpected end of RibEntry")); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 298 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 299 | else if (val->type() != tlv::Name) { |
| 300 | 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] | 301 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 302 | m_prefix.wireDecode(*val); |
| 303 | ++val; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 304 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 305 | m_routes.clear(); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 306 | for (; val != m_wire.elements_end(); ++val) { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 307 | if (val->type() != tlv::nfd::Route) { |
| 308 | 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] | 309 | } |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 310 | m_routes.emplace_back(*val); |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 314 | bool |
| 315 | operator==(const RibEntry& a, const RibEntry& b) |
| 316 | { |
| 317 | const auto& aRoutes = a.getRoutes(); |
| 318 | const auto& bRoutes = b.getRoutes(); |
| 319 | |
| 320 | if (a.getName() != b.getName() || |
| 321 | aRoutes.size() != bRoutes.size()) |
| 322 | return false; |
| 323 | |
| 324 | std::vector<bool> matched(bRoutes.size(), false); |
| 325 | return std::all_of(aRoutes.begin(), aRoutes.end(), |
| 326 | [&] (const Route& route) { |
| 327 | for (size_t i = 0; i < bRoutes.size(); ++i) { |
| 328 | if (!matched[i] && bRoutes[i] == route) { |
| 329 | matched[i] = true; |
| 330 | return true; |
| 331 | } |
| 332 | } |
| 333 | return false; |
| 334 | }); |
| 335 | } |
| 336 | |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 337 | std::ostream& |
| 338 | operator<<(std::ostream& os, const RibEntry& entry) |
| 339 | { |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 340 | os << "RibEntry(Prefix: " << entry.getName() << ",\n" |
| 341 | << " Routes: ["; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 342 | |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 343 | std::copy(entry.getRoutes().begin(), entry.getRoutes().end(), |
| 344 | make_ostream_joiner(os, ",\n ")); |
| 345 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 346 | os << "]\n"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 347 | |
Davide Pesavento | 484bbe5 | 2017-02-15 00:03:46 -0500 | [diff] [blame] | 348 | return os << " )"; |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | } // namespace nfd |
| 352 | } // namespace ndn |