Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 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 "fib-entry.hpp" |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 23 | #include "encoding/block-helpers.hpp" |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -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" |
| 27 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 28 | #include <boost/range/adaptor/reversed.hpp> |
| 29 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace nfd { |
| 32 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 33 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<NextHopRecord>)); |
| 34 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<FibEntry>)); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 35 | |
| 36 | NextHopRecord::NextHopRecord() |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 37 | : m_faceId(0) // INVALID_FACEID |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 38 | , m_cost(0) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | NextHopRecord::NextHopRecord(const Block& block) |
| 43 | { |
| 44 | this->wireDecode(block); |
| 45 | } |
| 46 | |
| 47 | NextHopRecord& |
| 48 | NextHopRecord::setFaceId(uint64_t faceId) |
| 49 | { |
| 50 | m_faceId = faceId; |
| 51 | m_wire.reset(); |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | NextHopRecord& |
| 56 | NextHopRecord::setCost(uint64_t cost) |
| 57 | { |
| 58 | m_cost = cost; |
| 59 | m_wire.reset(); |
| 60 | return *this; |
| 61 | } |
| 62 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 63 | template<encoding::Tag TAG> |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 64 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 65 | NextHopRecord::wireEncode(EncodingImpl<TAG>& block) const |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 66 | { |
| 67 | size_t totalLength = 0; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 68 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 69 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::Cost, m_cost); |
| 70 | totalLength += prependNonNegativeIntegerBlock(block, ndn::tlv::nfd::FaceId, m_faceId); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 71 | |
| 72 | totalLength += block.prependVarNumber(totalLength); |
| 73 | totalLength += block.prependVarNumber(ndn::tlv::nfd::NextHopRecord); |
| 74 | return totalLength; |
| 75 | } |
| 76 | |
| 77 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 78 | NextHopRecord::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 79 | |
| 80 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 81 | NextHopRecord::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 82 | |
| 83 | const Block& |
| 84 | NextHopRecord::wireEncode() const |
| 85 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 86 | if (m_wire.hasWire()) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 87 | return m_wire; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 88 | |
| 89 | EncodingEstimator estimator; |
| 90 | size_t estimatedSize = wireEncode(estimator); |
| 91 | |
| 92 | EncodingBuffer buffer(estimatedSize, 0); |
| 93 | wireEncode(buffer); |
| 94 | |
| 95 | m_wire = buffer.block(); |
| 96 | return m_wire; |
| 97 | } |
| 98 | |
| 99 | void |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 100 | NextHopRecord::wireDecode(const Block& block) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 101 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 102 | if (block.type() != tlv::nfd::NextHopRecord) { |
| 103 | BOOST_THROW_EXCEPTION(Error("expecting NextHopRecord, but Block has type " + to_string(block.type()))); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 104 | } |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 105 | m_wire = block; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 106 | m_wire.parse(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 107 | Block::element_const_iterator val = m_wire.elements_begin(); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 108 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 109 | if (val == m_wire.elements_end()) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 110 | BOOST_THROW_EXCEPTION(Error("unexpected end of NextHopRecord")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 111 | } |
| 112 | else if (val->type() != tlv::nfd::FaceId) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 113 | BOOST_THROW_EXCEPTION(Error("expecting FaceId, but Block has type " + to_string(val->type()))); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 114 | } |
| 115 | m_faceId = readNonNegativeInteger(*val); |
| 116 | ++val; |
| 117 | |
| 118 | if (val == m_wire.elements_end()) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 119 | BOOST_THROW_EXCEPTION(Error("unexpected end of NextHopRecord")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 120 | } |
| 121 | else if (val->type() != tlv::nfd::Cost) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 122 | BOOST_THROW_EXCEPTION(Error("expecting Cost, but Block has type " + to_string(val->type()))); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 123 | } |
| 124 | m_cost = readNonNegativeInteger(*val); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 125 | ++val; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 128 | bool |
| 129 | operator==(const NextHopRecord& a, const NextHopRecord& b) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 130 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 131 | return a.getFaceId() == b.getFaceId() && |
| 132 | a.getCost() == b.getCost(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 135 | std::ostream& |
| 136 | operator<<(std::ostream& os, const NextHopRecord& nh) |
| 137 | { |
| 138 | return os << "NextHopRecord(" |
| 139 | << "FaceId: " << nh.getFaceId() << ", " |
| 140 | << "Cost: " << nh.getCost() |
| 141 | << ")"; |
| 142 | } |
| 143 | |
| 144 | //////////////////// |
| 145 | |
| 146 | FibEntry::FibEntry() = default; |
| 147 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 148 | FibEntry::FibEntry(const Block& block) |
| 149 | { |
| 150 | this->wireDecode(block); |
| 151 | } |
| 152 | |
| 153 | FibEntry& |
| 154 | FibEntry::setPrefix(const Name& prefix) |
| 155 | { |
| 156 | m_prefix = prefix; |
| 157 | m_wire.reset(); |
| 158 | return *this; |
| 159 | } |
| 160 | |
| 161 | FibEntry& |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 162 | FibEntry::addNextHopRecord(const NextHopRecord& nh) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 163 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 164 | m_nextHopRecords.push_back(nh); |
| 165 | m_wire.reset(); |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | FibEntry& |
| 170 | FibEntry::clearNextHopRecords() |
| 171 | { |
| 172 | m_nextHopRecords.clear(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 173 | m_wire.reset(); |
| 174 | return *this; |
| 175 | } |
| 176 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 177 | template<encoding::Tag TAG> |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 178 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 179 | FibEntry::wireEncode(EncodingImpl<TAG>& block) const |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 180 | { |
| 181 | size_t totalLength = 0; |
| 182 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 183 | for (const auto& nh : m_nextHopRecords | boost::adaptors::reversed) { |
| 184 | totalLength += nh.wireEncode(block); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 185 | } |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 186 | totalLength += m_prefix.wireEncode(block); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 187 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 188 | totalLength += block.prependVarNumber(totalLength); |
| 189 | totalLength += block.prependVarNumber(tlv::nfd::FibEntry); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 190 | return totalLength; |
| 191 | } |
| 192 | |
| 193 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 194 | FibEntry::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& block) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 195 | |
| 196 | template size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 197 | FibEntry::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& block) const; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 198 | |
| 199 | const Block& |
| 200 | FibEntry::wireEncode() const |
| 201 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 202 | if (m_wire.hasWire()) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 203 | return m_wire; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 204 | |
| 205 | EncodingEstimator estimator; |
| 206 | size_t estimatedSize = wireEncode(estimator); |
| 207 | |
| 208 | EncodingBuffer buffer(estimatedSize, 0); |
| 209 | wireEncode(buffer); |
| 210 | |
| 211 | m_wire = buffer.block(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 212 | return m_wire; |
| 213 | } |
| 214 | |
| 215 | void |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 216 | FibEntry::wireDecode(const Block& block) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 217 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 218 | if (block.type() != tlv::nfd::FibEntry) { |
| 219 | BOOST_THROW_EXCEPTION(Error("expecting FibEntry, but Block has type " + to_string(block.type()))); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 220 | } |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 221 | m_wire = block; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 222 | m_wire.parse(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 223 | Block::element_const_iterator val = m_wire.elements_begin(); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 224 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 225 | if (val == m_wire.elements_end()) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 226 | BOOST_THROW_EXCEPTION(Error("unexpected end of FibEntry")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 227 | } |
| 228 | else if (val->type() != tlv::Name) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 229 | BOOST_THROW_EXCEPTION(Error("expecting Name, but Block has type " + to_string(val->type()))); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 230 | } |
| 231 | m_prefix.wireDecode(*val); |
| 232 | ++val; |
| 233 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 234 | m_nextHopRecords.clear(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 235 | for (; val != m_wire.elements_end(); ++val) { |
| 236 | if (val->type() != tlv::nfd::NextHopRecord) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 237 | BOOST_THROW_EXCEPTION(Error("expecting NextHopRecord, but Block has type " + to_string(val->type()))); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 238 | } |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 239 | m_nextHopRecords.emplace_back(*val); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame^] | 243 | bool |
| 244 | operator==(const FibEntry& a, const FibEntry& b) |
| 245 | { |
| 246 | const auto& aNextHops = a.getNextHopRecords(); |
| 247 | const auto& bNextHops = b.getNextHopRecords(); |
| 248 | |
| 249 | if (a.getPrefix() != b.getPrefix() || |
| 250 | aNextHops.size() != bNextHops.size()) |
| 251 | return false; |
| 252 | |
| 253 | std::vector<bool> matched(bNextHops.size(), false); |
| 254 | return std::all_of(aNextHops.begin(), aNextHops.end(), |
| 255 | [&] (const NextHopRecord& nh) { |
| 256 | for (size_t i = 0; i < bNextHops.size(); ++i) { |
| 257 | if (!matched[i] && bNextHops[i] == nh) { |
| 258 | matched[i] = true; |
| 259 | return true; |
| 260 | } |
| 261 | } |
| 262 | return false; |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | std::ostream& |
| 267 | operator<<(std::ostream& os, const FibEntry& entry) |
| 268 | { |
| 269 | os << "FibEntry(Prefix: " << entry.getPrefix() << ",\n" |
| 270 | << " NextHops: ["; |
| 271 | |
| 272 | bool first = true; |
| 273 | for (const auto& nh : entry.getNextHopRecords()) { |
| 274 | if (!first) |
| 275 | os << ",\n "; |
| 276 | first = false; |
| 277 | os << nh; |
| 278 | } |
| 279 | os << "]\n"; |
| 280 | |
| 281 | return os << " )"; |
| 282 | } |
| 283 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 284 | } // namespace nfd |
| 285 | } // namespace ndn |