blob: e358404089e25d4ecf42f0ed1540b4b9fc35ede8 [file] [log] [blame]
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shid701e5b2017-07-26 01:30:41 +00002/*
Junxiao Shid21abd32017-06-30 02:56:40 +00003 * Copyright (c) 2013-2017 Regents of the University of California.
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07004 *
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_LINK_HPP
23#define NDN_LINK_HPP
24
25#include "data.hpp"
Junxiao Shid21abd32017-06-30 02:56:40 +000026#include "delegation-list.hpp"
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070027#include <set>
28
29namespace ndn {
30
Junxiao Shid21abd32017-06-30 02:56:40 +000031/** @brief represents a Link object
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070032 */
33class Link : public Data
34{
35public:
36 class Error : public Data::Error
37 {
38 public:
39 explicit
40 Error(const std::string& what)
41 : Data::Error(what)
42 {
43 }
44 };
45
Junxiao Shid21abd32017-06-30 02:56:40 +000046 /** @brief Create an empty Link object
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070047 *
Junxiao Shid21abd32017-06-30 02:56:40 +000048 * Note that in certain contexts that use Link::shared_from_this(), Link must be
49 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070050 *
Junxiao Shid21abd32017-06-30 02:56:40 +000051 * shared_ptr<Link> linkObject = make_shared<Link>();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070052 */
Junxiao Shid21abd32017-06-30 02:56:40 +000053 Link();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070054
Junxiao Shid21abd32017-06-30 02:56:40 +000055 /** @brief Decode a Link object from a Block
56 * @param wire a TLV block
57 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070058 *
Junxiao Shid21abd32017-06-30 02:56:40 +000059 * Note that in certain contexts that use Link::shared_from_this(), Link must be
60 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070061 *
Junxiao Shid21abd32017-06-30 02:56:40 +000062 * shared_ptr<Link> linkObject = make_shared<Link>(block);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070063 */
64 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000065 Link(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070066
Junxiao Shid21abd32017-06-30 02:56:40 +000067 /** @brief Create a Link object with the given name and delegations
68 * @param name A reference to the name of the redirected namespace
69 * @param dels Delegations in payload
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070070 *
Junxiao Shid21abd32017-06-30 02:56:40 +000071 * Note that in certain contexts that use Link::shared_from_this(), Link must be
72 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070073 *
Junxiao Shid21abd32017-06-30 02:56:40 +000074 * shared_ptr<Link> link = make_shared<Link>(name, dels);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070075 */
76 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000077 Link(const Name& name, std::initializer_list<Delegation> dels = {});
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070078
Junxiao Shid21abd32017-06-30 02:56:40 +000079 /** @brief Decode from the wire format
80 * @param wire a TLV block
81 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070082 */
Junxiao Shid21abd32017-06-30 02:56:40 +000083 void
84 wireDecode(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070085
Junxiao Shid21abd32017-06-30 02:56:40 +000086 /** @brief Get the delegations
87 */
88 const DelegationList&
89 getDelegationList() const
90 {
91 return m_delList;
92 }
93
94 /** @brief Set the delegations
95 * @note This is more efficient than multiple addDelegation and removeDelegation invocations.
96 */
97 void
98 setDelegationList(const DelegationList& dels);
99
100 /** @brief Add a delegation in the format of <Name, Preference>
101 * @param preference The preference of the delegation to be added
102 * @param name The name of the delegation to be added
103 * @note If a delegation with @p name exists, its preference will be updated
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700104 */
105 void
106 addDelegation(uint32_t preference, const Name& name);
107
Junxiao Shid21abd32017-06-30 02:56:40 +0000108 /** @brief Remove a delegation whose name is @p name
109 * @param name The name of the delegation to be removed
110 * @return true if delegation is removed, otherwise false
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700111 */
112 bool
113 removeDelegation(const Name& name);
114
Junxiao Shid21abd32017-06-30 02:56:40 +0000115public: // deprecated APIs
116 using DelegationSet = std::set<std::pair<uint32_t, Name>>;
117 using DelegationTuple = std::tuple<uint32_t, Name>;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700118
Junxiao Shid21abd32017-06-30 02:56:40 +0000119 class PairInitializerListHelper
120 {
121 public:
122 PairInitializerListHelper(std::initializer_list<std::pair<uint32_t, Name>> dels);
123
124 private:
125 DelegationList m_delList;
126 friend class Link;
127 };
128
129 /** @brief Create a Link object with the given name and delegations
130 * @param name A reference to the name of the redirected namespace
131 * @param dels Delegations in payload
132 * @deprecated use Link(const Name&, std::initializer_list<Delegation>)
133 * @note This overload is selected only if the caller explicitly passes
134 * std::initializer_list<std::pair<uint32_t, Name>> to Link constructor;
135 * otherwise, Link(const Name&, std::initializer_list<Delegation>) is preferred.
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700136 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000137 DEPRECATED(
138 Link(const Name& name, PairInitializerListHelper dels));
139
140 /** @deprecated use getDelegationList()
141 */
142 DEPRECATED(
143 const DelegationSet&
144 getDelegations() const);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700145
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700146 /** @brief gets the delegation at @p index from @p block
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700147 * @param block wire format of a Link object
148 * @param index 0-based index of a delegation in the Link object
149 * @return delegation preference and name
150 * @throw std::out_of_range index is out of range
Junxiao Shid21abd32017-06-30 02:56:40 +0000151 * @deprecated use Link(block, false).getDelegationList().at(index)
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700152 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000153 DEPRECATED(
154 static DelegationTuple
155 getDelegationFromWire(const Block& block, size_t index));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700156
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700157 /** @brief finds index of a delegation with @p delegationName from @p block
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700158 * @param block wire format of a Link object
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700159 * @param delegationName delegation name in the Link object
160 * @return 0-based index of the first delegation with @p delegationName ,
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700161 * or -1 if no such delegation exists
Junxiao Shid21abd32017-06-30 02:56:40 +0000162 * @deprecated find within Link(block, false).getDelegationList()
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700163 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000164 DEPRECATED(
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700165 static ssize_t
Junxiao Shid21abd32017-06-30 02:56:40 +0000166 findDelegationFromWire(const Block& block, const Name& delegationName));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700167
Junxiao Shid21abd32017-06-30 02:56:40 +0000168 /** @deprecated use Link(block, false).getDelegationList().size()
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700169 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000170 DEPRECATED(
171 static ssize_t
172 countDelegationsFromWire(const Block& block));
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700173
Junxiao Shid21abd32017-06-30 02:56:40 +0000174private:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700175 void
176 encodeContent();
177
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700178private:
Junxiao Shid21abd32017-06-30 02:56:40 +0000179 DelegationList m_delList;
180 mutable bool m_isDelSetDirty = false;
181 mutable DelegationSet m_delSet;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700182};
183
184} // namespace ndn
185
186#endif // NDN_LINK_HPP