blob: e4e9f38fb26d991aab502aab0dfea6aa20a71cac [file] [log] [blame]
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 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_LINK_HPP
23#define NDN_LINK_HPP
24
25#include "data.hpp"
26#include <set>
27
28namespace ndn {
29
30const size_t INVALID_SELECTED_DELEGATION_INDEX = std::numeric_limits<size_t>::max();
31
32/** @brief represents a Link instance
33 */
34class Link : public Data
35{
36public:
37 class Error : public Data::Error
38 {
39 public:
40 explicit
41 Error(const std::string& what)
42 : Data::Error(what)
43 {
44 }
45 };
46
47 // The ordering is based on the preference number and needs to be preserved
48 typedef std::set<std::pair<uint32_t, Name>> DelegationSet;
49
50 /**
51 * @brief Create an empty Link object
52 *
53 * Note that in certain contexts that use Link::shared_from_this(), Link must be
54 * created using `make_shared`:
55 *
56 * shared_ptr<Link> linkObject = make_shared<Link>();
57 *
58 * Otherwise, Link::shared_from_this() will throw std::bad_weak_ptr.
59 */
60 Link() = default;
61
62 /**
63 * @brief Create a Link object from a Block
64 *
65 * Note that in certain contexts that use Link::shared_from_this(), Link must be
66 * created using `make_shared`:
67 *
68 * shared_ptr<Link> linkObject = make_shared<Link>(block);
69 *
70 * Otherwise, Link::shared_from_this() will throw std::bad_weak_ptr.
71 */
72 explicit
73 Link(const Block& block);
74
75 /**
76 * @brief Create a Link object with the given name
77 *
78 * @param name A reference to the name of the redirected namespace
79 *
80 * Note that in certain contexts that use Link::shared_from_this(), Link must be
81 * created using `make_shared`:
82 *
83 * shared_ptr<Link> link = make_shared<Link>(name);
84 *
85 * Otherwise, Link::shared_from_this() will throw std::bad_weak_ptr.
86 */
87 explicit
88 Link(const Name& name);
89
90 /**
91 * @brief Create a Link object with the given name and pairs of <Preference, Name>
92 *
93 * @param name A reference to the name of the redirected namespace
94 * @param links A reference to the list of pairs of the redirected namespace
95 * along with its priority
96 *
97 * Note that in certain contexts that use Link::shared_from_this(), Link must be
98 * created using `make_shared`:
99 *
100 * shared_ptr<Link> link = make_shared<Link>(name, links);
101 *
102 * Otherwise, Link::shared_from_this() will throw std::bad_weak_ptr.
103 */
104 Link(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> links);
105
106 /**
107 * @brief Add a delegation in the format of <Name, Preference>
108 * @param preference The preference of the delegation to be added
109 * @param name The name of the delegation to be added
110 * @note If a delegation with @p name exists, its preference will be updated
111 */
112 void
113 addDelegation(uint32_t preference, const Name& name);
114
115 /**
116 * @brief Remove a delegation whose name is @p name
117 * @param name The name of the delegation to be removed
118 * @return true if delegation is removed, otherwise false
119 */
120 bool
121 removeDelegation(const Name& name);
122
123 /**
124 * @brief Get the pairs of <Name, Preference>
125 * @return a set of delegations
126 */
127 const DelegationSet&
128 getDelegations() const;
129
130 /**
131 * @brief Decode from the wire format
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700132 * @warning This method does not preserve the relative order between delegations.
133 * To get a delegation by index, use \p getDelegationFromWire method.
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700134 */
135 void
136 wireDecode(const Block& wire);
137
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700138 /** @brief gets the delegation at \p index from \p block
139 * @param block wire format of a Link object
140 * @param index 0-based index of a delegation in the Link object
141 * @return delegation preference and name
142 * @throw std::out_of_range index is out of range
143 */
144 static std::tuple<uint32_t, Name>
145 getDelegationFromWire(const Block& block, size_t index);
146
147 /** @brief finds index of a delegation with \p delegationName from \p block
148 * @param block wire format of a Link object
149 * @return 0-based index of the first delegation with \p delegationName ,
150 * or -1 if no such delegation exists
151 */
152 static ssize_t
153 findDelegationFromWire(const Block& block, const Name& delegationName);
154
155 static ssize_t
156 countDelegationsFromWire(const Block& block);
157
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700158protected:
Spyridon Mastorakisc8188b32015-04-18 18:33:38 -0700159 /** @brief prepend Link object as a Content block to the encoder
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700160 *
161 * The outermost Content element is not part of Link object structure.
162 */
163 template<encoding::Tag TAG>
164 size_t
165 encodeContent(EncodingImpl<TAG>& encoder) const;
166
167 void
168 encodeContent();
169
170 void
171 decodeContent();
172
173private:
174 bool
175 removeDelegationNoEncode(const Name& name);
176
177private:
178 DelegationSet m_delegations;
179};
180
181} // namespace ndn
182
183#endif // NDN_LINK_HPP