blob: 3ccb7a3d1949d85538e321e305f7c4f1e23644ee [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
132 */
133 void
134 wireDecode(const Block& wire);
135
136protected:
137 /** \brief prepend Link object as a Content block to the encoder
138 *
139 * The outermost Content element is not part of Link object structure.
140 */
141 template<encoding::Tag TAG>
142 size_t
143 encodeContent(EncodingImpl<TAG>& encoder) const;
144
145 void
146 encodeContent();
147
148 void
149 decodeContent();
150
151private:
152 bool
153 removeDelegationNoEncode(const Name& name);
154
155private:
156 DelegationSet m_delegations;
157};
158
159} // namespace ndn
160
161#endif // NDN_LINK_HPP