Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 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 | |
| 22 | #ifndef NDN_MANAGEMENT_NFD_RIB_ENTRY_HPP |
| 23 | #define NDN_MANAGEMENT_NFD_RIB_ENTRY_HPP |
| 24 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 25 | #include "nfd-rib-flags.hpp" // include this first, to ensure it compiles on its own. |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 26 | #include "../name.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 27 | #include "../util/time.hpp" |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 28 | |
| 29 | #include <list> |
| 30 | |
| 31 | namespace ndn { |
| 32 | namespace nfd { |
| 33 | |
| 34 | /** |
| 35 | * @ingroup management |
| 36 | * |
| 37 | * @brief Data abstraction for Route |
| 38 | * |
| 39 | * A route indicates the availability of content via a certain face and |
| 40 | * provides meta-information about the face. |
| 41 | * |
| 42 | * Route := ROUTE-TYPE TLV-LENGTH |
| 43 | * FaceId |
| 44 | * Origin |
| 45 | * Cost |
| 46 | * Flags |
| 47 | * ExpirationPeriod? |
| 48 | * |
| 49 | * @sa http://redmine.named-data.net/projects/nfd/wiki/RibMgmt |
| 50 | */ |
Chengyu Fan | a14d491 | 2014-08-18 22:30:29 -0500 | [diff] [blame] | 51 | class Route : public RibFlagsTraits<Route> |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 52 | { |
| 53 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 54 | class Error : public tlv::Error |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 55 | { |
| 56 | public: |
| 57 | explicit |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 58 | Error(const std::string& what) : tlv::Error(what) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | Route(); |
| 64 | |
| 65 | explicit |
| 66 | Route(const Block& block); |
| 67 | |
| 68 | uint64_t |
| 69 | getFaceId() const |
| 70 | { |
| 71 | return m_faceId; |
| 72 | } |
| 73 | |
| 74 | Route& |
| 75 | setFaceId(uint64_t faceId) |
| 76 | { |
| 77 | m_faceId = faceId; |
| 78 | m_wire.reset(); |
| 79 | return *this; |
| 80 | } |
| 81 | |
| 82 | uint64_t |
| 83 | getOrigin() const |
| 84 | { |
| 85 | return m_origin; |
| 86 | } |
| 87 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 88 | /** @brief set Origin |
| 89 | * @param origin a code defined in ndn::nfd::RouteOrigin |
| 90 | */ |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 91 | Route& |
| 92 | setOrigin(uint64_t origin) |
| 93 | { |
| 94 | m_origin = origin; |
| 95 | m_wire.reset(); |
| 96 | return *this; |
| 97 | } |
| 98 | |
| 99 | uint64_t |
| 100 | getCost() const |
| 101 | { |
| 102 | return m_cost; |
| 103 | } |
| 104 | |
| 105 | Route& |
| 106 | setCost(uint64_t cost) |
| 107 | { |
| 108 | m_cost = cost; |
| 109 | m_wire.reset(); |
| 110 | return *this; |
| 111 | } |
| 112 | |
| 113 | uint64_t |
| 114 | getFlags() const |
| 115 | { |
| 116 | return m_flags; |
| 117 | } |
| 118 | |
Junxiao Shi | 65f1a71 | 2014-11-20 14:59:36 -0700 | [diff] [blame] | 119 | /** @brief set route inheritance flags |
| 120 | * @param flags a bitwise OR'ed code from ndn::nfd::RouteFlags |
| 121 | */ |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 122 | Route& |
| 123 | setFlags(uint64_t flags) |
| 124 | { |
| 125 | m_flags = flags; |
| 126 | m_wire.reset(); |
| 127 | return *this; |
| 128 | } |
| 129 | |
| 130 | static const time::milliseconds INFINITE_EXPIRATION_PERIOD; |
| 131 | |
| 132 | const time::milliseconds& |
| 133 | getExpirationPeriod() const |
| 134 | { |
| 135 | return m_expirationPeriod; |
| 136 | } |
| 137 | |
| 138 | Route& |
| 139 | setExpirationPeriod(const time::milliseconds& expirationPeriod) |
| 140 | { |
| 141 | m_expirationPeriod = expirationPeriod; |
| 142 | |
| 143 | m_hasInfiniteExpirationPeriod = m_expirationPeriod == INFINITE_EXPIRATION_PERIOD; |
| 144 | |
| 145 | m_wire.reset(); |
| 146 | return *this; |
| 147 | } |
| 148 | |
| 149 | bool |
| 150 | hasInfiniteExpirationPeriod() const |
| 151 | { |
| 152 | return m_hasInfiniteExpirationPeriod; |
| 153 | } |
| 154 | |
| 155 | template<bool T> |
| 156 | size_t |
| 157 | wireEncode(EncodingImpl<T>& block) const; |
| 158 | |
| 159 | const Block& |
| 160 | wireEncode() const; |
| 161 | |
| 162 | void |
| 163 | wireDecode(const Block& wire); |
| 164 | |
| 165 | private: |
| 166 | uint64_t m_faceId; |
| 167 | uint64_t m_origin; |
| 168 | uint64_t m_cost; |
| 169 | uint64_t m_flags; |
| 170 | time::milliseconds m_expirationPeriod; |
| 171 | bool m_hasInfiniteExpirationPeriod; |
| 172 | |
| 173 | mutable Block m_wire; |
| 174 | }; |
| 175 | |
| 176 | std::ostream& |
| 177 | operator<<(std::ostream& os, const Route& route); |
| 178 | |
| 179 | /** |
| 180 | * @ingroup management |
| 181 | * |
| 182 | * @brief Data abstraction for RIB entry |
| 183 | * |
| 184 | * A RIB entry contains one or more routes for the name prefix |
| 185 | * |
| 186 | * RibEntry := RIB-ENTRY-TYPE TLV-LENGTH |
| 187 | * Name |
| 188 | * Route+ |
| 189 | * |
| 190 | * @sa http://redmine.named-data.net/projects/nfd/wiki/RibMgmt |
| 191 | */ |
| 192 | class RibEntry |
| 193 | { |
| 194 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 195 | class Error : public tlv::Error |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 196 | { |
| 197 | public: |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 198 | Error(const std::string& what) : tlv::Error(what) |
Vince Lehman | dbf3f70 | 2014-07-15 13:00:43 -0500 | [diff] [blame] | 199 | { |
| 200 | } |
| 201 | }; |
| 202 | |
| 203 | typedef std::list<Route> RouteList; |
| 204 | typedef RouteList::const_iterator iterator; |
| 205 | |
| 206 | RibEntry(); |
| 207 | |
| 208 | explicit |
| 209 | RibEntry(const Block& block); |
| 210 | |
| 211 | const Name& |
| 212 | getName() const |
| 213 | { |
| 214 | return m_prefix; |
| 215 | } |
| 216 | |
| 217 | RibEntry& |
| 218 | setName(const Name& prefix) |
| 219 | { |
| 220 | m_prefix = prefix; |
| 221 | m_wire.reset(); |
| 222 | return *this; |
| 223 | } |
| 224 | |
| 225 | const std::list<Route>& |
| 226 | getRoutes() const |
| 227 | { |
| 228 | return m_routes; |
| 229 | } |
| 230 | |
| 231 | RibEntry& |
| 232 | addRoute(const Route& route) |
| 233 | { |
| 234 | m_routes.push_back(route); |
| 235 | m_wire.reset(); |
| 236 | return *this; |
| 237 | } |
| 238 | |
| 239 | RibEntry& |
| 240 | clearRoutes() |
| 241 | { |
| 242 | m_routes.clear(); |
| 243 | return *this; |
| 244 | } |
| 245 | |
| 246 | template<bool T> |
| 247 | size_t |
| 248 | wireEncode(EncodingImpl<T>& block) const; |
| 249 | |
| 250 | const Block& |
| 251 | wireEncode() const; |
| 252 | |
| 253 | void |
| 254 | wireDecode(const Block& wire); |
| 255 | |
| 256 | iterator |
| 257 | begin() const; |
| 258 | |
| 259 | iterator |
| 260 | end() const; |
| 261 | |
| 262 | private: |
| 263 | Name m_prefix; |
| 264 | RouteList m_routes; |
| 265 | |
| 266 | mutable Block m_wire; |
| 267 | }; |
| 268 | |
| 269 | inline RibEntry::iterator |
| 270 | RibEntry::begin() const |
| 271 | { |
| 272 | return m_routes.begin(); |
| 273 | } |
| 274 | |
| 275 | inline RibEntry::iterator |
| 276 | RibEntry::end() const |
| 277 | { |
| 278 | return m_routes.end(); |
| 279 | } |
| 280 | |
| 281 | std::ostream& |
| 282 | operator<<(std::ostream& os, const RibEntry& entry); |
| 283 | |
| 284 | } // namespace nfd |
| 285 | } // namespace ndn |
| 286 | |
| 287 | #endif // NDN_MANAGEMENT_NFD_RIB_ENTRY_HPP |