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