NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
key-locator.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "key-locator.hpp"
24 
25 namespace ndn {
26 
27 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<KeyLocator>));
28 BOOST_CONCEPT_ASSERT((WireEncodable<KeyLocator>));
30 BOOST_CONCEPT_ASSERT((WireDecodable<KeyLocator>));
31 static_assert(std::is_base_of<tlv::Error, KeyLocator::Error>::value,
32  "KeyLocator::Error must inherit from tlv::Error");
33 
35  : m_type(KeyLocator_None)
36 {
37 }
38 
40 {
41  wireDecode(wire);
42 }
43 
45 {
46  setName(name);
47 }
48 
49 template<encoding::Tag TAG>
50 size_t
52 {
53  // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH (Name | KeyDigest)
54  // KeyDigest ::= KEY-DIGEST-TYPE TLV-LENGTH BYTE+
55 
56  size_t totalLength = 0;
57 
58  switch (m_type) {
59  case KeyLocator_None:
60  break;
61  case KeyLocator_Name:
62  totalLength += m_name.wireEncode(encoder);
63  break;
65  totalLength += encoder.prependBlock(m_keyDigest);
66  break;
67  default:
68  BOOST_THROW_EXCEPTION(Error("Unsupported KeyLocator type"));
69  }
70 
71  totalLength += encoder.prependVarNumber(totalLength);
72  totalLength += encoder.prependVarNumber(tlv::KeyLocator);
73  return totalLength;
74 }
75 
76 template size_t
77 KeyLocator::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
78 
79 template size_t
80 KeyLocator::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
81 
82 const Block&
84 {
85  if (m_wire.hasWire())
86  return m_wire;
87 
88  EncodingEstimator estimator;
89  size_t estimatedSize = wireEncode(estimator);
90 
91  EncodingBuffer buffer(estimatedSize, 0);
92  wireEncode(buffer);
93 
94  m_wire = buffer.block();
95  return m_wire;
96 }
97 
98 void
100 {
101  if (wire.type() != tlv::KeyLocator)
102  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type during KeyLocator decoding"));
103 
104  m_wire = wire;
105  m_wire.parse();
106 
107  if (m_wire.elements().empty()) {
108  m_type = KeyLocator_None;
109  return;
110  }
111 
112  switch (m_wire.elements_begin()->type()) {
113  case tlv::Name:
114  m_type = KeyLocator_Name;
115  m_name.wireDecode(*m_wire.elements_begin());
116  break;
117  case tlv::KeyDigest:
118  m_type = KeyLocator_KeyDigest;
119  m_keyDigest = *m_wire.elements_begin();
120  break;
121  default:
122  m_type = KeyLocator_Unknown;
123  break;
124  }
125 }
126 
127 KeyLocator&
129 {
130  m_wire.reset();
131  m_type = KeyLocator_None;
132  m_name.clear();
133  m_keyDigest.reset();
134  return *this;
135 }
136 
137 const Name&
139 {
140  if (m_type != KeyLocator_Name)
141  BOOST_THROW_EXCEPTION(Error("KeyLocator type is not Name"));
142 
143  return m_name;
144 }
145 
146 KeyLocator&
148 {
149  this->clear();
150  m_type = KeyLocator_Name;
151  m_name = name;
152  return *this;
153 }
154 
155 const Block&
157 {
158  if (m_type != KeyLocator_KeyDigest)
159  BOOST_THROW_EXCEPTION(Error("KeyLocator type is not KeyDigest"));
160 
161  return m_keyDigest;
162 }
163 
164 KeyLocator&
166 {
167  if (keyDigest.type() != tlv::KeyDigest)
168  BOOST_THROW_EXCEPTION(Error("expecting KeyDigest block"));
169 
170  this->clear();
171  m_type = KeyLocator_KeyDigest;
172  m_keyDigest = keyDigest;
173  return *this;
174 }
175 
176 KeyLocator&
178 {
179  // WARNING: ConstBufferPtr is shared_ptr<const Buffer>
180  // This function takes a constant reference of a shared pointer.
181  // It MUST NOT change the reference count of that shared pointer.
182 
183  return this->setKeyDigest(makeBinaryBlock(tlv::KeyDigest, keyDigest->get(), keyDigest->size()));
184 }
185 
186 bool
188 {
189  return wireEncode() == other.wireEncode();
190 }
191 
192 } // namespace ndn
element_const_iterator elements_begin() const
Definition: block.cpp:589
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
Copyright (c) 2011-2015 Regents of the University of California.
EncodingImpl< EstimatorTag > EncodingEstimator
const Block & getKeyDigest() const
get KeyDigest element
KeyLocator & setName(const Name &name)
set Name element
const Name & getName() const
get Name element
void wireDecode(const Block &wire)
decode from wire encoding
Definition: key-locator.cpp:99
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
KeyLocator & setKeyDigest(const Block &keyDigest)
set KeyDigest element
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
KeyLocator()
construct an empty KeyLocator
Definition: key-locator.cpp:34
indicates KeyLocator contains a Name
Definition: key-locator.hpp:49
const Block & wireEncode() const
Definition: key-locator.cpp:83
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
EncodingImpl< EncoderTag > EncodingBuffer
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block type type with value from a buffer value of size length.
indicates KeyLocator is empty (internal use only)
Definition: key-locator.hpp:46
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
Name abstraction to represent an absolute name.
Definition: name.hpp:46
indicates KeyLocator contains an unknown element
Definition: key-locator.hpp:55
KeyLocator & clear()
clear KeyLocator
size_t wireEncode(EncodingImpl< TAG > &encoder) const
prepend wire encoding
Definition: key-locator.cpp:51
indicates KeyLocator contains a KeyDigest
Definition: key-locator.hpp:52
const element_container & elements() const
Get all subelements.
Definition: block.hpp:342
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
Definition: name.cpp:122
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
void wireDecode(const Block &wire)
Definition: name.cpp:161
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
void clear()
Clear all the components.
Definition: name.hpp:210
bool operator==(const KeyLocator &other) const
uint32_t type() const
Definition: block.hpp:324