All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
key-locator.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
8 #ifndef NDN_KEY_LOCATOR_HPP
9 #define NDN_KEY_LOCATOR_HPP
10 
11 #include "encoding/block.hpp"
12 #include "name.hpp"
13 
14 namespace ndn {
15 
17 {
18 public:
19  class Error : public std::runtime_error
20  {
21  public:
22  explicit
23  Error(const std::string& what)
24  : std::runtime_error(what)
25  {
26  }
27  };
28 
29  enum {
30  KeyLocator_None = 65535, // just an arbitrarily large number (used only internally)
32 
34  };
35 
37  : m_type(KeyLocator_None)
38  {
39  }
40 
41  KeyLocator(const Name& name)
42  {
43  setName(name);
44  }
45 
49  explicit
50  KeyLocator(const Block& wire)
51  {
52  wireDecode(wire);
53  }
54 
56 
57  template<bool T>
58  size_t
59  wireEncode(EncodingImpl<T>& block) const;
60 
61  const Block&
62  wireEncode() const;
63 
64  void
65  wireDecode(const Block& wire);
66 
68 
69  bool
70  empty() const
71  {
72  return m_type == KeyLocator_None;
73  }
74 
75  uint32_t
76  getType() const { return m_type; }
77 
79  // Helper methods for different types of key locators
80  //
81  // For now only Name type is actually supported
82 
83  const Name&
84  getName() const;
85 
86  void
87  setName(const Name& name);
88 
89 public: // EqualityComparable concept
90  bool
91  operator==(const KeyLocator& other) const;
92 
93  bool
94  operator!=(const KeyLocator& other) const;
95 
96 private:
97  uint32_t m_type;
98  Name m_name;
99 
100  mutable Block m_wire;
101 };
102 
103 template<bool T>
104 inline size_t
106 {
107  // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH KeyLocatorValue
108 
109  // KeyLocatorValue ::= Name |
110  // KeyLocatorDigest | (not supported yet)
111  // ...
112 
113  // KeyLocatorDigest ::= KEY-LOCATOR-DIGEST-TYPE TLV-LENGTH BYTE+
114 
115  size_t totalLength = 0;
116 
117  switch (m_type) {
118  case KeyLocator_None:
119  break;
120  case KeyLocator_Name:
121  totalLength += m_name.wireEncode(block);
122  break;
123  default:
124  throw Error("Unsupported KeyLocator type");
125  }
126 
127  totalLength += block.prependVarNumber(totalLength);
128  totalLength += block.prependVarNumber(Tlv::KeyLocator);
129  return totalLength;
130 }
131 
132 inline const Block&
134 {
135  if (m_wire.hasWire())
136  return m_wire;
137 
138  EncodingEstimator estimator;
139  size_t estimatedSize = wireEncode(estimator);
140 
141  EncodingBuffer buffer(estimatedSize, 0);
142  wireEncode(buffer);
143 
144  m_wire = buffer.block();
145  return m_wire;
146 }
147 
148 inline void
150 {
151  if (value.type() != Tlv::KeyLocator)
152  throw Error("Unexpected TLV type during KeyLocator decoding");
153 
154  m_wire = value;
155  m_wire.parse();
156 
157  if (!m_wire.elements().empty() && m_wire.elements_begin()->type() == Tlv::Name)
158  {
159  m_type = KeyLocator_Name;
160  m_name.wireDecode(*m_wire.elements_begin());
161  }
162  else
163  {
164  m_type = KeyLocator_Unknown;
165  }
166 }
167 
168 inline const Name&
170 {
171  if (m_type != KeyLocator_Name)
172  throw Error("Requested Name, but KeyLocator is not of the Name type");
173 
174  return m_name;
175 }
176 
177 inline void
179 {
180  m_wire.reset();
181  m_type = KeyLocator_Name;
182  m_name = name;
183 }
184 
185 inline bool
187 {
188  return wireEncode() == other.wireEncode();
189 }
190 
191 inline bool
193 {
194  return !this->operator==(other);
195 }
196 
197 } // namespace ndn
198 
199 #endif
void wireDecode(const Block &wire)
void setName(const Name &name)
const element_container & elements() const
Get all subelements.
Definition: block.hpp:464
Class representing wire element of the NDN packet.
Definition: block.hpp:26
Error(const std::string &what)
Definition: key-locator.hpp:23
const Name & getName() const
element_const_iterator elements_begin() const
Definition: block.hpp:470
bool empty() const
Definition: key-locator.hpp:70
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: name.hpp:711
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
uint32_t type() const
Definition: block.hpp:320
const Block & wireEncode() const
uint32_t getType() const
Definition: key-locator.hpp:76
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
size_t wireEncode(EncodingImpl< T > &block) const
bool operator==(const KeyLocator &other) const
void wireDecode(const Block &wire)
Definition: name.hpp:746
bool operator!=(const KeyLocator &other) const
KeyLocator(const Name &name)
Definition: key-locator.hpp:41
Class representing wire element of the NDN packet.
KeyLocator(const Block &wire)
Create from wire encoding.
Definition: key-locator.hpp:50