blob: 8b822382614910001736ae7ec22b71eb92509a89 [file] [log] [blame]
Junxiao Shi7357ef22016-09-07 02:39:37 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento6ad3d532017-02-17 01:43:57 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Junxiao Shi7357ef22016-09-07 02:39:37 +00004 *
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_MGMT_NFD_RIB_ENTRY_HPP
23#define NDN_MGMT_NFD_RIB_ENTRY_HPP
24
Davide Pesaventod1a415c2017-02-20 00:41:42 -050025#include "route-flags-traits.hpp"
Davide Pesavento484bbe52017-02-15 00:03:46 -050026#include "../../encoding/block.hpp"
Junxiao Shi7357ef22016-09-07 02:39:37 +000027#include "../../name.hpp"
28#include "../../util/time.hpp"
29
Junxiao Shi7357ef22016-09-07 02:39:37 +000030namespace ndn {
31namespace nfd {
32
33/**
Davide Pesavento484bbe52017-02-15 00:03:46 -050034 * \ingroup management
35 * \brief represents a route in a RibEntry
Junxiao Shi7357ef22016-09-07 02:39:37 +000036 *
37 * A route indicates the availability of content via a certain face and
38 * provides meta-information about the face.
39 *
Davide Pesavento484bbe52017-02-15 00:03:46 -050040 * \sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt#Route
Junxiao Shi7357ef22016-09-07 02:39:37 +000041 */
Davide Pesaventod1a415c2017-02-20 00:41:42 -050042class Route : public RouteFlagsTraits<Route>
Junxiao Shi7357ef22016-09-07 02:39:37 +000043{
44public:
45 class Error : public tlv::Error
46 {
47 public:
48 explicit
Davide Pesavento6ad3d532017-02-17 01:43:57 -050049 Error(const std::string& what)
50 : tlv::Error(what)
Junxiao Shi7357ef22016-09-07 02:39:37 +000051 {
52 }
53 };
54
55 Route();
56
57 explicit
58 Route(const Block& block);
59
60 uint64_t
61 getFaceId() const
62 {
63 return m_faceId;
64 }
65
66 Route&
Davide Pesavento6ad3d532017-02-17 01:43:57 -050067 setFaceId(uint64_t faceId);
Junxiao Shi7357ef22016-09-07 02:39:37 +000068
69 uint64_t
70 getOrigin() const
71 {
72 return m_origin;
73 }
74
75 /** @brief set Origin
76 * @param origin a code defined in ndn::nfd::RouteOrigin
77 */
78 Route&
Davide Pesavento6ad3d532017-02-17 01:43:57 -050079 setOrigin(uint64_t origin);
Junxiao Shi7357ef22016-09-07 02:39:37 +000080
81 uint64_t
82 getCost() const
83 {
84 return m_cost;
85 }
86
87 Route&
Davide Pesavento6ad3d532017-02-17 01:43:57 -050088 setCost(uint64_t cost);
Junxiao Shi7357ef22016-09-07 02:39:37 +000089
90 uint64_t
91 getFlags() const
92 {
93 return m_flags;
94 }
95
96 /** @brief set route inheritance flags
97 * @param flags a bitwise OR'ed code from ndn::nfd::RouteFlags
98 */
99 Route&
Davide Pesavento6ad3d532017-02-17 01:43:57 -0500100 setFlags(uint64_t flags);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000101
Davide Pesavento484bbe52017-02-15 00:03:46 -0500102 bool
103 hasExpirationPeriod() const
104 {
105 return !!m_expirationPeriod;
106 }
Junxiao Shi7357ef22016-09-07 02:39:37 +0000107
Davide Pesavento6ad3d532017-02-17 01:43:57 -0500108 time::milliseconds
Junxiao Shi7357ef22016-09-07 02:39:37 +0000109 getExpirationPeriod() const
110 {
Davide Pesavento484bbe52017-02-15 00:03:46 -0500111 return m_expirationPeriod ? *m_expirationPeriod : time::milliseconds::max();
Junxiao Shi7357ef22016-09-07 02:39:37 +0000112 }
113
114 Route&
Davide Pesavento6ad3d532017-02-17 01:43:57 -0500115 setExpirationPeriod(time::milliseconds expirationPeriod);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000116
Davide Pesavento484bbe52017-02-15 00:03:46 -0500117 Route&
118 unsetExpirationPeriod();
Junxiao Shi7357ef22016-09-07 02:39:37 +0000119
120 template<encoding::Tag TAG>
121 size_t
122 wireEncode(EncodingImpl<TAG>& block) const;
123
124 const Block&
125 wireEncode() const;
126
127 void
Davide Pesavento484bbe52017-02-15 00:03:46 -0500128 wireDecode(const Block& block);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000129
130private:
131 uint64_t m_faceId;
132 uint64_t m_origin;
133 uint64_t m_cost;
134 uint64_t m_flags;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500135 optional<time::milliseconds> m_expirationPeriod;
Junxiao Shi7357ef22016-09-07 02:39:37 +0000136
137 mutable Block m_wire;
138};
139
Davide Pesavento484bbe52017-02-15 00:03:46 -0500140bool
141operator==(const Route& a, const Route& b);
142
143inline bool
144operator!=(const Route& a, const Route& b)
145{
146 return !(a == b);
147}
148
Junxiao Shi7357ef22016-09-07 02:39:37 +0000149std::ostream&
150operator<<(std::ostream& os, const Route& route);
151
Davide Pesavento484bbe52017-02-15 00:03:46 -0500152
Junxiao Shi7357ef22016-09-07 02:39:37 +0000153/**
Davide Pesavento484bbe52017-02-15 00:03:46 -0500154 * \ingroup management
155 * \brief represents an item in NFD RIB dataset
Junxiao Shi7357ef22016-09-07 02:39:37 +0000156 *
Davide Pesavento484bbe52017-02-15 00:03:46 -0500157 * A RIB entry contains one or more routes for a name prefix
Junxiao Shi7357ef22016-09-07 02:39:37 +0000158 *
Davide Pesavento484bbe52017-02-15 00:03:46 -0500159 * \sa https://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset
Junxiao Shi7357ef22016-09-07 02:39:37 +0000160 */
161class RibEntry
162{
163public:
164 class Error : public tlv::Error
165 {
166 public:
Davide Pesavento6ad3d532017-02-17 01:43:57 -0500167 explicit
168 Error(const std::string& what)
169 : tlv::Error(what)
Junxiao Shi7357ef22016-09-07 02:39:37 +0000170 {
171 }
172 };
173
Junxiao Shi7357ef22016-09-07 02:39:37 +0000174 RibEntry();
175
176 explicit
177 RibEntry(const Block& block);
178
179 const Name&
180 getName() const
181 {
182 return m_prefix;
183 }
184
185 RibEntry&
Davide Pesavento6ad3d532017-02-17 01:43:57 -0500186 setName(const Name& prefix);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000187
Davide Pesavento484bbe52017-02-15 00:03:46 -0500188 const std::vector<Route>&
Junxiao Shi7357ef22016-09-07 02:39:37 +0000189 getRoutes() const
190 {
191 return m_routes;
192 }
193
Davide Pesavento484bbe52017-02-15 00:03:46 -0500194 template<typename InputIt>
195 RibEntry&
196 setRoutes(InputIt first, InputIt last)
197 {
198 m_routes.assign(first, last);
199 m_wire.reset();
200 return *this;
201 }
202
Junxiao Shi7357ef22016-09-07 02:39:37 +0000203 RibEntry&
Davide Pesavento6ad3d532017-02-17 01:43:57 -0500204 addRoute(const Route& route);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000205
206 RibEntry&
Davide Pesavento6ad3d532017-02-17 01:43:57 -0500207 clearRoutes();
Junxiao Shi7357ef22016-09-07 02:39:37 +0000208
209 template<encoding::Tag TAG>
210 size_t
211 wireEncode(EncodingImpl<TAG>& block) const;
212
213 const Block&
214 wireEncode() const;
215
216 void
Davide Pesavento484bbe52017-02-15 00:03:46 -0500217 wireDecode(const Block& block);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000218
219private:
220 Name m_prefix;
Davide Pesavento484bbe52017-02-15 00:03:46 -0500221 std::vector<Route> m_routes;
Junxiao Shi7357ef22016-09-07 02:39:37 +0000222
223 mutable Block m_wire;
224};
225
Davide Pesavento484bbe52017-02-15 00:03:46 -0500226bool
227operator==(const RibEntry& a, const RibEntry& b);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000228
Davide Pesavento484bbe52017-02-15 00:03:46 -0500229inline bool
230operator!=(const RibEntry& a, const RibEntry& b)
Junxiao Shi7357ef22016-09-07 02:39:37 +0000231{
Davide Pesavento484bbe52017-02-15 00:03:46 -0500232 return !(a == b);
Junxiao Shi7357ef22016-09-07 02:39:37 +0000233}
234
235std::ostream&
236operator<<(std::ostream& os, const RibEntry& entry);
237
238} // namespace nfd
239} // namespace ndn
240
241#endif // NDN_MGMT_NFD_RIB_ENTRY_HPP