All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
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  uint64_t
164  getCongestionMark() const;
165 
175  ptr_lib::shared_ptr<Name>
176  getFullName(WireFormat& wireFormat = *WireFormat::getDefaultWireFormat()) const;
177 
182  const SignedBlob&
184  {
185  if (getDefaultWireEncodingChangeCount_ != getChangeCount()) {
186  // The values have changed, so the default wire encoding is invalidated.
187  // This method can be called on a const object, but we want to be able to update the default cached value.
188  const_cast<Data*>(this)->defaultWireEncoding_ = SignedBlob();
189  const_cast<Data*>(this)->defaultWireEncodingFormat_ = 0;
190  const_cast<Data*>(this)->getDefaultWireEncodingChangeCount_ = getChangeCount();
191  }
192 
193  return defaultWireEncoding_;
194  }
195 
201  WireFormat*
202  getDefaultWireEncodingFormat() const { return defaultWireEncodingFormat_; }
203 
209  Data&
210  setSignature(const Signature& signature)
211  {
212  signature_.set(signature.clone());
213  ++changeCount_;
214  return *this;
215  }
216 
222  virtual Data&
223  setName(const Name& name);
224 
230  Data&
231  setMetaInfo(const MetaInfo& metaInfo)
232  {
233  metaInfo_.set(metaInfo);
234  ++changeCount_;
235  return *this;
236  }
237 
243  Data&
244  setContent(const std::vector<uint8_t>& content)
245  {
246  return setContent(Blob(content));
247  }
248 
249  Data&
250  setContent(const uint8_t* content, size_t contentLength)
251  {
252  return setContent(Blob(content, contentLength));
253  }
254 
255  Data&
256  setContent(const Blob& content)
257  {
258  content_ = content;
259  ++changeCount_;
260  return *this;
261  }
262 
270  Data&
271  setLpPacket(const ptr_lib::shared_ptr<LpPacket>& lpPacket)
272  {
273  lpPacket_ = lpPacket;
274  // Don't update changeCount_ since this doesn't affect the wire encoding.
275  return *this;
276  }
277 
282  uint64_t
284  {
285  // Make sure each of the checkChanged is called.
286  bool changed = signature_.checkChanged();
287  changed = name_.checkChanged() || changed;
288  changed = metaInfo_.checkChanged() || changed;
289  if (changed)
290  // A child object has changed, so update the change count.
291  // This method can be called on a const object, but we want to be able to update the changeCount_.
292  ++const_cast<Data*>(this)->changeCount_;
293 
294  return changeCount_;
295  }
296 
297 private:
298  void
299  setDefaultWireEncoding
300  (const SignedBlob& defaultWireEncoding,
301  WireFormat *defaultWireEncodingFormat)
302  {
303  defaultWireEncoding_ = defaultWireEncoding;
304  defaultWireEncodingFormat_ = defaultWireEncodingFormat;
305  // Set getDefaultWireEncodingChangeCount_ so that the next call to
306  // getDefaultWireEncoding() won't clear defaultWireEncoding_.
307  getDefaultWireEncodingChangeCount_ = getChangeCount();
308  }
309 
310  SharedPointerChangeCounter<Signature> signature_;
311  ChangeCounter<Name> name_;
312  ChangeCounter<MetaInfo> metaInfo_;
313  Blob content_;
314  SignedBlob defaultWireEncoding_;
315  WireFormat *defaultWireEncodingFormat_;
316  ptr_lib::shared_ptr<Name> defaultFullName_;
317  uint64_t getDefaultWireEncodingChangeCount_;
318  ptr_lib::shared_ptr<LpPacket> lpPacket_;
319  uint64_t changeCount_;
320 };
321 
322 }
323 
324 #endif
virtual ~Data()
The virtual destructor.
Definition: data.cpp:71
void set(const DataLite &dataLite)
Clear this data object, and set the values by copying from dataLite.
Definition: data.cpp:146
Data()
Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignatur...
Definition: data.cpp:39
Definition: data.hpp:37
uint64_t getIncomingFaceId() const
Get the incoming face ID according to the incoming packet header.
Definition: data.cpp:92
Data & setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.hpp:210
virtual ptr_lib::shared_ptr< Signature > clone() const =0
Return a pointer to a new Signature which is a copy of this signature.
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:178
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<uint8_t>.
Definition: blob.hpp:42
virtual Data & setName(const Name &name)
Set name to a copy of the given Name.
Definition: data.cpp:170
uint64_t getCongestionMark() const
Get the congestion mark according to the incoming packet header.
Definition: data.cpp:102
WireFormat * getDefaultWireEncodingFormat() const
Get the WireFormat which is used by getDefaultWireEncoding().
Definition: data.hpp:202
Data & setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.hpp:231
Data & operator=(const Data &data)
The assignment operator: Copy fields and make a clone of the signature.
Definition: data.cpp:75
Data & setContent(const std::vector< uint8_t > &content)
Set the content to a copy of the data in the vector.
Definition: data.hpp:244
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:197
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:112
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:283
Data & setLpPacket(const ptr_lib::shared_ptr< LpPacket > &lpPacket)
An internal library method to set the LpPacket for an incoming packet.
Definition: data.hpp:271
const SignedBlob & getDefaultWireEncoding() const
Return a reference to the defaultWireEncoding, which was encoded with getDefaultWireEncodingFormat()...
Definition: data.hpp:183