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-2018 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_DATA_HPP
23 #define NDN_DATA_HPP
24 
25 #include "meta-info.hpp"
26 #include "name.hpp"
27 #include "packet-base.hpp"
28 #include "signature.hpp"
29 #include "encoding/block.hpp"
30 
31 namespace ndn {
32 
35 class Data : public PacketBase, public std::enable_shared_from_this<Data>
36 {
37 public:
38  class Error : public tlv::Error
39  {
40  public:
41  using tlv::Error::Error;
42  };
43 
48  explicit
49  Data(const Name& name = Name());
50 
57  explicit
58  Data(const Block& wire);
59 
67  template<encoding::Tag TAG>
68  size_t
69  wireEncode(EncodingImpl<TAG>& encoder, bool wantUnsignedPortionOnly = false) const;
70 
87  const Block&
88  wireEncode(EncodingBuffer& encoder, const Block& signatureValue) const;
89 
97  const Block&
98  wireEncode() const;
99 
102  void
103  wireDecode(const Block& wire);
104 
107  bool
108  hasWire() const
109  {
110  return m_wire.hasWire();
111  }
112 
117  const Name&
118  getFullName() const;
119 
120 public: // Data fields
123  const Name&
124  getName() const
125  {
126  return m_name;
127  }
128 
132  Data&
133  setName(const Name& name);
134 
137  const MetaInfo&
138  getMetaInfo() const
139  {
140  return m_metaInfo;
141  }
142 
146  Data&
147  setMetaInfo(const MetaInfo& metaInfo);
148 
154  const Block&
155  getContent() const;
156 
164  Data&
165  setContent(const Block& block);
166 
172  Data&
173  setContent(const uint8_t* value, size_t valueSize);
174 
179  Data&
180  setContent(ConstBufferPtr value);
181 
184  const Signature&
185  getSignature() const
186  {
187  return m_signature;
188  }
189 
193  Data&
194  setSignature(const Signature& signature);
195 
199  Data&
200  setSignatureValue(const Block& value);
201 
202 public: // MetaInfo fields
203  uint32_t
205  {
206  return m_metaInfo.getType();
207  }
208 
209  Data&
210  setContentType(uint32_t type);
211 
212  time::milliseconds
214  {
215  return m_metaInfo.getFreshnessPeriod();
216  }
217 
218  Data&
219  setFreshnessPeriod(time::milliseconds freshnessPeriod);
220 
221  const optional<name::Component>&
223  {
224  return m_metaInfo.getFinalBlock();
225  }
226 
227  Data&
228  setFinalBlock(optional<name::Component> finalBlockId);
229 
233  [[deprecated("use getFinalBlock")]]
235  getFinalBlockId() const;
236 
240  [[deprecated("use setFinalBlock")]]
241  Data&
242  setFinalBlockId(const name::Component& finalBlockId);
243 
244 protected:
248  void
249  resetWire();
250 
251 private:
252  Name m_name;
253  MetaInfo m_metaInfo;
254  Block m_content;
255  Signature m_signature;
256 
257  mutable Block m_wire;
258  mutable Name m_fullName;
259 };
260 
261 #ifndef DOXYGEN
262 extern template size_t
263 Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const;
264 
265 extern template size_t
266 Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const;
267 #endif
268 
269 std::ostream&
270 operator<<(std::ostream& os, const Data& data);
271 
272 bool
273 operator==(const Data& lhs, const Data& rhs);
274 
275 inline bool
276 operator!=(const Data& lhs, const Data& rhs)
277 {
278  return !(lhs == rhs);
279 }
280 
281 } // namespace ndn
282 
283 #endif // NDN_DATA_HPP
Data & setContentType(uint32_t type)
Definition: data.cpp:290
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:65
void wireDecode(const Block &wire)
Decode from wire in NDN Packet Format v0.2 or v0.3.
Definition: data.cpp:122
time::milliseconds getFreshnessPeriod() const
Definition: data.hpp:213
Data & setSignature(const Signature &signature)
Set Signature.
Definition: data.cpp:274
uint32_t getContentType() const
Definition: data.hpp:204
Data & setName(const Name &name)
Set name.
Definition: data.cpp:218
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:337
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:243
const Signature & getSignature() const
Get Signature.
Definition: data.hpp:185
void resetWire()
Clear wire encoding and cached FullName.
Definition: data.cpp:211
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
const MetaInfo & getMetaInfo() const
Get MetaInfo.
Definition: data.hpp:138
const optional< name::Component > & getFinalBlock() const
Definition: data.hpp:222
bool operator!=(const Data &lhs, const Data &rhs)
Definition: data.hpp:276
Data & setSignatureValue(const Block &value)
Set SignatureValue.
Definition: data.cpp:282
name::Component getFinalBlockId() const
Definition: data.cpp:314
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
Represents an absolute name.
Definition: name.hpp:42
const Block & wireEncode() const
Encode to a Block.
Definition: data.cpp:106
const Name & getName() const
Get name.
Definition: data.hpp:124
Represents a name component.
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:298
Data & setFinalBlock(optional< name::Component > finalBlockId)
Definition: data.cpp:306
const Block & getContent() const
Get Content.
Definition: data.cpp:234
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:328
Data & setMetaInfo(const MetaInfo &metaInfo)
Set MetaInfo.
Definition: data.cpp:226
const Name & getFullName() const
Get full name including implicit digest.
Definition: data.cpp:197
Data & setFinalBlockId(const name::Component &finalBlockId)
Definition: data.cpp:320
Represents a Data packet.
Definition: data.hpp:35
bool hasWire() const
Check if this instance has cached wire encoding.
Definition: data.hpp:108
EncodingImpl< EncoderTag > EncodingBuffer
represents an error in TLV encoding or decoding
EncodingImpl< EstimatorTag > EncodingEstimator
Holds SignatureInfo and SignatureValue in a Data packet.
Definition: signature.hpp:37
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126