Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_INTEREST_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 23 | #define NDN_INTEREST_HPP |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 24 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 25 | #include "delegation-list.hpp" |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 26 | #include "link.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 27 | #include "name.hpp" |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 28 | #include "selectors.hpp" |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 29 | #include "tag-host.hpp" |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 30 | #include "util/time.hpp" |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 33 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 34 | class Data; |
| 35 | |
Junxiao Shi | 7007a3c | 2014-11-20 22:37:55 -0700 | [diff] [blame] | 36 | /** @var const unspecified_duration_type DEFAULT_INTEREST_LIFETIME; |
| 37 | * @brief default value for InterestLifetime |
| 38 | */ |
| 39 | const time::milliseconds DEFAULT_INTEREST_LIFETIME = time::milliseconds(4000); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 40 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 41 | /** @brief represents an Interest packet |
Jeff Thompson | 8238d00 | 2013-07-10 11:56:49 -0700 | [diff] [blame] | 42 | */ |
Alexander Afanasyev | a3887ae | 2014-12-29 16:11:57 -0800 | [diff] [blame] | 43 | class Interest : public TagHost, public enable_shared_from_this<Interest> |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 45 | public: |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 46 | class Error : public tlv::Error |
| 47 | { |
| 48 | public: |
| 49 | explicit |
| 50 | Error(const std::string& what) |
| 51 | : tlv::Error(what) |
| 52 | { |
| 53 | } |
| 54 | }; |
| 55 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 56 | /** @brief Create a new Interest with the given name and interest lifetime |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 57 | * @throw std::invalid_argument InterestLifetime is negative |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 58 | * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 59 | * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior. |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 60 | */ |
Junxiao Shi | 909ffef | 2017-07-07 22:12:27 +0000 | [diff] [blame] | 61 | explicit |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 62 | Interest(const Name& name = Name(), time::milliseconds interestLifetime = DEFAULT_INTEREST_LIFETIME); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 64 | /** @brief Create from wire encoding |
| 65 | * @warning In certain contexts that use Interest::shared_from_this(), Interest must be created |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 66 | * using `make_shared`. Otherwise, .shared_from_this() will trigger undefined behavior. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | */ |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 68 | explicit |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 69 | Interest(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 70 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 71 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 72 | * @brief Fast encoding or block size estimation |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 73 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 74 | template<encoding::Tag TAG> |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 75 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 76 | wireEncode(EncodingImpl<TAG>& encoder) const; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 77 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 78 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 79 | * @brief Encode to a wire format |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 80 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 81 | const Block& |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 82 | wireEncode() const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 83 | |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 84 | /** |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 85 | * @brief Decode from the wire format |
Jeff Thompson | 1b4a7b1 | 2013-11-15 12:00:02 -0800 | [diff] [blame] | 86 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 87 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 88 | wireDecode(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * @brief Check if already has wire |
| 92 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 93 | bool |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 94 | hasWire() const |
| 95 | { |
| 96 | return m_wire.hasWire(); |
| 97 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 98 | |
Jeff Thompson | d9e278c | 2013-07-08 15:20:13 -0700 | [diff] [blame] | 99 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 100 | * @brief Encode the name according to the NDN URI Scheme |
| 101 | * |
| 102 | * If there are interest selectors, this method will append "?" and add the selectors as |
| 103 | * a query string. For example, "/test/name?ndn.ChildSelector=1" |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 104 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 105 | std::string |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 106 | toUri() const; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 107 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 108 | public: // matching |
| 109 | /** @brief Check if Interest, including selectors, matches the given @p name |
| 110 | * @param name The name to be matched. If this is a Data name, it shall contain the |
| 111 | * implicit digest component |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 112 | */ |
| 113 | bool |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 114 | matchesName(const Name& name) const; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 116 | /** |
| 117 | * @brief Check if Interest can be satisfied by @p data. |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 118 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 119 | * This method considers Name, MinSuffixComponents, MaxSuffixComponents, |
| 120 | * PublisherPublicKeyLocator, and Exclude. |
| 121 | * This method does not consider ChildSelector and MustBeFresh. |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 122 | */ |
| 123 | bool |
| 124 | matchesData(const Data& data) const; |
| 125 | |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 126 | /** |
| 127 | * @brief Check if Interest matches @p other interest |
| 128 | * |
| 129 | * Interest matches @p other if both have the same name, selectors, and link. Other fields |
| 130 | * (e.g., Nonce) may be different. |
| 131 | * |
| 132 | * @todo Implement distinguishing interests by link. The current implementation checks only |
| 133 | * name+selectors (Issue #3162). |
| 134 | */ |
| 135 | bool |
| 136 | matchesInterest(const Interest& other) const; |
| 137 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 138 | public: // Name, Nonce, and Guiders |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 139 | const Name& |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 140 | getName() const |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 141 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 142 | return m_name; |
| 143 | } |
| 144 | |
| 145 | Interest& |
| 146 | setName(const Name& name) |
| 147 | { |
| 148 | m_name = name; |
| 149 | m_wire.reset(); |
| 150 | return *this; |
Jeff Thompson | c0486c1 | 2013-07-16 14:36:16 -0700 | [diff] [blame] | 151 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 152 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 153 | /** @brief Check if Nonce set |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 154 | */ |
| 155 | bool |
| 156 | hasNonce() const |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 157 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 158 | return static_cast<bool>(m_nonce); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 161 | /** @brief Get nonce |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 162 | * |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 163 | * If nonce was not set before this call, it will be automatically assigned to a random value |
| 164 | */ |
| 165 | uint32_t |
| 166 | getNonce() const; |
| 167 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 168 | /** @brief Set nonce |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 169 | */ |
| 170 | Interest& |
| 171 | setNonce(uint32_t nonce); |
| 172 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 173 | /** @brief Refresh nonce |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 174 | * |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 175 | * It's guaranteed that new nonce value differs from the existing one. |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 176 | * |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 177 | * If nonce is already set, it will be updated to a different random value. |
| 178 | * If nonce is not set, this method does nothing. |
Alexander Afanasyev | c393217 | 2014-07-10 18:53:56 -0700 | [diff] [blame] | 179 | */ |
| 180 | void |
| 181 | refreshNonce(); |
| 182 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 183 | time::milliseconds |
| 184 | getInterestLifetime() const |
| 185 | { |
| 186 | return m_interestLifetime; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @brief Set Interest's lifetime |
| 191 | * @throw std::invalid_argument specified lifetime is < 0 |
| 192 | */ |
| 193 | Interest& |
| 194 | setInterestLifetime(time::milliseconds interestLifetime); |
| 195 | |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 196 | const DelegationList& |
| 197 | getForwardingHint() const |
| 198 | { |
| 199 | return m_forwardingHint; |
| 200 | } |
| 201 | |
| 202 | Interest& |
| 203 | setForwardingHint(const DelegationList& value); |
| 204 | |
Junxiao Shi | b2a7033 | 2017-07-07 22:15:03 +0000 | [diff] [blame] | 205 | /** @brief modify ForwardingHint in-place |
| 206 | * @tparam Modifier a unary function that accepts DelegationList& |
| 207 | * |
| 208 | * This is equivalent to, but more efficient (avoids copying) than: |
| 209 | * @code |
| 210 | * auto fh = interest.getForwardingHint(); |
| 211 | * modifier(fh); |
| 212 | * interest.setForwardingHint(fh); |
| 213 | * @endcode |
| 214 | */ |
| 215 | template<typename Modifier> |
| 216 | Interest& |
| 217 | modifyForwardingHint(const Modifier& modifier) |
| 218 | { |
| 219 | modifier(m_forwardingHint); |
| 220 | m_wire.reset(); |
| 221 | return *this; |
| 222 | } |
| 223 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 224 | public: // Selectors |
| 225 | /** |
| 226 | * @return true if Interest has any selector present |
| 227 | */ |
| 228 | bool |
| 229 | hasSelectors() const |
| 230 | { |
| 231 | return !m_selectors.empty(); |
| 232 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 233 | |
Junxiao Shi | 2af905b | 2014-11-27 13:10:54 -0700 | [diff] [blame] | 234 | const Selectors& |
| 235 | getSelectors() const |
| 236 | { |
| 237 | return m_selectors; |
| 238 | } |
| 239 | |
| 240 | Interest& |
| 241 | setSelectors(const Selectors& selectors) |
| 242 | { |
| 243 | m_selectors = selectors; |
| 244 | m_wire.reset(); |
| 245 | return *this; |
| 246 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 247 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 248 | int |
| 249 | getMinSuffixComponents() const |
| 250 | { |
| 251 | return m_selectors.getMinSuffixComponents(); |
| 252 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 253 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 254 | Interest& |
| 255 | setMinSuffixComponents(int minSuffixComponents) |
| 256 | { |
| 257 | m_selectors.setMinSuffixComponents(minSuffixComponents); |
| 258 | m_wire.reset(); |
| 259 | return *this; |
| 260 | } |
| 261 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 262 | int |
| 263 | getMaxSuffixComponents() const |
| 264 | { |
| 265 | return m_selectors.getMaxSuffixComponents(); |
| 266 | } |
| 267 | |
| 268 | Interest& |
| 269 | setMaxSuffixComponents(int maxSuffixComponents) |
| 270 | { |
| 271 | m_selectors.setMaxSuffixComponents(maxSuffixComponents); |
| 272 | m_wire.reset(); |
| 273 | return *this; |
| 274 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 275 | |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 276 | const KeyLocator& |
| 277 | getPublisherPublicKeyLocator() const |
| 278 | { |
| 279 | return m_selectors.getPublisherPublicKeyLocator(); |
| 280 | } |
| 281 | |
| 282 | Interest& |
| 283 | setPublisherPublicKeyLocator(const KeyLocator& keyLocator) |
| 284 | { |
| 285 | m_selectors.setPublisherPublicKeyLocator(keyLocator); |
| 286 | m_wire.reset(); |
| 287 | return *this; |
| 288 | } |
| 289 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 290 | const Exclude& |
| 291 | getExclude() const |
| 292 | { |
| 293 | return m_selectors.getExclude(); |
| 294 | } |
| 295 | |
| 296 | Interest& |
| 297 | setExclude(const Exclude& exclude) |
| 298 | { |
| 299 | m_selectors.setExclude(exclude); |
| 300 | m_wire.reset(); |
| 301 | return *this; |
| 302 | } |
| 303 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 304 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 305 | getChildSelector() const |
| 306 | { |
| 307 | return m_selectors.getChildSelector(); |
| 308 | } |
| 309 | |
| 310 | Interest& |
| 311 | setChildSelector(int childSelector) |
| 312 | { |
| 313 | m_selectors.setChildSelector(childSelector); |
| 314 | m_wire.reset(); |
| 315 | return *this; |
| 316 | } |
| 317 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 318 | int |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 319 | getMustBeFresh() const |
| 320 | { |
| 321 | return m_selectors.getMustBeFresh(); |
| 322 | } |
| 323 | |
| 324 | Interest& |
| 325 | setMustBeFresh(bool mustBeFresh) |
| 326 | { |
| 327 | m_selectors.setMustBeFresh(mustBeFresh); |
| 328 | m_wire.reset(); |
| 329 | return *this; |
| 330 | } |
| 331 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 332 | public: // Link and SelectedDelegation |
| 333 | /** |
| 334 | * @brief Check whether the Interest contains a Link object |
| 335 | * @return True if there is a link object, otherwise false |
| 336 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 337 | bool |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 338 | hasLink() const; |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 339 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 340 | /** |
| 341 | * @brief Get the link object for this interest |
| 342 | * @return The link object if there is one contained in this interest |
| 343 | * @throws Interest::Error if there is no link object contained in the interest |
| 344 | * @throws tlv::Error if the incorporated link object is malformed |
| 345 | */ |
| 346 | const Link& |
| 347 | getLink() const; |
| 348 | |
| 349 | /** |
| 350 | * @brief Set the link object for this interest |
| 351 | * @param link The link object that will be included in this interest (in wire format) |
| 352 | * @post !hasSelectedDelegation() |
| 353 | */ |
| 354 | void |
| 355 | setLink(const Block& link); |
| 356 | |
| 357 | /** |
| 358 | * @brief Delete the link object for this interest |
| 359 | * @post !hasLink() |
| 360 | */ |
| 361 | void |
| 362 | unsetLink(); |
| 363 | |
| 364 | /** |
| 365 | * @brief Check whether the Interest includes a selected delegation |
| 366 | * @return True if there is a selected delegation, otherwise false |
| 367 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 368 | bool |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 369 | hasSelectedDelegation() const; |
| 370 | |
| 371 | /** |
| 372 | * @brief Get the name of the selected delegation |
| 373 | * @return The name of the selected delegation |
| 374 | * @throw Error SelectedDelegation is not set. |
| 375 | */ |
| 376 | Name |
| 377 | getSelectedDelegation() const; |
| 378 | |
| 379 | /** |
| 380 | * @brief Set the selected delegation |
| 381 | * @param delegationName The name of the selected delegation |
| 382 | * @throw Error Link is not set. |
| 383 | * @throw std::invalid_argument @p delegationName does not exist in Link. |
| 384 | */ |
| 385 | void |
| 386 | setSelectedDelegation(const Name& delegationName); |
| 387 | |
| 388 | /** |
| 389 | * @brief Set the selected delegation |
| 390 | * @param delegationIndex The index of the selected delegation |
| 391 | * @throw Error Link is not set. |
| 392 | * @throw std::out_of_range @p delegationIndex is out of bound in Link. |
| 393 | */ |
| 394 | void |
| 395 | setSelectedDelegation(size_t delegationIndex); |
| 396 | |
| 397 | /** |
| 398 | * @brief Unset the selected delegation |
| 399 | */ |
| 400 | void |
| 401 | unsetSelectedDelegation(); |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 402 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 403 | private: |
| 404 | Name m_name; |
| 405 | Selectors m_selectors; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 406 | mutable optional<uint32_t> m_nonce; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 407 | time::milliseconds m_interestLifetime; |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 408 | DelegationList m_forwardingHint; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 409 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 410 | mutable Block m_link; |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 411 | mutable shared_ptr<Link> m_linkCached; |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 412 | size_t m_selectedDelegationIndex; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 413 | mutable Block m_wire; |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 414 | }; |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 415 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 416 | std::ostream& |
| 417 | operator<<(std::ostream& os, const Interest& interest); |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 418 | |
| 419 | inline std::string |
| 420 | Interest::toUri() const |
| 421 | { |
| 422 | std::ostringstream os; |
| 423 | os << *this; |
| 424 | return os.str(); |
| 425 | } |
| 426 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 427 | inline bool |
| 428 | operator==(const Interest& lhs, const Interest& rhs) |
| 429 | { |
| 430 | return lhs.wireEncode() == rhs.wireEncode(); |
| 431 | } |
| 432 | |
| 433 | inline bool |
| 434 | operator!=(const Interest& lhs, const Interest& rhs) |
| 435 | { |
| 436 | return !(lhs == rhs); |
| 437 | } |
| 438 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 439 | } // namespace ndn |
| 440 | |
| 441 | #endif // NDN_INTEREST_HPP |