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