Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [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 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> | ||||
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
6 | */ | ||||
7 | |||||
Jeff Thompson | 71b2f87 | 2013-12-17 12:03:17 -0800 | [diff] [blame] | 8 | #ifndef NDN_KEY_LOCATOR_HPP |
9 | #define NDN_KEY_LOCATOR_HPP | ||||
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 11 | #include "encoding/block.hpp" |
Jeff Thompson | 2e6269c | 2013-08-22 10:36:00 -0700 | [diff] [blame] | 12 | #include "name.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 13 | |
14 | namespace ndn { | ||||
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 15 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 16 | class KeyLocator { |
17 | public: | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 18 | class Error : public std::runtime_error |
19 | { | ||||
20 | public: | ||||
21 | explicit | ||||
22 | Error(const std::string& what) | ||||
23 | : std::runtime_error(what) | ||||
24 | { | ||||
25 | } | ||||
26 | }; | ||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 28 | enum { |
Alexander Afanasyev | 9cae668 | 2014-02-19 14:18:56 -0800 | [diff] [blame] | 29 | KeyLocator_None = 65535, // just an arbitrarily large number (used only internally) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 30 | KeyLocator_Name = 0, |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 32 | KeyLocator_Unknown = 255 |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 33 | }; |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 34 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 35 | KeyLocator() |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 36 | : m_type(KeyLocator_None) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 37 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 38 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 40 | KeyLocator(const Name& name); |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 42 | /////////////////////////////////////////////////////////////////////////////// |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 44 | template<bool T> |
45 | size_t | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 46 | wireEncode(EncodingImpl<T>& block) const; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 47 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 48 | const Block& |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 49 | wireEncode() const; |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 50 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 51 | void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 52 | wireDecode(const Block& wire); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 54 | /////////////////////////////////////////////////////////////////////////////// |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 55 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 56 | bool |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 57 | empty() const |
58 | { | ||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 59 | return m_type == KeyLocator_None; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 60 | } |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 61 | |
62 | uint32_t | ||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 63 | getType() const { return m_type; } |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 64 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 65 | //////////////////////////////////////////////////////// |
66 | // Helper methods for different types of key locators | ||||
67 | // | ||||
68 | // For now only Name type is actually supported | ||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 70 | const Name& |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 71 | getName() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 73 | void |
74 | setName(const Name& name); | ||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 75 | |
76 | public: // EqualityComparable concept | ||||
77 | bool | ||||
78 | operator==(const KeyLocator& other) const; | ||||
79 | |||||
80 | bool | ||||
81 | operator!=(const KeyLocator& other) const; | ||||
82 | |||||
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 83 | private: |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 84 | uint32_t m_type; |
85 | Name m_name; | ||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 87 | mutable Block m_wire; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 88 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 90 | inline |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 91 | KeyLocator::KeyLocator(const Name& name) |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 92 | { |
93 | setName(name); | ||||
94 | } | ||||
95 | |||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 96 | template<bool T> |
97 | inline size_t | ||||
98 | KeyLocator::wireEncode(EncodingImpl<T>& block) const | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 99 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 100 | // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH KeyLocatorValue |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 102 | // KeyLocatorValue ::= Name | |
103 | // KeyLocatorDigest | (not supported yet) | ||||
104 | // ... | ||||
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 106 | // KeyLocatorDigest ::= KEY-LOCATOR-DIGEST-TYPE TLV-LENGTH BYTE+ |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 108 | size_t total_len = 0; |
109 | |||||
110 | switch (m_type) { | ||||
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 111 | case KeyLocator_None: |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 112 | break; |
113 | case KeyLocator_Name: | ||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 114 | total_len += m_name.wireEncode(block); |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 115 | break; |
116 | default: | ||||
117 | throw Error("Unsupported KeyLocator type"); | ||||
118 | } | ||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 119 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 120 | total_len += block.prependVarNumber(total_len); |
121 | total_len += block.prependVarNumber(Tlv::KeyLocator); | ||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 122 | return total_len; |
123 | } | ||||
124 | |||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 125 | inline const Block& |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 126 | KeyLocator::wireEncode() const |
127 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 128 | if (m_wire.hasWire()) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 129 | return m_wire; |
130 | |||||
131 | EncodingEstimator estimator; | ||||
132 | size_t estimatedSize = wireEncode(estimator); | ||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 133 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 134 | EncodingBuffer buffer(estimatedSize, 0); |
135 | wireEncode(buffer); | ||||
136 | |||||
137 | m_wire = buffer.block(); | ||||
138 | return m_wire; | ||||
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 139 | } |
140 | |||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 141 | inline void |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 142 | KeyLocator::wireDecode(const Block& value) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 143 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 144 | if (value.type() != Tlv::KeyLocator) |
145 | throw Error("Unexpected TLV type during KeyLocator decoding"); | ||||
146 | |||||
147 | m_wire = value; | ||||
148 | m_wire.parse(); | ||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 150 | if (!m_wire.elements().empty() && m_wire.elements_begin()->type() == Tlv::Name) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 151 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 152 | m_type = KeyLocator_Name; |
153 | m_name.wireDecode(*m_wire.elements_begin()); | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 154 | } |
155 | else | ||||
156 | { | ||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 157 | m_type = KeyLocator_Unknown; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 158 | } |
159 | } | ||||
160 | |||||
161 | inline const Name& | ||||
162 | KeyLocator::getName() const | ||||
163 | { | ||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 164 | if (m_type != KeyLocator_Name) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 165 | throw Error("Requested Name, but KeyLocator is not of the Name type"); |
166 | |||||
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 167 | return m_name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 168 | } |
169 | |||||
170 | inline void | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 171 | KeyLocator::setName(const Name& name) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 172 | { |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 173 | m_wire.reset(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 174 | m_type = KeyLocator_Name; |
175 | m_name = name; | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 176 | } |
177 | |||||
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 178 | inline bool |
179 | KeyLocator::operator==(const KeyLocator& other) const | ||||
180 | { | ||||
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 181 | return wireEncode() == other.wireEncode(); |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 182 | } |
183 | |||||
184 | inline bool | ||||
185 | KeyLocator::operator!=(const KeyLocator& other) const | ||||
186 | { | ||||
187 | return !this->operator==(other); | ||||
188 | } | ||||
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 189 | |
190 | } // namespace ndn | ||||
191 | |||||
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 192 | #endif |