data.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_DATA_HPP
23 #define NDN_DATA_HPP
24 
25 #include "name.hpp"
26 #include "signature.hpp"
27 #include "meta-info.hpp"
28 #include "util/signed-blob.hpp"
29 #include "encoding/wire-format.hpp"
30 #include "util/change-counter.hpp"
31 #include "lite/data-lite.hpp"
32 
33 namespace ndn {
34 
35 class LpPacket;
36 
37 class Data {
38 public:
42  Data();
43 
48  Data(const Name& name);
49 
54  Data(const Data& data);
55 
59  virtual ~Data();
60 
66  Data& operator=(const Data& data);
67 
77 
84  virtual void
85  wireDecode(const Blob& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
86 
94  void
96  (const uint8_t* input, size_t inputLength,
98  {
99  wireDecode(Blob(input, inputLength), wireFormat);
100  }
101 
108  void
109  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
110  {
111  wireDecode(&input[0], input.size(), wireFormat);
112  }
113 
122  void
123  get(DataLite& dataLite) const;
124 
129  void
130  set(const DataLite& dataLite);
131 
132  const Signature*
133  getSignature() const { return signature_.get(); }
134 
135  Signature*
136  getSignature() { return signature_.get(); }
137 
138  const Name&
139  getName() const { return name_.get(); }
140 
141  Name&
142  getName() { return name_.get(); }
143 
144  const MetaInfo&
145  getMetaInfo() const { return metaInfo_.get(); }
146 
147  MetaInfo& getMetaInfo() { return metaInfo_.get(); }
148 
149  const Blob&
150  getContent() const { return content_; }
151 
156  uint64_t
157  getIncomingFaceId() const;
158 
168  ptr_lib::shared_ptr<Name>
169  getFullName(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
170 
175  const SignedBlob&
177  {
178  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
179  // The values have changed, so the default wire encoding is invalidated.
180  // This method can be called on a const object, but we want to be able to update the default cached value.
181  const_cast<Data*>(this)->defaultWireEncoding_ = SignedBlob();
182  const_cast<Data*>(this)->defaultWireEncodingFormat_ = 0;
183  const_cast<Data*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
184  }
185 
186  return defaultWireEncoding_;
187  }
188 
194  WireFormat*
195  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
196 
202  Data&
203  setSignature(const Signature& signature)
204  {
205  signature_.set(signature.clone());
206  ++changeCount_;
207  return *this;
208  }
209 
215  virtual Data&
216  setName(const Name& name);
217 
223  Data&
224  setMetaInfo(const MetaInfo& metaInfo)
225  {
226  metaInfo_.set(metaInfo);
227  ++changeCount_;
228  return *this;
229  }
230 
236  Data&
237  setContent(const std::vector<uint8_t>& content)
238  {
239  return setContent(Blob(content));
240  }
241 
242  Data&
243  setContent(const uint8_t* content, size_t contentLength)
244  {
245  return setContent(Blob(content, contentLength));
246  }
247 
248  Data&
249  setContent(const Blob& content)
250  {
251  content_ = content;
252  ++changeCount_;
253  return *this;
254  }
255 
263  Data&
264  setLpPacket(const ptr_lib::shared_ptr<LpPacket>& lpPacket)
265  {
266  lpPacket_ = lpPacket;
267  // Don't update changeCount_ since this doesn't affect the wire encoding.
268  return *this;
269  }
270 
275  uint64_t
277  {
278  // Make sure each of the checkChanged is called.
279  bool changed = signature_.checkChanged();
280  changed = name_.checkChanged() || changed;
281  changed = metaInfo_.checkChanged() || changed;
282  if (changed)
283  // A child object has changed, so update the change count.
284  // This method can be called on a const object, but we want to be able to update the changeCount_.
285  ++const_cast<Data*>(this)->changeCount_;
286 
287  return changeCount_;
288  }
289 
290 private:
291  void
292  setDefaultWireEncoding
293  (const SignedBlob& defaultWireEncoding,
294  WireFormat *defaultWireEncodingFormat)
295  {
296  defaultWireEncoding_ = defaultWireEncoding;
297  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
298  // Set getDefaultWireEncodingChangeCount_ so that the next call to
299  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
300  getDefaultWireEncodingChangeCount_ = getChangeCount();
301  }
302 
303  SharedPointerChangeCounter<Signature> signature_;
304  ChangeCounter<Name> name_;
305  ChangeCounter<MetaInfo> metaInfo_;
306  Blob content_;
307  SignedBlob defaultWireEncoding_;
308  WireFormat *defaultWireEncodingFormat_;
309  ptr_lib::shared_ptr<Name> defaultFullName_;
310  uint64_t getDefaultWireEncodingChangeCount_;
311  ptr_lib::shared_ptr<LpPacket> lpPacket_;
312  uint64_t changeCount_;
313 };
314 
315 }
316 
317 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:36
virtual ~Data()
The virtual destructor.
Definition: data.cpp:70
void set(const DataLite &dataLite)
Clear this data object, and set the values by copying from dataLite.
Definition: data.cpp:135
Data()
Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignatur...
Definition: data.cpp:38
Definition: data.hpp:37
uint64_t getIncomingFaceId() const
Get the incoming face ID according to the incoming packet header.
Definition: data.cpp:91
Data & setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.hpp:203
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
SignedBlob wireEncode(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Encode this Data for a particular wire format.
Definition: data.cpp:167
A Signature is an abstract base class providing methods to work with the signature information in a D...
Definition: signature.hpp:35
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
virtual Data & setName(const Name &name)
Set name to a copy of the given Name.
Definition: data.cpp:159
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: data.hpp:195
Data & setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.hpp:224
Data & operator=(const Data &data)
The assignment operator: Copy fields and make a clone of the signature.
Definition: data.cpp:74
Data & setContent(const std::vector< uint8_t > &content)
Set the content to a copy of the data in the vector.
Definition: data.hpp:237
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:34
virtual void wireDecode(const Blob &input, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Decode the input using a particular wire format and update this Data.
Definition: data.cpp:186
virtual ptr_lib::shared_ptr< Signature > clone() const =0
Return a pointer to a new Signature which is a copy of this signature.
A SignedBlob extends Blob to keep the offsets of a signed portion (e.g., the bytes of Data packet)...
Definition: signed-blob.hpp:34
Definition: wire-format.hpp:39
ptr_lib::shared_ptr< Name > getFullName(WireFormat &wireFormat=*WireFormat::getDefaultWireFormat()) const
Get the Data packet's full name, which includes the final ImplicitSha256Digest component based on the...
Definition: data.cpp:101
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:35
void wireDecode(const std::vector< uint8_t > &input, WireFormat &wireFormat=*WireFormat::getDefaultWireFormat())
Decode the input using a particular wire format and update this Data.
Definition: data.hpp:109
A DataLite holds a NameLite and other fields to represent an NDN Data packet.
Definition: data-lite.hpp:34
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: data.hpp:276
Data & setLpPacket(const ptr_lib::shared_ptr< LpPacket > &lpPacket)
An internal library method to set the LpPacket for an incoming packet.
Definition: data.hpp:264
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: data.hpp:176