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 
32 struct ndn_Data;
33 
34 namespace ndn {
35 
36 class Data {
37 public:
41  Data();
42 
47  Data(const Name& name);
48 
53  Data(const Data& data);
54 
58  virtual ~Data();
59 
65  Data& operator=(const Data& data);
66 
76 
83  virtual void
84  wireDecode(const Blob& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat());
85 
93  void
95  (const uint8_t* input, size_t inputLength,
97  {
98  wireDecode(Blob(input, inputLength), wireFormat);
99  }
100 
107  void
108  wireDecode(const std::vector<uint8_t>& input, WireFormat& wireFormat = *WireFormat::getDefaultWireFormat())
109  {
110  wireDecode(&input[0], input.size(), wireFormat);
111  }
112 
118  void
119  get(struct ndn_Data& dataStruct) const;
120 
125  void
126  set(const struct ndn_Data& dataStruct);
127 
128  const Signature*
129  getSignature() const { return signature_.get(); }
130 
131  Signature*
132  getSignature() { return signature_.get(); }
133 
134  const Name&
135  getName() const { return name_.get(); }
136 
137  Name&
138  getName() { return name_.get(); }
139 
140  const MetaInfo&
141  getMetaInfo() const { return metaInfo_.get(); }
142 
143  MetaInfo& getMetaInfo() { return metaInfo_.get(); }
144 
145  const Blob&
146  getContent() const { return content_; }
147 
152  const SignedBlob&
154  {
155  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
156  // The values have changed, so the default wire encoding is invalidated.
157  // This method can be called on a const object, but we want to be able to update the default cached value.
158  const_cast<Data*>(this)->defaultWireEncoding_ = SignedBlob();
159  const_cast<Data*>(this)->defaultWireEncodingFormat_ = 0;
160  const_cast<Data*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
161  }
162 
163  return defaultWireEncoding_;
164  }
165 
171  WireFormat*
172  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
173 
179  Data&
180  setSignature(const Signature& signature)
181  {
182  signature_.set(signature.clone());
183  ++changeCount_;
184  return *this;
185  }
186 
192  virtual Data&
193  setName(const Name& name);
194 
200  Data&
201  setMetaInfo(const MetaInfo& metaInfo)
202  {
203  metaInfo_.set(metaInfo);
204  ++changeCount_;
205  return *this;
206  }
207 
213  Data&
214  setContent(const std::vector<uint8_t>& content)
215  {
216  return setContent(Blob(content));
217  }
218 
219  Data&
220  setContent(const uint8_t* content, size_t contentLength)
221  {
222  return setContent(Blob(content, contentLength));
223  }
224 
225  Data&
226  setContent(const Blob& content)
227  {
228  content_ = content;
229  ++changeCount_;
230  return *this;
231  }
232 
237  uint64_t
239  {
240  // Make sure each of the checkChanged is called.
241  bool changed = signature_.checkChanged();
242  changed = name_.checkChanged() || changed;
243  changed = metaInfo_.checkChanged() || changed;
244  if (changed)
245  // A child object has changed, so update the change count.
246  // This method can be called on a const object, but we want to be able to update the changeCount_.
247  ++const_cast<Data*>(this)->changeCount_;
248 
249  return changeCount_;
250  }
251 
252 private:
253  void
254  setDefaultWireEncoding
255  (const SignedBlob& defaultWireEncoding,
256  WireFormat *defaultWireEncodingFormat)
257  {
258  defaultWireEncoding_ = defaultWireEncoding;
259  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
260  // Set getDefaultWireEncodingChangeCount_ so that the next call to
261  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
262  getDefaultWireEncodingChangeCount_ = getChangeCount();
263  }
264 
265  SharedPointerChangeCounter<Signature> signature_;
266  ChangeCounter<Name> name_;
267  ChangeCounter<MetaInfo> metaInfo_;
268  Blob content_;
269  SignedBlob defaultWireEncoding_;
270  WireFormat *defaultWireEncodingFormat_;
271  uint64_t getDefaultWireEncodingChangeCount_;
272  uint64_t changeCount_;
273 };
274 
275 }
276 
277 #endif
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
virtual ~Data()
The virtual destructor.
Definition: data.cpp:55
Data()
Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignatur...
Definition: data.cpp:34
Definition: data.hpp:36
Data & setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.hpp:180
Definition: data-types.h:86
struct ndn_Blob content
A Blob with a pointer to the content.
Definition: data-types.h:90
void set(const struct ndn_Data &dataStruct)
Clear this data object, and set the values by copying from the ndn_Data struct.
Definition: data.cpp:85
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:42
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:36
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:172
Data & setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.hpp:201
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:214
static WireFormat * getDefaultWireFormat()
Return the default WireFormat used by default encoding and decoding methods which was set with setDef...
Definition: wire-format.cpp:36
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:37
A MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:36
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:108
uint64_t getChangeCount() const
Get the change count, which is incremented each time this object (or a child object) is changed...
Definition: data.hpp:238
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: data.hpp:153