blob: 6a46e028902863f0ccf01d26b4f58c181f4653d7 [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
28namespace ndn {
29
Junxiao Shid21abd32017-06-30 02:56:40 +000030/** @brief represents a Link object
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070031 */
32class Link : public Data
33{
34public:
35 class Error : public Data::Error
36 {
37 public:
38 explicit
39 Error(const std::string& what)
40 : Data::Error(what)
41 {
42 }
43 };
44
Junxiao Shid21abd32017-06-30 02:56:40 +000045 /** @brief Create an empty Link object
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070046 *
Junxiao Shid21abd32017-06-30 02:56:40 +000047 * Note that in certain contexts that use Link::shared_from_this(), Link must be
48 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070049 *
Junxiao Shid21abd32017-06-30 02:56:40 +000050 * shared_ptr<Link> linkObject = make_shared<Link>();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070051 */
Junxiao Shid21abd32017-06-30 02:56:40 +000052 Link();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070053
Junxiao Shid21abd32017-06-30 02:56:40 +000054 /** @brief Decode a Link object from a Block
55 * @param wire a TLV block
56 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070057 *
Junxiao Shid21abd32017-06-30 02:56:40 +000058 * Note that in certain contexts that use Link::shared_from_this(), Link must be
59 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070060 *
Junxiao Shid21abd32017-06-30 02:56:40 +000061 * shared_ptr<Link> linkObject = make_shared<Link>(block);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070062 */
63 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000064 Link(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070065
Junxiao Shid21abd32017-06-30 02:56:40 +000066 /** @brief Create a Link object with the given name and delegations
67 * @param name A reference to the name of the redirected namespace
68 * @param dels Delegations in payload
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070069 *
Junxiao Shid21abd32017-06-30 02:56:40 +000070 * Note that in certain contexts that use Link::shared_from_this(), Link must be
71 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070072 *
Junxiao Shid21abd32017-06-30 02:56:40 +000073 * shared_ptr<Link> link = make_shared<Link>(name, dels);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070074 */
75 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000076 Link(const Name& name, std::initializer_list<Delegation> dels = {});
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070077
Junxiao Shid21abd32017-06-30 02:56:40 +000078 /** @brief Decode from the wire format
79 * @param wire a TLV block
80 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070081 */
Junxiao Shid21abd32017-06-30 02:56:40 +000082 void
83 wireDecode(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070084
Junxiao Shid21abd32017-06-30 02:56:40 +000085 /** @brief Get the delegations
86 */
87 const DelegationList&
88 getDelegationList() const
89 {
90 return m_delList;
91 }
92
93 /** @brief Set the delegations
94 * @note This is more efficient than multiple addDelegation and removeDelegation invocations.
95 */
96 void
97 setDelegationList(const DelegationList& dels);
98
99 /** @brief Add a delegation in the format of <Name, Preference>
100 * @param preference The preference of the delegation to be added
101 * @param name The name of the delegation to be added
102 * @note If a delegation with @p name exists, its preference will be updated
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700103 */
104 void
105 addDelegation(uint32_t preference, const Name& name);
106
Junxiao Shid21abd32017-06-30 02:56:40 +0000107 /** @brief Remove a delegation whose name is @p name
108 * @param name The name of the delegation to be removed
109 * @return true if delegation is removed, otherwise false
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700110 */
111 bool
112 removeDelegation(const Name& name);
113
Junxiao Shid21abd32017-06-30 02:56:40 +0000114private:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700115 void
116 encodeContent();
117
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700118private:
Junxiao Shid21abd32017-06-30 02:56:40 +0000119 DelegationList m_delList;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700120};
121
122} // namespace ndn
123
124#endif // NDN_LINK_HPP