Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP |
| 24 | |
| 25 | #include "../encoding/block.hpp" |
| 26 | #include "../encoding/encoding-buffer.hpp" |
| 27 | #include "../encoding/tlv-nfd.hpp" |
| 28 | #include "../name.hpp" |
| 29 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 30 | #include <list> |
| 31 | |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 32 | namespace ndn { |
| 33 | namespace nfd { |
| 34 | |
| 35 | // NextHopRecord := NEXT-HOP-RECORD TLV-LENGTH |
| 36 | // FaceId |
| 37 | // Cost |
| 38 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 39 | /** |
| 40 | * @ingroup management |
| 41 | */ |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 42 | class NextHopRecord |
| 43 | { |
| 44 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 45 | class Error : public tlv::Error |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 46 | { |
| 47 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 48 | Error(const std::string& what) : tlv::Error(what) {} |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | NextHopRecord() |
| 52 | : m_faceId(std::numeric_limits<uint64_t>::max()) |
| 53 | , m_cost(0) |
| 54 | { |
| 55 | } |
| 56 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 57 | explicit |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 58 | NextHopRecord(const Block& block) |
| 59 | { |
| 60 | wireDecode(block); |
| 61 | } |
| 62 | |
| 63 | uint64_t |
| 64 | getFaceId() const |
| 65 | { |
| 66 | return m_faceId; |
| 67 | } |
| 68 | |
| 69 | NextHopRecord& |
| 70 | setFaceId(uint64_t faceId) |
| 71 | { |
| 72 | m_faceId = faceId; |
| 73 | m_wire.reset(); |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | uint64_t |
| 78 | getCost() const |
| 79 | { |
| 80 | return m_cost; |
| 81 | } |
| 82 | |
| 83 | NextHopRecord& |
| 84 | setCost(uint64_t cost) |
| 85 | { |
| 86 | m_cost = cost; |
| 87 | m_wire.reset(); |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | template<bool T> |
| 92 | size_t |
| 93 | wireEncode(EncodingImpl<T>& block) const |
| 94 | { |
| 95 | size_t totalLength = 0; |
| 96 | totalLength += prependNonNegativeIntegerBlock(block, |
| 97 | ndn::tlv::nfd::Cost, |
| 98 | m_cost); |
| 99 | |
| 100 | totalLength += prependNonNegativeIntegerBlock(block, |
| 101 | ndn::tlv::nfd::FaceId, |
| 102 | m_faceId); |
| 103 | |
| 104 | totalLength += block.prependVarNumber(totalLength); |
| 105 | totalLength += block.prependVarNumber(ndn::tlv::nfd::NextHopRecord); |
| 106 | return totalLength; |
| 107 | } |
| 108 | |
| 109 | const Block& |
| 110 | wireEncode() const |
| 111 | { |
| 112 | if (m_wire.hasWire()) |
| 113 | { |
| 114 | return m_wire; |
| 115 | } |
| 116 | |
| 117 | EncodingEstimator estimator; |
| 118 | size_t estimatedSize = wireEncode(estimator); |
| 119 | |
| 120 | EncodingBuffer buffer(estimatedSize, 0); |
| 121 | wireEncode(buffer); |
| 122 | |
| 123 | m_wire = buffer.block(); |
| 124 | return m_wire; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | wireDecode(const Block& wire) |
| 129 | { |
| 130 | m_faceId = std::numeric_limits<uint64_t>::max(); |
| 131 | m_cost = 0; |
| 132 | |
| 133 | m_wire = wire; |
| 134 | |
| 135 | if (m_wire.type() != tlv::nfd::NextHopRecord) |
| 136 | { |
| 137 | std::stringstream error; |
| 138 | error << "Requested decoding of NextHopRecord, but Block is of a different type: #" |
| 139 | << m_wire.type(); |
| 140 | throw Error(error.str()); |
| 141 | } |
| 142 | m_wire.parse(); |
| 143 | |
| 144 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 145 | if (val == m_wire.elements_end()) |
| 146 | { |
| 147 | throw Error("Unexpected end of NextHopRecord"); |
| 148 | } |
| 149 | else if (val->type() != tlv::nfd::FaceId) |
| 150 | { |
| 151 | std::stringstream error; |
| 152 | error << "Expected FaceId, but Block is of a different type: #" |
| 153 | << val->type(); |
| 154 | throw Error(error.str()); |
| 155 | } |
| 156 | m_faceId = readNonNegativeInteger(*val); |
| 157 | ++val; |
| 158 | |
| 159 | if (val == m_wire.elements_end()) |
| 160 | { |
| 161 | throw Error("Unexpected end of NextHopRecord"); |
| 162 | } |
| 163 | else if (val->type() != tlv::nfd::Cost) |
| 164 | { |
| 165 | std::stringstream error; |
| 166 | error << "Expected Cost, but Block is of a different type: #" |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 167 | << m_wire.type(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 168 | throw Error(error.str()); |
| 169 | } |
| 170 | m_cost = readNonNegativeInteger(*val); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | private: |
| 175 | uint64_t m_faceId; |
| 176 | uint64_t m_cost; |
| 177 | |
| 178 | mutable Block m_wire; |
| 179 | }; |
| 180 | |
| 181 | |
| 182 | // FibEntry := FIB-ENTRY-TYPE TLV-LENGTH |
| 183 | // Name |
| 184 | // NextHopRecord* |
| 185 | |
Alexander Afanasyev | 4671bf7 | 2014-05-19 09:01:37 -0400 | [diff] [blame] | 186 | /** |
| 187 | * @ingroup management |
| 188 | */ |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 189 | class FibEntry |
| 190 | { |
| 191 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 192 | class Error : public tlv::Error |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 193 | { |
| 194 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 195 | Error(const std::string& what) : tlv::Error(what) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 196 | { |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | FibEntry() |
| 201 | { |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 202 | } |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 203 | |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 204 | explicit |
| 205 | FibEntry(const Block& block) |
| 206 | { |
| 207 | wireDecode(block); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | const Name& |
| 211 | getPrefix() const |
| 212 | { |
| 213 | return m_prefix; |
| 214 | } |
| 215 | |
| 216 | FibEntry& |
| 217 | setPrefix(const Name& prefix) |
| 218 | { |
| 219 | m_prefix = prefix; |
| 220 | m_wire.reset(); |
| 221 | return *this; |
| 222 | } |
| 223 | |
| 224 | const std::list<NextHopRecord>& |
| 225 | getNextHopRecords() const |
| 226 | { |
| 227 | return m_nextHopRecords; |
| 228 | } |
| 229 | |
| 230 | FibEntry& |
| 231 | addNextHopRecord(const NextHopRecord& nextHopRecord) |
| 232 | { |
| 233 | m_nextHopRecords.push_back(nextHopRecord); |
| 234 | m_wire.reset(); |
| 235 | return *this; |
| 236 | } |
| 237 | |
| 238 | template<typename T> |
| 239 | FibEntry& |
| 240 | setNextHopRecords(const T& begin, const T& end) |
| 241 | { |
| 242 | m_nextHopRecords.clear(); |
| 243 | m_nextHopRecords.assign(begin, end); |
| 244 | m_wire.reset(); |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | template<bool T> |
| 249 | size_t |
| 250 | wireEncode(EncodingImpl<T>& block) const |
| 251 | { |
| 252 | size_t totalLength = 0; |
| 253 | |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 254 | for (std::list<NextHopRecord>::const_reverse_iterator i = m_nextHopRecords.rbegin(); |
| 255 | i != m_nextHopRecords.rend(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 256 | ++i) |
| 257 | { |
| 258 | totalLength += i->wireEncode(block); |
| 259 | } |
| 260 | |
| 261 | totalLength += m_prefix.wireEncode(block); |
| 262 | totalLength += block.prependVarNumber(totalLength); |
| 263 | totalLength += block.prependVarNumber(tlv::nfd::FibEntry); |
| 264 | |
| 265 | return totalLength; |
| 266 | } |
| 267 | |
| 268 | const Block& |
| 269 | wireEncode() const |
| 270 | { |
| 271 | if (m_wire.hasWire()) |
| 272 | { |
| 273 | return m_wire; |
| 274 | } |
| 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(); |
| 283 | |
| 284 | return m_wire; |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | wireDecode(const Block& wire) |
| 289 | { |
| 290 | m_prefix.clear(); |
| 291 | m_nextHopRecords.clear(); |
| 292 | |
| 293 | m_wire = wire; |
| 294 | |
| 295 | if (m_wire.type() != tlv::nfd::FibEntry) |
| 296 | { |
| 297 | std::stringstream error; |
| 298 | error << "Requested decoding of FibEntry, but Block is of a different type: #" |
| 299 | << m_wire.type(); |
| 300 | throw Error(error.str()); |
| 301 | } |
| 302 | |
| 303 | m_wire.parse(); |
| 304 | |
| 305 | Block::element_const_iterator val = m_wire.elements_begin(); |
| 306 | if (val == m_wire.elements_end()) |
| 307 | { |
| 308 | throw Error("Unexpected end of FibEntry"); |
| 309 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 310 | else if (val->type() != tlv::Name) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 311 | { |
| 312 | std::stringstream error; |
| 313 | error << "Expected Name, but Block is of a different type: #" |
| 314 | << val->type(); |
| 315 | throw Error(error.str()); |
| 316 | } |
| 317 | m_prefix.wireDecode(*val); |
| 318 | ++val; |
| 319 | |
| 320 | for (; val != m_wire.elements_end(); ++val) |
| 321 | { |
| 322 | if (val->type() != tlv::nfd::NextHopRecord) |
| 323 | { |
| 324 | std::stringstream error; |
| 325 | error << "Expected NextHopRecords, but Block is of a different type: #" |
| 326 | << val->type(); |
| 327 | throw Error(error.str()); |
| 328 | } |
Alexander Afanasyev | 44b438a | 2014-03-19 12:51:49 -0700 | [diff] [blame] | 329 | m_nextHopRecords.push_back(NextHopRecord(*val)); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | private: |
| 334 | Name m_prefix; |
| 335 | std::list<NextHopRecord> m_nextHopRecords; |
| 336 | |
| 337 | mutable Block m_wire; |
| 338 | }; |
| 339 | |
| 340 | } // namespace nfd |
| 341 | } // namespace ndn |
| 342 | |
| 343 | #endif // NDN_MANAGEMENT_NFD_FIB_ENTRY_HPP |