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 "ndn-cxx/meta-info.hpp"
26 #include "ndn-cxx/name.hpp"
27 #include "ndn-cxx/signature.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 
230 protected:
234  void
235  resetWire();
236 
237 private:
238  Name m_name;
239  MetaInfo m_metaInfo;
240  Block m_content;
241  Signature m_signature;
242 
243  mutable Block m_wire;
244  mutable Name m_fullName;
245 };
246 
247 #ifndef DOXYGEN
248 extern template size_t
249 Data::wireEncode<encoding::EncoderTag>(EncodingBuffer&, bool) const;
250 
251 extern template size_t
252 Data::wireEncode<encoding::EstimatorTag>(EncodingEstimator&, bool) const;
253 #endif
254 
255 std::ostream&
256 operator<<(std::ostream& os, const Data& data);
257 
258 bool
259 operator==(const Data& lhs, const Data& rhs);
260 
261 inline bool
262 operator!=(const Data& lhs, const Data& rhs)
263 {
264  return !(lhs == rhs);
265 }
266 
267 } // namespace ndn
268 
269 #endif // NDN_DATA_HPP
Data & setContentType(uint32_t type)
Definition: data.cpp:288
Definition: data.cpp:26
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:272
uint32_t getContentType() const
Definition: data.hpp:204
Data & setName(const Name &name)
Set name.
Definition: data.cpp:216
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:321
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:241
const Signature & getSignature() const
Get Signature.
Definition: data.hpp:185
void resetWire()
Clear wire encoding and cached FullName.
Definition: data.cpp:209
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:262
Data & setSignatureValue(const Block &value)
Set SignatureValue.
Definition: data.cpp:280
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:43
const Block & wireEncode() const
Encode to a Block.
Definition: data.cpp:106
const Name & getName() const
Get name.
Definition: data.hpp:124
Data & setFreshnessPeriod(time::milliseconds freshnessPeriod)
Definition: data.cpp:296
Data & setFinalBlock(optional< name::Component > finalBlockId)
Definition: data.cpp:304
const Block & getContent() const
Get Content.
Definition: data.cpp:232
bool operator==(const Data &lhs, const Data &rhs)
Definition: data.cpp:312
Data & setMetaInfo(const MetaInfo &metaInfo)
Set MetaInfo.
Definition: data.cpp:224
const Name & getFullName() const
Get full name including implicit digest.
Definition: data.cpp:195
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
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator
Holds SignatureInfo and SignatureValue in a Data packet.
Definition: signature.hpp:37
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126