Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame^] | 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 | f8503d2 | 2017-02-17 01:19:10 -0500 | [diff] [blame] | 37 | : m_faceId(INVALID_FACE_ID) |
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 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame^] | 77 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(NextHopRecord); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 78 | |
| 79 | const Block& |
| 80 | NextHopRecord::wireEncode() const |
| 81 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 82 | if (m_wire.hasWire()) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 83 | return m_wire; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 84 | |
| 85 | EncodingEstimator estimator; |
| 86 | size_t estimatedSize = wireEncode(estimator); |
| 87 | |
| 88 | EncodingBuffer buffer(estimatedSize, 0); |
| 89 | wireEncode(buffer); |
| 90 | |
| 91 | m_wire = buffer.block(); |
| 92 | return m_wire; |
| 93 | } |
| 94 | |
| 95 | void |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 96 | NextHopRecord::wireDecode(const Block& block) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 97 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 98 | if (block.type() != tlv::nfd::NextHopRecord) { |
| 99 | 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] | 100 | } |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 101 | m_wire = block; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 102 | m_wire.parse(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 103 | Block::element_const_iterator val = m_wire.elements_begin(); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 104 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 105 | if (val == m_wire.elements_end()) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 106 | BOOST_THROW_EXCEPTION(Error("unexpected end of NextHopRecord")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 107 | } |
| 108 | else if (val->type() != tlv::nfd::FaceId) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 109 | 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] | 110 | } |
| 111 | m_faceId = readNonNegativeInteger(*val); |
| 112 | ++val; |
| 113 | |
| 114 | if (val == m_wire.elements_end()) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 115 | BOOST_THROW_EXCEPTION(Error("unexpected end of NextHopRecord")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 116 | } |
| 117 | else if (val->type() != tlv::nfd::Cost) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 118 | 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] | 119 | } |
| 120 | m_cost = readNonNegativeInteger(*val); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 121 | ++val; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 124 | bool |
| 125 | operator==(const NextHopRecord& a, const NextHopRecord& b) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 126 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 127 | return a.getFaceId() == b.getFaceId() && |
| 128 | a.getCost() == b.getCost(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 131 | std::ostream& |
| 132 | operator<<(std::ostream& os, const NextHopRecord& nh) |
| 133 | { |
| 134 | return os << "NextHopRecord(" |
| 135 | << "FaceId: " << nh.getFaceId() << ", " |
| 136 | << "Cost: " << nh.getCost() |
| 137 | << ")"; |
| 138 | } |
| 139 | |
| 140 | //////////////////// |
| 141 | |
| 142 | FibEntry::FibEntry() = default; |
| 143 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 144 | FibEntry::FibEntry(const Block& block) |
| 145 | { |
| 146 | this->wireDecode(block); |
| 147 | } |
| 148 | |
| 149 | FibEntry& |
| 150 | FibEntry::setPrefix(const Name& prefix) |
| 151 | { |
| 152 | m_prefix = prefix; |
| 153 | m_wire.reset(); |
| 154 | return *this; |
| 155 | } |
| 156 | |
| 157 | FibEntry& |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 158 | FibEntry::addNextHopRecord(const NextHopRecord& nh) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 159 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 160 | m_nextHopRecords.push_back(nh); |
| 161 | m_wire.reset(); |
| 162 | return *this; |
| 163 | } |
| 164 | |
| 165 | FibEntry& |
| 166 | FibEntry::clearNextHopRecords() |
| 167 | { |
| 168 | m_nextHopRecords.clear(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 169 | m_wire.reset(); |
| 170 | return *this; |
| 171 | } |
| 172 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 173 | template<encoding::Tag TAG> |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 174 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 175 | FibEntry::wireEncode(EncodingImpl<TAG>& block) const |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 176 | { |
| 177 | size_t totalLength = 0; |
| 178 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 179 | for (const auto& nh : m_nextHopRecords | boost::adaptors::reversed) { |
| 180 | totalLength += nh.wireEncode(block); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 181 | } |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 182 | totalLength += m_prefix.wireEncode(block); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 183 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 184 | totalLength += block.prependVarNumber(totalLength); |
| 185 | totalLength += block.prependVarNumber(tlv::nfd::FibEntry); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 186 | return totalLength; |
| 187 | } |
| 188 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame^] | 189 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(FibEntry); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 190 | |
| 191 | const Block& |
| 192 | FibEntry::wireEncode() const |
| 193 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 194 | if (m_wire.hasWire()) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 195 | return m_wire; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 196 | |
| 197 | EncodingEstimator estimator; |
| 198 | size_t estimatedSize = wireEncode(estimator); |
| 199 | |
| 200 | EncodingBuffer buffer(estimatedSize, 0); |
| 201 | wireEncode(buffer); |
| 202 | |
| 203 | m_wire = buffer.block(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 204 | return m_wire; |
| 205 | } |
| 206 | |
| 207 | void |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 208 | FibEntry::wireDecode(const Block& block) |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 209 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 210 | if (block.type() != tlv::nfd::FibEntry) { |
| 211 | 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] | 212 | } |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 213 | m_wire = block; |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 214 | m_wire.parse(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 215 | Block::element_const_iterator val = m_wire.elements_begin(); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 216 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 217 | if (val == m_wire.elements_end()) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 218 | BOOST_THROW_EXCEPTION(Error("unexpected end of FibEntry")); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 219 | } |
| 220 | else if (val->type() != tlv::Name) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 221 | 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] | 222 | } |
| 223 | m_prefix.wireDecode(*val); |
| 224 | ++val; |
| 225 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 226 | m_nextHopRecords.clear(); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 227 | for (; val != m_wire.elements_end(); ++val) { |
| 228 | if (val->type() != tlv::nfd::NextHopRecord) { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 229 | 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] | 230 | } |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 231 | m_nextHopRecords.emplace_back(*val); |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 235 | bool |
| 236 | operator==(const FibEntry& a, const FibEntry& b) |
| 237 | { |
| 238 | const auto& aNextHops = a.getNextHopRecords(); |
| 239 | const auto& bNextHops = b.getNextHopRecords(); |
| 240 | |
| 241 | if (a.getPrefix() != b.getPrefix() || |
| 242 | aNextHops.size() != bNextHops.size()) |
| 243 | return false; |
| 244 | |
| 245 | std::vector<bool> matched(bNextHops.size(), false); |
| 246 | return std::all_of(aNextHops.begin(), aNextHops.end(), |
| 247 | [&] (const NextHopRecord& nh) { |
| 248 | for (size_t i = 0; i < bNextHops.size(); ++i) { |
| 249 | if (!matched[i] && bNextHops[i] == nh) { |
| 250 | matched[i] = true; |
| 251 | return true; |
| 252 | } |
| 253 | } |
| 254 | return false; |
| 255 | }); |
| 256 | } |
| 257 | |
| 258 | std::ostream& |
| 259 | operator<<(std::ostream& os, const FibEntry& entry) |
| 260 | { |
| 261 | os << "FibEntry(Prefix: " << entry.getPrefix() << ",\n" |
| 262 | << " NextHops: ["; |
| 263 | |
Davide Pesavento | cf41576 | 2017-02-25 23:46:47 -0500 | [diff] [blame] | 264 | std::copy(entry.getNextHopRecords().begin(), entry.getNextHopRecords().end(), |
| 265 | make_ostream_joiner(os, ",\n ")); |
| 266 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 267 | os << "]\n"; |
| 268 | |
| 269 | return os << " )"; |
| 270 | } |
| 271 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 272 | } // namespace nfd |
| 273 | } // namespace ndn |