data.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
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.
20  */
21 
22 #ifndef NDN_CXX_DATA_HPP
23 #define NDN_CXX_DATA_HPP
24 
27 #include "ndn-cxx/meta-info.hpp"
28 #include "ndn-cxx/name.hpp"
31 
32 namespace ndn {
33 
34 class Signature;
35 
39 class Data : public PacketBase, public std::enable_shared_from_this<Data>
40 {
41 public:
42  class Error : public tlv::Error
43  {
44  public:
45  using tlv::Error::Error;
46  };
47 
53  explicit
54  Data(const Name& name = Name());
55 
62  explicit
63  Data(const Block& wire);
64 
73  template<encoding::Tag TAG>
74  size_t
75  wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly = false) const;
76 
93  const Block&
94  wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const;
95 
99  const Block&
100  wireEncode() const;
101 
104  void
105  wireDecode(const Block& wire);
106 
109  bool
110  hasWire() const noexcept
111  {
112  return m_wire.hasWire();
113  }
114 
119  const Name&
120  getFullName() const;
121 
122 public: // Data fields
125  const Name&
126  getName() const noexcept
127  {
128  return m_name;
129  }
130 
134  Data&
135  setName(const Name& name);
136 
139  const MetaInfo&
140  getMetaInfo() const noexcept
141  {
142  return m_metaInfo;
143  }
144 
148  Data&
149  setMetaInfo(const MetaInfo& metaInfo);
150 
154  bool
155  hasContent() const noexcept
156  {
157  return m_content.isValid();
158  }
159 
171  const Block&
172  getContent() const noexcept
173  {
174  return m_content;
175  }
176 
185  Data&
186  setContent(const Block& block);
187 
194  Data&
195  setContent(const uint8_t* value, size_t length);
196 
202  Data&
203  setContent(ConstBufferPtr value);
204 
210  Data&
211  unsetContent();
212 
216  [[deprecated("use getSignatureInfo and getSignatureValue")]]
217  Signature
218  getSignature() const;
219 
224  [[deprecated("use setSignatureInfo and setSignatureValue")]]
225  Data&
226  setSignature(const Signature& signature);
227 
230  const SignatureInfo&
231  getSignatureInfo() const noexcept
232  {
233  return m_signatureInfo;
234  }
235 
245  Data&
246  setSignatureInfo(const SignatureInfo& info);
247 
250  const Block&
251  getSignatureValue() const noexcept
252  {
253  return m_signatureValue;
254  }
255 
265  Data&
267 
272  InputBuffers
273  extractSignedRanges() const;
274 
275 public: // MetaInfo fields
276  uint32_t
278  {
279  return m_metaInfo.getType();
280  }
281 
282  Data&
283  setContentType(uint32_t type);
284 
285  time::milliseconds
287  {
288  return m_metaInfo.getFreshnessPeriod();
289  }
290 
291  Data&
292  setFreshnessPeriod(time::milliseconds freshnessPeriod);
293 
294  const optional<name::Component>&
296  {
297  return m_metaInfo.getFinalBlock();
298  }
299 
300  Data&
301  setFinalBlock(optional<name::Component> finalBlockId);
302 
303 public: // SignatureInfo fields
307  int32_t
308  getSignatureType() const noexcept
309  {
310  return m_signatureInfo.getSignatureType();
311  }
312 
315  optional<KeyLocator>
316  getKeyLocator() const noexcept
317  {
318  return m_signatureInfo.hasKeyLocator() ? make_optional(m_signatureInfo.getKeyLocator()) : nullopt;
319  }
320 
321 protected:
325  void
326  resetWire();
327 
328 private:
329  Name m_name;
330  MetaInfo m_metaInfo;
331  Block m_content;
332  SignatureInfo m_signatureInfo;
333  Block m_signatureValue;
334 
335  mutable Block m_wire;
336  mutable Name m_fullName;
337 };
338 
339 #ifndef DOXYGEN
340 extern template size_t
341 Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const;
342 
343 extern template size_t
344 Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const;
345 #endif
346 
347 std::ostream&
348 operator<<(std::ostream& os, const Data& data);
349 
350 bool
351 operator==(const Data& lhs, const Data& rhs);
352 
353 inline bool
354 operator!=(const Data& lhs, const Data& rhs)
355 {
356  return !(lhs == rhs);
357 }
358 
359 } // namespace ndn
360 
361 #endif // NDN_CXX_DATA_HPP
const Block & wireEncode() const
Encode into a Block.
Definition: data.cpp:108
Data & setContentType(uint32_t type)
Definition: data.cpp:343
Definition: data.cpp:26
Represents a SignatureInfo or InterestSignatureInfo TLV element.
void wireDecode(const Block &wire)
Decode from wire.
Definition: data.cpp:124
Data & setSignatureValue(ConstBufferPtr value)
Set SignatureValue.
Definition: data.cpp:318
Data & setSignature(const Signature &signature)
Set Signature.
Definition: data.cpp:301
Data & setName(const Name &name)
Set name.
Definition: data.cpp:227
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:383
Data(const Name &name=Name())
Construct an unsigned Data packet with given name and empty Content.
Definition: data.cpp:35
Data & setContent(const Block &block)
Set Content from a Block.
Definition: data.cpp:245
void resetWire()
Clear wire encoding and cached FullName.
Definition: data.cpp:220
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
optional< KeyLocator > getKeyLocator() const noexcept
Get KeyLocator.
Definition: data.hpp:316
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:354
const Block & getSignatureValue() const noexcept
Get SignatureValue.
Definition: data.hpp:251
bool hasWire() const noexcept
Check if this instance has cached wire encoding.
Definition: data.hpp:110
int32_t getSignatureType() const noexcept
Get SignatureType.
Definition: data.hpp:308
Data & setSignatureInfo(const SignatureInfo &info)
Set SignatureInfo.
Definition: data.cpp:310
InputBuffers extractSignedRanges() const
Extract ranges of Data covered by the signature.
Definition: data.cpp:330
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:58
base class to allow simple management of packet tags
Definition: packet-base.hpp:31
const Name & getName() const noexcept
Get name.
Definition: data.hpp:126
Represents an absolute name.
Definition: name.hpp:44
const MetaInfo & getMetaInfo() const noexcept
Get MetaInfo.
Definition: data.hpp:140
const Name & getFullName() const
Get full name including implicit digest.
Definition: data.cpp:206
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:353
Data & setFinalBlock(optional< name::Component > finalBlockId)
Definition: data.cpp:363
const Block & getContent() const noexcept
Get the Content element.
Definition: data.hpp:172
Data & unsetContent()
Remove the Content element.
Definition: data.cpp:287
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:373
Data & setMetaInfo(const MetaInfo &metaInfo)
Set MetaInfo.
Definition: data.cpp:237
time::milliseconds getFreshnessPeriod() const
Definition: data.hpp:286
Represents a Data packet.
Definition: data.hpp:39
bool hasContent() const noexcept
Return whether this Data has a Content element.
Definition: data.hpp:155
uint32_t getContentType() const
Definition: data.hpp:277
const optional< name::Component > & getFinalBlock() const
Definition: data.hpp:295
Signature getSignature() const
Get Signature.
Definition: data.cpp:295
EncodingImpl< EncoderTag > EncodingBuffer
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
EncodingImpl< EstimatorTag > EncodingEstimator
const SignatureInfo & getSignatureInfo() const noexcept
Get SignatureInfo.
Definition: data.hpp:231
Holds SignatureInfo and SignatureValue in a Data packet.
Definition: signature.hpp:38
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126