blob: 3fc4387de491da29ff2bbb49b38a8100d4f3b02c [file] [log] [blame]
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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
31const size_t INVALID_SELECTED_DELEGATION_INDEX = std::numeric_limits<size_t>::max();
32
Junxiao Shid21abd32017-06-30 02:56:40 +000033/** @brief represents a Link object
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070034 */
35class Link : public Data
36{
37public:
38 class Error : public Data::Error
39 {
40 public:
41 explicit
42 Error(const std::string& what)
43 : Data::Error(what)
44 {
45 }
46 };
47
Junxiao Shid21abd32017-06-30 02:56:40 +000048 /** @brief Create an empty Link object
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070049 *
Junxiao Shid21abd32017-06-30 02:56:40 +000050 * Note that in certain contexts that use Link::shared_from_this(), Link must be
51 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070052 *
Junxiao Shid21abd32017-06-30 02:56:40 +000053 * shared_ptr<Link> linkObject = make_shared<Link>();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070054 */
Junxiao Shid21abd32017-06-30 02:56:40 +000055 Link();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070056
Junxiao Shid21abd32017-06-30 02:56:40 +000057 /** @brief Decode a Link object from a Block
58 * @param wire a TLV block
59 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070060 *
Junxiao Shid21abd32017-06-30 02:56:40 +000061 * Note that in certain contexts that use Link::shared_from_this(), Link must be
62 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070063 *
Junxiao Shid21abd32017-06-30 02:56:40 +000064 * shared_ptr<Link> linkObject = make_shared<Link>(block);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070065 */
66 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000067 Link(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070068
Junxiao Shid21abd32017-06-30 02:56:40 +000069 /** @brief Create a Link object with the given name and delegations
70 * @param name A reference to the name of the redirected namespace
71 * @param dels Delegations in payload
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070072 *
Junxiao Shid21abd32017-06-30 02:56:40 +000073 * Note that in certain contexts that use Link::shared_from_this(), Link must be
74 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070075 *
Junxiao Shid21abd32017-06-30 02:56:40 +000076 * shared_ptr<Link> link = make_shared<Link>(name, dels);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070077 */
78 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000079 Link(const Name& name, std::initializer_list<Delegation> dels = {});
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070080
Junxiao Shid21abd32017-06-30 02:56:40 +000081 /** @brief Decode from the wire format
82 * @param wire a TLV block
83 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070084 */
Junxiao Shid21abd32017-06-30 02:56:40 +000085 void
86 wireDecode(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070087
Junxiao Shid21abd32017-06-30 02:56:40 +000088 /** @brief Get the delegations
89 */
90 const DelegationList&
91 getDelegationList() const
92 {
93 return m_delList;
94 }
95
96 /** @brief Set the delegations
97 * @note This is more efficient than multiple addDelegation and removeDelegation invocations.
98 */
99 void
100 setDelegationList(const DelegationList& dels);
101
102 /** @brief Add a delegation in the format of <Name, Preference>
103 * @param preference The preference of the delegation to be added
104 * @param name The name of the delegation to be added
105 * @note If a delegation with @p name exists, its preference will be updated
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700106 */
107 void
108 addDelegation(uint32_t preference, const Name& name);
109
Junxiao Shid21abd32017-06-30 02:56:40 +0000110 /** @brief Remove a delegation whose name is @p name
111 * @param name The name of the delegation to be removed
112 * @return true if delegation is removed, otherwise false
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700113 */
114 bool
115 removeDelegation(const Name& name);
116
Junxiao Shid21abd32017-06-30 02:56:40 +0000117public: // deprecated APIs
118 using DelegationSet = std::set<std::pair<uint32_t, Name>>;
119 using DelegationTuple = std::tuple<uint32_t, Name>;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700120
Junxiao Shid21abd32017-06-30 02:56:40 +0000121 class PairInitializerListHelper
122 {
123 public:
124 PairInitializerListHelper(std::initializer_list<std::pair<uint32_t, Name>> dels);
125
126 private:
127 DelegationList m_delList;
128 friend class Link;
129 };
130
131 /** @brief Create a Link object with the given name and delegations
132 * @param name A reference to the name of the redirected namespace
133 * @param dels Delegations in payload
134 * @deprecated use Link(const Name&, std::initializer_list<Delegation>)
135 * @note This overload is selected only if the caller explicitly passes
136 * std::initializer_list<std::pair<uint32_t, Name>> to Link constructor;
137 * otherwise, Link(const Name&, std::initializer_list<Delegation>) is preferred.
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700138 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000139 DEPRECATED(
140 Link(const Name& name, PairInitializerListHelper dels));
141
142 /** @deprecated use getDelegationList()
143 */
144 DEPRECATED(
145 const DelegationSet&
146 getDelegations() const);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700147
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700148 /** @brief gets the delegation at @p index from @p block
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700149 * @param block wire format of a Link object
150 * @param index 0-based index of a delegation in the Link object
151 * @return delegation preference and name
152 * @throw std::out_of_range index is out of range
Junxiao Shid21abd32017-06-30 02:56:40 +0000153 * @deprecated use Link(block, false).getDelegationList().at(index)
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700154 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000155 DEPRECATED(
156 static DelegationTuple
157 getDelegationFromWire(const Block& block, size_t index));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700158
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700159 /** @brief finds index of a delegation with @p delegationName from @p block
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700160 * @param block wire format of a Link object
Alexander Afanasyevf2a46222015-09-17 18:01:30 -0700161 * @param delegationName delegation name in the Link object
162 * @return 0-based index of the first delegation with @p delegationName ,
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700163 * or -1 if no such delegation exists
Junxiao Shid21abd32017-06-30 02:56:40 +0000164 * @deprecated find within Link(block, false).getDelegationList()
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700165 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000166 DEPRECATED(
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700167 static ssize_t
Junxiao Shid21abd32017-06-30 02:56:40 +0000168 findDelegationFromWire(const Block& block, const Name& delegationName));
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700169
Junxiao Shid21abd32017-06-30 02:56:40 +0000170 /** @deprecated use Link(block, false).getDelegationList().size()
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700171 */
Junxiao Shid21abd32017-06-30 02:56:40 +0000172 DEPRECATED(
173 static ssize_t
174 countDelegationsFromWire(const Block& block));
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700175
Junxiao Shid21abd32017-06-30 02:56:40 +0000176private:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700177 void
178 encodeContent();
179
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700180private:
Junxiao Shid21abd32017-06-30 02:56:40 +0000181 DelegationList m_delList;
182 mutable bool m_isDelSetDirty = false;
183 mutable DelegationSet m_delSet;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700184};
185
186} // namespace ndn
187
188#endif // NDN_LINK_HPP