Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame^] | 2 | /* |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 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" |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 26 | #include "delegation-list.hpp" |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 27 | #include <set> |
| 28 | |
| 29 | namespace ndn { |
| 30 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 31 | /** @brief represents a Link object |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 32 | */ |
| 33 | class Link : public Data |
| 34 | { |
| 35 | public: |
| 36 | class Error : public Data::Error |
| 37 | { |
| 38 | public: |
| 39 | explicit |
| 40 | Error(const std::string& what) |
| 41 | : Data::Error(what) |
| 42 | { |
| 43 | } |
| 44 | }; |
| 45 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 46 | /** @brief Create an empty Link object |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 47 | * |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 48 | * Note that in certain contexts that use Link::shared_from_this(), Link must be |
| 49 | * created using `make_shared`: |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 50 | * |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 51 | * shared_ptr<Link> linkObject = make_shared<Link>(); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 52 | */ |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 53 | Link(); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 55 | /** @brief Decode a Link object from a Block |
| 56 | * @param wire a TLV block |
| 57 | * @param wantSort if false, relative order among delegations is preserved |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 58 | * |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 59 | * Note that in certain contexts that use Link::shared_from_this(), Link must be |
| 60 | * created using `make_shared`: |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 61 | * |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 62 | * shared_ptr<Link> linkObject = make_shared<Link>(block); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 63 | */ |
| 64 | explicit |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 65 | Link(const Block& wire, bool wantSort = true); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 66 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 67 | /** @brief Create a Link object with the given name and delegations |
| 68 | * @param name A reference to the name of the redirected namespace |
| 69 | * @param dels Delegations in payload |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 70 | * |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 71 | * Note that in certain contexts that use Link::shared_from_this(), Link must be |
| 72 | * created using `make_shared`: |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 73 | * |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 74 | * shared_ptr<Link> link = make_shared<Link>(name, dels); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 75 | */ |
| 76 | explicit |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 77 | Link(const Name& name, std::initializer_list<Delegation> dels = {}); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 79 | /** @brief Decode from the wire format |
| 80 | * @param wire a TLV block |
| 81 | * @param wantSort if false, relative order among delegations is preserved |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 82 | */ |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 83 | void |
| 84 | wireDecode(const Block& wire, bool wantSort = true); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 86 | /** @brief Get the delegations |
| 87 | */ |
| 88 | const DelegationList& |
| 89 | getDelegationList() const |
| 90 | { |
| 91 | return m_delList; |
| 92 | } |
| 93 | |
| 94 | /** @brief Set the delegations |
| 95 | * @note This is more efficient than multiple addDelegation and removeDelegation invocations. |
| 96 | */ |
| 97 | void |
| 98 | setDelegationList(const DelegationList& dels); |
| 99 | |
| 100 | /** @brief Add a delegation in the format of <Name, Preference> |
| 101 | * @param preference The preference of the delegation to be added |
| 102 | * @param name The name of the delegation to be added |
| 103 | * @note If a delegation with @p name exists, its preference will be updated |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 104 | */ |
| 105 | void |
| 106 | addDelegation(uint32_t preference, const Name& name); |
| 107 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 108 | /** @brief Remove a delegation whose name is @p name |
| 109 | * @param name The name of the delegation to be removed |
| 110 | * @return true if delegation is removed, otherwise false |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 111 | */ |
| 112 | bool |
| 113 | removeDelegation(const Name& name); |
| 114 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 115 | public: // deprecated APIs |
| 116 | using DelegationSet = std::set<std::pair<uint32_t, Name>>; |
| 117 | using DelegationTuple = std::tuple<uint32_t, Name>; |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 119 | class PairInitializerListHelper |
| 120 | { |
| 121 | public: |
| 122 | PairInitializerListHelper(std::initializer_list<std::pair<uint32_t, Name>> dels); |
| 123 | |
| 124 | private: |
| 125 | DelegationList m_delList; |
| 126 | friend class Link; |
| 127 | }; |
| 128 | |
| 129 | /** @brief Create a Link object with the given name and delegations |
| 130 | * @param name A reference to the name of the redirected namespace |
| 131 | * @param dels Delegations in payload |
| 132 | * @deprecated use Link(const Name&, std::initializer_list<Delegation>) |
| 133 | * @note This overload is selected only if the caller explicitly passes |
| 134 | * std::initializer_list<std::pair<uint32_t, Name>> to Link constructor; |
| 135 | * otherwise, Link(const Name&, std::initializer_list<Delegation>) is preferred. |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 136 | */ |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 137 | DEPRECATED( |
| 138 | Link(const Name& name, PairInitializerListHelper dels)); |
| 139 | |
| 140 | /** @deprecated use getDelegationList() |
| 141 | */ |
| 142 | DEPRECATED( |
| 143 | const DelegationSet& |
| 144 | getDelegations() const); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 145 | |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 146 | /** @brief gets the delegation at @p index from @p block |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 147 | * @param block wire format of a Link object |
| 148 | * @param index 0-based index of a delegation in the Link object |
| 149 | * @return delegation preference and name |
| 150 | * @throw std::out_of_range index is out of range |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 151 | * @deprecated use Link(block, false).getDelegationList().at(index) |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 152 | */ |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 153 | DEPRECATED( |
| 154 | static DelegationTuple |
| 155 | getDelegationFromWire(const Block& block, size_t index)); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 156 | |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 157 | /** @brief finds index of a delegation with @p delegationName from @p block |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 158 | * @param block wire format of a Link object |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 159 | * @param delegationName delegation name in the Link object |
| 160 | * @return 0-based index of the first delegation with @p delegationName , |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 161 | * or -1 if no such delegation exists |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 162 | * @deprecated find within Link(block, false).getDelegationList() |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 163 | */ |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 164 | DEPRECATED( |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 165 | static ssize_t |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 166 | findDelegationFromWire(const Block& block, const Name& delegationName)); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 167 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 168 | /** @deprecated use Link(block, false).getDelegationList().size() |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 169 | */ |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 170 | DEPRECATED( |
| 171 | static ssize_t |
| 172 | countDelegationsFromWire(const Block& block)); |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 174 | private: |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 175 | void |
| 176 | encodeContent(); |
| 177 | |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 178 | private: |
Junxiao Shi | d21abd3 | 2017-06-30 02:56:40 +0000 | [diff] [blame] | 179 | DelegationList m_delList; |
| 180 | mutable bool m_isDelSetDirty = false; |
| 181 | mutable DelegationSet m_delSet; |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | } // namespace ndn |
| 185 | |
| 186 | #endif // NDN_LINK_HPP |