Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 10 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #ifndef NDN_INTEREST_H |
| 14 | #define NDN_INTEREST_H |
| 15 | |
| 16 | #include <ndn-cpp/common.h> |
| 17 | #include <ndn-cpp/fields/name.h> |
| 18 | #include <ndn-cpp/fields/exclude.h> |
| 19 | #include <ndn-cpp/helpers/hash.h> |
| 20 | |
| 21 | namespace ndn { |
| 22 | |
| 23 | /** |
| 24 | * @brief Class abstracting operations with Interests (constructing and getting access to Interest fields) |
| 25 | */ |
| 26 | class Interest |
| 27 | { |
| 28 | public: |
| 29 | /** |
| 30 | * @brief Default constructor, creates an interest for / prefix without any selectors |
| 31 | */ |
| 32 | Interest (); |
| 33 | |
| 34 | /** |
| 35 | * @brief Create an interest for the name |
| 36 | * @param name name of the data to request |
| 37 | */ |
| 38 | Interest (const Name &name); |
| 39 | |
| 40 | /** |
| 41 | * @brief Copy constructor |
| 42 | * @param interest interest to copy |
| 43 | */ |
| 44 | Interest (const Interest &interest); |
| 45 | |
| 46 | /** |
| 47 | * @brief Create an interest based on ccn_parsed_interest data structure |
| 48 | * @param interest pointer to ccn_parsed_interest data structure |
| 49 | * |
| 50 | * This method will create an interest with empty name, since ccn_parsed_interest structure |
| 51 | * has limited amount of information |
| 52 | */ |
| 53 | Interest (const ccn_parsed_interest *interest); |
| 54 | |
| 55 | /** |
| 56 | * @brief Set interest name |
| 57 | * @param name name of the interest |
| 58 | * @return reference to self (to allow method chaining) |
| 59 | * |
| 60 | * In some cases, a direct access to and manipulation of name using getName is more efficient |
| 61 | */ |
| 62 | inline Interest & |
| 63 | setName (const Name &name); |
| 64 | |
| 65 | /** |
| 66 | * @brief Get interest name (const reference) |
| 67 | * @returns name of the interest |
| 68 | */ |
| 69 | inline const Name & |
| 70 | getName () const; |
| 71 | |
| 72 | /** |
| 73 | * @brief Get interest name (reference) |
| 74 | * @returns name of the interest |
| 75 | */ |
| 76 | inline Name & |
| 77 | getName (); |
| 78 | |
| 79 | /** |
| 80 | * @brief Set interest lifetime (time_duration) |
| 81 | * @param interestLifetime interest lifetime specified as a time_duration value. |
| 82 | * Negative value means that InterestLifetime is not set. |
| 83 | * @return reference to self (to allow method chaining) |
| 84 | */ |
| 85 | inline Interest & |
| 86 | setInterestLifetime (const TimeInterval &interestLifetime); |
| 87 | |
| 88 | /** |
| 89 | * @brief Set interest lifetime (double) |
| 90 | * @param interestLifetime interest lifetime expressed in seconds, with possible fractional seconds (double). |
| 91 | * Negative value means that InterestLifetime is not set. |
| 92 | * @return reference to self (to allow method chaining) |
| 93 | */ |
| 94 | inline Interest & |
| 95 | setInterestLifetime (double interestLifetimeSeconds); |
| 96 | |
| 97 | /** |
| 98 | * @brief Get interest lifetime |
| 99 | * @return TimeInterval representing lifetime of the interest. |
| 100 | * Use time_duration::total_seconds () or time_duration::total_microseconds (), |
| 101 | * if you need interest lifetime as a plain number. |
| 102 | * @see http://www.boost.org/doc/libs/1_53_0/doc/html/date_time/posix_time.html |
| 103 | */ |
| 104 | inline const TimeInterval & |
| 105 | getInterestLifetime () const; |
| 106 | |
| 107 | /** |
| 108 | * @brief Set intended interest scope |
| 109 | * @param scope requested scope of the interest @see Scope |
| 110 | * @return reference to self (to allow method chaining) |
| 111 | */ |
| 112 | inline Interest & |
| 113 | setScope (uint8_t scope); |
| 114 | |
| 115 | /** |
| 116 | * @brief Get intended interest scope |
| 117 | * @return intended interest scope @see Scope |
| 118 | */ |
| 119 | inline uint8_t |
| 120 | getScope () const; |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////// |
| 123 | // SELECTORS // |
| 124 | /////////////////////////////////////////////////////////////////////// |
| 125 | |
| 126 | /** |
| 127 | * @brief Enum defining constants for AnswerOriginKind selector field |
| 128 | */ |
| 129 | enum AnswerOriginKind |
| 130 | { |
| 131 | AOK_CS = 0x1, ///< @brief request item from the content store |
| 132 | AOK_NEW = 0x2, ///< @brief request item from the original producer |
| 133 | AOK_DEFAULT = 0x3, ///< @brief default: either from content store or original producer |
| 134 | AOK_STALE = 0x4, ///< @brief Allow stale data |
| 135 | AOK_EXPIRE = 0x10 ///< @brief Allow expired data (?) |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * @brief Enum defining constants for ChildSelector field |
| 140 | */ |
| 141 | enum ChildSelector |
| 142 | { |
| 143 | CHILD_LEFT = 0, ///< @brief request left child |
| 144 | CHILD_RIGHT = 1, ///< @brief request right child |
| 145 | CHILD_DEFAULT = 2 ///< @brief do not specify which child is requested |
| 146 | }; |
| 147 | |
| 148 | /** |
| 149 | * @brief Enum defining constants for Scope field |
| 150 | */ |
| 151 | enum Scope |
| 152 | { |
| 153 | NO_SCOPE = 255, ///< @brief Interest scope is not defined |
| 154 | SCOPE_LOCAL_CCND = 0, ///< @brief Interest scope is only toward local NDN daemon |
| 155 | SCOPE_LOCAL_HOST = 1, ///< @brief Interest scope is within local host (any local application only) |
| 156 | SCOPE_NEXT_HOST = 2 ///< @brief Interest scope is within local host and immediate neighboring node |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * @brief Set interest selector for maximum suffix components |
| 161 | * @param maxSuffixComponents maximum number of suffix components. If Interest::ncomps, then not restricted |
| 162 | * @return reference to self (to allow method chaining) |
| 163 | */ |
| 164 | inline Interest & |
| 165 | setMaxSuffixComponents (uint32_t maxSuffixComponents); |
| 166 | |
| 167 | /** |
| 168 | * \brief Get interest selector for maximum suffix components |
| 169 | * |
| 170 | * MaxSuffixComponents refer to the number of name components beyond those in the prefix, |
| 171 | * and counting the implicit digest, that may occur in the matching ContentObject. |
| 172 | * For more information, see http://www.ccnx.org/releases/latest/doc/technical/InterestMessage.html |
| 173 | **/ |
| 174 | inline uint32_t |
| 175 | getMaxSuffixComponents () const; |
| 176 | |
| 177 | /** |
| 178 | * @brief Set interest selector for minimum suffix components |
| 179 | * @param minSuffixComponents minimum number of suffix components. If Interest::ncomps, then not restricted |
| 180 | * @return reference to self (to allow method chaining) |
| 181 | */ |
| 182 | inline Interest & |
| 183 | setMinSuffixComponents (uint32_t minSuffixComponents); |
| 184 | |
| 185 | /** |
| 186 | * \brief Get interest selector for minimum suffix components |
| 187 | * |
| 188 | * MinSuffixComponents refer to the number of name components beyond those in the prefix, |
| 189 | * and counting the implicit digest, that may occur in the matching ContentObject. |
| 190 | * For more information, see http://www.ccnx.org/releases/latest/doc/technical/InterestMessage.html |
| 191 | **/ |
| 192 | inline uint32_t |
| 193 | getMinSuffixComponents () const; |
| 194 | |
| 195 | /** |
| 196 | * @brief Set interest selector for answer origin kind |
| 197 | * @param answerOriginKind type of answer @see AnswerOriginKind |
| 198 | * @return reference to self (to allow method chaining) |
| 199 | */ |
| 200 | inline Interest & |
| 201 | setAnswerOriginKind (uint32_t answerOriginKind); |
| 202 | |
| 203 | /** |
| 204 | * @brief Get interest selector for answer origin kind |
| 205 | */ |
| 206 | inline uint32_t |
| 207 | getAnswerOriginKind () const; |
| 208 | |
| 209 | /** |
| 210 | * @brief Set interest selector for child selector |
| 211 | * @param child child selector @see ChildSelector |
| 212 | * @return reference to self (to allow method chaining) |
| 213 | * |
| 214 | * Often a given interest will match more than one ContentObject within a given content store. |
| 215 | * The ChildSelector provides a way of expressing a preference for which of these should be returned. |
| 216 | * If the value is false, the leftmost child is preferred. If true, the rightmost child is preferred. |
| 217 | * \see http://www.ccnx.org/releases/latest/doc/technical/InterestMessage.html for more information. |
| 218 | */ |
| 219 | inline Interest & |
| 220 | setChildSelector (uint8_t child); |
| 221 | |
| 222 | /** |
| 223 | * @brief Get interest selector for child selector |
| 224 | */ |
| 225 | inline uint8_t |
| 226 | getChildSelector () const; |
| 227 | |
| 228 | /** |
| 229 | * @brief Set interest selector for publisher public key digest |
| 230 | * @param digest publisher public key digest |
| 231 | * @return reference to self (to allow method chaining) |
| 232 | * |
| 233 | * Currently, this method has no effect |
| 234 | * @todo Implement PublisherPublicKeyDigest |
| 235 | */ |
| 236 | inline Interest & |
| 237 | setPublisherPublicKeyDigest(const Hash &digest); |
| 238 | |
| 239 | /** |
| 240 | * @brief Get interest selector for publisher public key digest |
| 241 | * |
| 242 | * @todo Implement |
| 243 | */ |
| 244 | inline const Hash& |
| 245 | getPublisherPublicKeyDigest () const; |
| 246 | |
| 247 | /** |
| 248 | * @brief Set exclude filter |
| 249 | * @param exclude An exclude filter to set |
| 250 | * |
| 251 | * In some cases, a direct access to and manipulation of exclude filter using getExclude is more efficient |
| 252 | */ |
| 253 | inline void |
| 254 | setExclude (const Exclude &exclude); |
| 255 | |
| 256 | /** |
| 257 | * @brief Get exclude filter (const reference) |
| 258 | */ |
| 259 | inline const Exclude & |
| 260 | getExclude () const; |
| 261 | |
| 262 | /** |
| 263 | * @brief Get exclude filter (reference) |
| 264 | */ |
| 265 | inline Exclude & |
| 266 | getExclude (); |
| 267 | |
| 268 | /////////////////////////////////////////////////////////////////////// |
| 269 | // HELPERS // |
| 270 | /////////////////////////////////////////////////////////////////////// |
| 271 | |
| 272 | /** |
| 273 | * @brief Compare equality of two interests |
| 274 | */ |
| 275 | bool |
| 276 | operator== (const Interest &interest); |
| 277 | |
| 278 | public: |
| 279 | // Data Members (public): |
| 280 | /// @brief Value indicating that number of components parameter is invalid |
| 281 | const static uint32_t ncomps = static_cast<uint32_t> (-1); |
| 282 | |
| 283 | private: |
| 284 | Name m_name; |
| 285 | uint32_t m_maxSuffixComponents; |
| 286 | uint32_t m_minSuffixComponents; |
| 287 | uint32_t m_answerOriginKind; |
| 288 | TimeInterval m_interestLifetime; // lifetime in seconds |
| 289 | |
| 290 | uint8_t m_scope; |
| 291 | uint8_t m_childSelector; |
| 292 | // not used now |
| 293 | Hash m_publisherPublicKeyDigest; |
| 294 | Exclude m_exclude; |
| 295 | |
| 296 | Ptr<Blob> m_wire; |
| 297 | }; |
| 298 | |
| 299 | typedef boost::shared_ptr<Interest> InterestPtr; |
| 300 | |
| 301 | namespace Error |
| 302 | { |
| 303 | /** |
| 304 | * @brief Exception that is thrown in case of error during interest construction or parsing |
| 305 | */ |
| 306 | struct Interest: |
| 307 | virtual boost::exception, virtual std::exception {}; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | inline Interest & |
| 313 | Interest::setName (const Name &name) |
| 314 | { |
| 315 | m_name = name; |
| 316 | return *this; |
| 317 | } |
| 318 | |
| 319 | inline const Name & |
| 320 | Interest::getName () const |
| 321 | { |
| 322 | return m_name; |
| 323 | } |
| 324 | |
| 325 | inline Name & |
| 326 | Interest::getName () |
| 327 | { |
| 328 | return m_name; |
| 329 | } |
| 330 | |
| 331 | inline Interest & |
| 332 | Interest::setInterestLifetime (const TimeInterval &interestLifetime) |
| 333 | { |
| 334 | m_interestLifetime = interestLifetime; |
| 335 | return *this; |
| 336 | } |
| 337 | |
| 338 | inline Interest & |
| 339 | Interest::setInterestLifetime (double interestLifetimeSeconds) |
| 340 | { |
| 341 | m_interestLifetime = time::Seconds (interestLifetimeSeconds); |
| 342 | return *this; |
| 343 | } |
| 344 | |
| 345 | inline const TimeInterval & |
| 346 | Interest::getInterestLifetime () const |
| 347 | { |
| 348 | return m_interestLifetime; |
| 349 | } |
| 350 | |
| 351 | inline Interest & |
| 352 | Interest::setScope (uint8_t scope) |
| 353 | { |
| 354 | m_scope = scope; |
| 355 | return *this; |
| 356 | } |
| 357 | |
| 358 | inline uint8_t |
| 359 | Interest::getScope () const |
| 360 | { |
| 361 | return m_scope; |
| 362 | } |
| 363 | |
| 364 | /////////////////////////////////////////////////////////////////////// |
| 365 | // SELECTORS // |
| 366 | /////////////////////////////////////////////////////////////////////// |
| 367 | |
| 368 | |
| 369 | inline Interest & |
| 370 | Interest::setMaxSuffixComponents (uint32_t maxSuffixComponents) |
| 371 | { |
| 372 | m_maxSuffixComponents = maxSuffixComponents; |
| 373 | return *this; |
| 374 | } |
| 375 | |
| 376 | inline uint32_t |
| 377 | Interest::getMaxSuffixComponents () const |
| 378 | { |
| 379 | return m_maxSuffixComponents; |
| 380 | } |
| 381 | |
| 382 | inline Interest & |
| 383 | Interest::setMinSuffixComponents (uint32_t minSuffixComponents) |
| 384 | { |
| 385 | m_minSuffixComponents = minSuffixComponents; |
| 386 | return *this; |
| 387 | } |
| 388 | |
| 389 | inline uint32_t |
| 390 | Interest::getMinSuffixComponents () const |
| 391 | { |
| 392 | return m_minSuffixComponents; |
| 393 | } |
| 394 | |
| 395 | inline Interest & |
| 396 | Interest::setAnswerOriginKind (uint32_t answerOriginKind) |
| 397 | { |
| 398 | m_answerOriginKind = answerOriginKind; |
| 399 | return *this; |
| 400 | } |
| 401 | |
| 402 | inline uint32_t |
| 403 | Interest::getAnswerOriginKind () const |
| 404 | { |
| 405 | return m_answerOriginKind; |
| 406 | } |
| 407 | |
| 408 | inline Interest & |
| 409 | Interest::setChildSelector (uint8_t childSelector) |
| 410 | { |
| 411 | m_childSelector = childSelector; |
| 412 | return *this; |
| 413 | } |
| 414 | |
| 415 | inline uint8_t |
| 416 | Interest::getChildSelector () const |
| 417 | { |
| 418 | return m_childSelector; |
| 419 | } |
| 420 | |
| 421 | inline Interest & |
| 422 | Interest::setPublisherPublicKeyDigest(const Hash &publisherPublicKeyDigest) |
| 423 | { |
| 424 | m_publisherPublicKeyDigest = publisherPublicKeyDigest; |
| 425 | return *this; |
| 426 | } |
| 427 | |
| 428 | inline const Hash& |
| 429 | Interest::getPublisherPublicKeyDigest () const |
| 430 | { |
| 431 | return m_publisherPublicKeyDigest; |
| 432 | } |
| 433 | |
| 434 | inline void |
| 435 | Interest::setExclude (const Exclude &exclude) |
| 436 | { |
| 437 | m_exclude = exclude; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * @brief Get exclude filter (const reference) |
| 442 | */ |
| 443 | inline const Exclude & |
| 444 | Interest::getExclude () const |
| 445 | { |
| 446 | return m_exclude; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * @brief Get exclude filter (reference) |
| 451 | */ |
| 452 | inline Exclude & |
| 453 | Interest::getExclude () |
| 454 | { |
| 455 | return m_exclude; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | } // ndn |
| 460 | |
| 461 | #endif // NDN_INTEREST_H |