blob: b3e9bf22cb69d6b1a903817fff536d9e30f73604 [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 Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 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:
Junxiao Shi68b53852018-07-25 13:56:38 -060038 using Data::Error::Error;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070039 };
40
Junxiao Shid21abd32017-06-30 02:56:40 +000041 /** @brief Create an empty Link object
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070042 *
Junxiao Shid21abd32017-06-30 02:56:40 +000043 * Note that in certain contexts that use Link::shared_from_this(), Link must be
44 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070045 *
Junxiao Shid21abd32017-06-30 02:56:40 +000046 * shared_ptr<Link> linkObject = make_shared<Link>();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070047 */
Junxiao Shid21abd32017-06-30 02:56:40 +000048 Link();
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070049
Junxiao Shid21abd32017-06-30 02:56:40 +000050 /** @brief Decode a Link object from a Block
51 * @param wire a TLV block
52 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070053 *
Junxiao Shid21abd32017-06-30 02:56:40 +000054 * Note that in certain contexts that use Link::shared_from_this(), Link must be
55 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070056 *
Junxiao Shid21abd32017-06-30 02:56:40 +000057 * shared_ptr<Link> linkObject = make_shared<Link>(block);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070058 */
59 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000060 Link(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070061
Junxiao Shid21abd32017-06-30 02:56:40 +000062 /** @brief Create a Link object with the given name and delegations
63 * @param name A reference to the name of the redirected namespace
64 * @param dels Delegations in payload
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070065 *
Junxiao Shid21abd32017-06-30 02:56:40 +000066 * Note that in certain contexts that use Link::shared_from_this(), Link must be
67 * created using `make_shared`:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070068 *
Junxiao Shid21abd32017-06-30 02:56:40 +000069 * shared_ptr<Link> link = make_shared<Link>(name, dels);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070070 */
71 explicit
Junxiao Shid21abd32017-06-30 02:56:40 +000072 Link(const Name& name, std::initializer_list<Delegation> dels = {});
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070073
Junxiao Shid21abd32017-06-30 02:56:40 +000074 /** @brief Decode from the wire format
75 * @param wire a TLV block
76 * @param wantSort if false, relative order among delegations is preserved
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070077 */
Junxiao Shid21abd32017-06-30 02:56:40 +000078 void
79 wireDecode(const Block& wire, bool wantSort = true);
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070080
Junxiao Shid21abd32017-06-30 02:56:40 +000081 /** @brief Get the delegations
82 */
83 const DelegationList&
84 getDelegationList() const
85 {
86 return m_delList;
87 }
88
89 /** @brief Set the delegations
90 * @note This is more efficient than multiple addDelegation and removeDelegation invocations.
91 */
92 void
93 setDelegationList(const DelegationList& dels);
94
95 /** @brief Add a delegation in the format of <Name, Preference>
96 * @param preference The preference of the delegation to be added
97 * @param name The name of the delegation to be added
98 * @note If a delegation with @p name exists, its preference will be updated
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -070099 */
100 void
101 addDelegation(uint32_t preference, const Name& name);
102
Junxiao Shid21abd32017-06-30 02:56:40 +0000103 /** @brief Remove a delegation whose name is @p name
104 * @param name The name of the delegation to be removed
105 * @return true if delegation is removed, otherwise false
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700106 */
107 bool
108 removeDelegation(const Name& name);
109
Junxiao Shid21abd32017-06-30 02:56:40 +0000110private:
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700111 void
112 encodeContent();
113
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700114private:
Junxiao Shid21abd32017-06-30 02:56:40 +0000115 DelegationList m_delList;
Spyridon Mastorakis3b54e852015-04-07 08:03:25 -0700116};
117
118} // namespace ndn
119
120#endif // NDN_LINK_HPP