Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 28 | namespace ndn { |
| 29 | |
| 30 | const size_t INVALID_SELECTED_DELEGATION_INDEX = std::numeric_limits<size_t>::max(); |
| 31 | |
| 32 | /** @brief represents a Link instance |
| 33 | */ |
| 34 | class Link : public Data |
| 35 | { |
| 36 | public: |
| 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 |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 132 | * @warning This method does not preserve the relative order between delegations. |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame^] | 133 | * To get a delegation by index, use @p getDelegationFromWire method. |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 134 | */ |
| 135 | void |
| 136 | wireDecode(const Block& wire); |
| 137 | |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame^] | 138 | /** @brief gets the delegation at @p index from @p block |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 139 | * @param block wire format of a Link object |
| 140 | * @param index 0-based index of a delegation in the Link object |
| 141 | * @return delegation preference and name |
| 142 | * @throw std::out_of_range index is out of range |
| 143 | */ |
| 144 | static std::tuple<uint32_t, Name> |
| 145 | getDelegationFromWire(const Block& block, size_t index); |
| 146 | |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame^] | 147 | /** @brief finds index of a delegation with @p delegationName from @p block |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 148 | * @param block wire format of a Link object |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame^] | 149 | * @param delegationName delegation name in the Link object |
| 150 | * @return 0-based index of the first delegation with @p delegationName , |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 151 | * or -1 if no such delegation exists |
| 152 | */ |
| 153 | static ssize_t |
| 154 | findDelegationFromWire(const Block& block, const Name& delegationName); |
| 155 | |
| 156 | static ssize_t |
| 157 | countDelegationsFromWire(const Block& block); |
| 158 | |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 159 | protected: |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 160 | /** @brief prepend Link object as a Content block to the encoder |
Spyridon Mastorakis | 3b54e85 | 2015-04-07 08:03:25 -0700 | [diff] [blame] | 161 | * |
| 162 | * The outermost Content element is not part of Link object structure. |
| 163 | */ |
| 164 | template<encoding::Tag TAG> |
| 165 | size_t |
| 166 | encodeContent(EncodingImpl<TAG>& encoder) const; |
| 167 | |
| 168 | void |
| 169 | encodeContent(); |
| 170 | |
| 171 | void |
| 172 | decodeContent(); |
| 173 | |
| 174 | private: |
| 175 | bool |
| 176 | removeDelegationNoEncode(const Name& name); |
| 177 | |
| 178 | private: |
| 179 | DelegationSet m_delegations; |
| 180 | }; |
| 181 | |
| 182 | } // namespace ndn |
| 183 | |
| 184 | #endif // NDN_LINK_HPP |