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 
163  const SignedBlob&
165  {
166  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
167  // The values have changed, so the default wire encoding is invalidated.
168  // This method can be called on a const object, but we want to be able to update the default cached value.
169  const_cast<Data*>(this)->defaultWireEncoding_ = SignedBlob();
170  const_cast<Data*>(this)->defaultWireEncodingFormat_ = 0;
171  const_cast<Data*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
172  }
173 
174  return defaultWireEncoding_;
175  }
176 
182  WireFormat*
183  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
184 
190  Data&
191  setSignature(const Signature& signature)
192  {
193  signature_.set(signature.clone());
194  ++changeCount_;
195  return *this;
196  }
197 
203  virtual Data&
204  setName(const Name& name);
205 
211  Data&
212  setMetaInfo(const MetaInfo& metaInfo)
213  {
214  metaInfo_.set(metaInfo);
215  ++changeCount_;
216  return *this;
217  }
218 
224  Data&
225  setContent(const std::vector<uint8_t>& content)
226  {
227  return setContent(Blob(content));
228  }
229 
230  Data&
231  setContent(const uint8_t* content, size_t contentLength)
232  {
233  return setContent(Blob(content, contentLength));
234  }
235 
236  Data&
237  setContent(const Blob& content)
238  {
239  content_ = content;
240  ++changeCount_;
241  return *this;
242  }
243 
251  Data&
252  setLpPacket(const ptr_lib::shared_ptr<LpPacket>& lpPacket)
253  {
254  lpPacket_ = lpPacket;
255  // Don't update changeCount_ since this doesn't affect the wire encoding.
256  return *this;
257  }
258 
263  uint64_t
265  {
266  // Make sure each of the checkChanged is called.
267  bool changed = signature_.checkChanged();
268  changed = name_.checkChanged() || changed;
269  changed = metaInfo_.checkChanged() || changed;
270  if (changed)
271  // A child object has changed, so update the change count.
272  // This method can be called on a const object, but we want to be able to update the changeCount_.
273  ++const_cast<Data*>(this)->changeCount_;
274 
275  return changeCount_;
276  }
277 
278 private:
279  void
280  setDefaultWireEncoding
281  (const SignedBlob& defaultWireEncoding,
282  WireFormat *defaultWireEncodingFormat)
283  {
284  defaultWireEncoding_ = defaultWireEncoding;
285  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
286  // Set getDefaultWireEncodingChangeCount_ so that the next call to
287  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
288  getDefaultWireEncodingChangeCount_ = getChangeCount();
289  }
290 
291  SharedPointerChangeCounter<Signature> signature_;
292  ChangeCounter<Name> name_;
293  ChangeCounter<MetaInfo> metaInfo_;
294  Blob content_;
295  SignedBlob defaultWireEncoding_;
296  WireFormat *defaultWireEncodingFormat_;
297  uint64_t getDefaultWireEncodingChangeCount_;
298  ptr_lib::shared_ptr<LpPacket> lpPacket_;
299  uint64_t changeCount_;
300 };
301 
302 }
303 
304 #endif
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
virtual ~Data()
The virtual destructor.
Definition: data.cpp:58
void set(const DataLite &dataLite)
Clear this data object, and set the values by copying from dataLite.
Definition: data.cpp:98
Data()
Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignatur...
Definition: data.cpp:37
Definition: data.hpp:37
uint64_t getIncomingFaceId() const
Get the incoming face ID according to the incoming packet header.
Definition: data.cpp:79
Data & setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.hpp:191
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:130
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:122
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: data.hpp:183
Data & setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.hpp:212
Data & operator=(const Data &data)
The assignment operator: Copy fields and make a clone of the signature.
Definition: data.cpp:62
Data & setContent(const std::vector< uint8_t > &content)
Set the content to a copy of the data in the vector.
Definition: data.hpp:225
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:149
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
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:264
Data & setLpPacket(const ptr_lib::shared_ptr< LpPacket > &lpPacket)
An internal library method to set the LpPacket for an incoming packet.
Definition: data.hpp:252
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: data.hpp:164