Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Jeff Thompson | 71b2f87 | 2013-12-17 12:03:17 -0800 | [diff] [blame] | 22 | #ifndef NDN_KEY_LOCATOR_HPP |
| 23 | #define NDN_KEY_LOCATOR_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 25 | #include "encoding/encoding-buffer.hpp" |
Jeff Thompson | 2e6269c | 2013-08-22 10:36:00 -0700 | [diff] [blame] | 26 | #include "name.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 30 | class KeyLocator |
| 31 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 32 | public: |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 33 | class Error : public std::runtime_error |
| 34 | { |
| 35 | public: |
| 36 | explicit |
| 37 | Error(const std::string& what) |
| 38 | : std::runtime_error(what) |
| 39 | { |
| 40 | } |
| 41 | }; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 43 | enum Type { |
| 44 | /** \brief indicates KeyLocator is empty (internal use only) |
| 45 | */ |
| 46 | KeyLocator_None = 65535, |
| 47 | /** \brief indicates KeyLocator contains a Name |
| 48 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 49 | KeyLocator_Name = 0, |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 50 | /** \brief indicates KeyLocator contains a KeyDigest |
| 51 | */ |
| 52 | KeyLocator_KeyDigest = 1, |
| 53 | /** \brief indicates KeyLocator contains an unknown element |
| 54 | */ |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 55 | KeyLocator_Unknown = 255 |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 56 | }; |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 57 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 58 | public: // constructors |
| 59 | /** \brief construct an empty KeyLocator |
| 60 | */ |
| 61 | KeyLocator(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 62 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 63 | /** \brief construct from wire encoding |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 64 | */ |
| 65 | explicit |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 66 | KeyLocator(const Block& wire); |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 67 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 68 | /** \brief construct from Name |
| 69 | * \note implicit conversion is permitted |
| 70 | */ |
| 71 | KeyLocator(const Name& name); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 72 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 73 | public: // encode and decode |
| 74 | /** \brief prepend wire encoding |
| 75 | * \param block EncodingBuffer or Estimator |
| 76 | */ |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 77 | template<bool T> |
| 78 | size_t |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 79 | wireEncode(EncodingImpl<T>& block) const; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 80 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 81 | /** \return wire encoding |
| 82 | */ |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 83 | const Block& |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 84 | wireEncode() const; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 86 | /** \brief decode from wire encoding |
| 87 | * \throw Error outer TLV type is not KeyLocator |
| 88 | * \note No error is thrown for unrecognized inner TLV, but type becomes KeyLocator_Unknown. |
| 89 | */ |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 90 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 91 | wireDecode(const Block& wire); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 93 | public: // attributes |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 94 | bool |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 95 | empty() const |
| 96 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 97 | return m_type == KeyLocator_None; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 98 | } |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 99 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 100 | Type |
| 101 | getType() const |
| 102 | { |
| 103 | return m_type; |
| 104 | } |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 106 | /** \brief clear KeyLocator |
| 107 | * \details type becomes KeyLocator_None |
| 108 | * \return self |
| 109 | */ |
| 110 | KeyLocator& |
| 111 | clear(); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 112 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 113 | /** \brief get Name element |
| 114 | * \throw Error if type is not KeyLocator_Name |
| 115 | */ |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 116 | const Name& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 117 | getName() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 118 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 119 | /** \brief set Name element |
| 120 | * \details type becomes KeyLocator_Name |
| 121 | * \return self |
| 122 | */ |
| 123 | KeyLocator& |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 124 | setName(const Name& name); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 125 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 126 | /** \brief get KeyDigest element |
| 127 | * \throw Error if type is not KeyLocator_KeyDigest |
| 128 | */ |
| 129 | const Block& |
| 130 | getKeyDigest() const; |
| 131 | |
| 132 | /** \brief set KeyDigest element |
| 133 | * \details type becomes KeyLocator_KeyDigest |
| 134 | * \throw Error if Block type is not KeyDigest |
| 135 | * \return self |
| 136 | */ |
| 137 | KeyLocator& |
| 138 | setKeyDigest(const Block& keyDigest); |
| 139 | |
| 140 | /** \brief set KeyDigest value |
| 141 | * \details type becomes KeyLocator_KeyDigest |
| 142 | * \return self |
| 143 | */ |
| 144 | KeyLocator& |
| 145 | setKeyDigest(const ConstBufferPtr& keyDigest); |
| 146 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 147 | public: // EqualityComparable concept |
| 148 | bool |
| 149 | operator==(const KeyLocator& other) const; |
| 150 | |
| 151 | bool |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 152 | operator!=(const KeyLocator& other) const |
| 153 | { |
| 154 | return !this->operator==(other); |
| 155 | } |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 156 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 157 | private: |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 158 | Type m_type; |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 159 | Name m_name; |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 160 | Block m_keyDigest; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 161 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 162 | mutable Block m_wire; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 163 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 164 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 165 | } // namespace ndn |
| 166 | |
Junxiao Shi | bc5030d | 2014-09-01 11:53:12 -0700 | [diff] [blame^] | 167 | #endif // NDN_KEY_LOCATOR_HPP |