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 Data {
36 public:
40  Data();
41 
46  Data(const Name& name);
47 
52  Data(const Data& data);
53 
57  virtual ~Data();
58 
64  Data& operator=(const Data& data);
65 
75 
82  virtual void
83  wireDecode(const Blob& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
84 
92  void
94  (const uint8_t* input, size_t inputLength,
96  {
97  wireDecode(Blob(input, inputLength), wireFormat);
98  }
99 
106  void
107  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
108  {
109  wireDecode(&input[0], input.size(), wireFormat);
110  }
111 
120  void
121  get(DataLite& dataLite) const;
122 
127  void
128  set(const DataLite& dataLite);
129 
130  const Signature*
131  getSignature() const { return signature_.get(); }
132 
133  Signature*
134  getSignature() { return signature_.get(); }
135 
136  const Name&
137  getName() const { return name_.get(); }
138 
139  Name&
140  getName() { return name_.get(); }
141 
142  const MetaInfo&
143  getMetaInfo() const { return metaInfo_.get(); }
144 
145  MetaInfo& getMetaInfo() { return metaInfo_.get(); }
146 
147  const Blob&
148  getContent() const { return content_; }
149 
154  const SignedBlob&
156  {
157  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
158  // The values have changed, so the default wire encoding is invalidated.
159  // This method can be called on a const object, but we want to be able to update the default cached value.
160  const_cast<Data*>(this)->defaultWireEncoding_ = SignedBlob();
161  const_cast<Data*>(this)->defaultWireEncodingFormat_ = 0;
162  const_cast<Data*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
163  }
164 
165  return defaultWireEncoding_;
166  }
167 
173  WireFormat*
174  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
175 
181  Data&
182  setSignature(const Signature& signature)
183  {
184  signature_.set(signature.clone());
185  ++changeCount_;
186  return *this;
187  }
188 
194  virtual Data&
195  setName(const Name& name);
196 
202  Data&
203  setMetaInfo(const MetaInfo& metaInfo)
204  {
205  metaInfo_.set(metaInfo);
206  ++changeCount_;
207  return *this;
208  }
209 
215  Data&
216  setContent(const std::vector<uint8_t>& content)
217  {
218  return setContent(Blob(content));
219  }
220 
221  Data&
222  setContent(const uint8_t* content, size_t contentLength)
223  {
224  return setContent(Blob(content, contentLength));
225  }
226 
227  Data&
228  setContent(const Blob& content)
229  {
230  content_ = content;
231  ++changeCount_;
232  return *this;
233  }
234 
239  uint64_t
241  {
242  // Make sure each of the checkChanged is called.
243  bool changed = signature_.checkChanged();
244  changed = name_.checkChanged() || changed;
245  changed = metaInfo_.checkChanged() || changed;
246  if (changed)
247  // A child object has changed, so update the change count.
248  // This method can be called on a const object, but we want to be able to update the changeCount_.
249  ++const_cast<Data*>(this)->changeCount_;
250 
251  return changeCount_;
252  }
253 
254 private:
255  void
256  setDefaultWireEncoding
257  (const SignedBlob& defaultWireEncoding,
258  WireFormat *defaultWireEncodingFormat)
259  {
260  defaultWireEncoding_ = defaultWireEncoding;
261  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
262  // Set getDefaultWireEncodingChangeCount_ so that the next call to
263  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
264  getDefaultWireEncodingChangeCount_ = getChangeCount();
265  }
266 
267  SharedPointerChangeCounter<Signature> signature_;
268  ChangeCounter<Name> name_;
269  ChangeCounter<MetaInfo> metaInfo_;
270  Blob content_;
271  SignedBlob defaultWireEncoding_;
272  WireFormat *defaultWireEncodingFormat_;
273  uint64_t getDefaultWireEncodingChangeCount_;
274  uint64_t changeCount_;
275 };
276 
277 }
278 
279 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
virtual ~Data()
The virtual destructor.
Definition: data.cpp:55
void set(const DataLite &dataLite)
Clear this data object, and set the values by copying from dataLite.
Definition: data.cpp:85
Data()
Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignatur...
Definition: data.cpp:34
Definition: data.hpp:35
Data & setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.hpp:182
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:113
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:105
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: data.hpp:174
Data & setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.hpp:203
Data & operator=(const Data &data)
The assignment operator: Copy fields and make a clone of the signature.
Definition: data.cpp:59
Data & setContent(const std::vector< uint8_t > &content)
Set the content to a copy of the data in the vector.
Definition: data.hpp:216
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:132
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:36
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:107
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:240
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: data.hpp:155