All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
data.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
7 #ifndef NDN_DATA_HPP
8 #define NDN_DATA_HPP
9 
10 #include "common.hpp"
11 #include "name.hpp"
12 #include "encoding/block.hpp"
13 
14 #include "signature.hpp"
15 #include "meta-info.hpp"
16 #include "key-locator.hpp"
18 
19 namespace ndn {
20 
21 class Data : public enable_shared_from_this<Data>
22 {
23 public:
24  class Error : public std::runtime_error
25  {
26  public:
27  explicit
28  Error(const std::string& what)
29  : std::runtime_error(what)
30  {
31  }
32  };
33 
37  Data();
38 
43  Data(const Name& name);
44 
48  explicit
49  Data(const Block& wire)
50  {
51  wireDecode(wire);
52  }
53 
57  ~Data();
58 
62  template<bool T>
63  size_t
64  wireEncode(EncodingImpl<T>& block, bool unsignedPortion = false) const;
65 
69  const Block&
70  wireEncode() const;
71 
75  void
76  wireDecode(const Block& wire);
77 
81  bool
82  hasWire() const;
83 
85 
86  const Name&
87  getName() const;
88 
95  void
96  setName(const Name& name);
97 
98  //
99 
100  const MetaInfo&
101  getMetaInfo() const;
102 
108  void
109  setMetaInfo(const MetaInfo& metaInfo);
110 
111  //
112 
116  // MetaInfo proxy methods
117 
118  uint32_t
119  getContentType() const;
120 
121  void
122  setContentType(uint32_t type);
123 
124  //
125 
126  const time::milliseconds&
127  getFreshnessPeriod() const;
128 
129  void
130  setFreshnessPeriod(const time::milliseconds& freshnessPeriod);
131 
132  //
133 
134  const name::Component&
135  getFinalBlockId() const;
136 
137  void
138  setFinalBlockId(const name::Component& finalBlockId);
139 
140  //
144 
151  const Block&
152  getContent() const;
153 
159  void
160  setContent(const uint8_t* content, size_t contentLength);
161 
162  void
163  setContent(const Block& content);
164 
165  void
166  setContent(const ConstBufferPtr& contentValue);
167 
168  //
169 
170  const Signature&
171  getSignature() const;
172 
177  void
178  setSignature(const Signature& signature);
179 
180  void
181  setSignatureValue(const Block& value);
182 
184 
187 
189  getLocalControlHeader() const;
190 
191  uint64_t
192  getIncomingFaceId() const;
193 
194  void
195  setIncomingFaceId(uint64_t incomingFaceId);
196 
197 public: // EqualityComparable concept
198  bool
199  operator==(const Data& other) const;
200 
201  bool
202  operator!=(const Data& other) const;
203 
204 private:
208  void
209  onChanged();
210 
211 private:
212  Name m_name;
213  MetaInfo m_metaInfo;
214  mutable Block m_content;
215  Signature m_signature;
216 
217  mutable Block m_wire;
218 
219  nfd::LocalControlHeader m_localControlHeader;
221 };
222 
223 inline
225  : m_content(Tlv::Content) // empty content
226 {
227 }
228 
229 inline
230 Data::Data(const Name& name)
231  : m_name(name)
232 {
233 }
234 
235 inline
237 {
238 }
239 
240 template<bool T>
241 inline size_t
242 Data::wireEncode(EncodingImpl<T>& block, bool unsignedPortion/* = false*/) const
243 {
244  size_t totalLength = 0;
245 
246  // Data ::= DATA-TLV TLV-LENGTH
247  // Name
248  // MetaInfo
249  // Content
250  // Signature
251 
252  // (reverse encoding)
253 
254  if (!unsignedPortion && !m_signature)
255  {
256  throw Error("Requested wire format, but data packet has not been signed yet");
257  }
258 
259  if (!unsignedPortion)
260  {
261  // SignatureValue
262  totalLength += prependBlock(block, m_signature.getValue());
263  }
264 
265  // SignatureInfo
266  totalLength += prependBlock(block, m_signature.getInfo());
267 
268  // Content
269  totalLength += prependBlock(block, getContent());
270 
271  // MetaInfo
272  totalLength += getMetaInfo().wireEncode(block);
273 
274  // Name
275  totalLength += getName().wireEncode(block);
276 
277  if (!unsignedPortion)
278  {
279  totalLength += block.prependVarNumber (totalLength);
280  totalLength += block.prependVarNumber (Tlv::Data);
281  }
282  return totalLength;
283 }
284 
285 inline const Block&
287 {
288  if (m_wire.hasWire())
289  return m_wire;
290 
291  EncodingEstimator estimator;
292  size_t estimatedSize = wireEncode(estimator);
293 
294  EncodingBuffer buffer(estimatedSize, 0);
295  wireEncode(buffer);
296 
297  const_cast<Data*>(this)->wireDecode(buffer.block());
298  return m_wire;
299 }
300 
305 inline void
307 {
308  m_wire = wire;
309  m_wire.parse();
310 
311  // Data ::= DATA-TLV TLV-LENGTH
312  // Name
313  // MetaInfo
314  // Content
315  // Signature
316 
317  // Name
318  m_name.wireDecode(m_wire.get(Tlv::Name));
319 
320  // MetaInfo
321  m_metaInfo.wireDecode(m_wire.get(Tlv::MetaInfo));
322 
323  // Content
324  m_content = m_wire.get(Tlv::Content);
325 
327  // Signature //
329 
330  // SignatureInfo
331  m_signature.setInfo(m_wire.get(Tlv::SignatureInfo));
332 
333  // SignatureValue
335  if (val != m_wire.elements_end())
336  m_signature.setValue(*val);
337 }
338 
339 inline bool
341 {
342  return m_wire.hasWire();
343 }
344 
345 inline const Name&
347 {
348  return m_name;
349 }
350 
351 inline void
352 Data::setName(const Name& name)
353 {
354  onChanged();
355  m_name = name;
356 }
357 
358 inline const MetaInfo&
360 {
361  return m_metaInfo;
362 }
363 
364 inline void
365 Data::setMetaInfo(const MetaInfo& metaInfo)
366 {
367  onChanged();
368  m_metaInfo = metaInfo;
369 }
370 
371 inline uint32_t
373 {
374  return m_metaInfo.getType();
375 }
376 
377 inline void
378 Data::setContentType(uint32_t type)
379 {
380  onChanged();
381  m_metaInfo.setType(type);
382 }
383 
384 inline const time::milliseconds&
386 {
387  return m_metaInfo.getFreshnessPeriod();
388 }
389 
390 inline void
391 Data::setFreshnessPeriod(const time::milliseconds& freshnessPeriod)
392 {
393  onChanged();
394  m_metaInfo.setFreshnessPeriod(freshnessPeriod);
395 }
396 
397 inline const name::Component&
399 {
400  return m_metaInfo.getFinalBlockId();
401 }
402 
403 inline void
405 {
406  onChanged();
407  m_metaInfo.setFinalBlockId(finalBlockId);
408 }
409 
410 inline const Block&
412 {
413  if (m_content.empty())
414  m_content = dataBlock(Tlv::Content, reinterpret_cast<const uint8_t*>(0), 0);
415 
416  if (!m_content.hasWire())
417  m_content.encode();
418  return m_content;
419 }
420 
421 inline void
422 Data::setContent(const uint8_t* content, size_t contentLength)
423 {
424  onChanged();
425 
426  m_content = dataBlock(Tlv::Content, content, contentLength);
427 }
428 
429 inline void
430 Data::setContent(const ConstBufferPtr& contentValue)
431 {
432  onChanged();
433 
434  m_content = Block(Tlv::Content, contentValue); // not real a wire encoding yet
435 }
436 
437 inline void
438 Data::setContent(const Block& content)
439 {
440  onChanged();
441 
442  if (content.type() == Tlv::Content)
443  m_content = content;
444  else {
445  m_content = Block(Tlv::Content, content);
446  }
447 }
448 
449 inline const Signature&
451 {
452  return m_signature;
453 }
454 
455 inline void
456 Data::setSignature(const Signature& signature)
457 {
458  onChanged();
459  m_signature = signature;
460 }
461 
462 inline void
464 {
465  onChanged();
466  m_signature.setValue(value);
467 }
468 
469 //
470 
473 {
474  return m_localControlHeader;
475 }
476 
477 inline const nfd::LocalControlHeader&
479 {
480  return m_localControlHeader;
481 }
482 
483 inline uint64_t
485 {
487 }
488 
489 inline void
490 Data::setIncomingFaceId(uint64_t incomingFaceId)
491 {
492  getLocalControlHeader().setIncomingFaceId(incomingFaceId);
493  // ! do not reset Data's wire !
494 }
495 
496 inline void
497 Data::onChanged()
498 {
499  // The values have changed, so the wire format is invalidated
500 
501  // !!!Note!!! Signature is not invalidated and it is responsibility of
502  // the application to do proper re-signing if necessary
503 
504  m_wire.reset();
505 }
506 
507 inline bool
508 Data::operator==(const Data& other) const
509 {
510  return getName() == other.getName() &&
511  getMetaInfo() == other.getMetaInfo() &&
512  getContent() == other.getContent() &&
513  getSignature() == other.getSignature();
514 }
515 
516 inline bool
517 Data::operator!=(const Data& other) const
518 {
519  return !(*this == other);
520 }
521 
522 inline std::ostream&
523 operator<<(std::ostream& os, const Data& data)
524 {
525  os << "Name: " << data.getName() << "\n";
526  os << "MetaInfo: " << data.getMetaInfo() << "\n";
527  os << "Content: (size: " << data.getContent().value_size() << ")\n";
528  os << "Signature: (type: " << data.getSignature().getType() <<
529  ", value_length: "<< data.getSignature().getValue().value_size() << ")";
530  os << std::endl;
531 
532  return os;
533 }
534 
535 } // namespace ndn
536 
537 #endif
Error(const std::string &what)
Definition: data.hpp:28
bool empty() const
Check if the Block is empty.
Definition: block.hpp:281
void wireDecode(const Block &wire)
Decode from the wire format.
Definition: data.hpp:306
void setContentType(uint32_t type)
Definition: data.hpp:378
Data()
Create an empty Data object.
Definition: data.hpp:224
uint32_t getContentType() const
Definition: data.hpp:372
element_const_iterator find(uint32_t type) const
Definition: block.hpp:343
void setInfo(const Block &info)
Definition: signature.hpp:63
void setMetaInfo(const MetaInfo &metaInfo)
Set metaInfo to a copy of the given MetaInfo.
Definition: data.hpp:365
Class representing wire element of the NDN packet.
Definition: block.hpp:26
const Block & getValue() const
Definition: signature.hpp:79
const time::milliseconds & getFreshnessPeriod() const
Definition: meta-info.hpp:68
void setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: data.hpp:391
const MetaInfo & getMetaInfo() const
Definition: data.hpp:359
bool operator!=(const Data &other) const
Definition: data.hpp:517
const Name & getName() const
Definition: data.hpp:346
element_const_iterator elements_end() const
Definition: block.hpp:476
~Data()
The destructor.
Definition: data.hpp:236
uint32_t getType() const
Definition: signature.hpp:50
const name::Component & getFinalBlockId() const
Definition: meta-info.hpp:82
ptr_lib::shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:17
size_t prependBlock(EncodingImpl< P > &encoder, const Block &block)
MetaInfo & setFreshnessPeriod(const time::milliseconds &freshnessPeriod)
Definition: meta-info.hpp:74
size_t wireEncode(EncodingImpl< T > &block) const
Definition: meta-info.hpp:118
void setValue(const Block &value)
Definition: signature.hpp:86
void setContent(const uint8_t *content, size_t contentLength)
Set the content to a copy of the data in the vector.
Definition: data.hpp:422
MetaInfo & setFinalBlockId(const name::Component &finalBlockId)
Definition: meta-info.hpp:88
void setIncomingFaceId(uint64_t incomingFaceId)
Definition: data.hpp:490
An MetaInfo holds the meta info which is signed inside the data packet.
Definition: meta-info.hpp:17
const name::Component & getFinalBlockId() const
Definition: data.hpp:398
const Block & get(uint32_t type) const
Get the first subelement of the requested type.
Definition: block.hpp:326
element_container::const_iterator element_const_iterator
Definition: block.hpp:31
void reset()
Reset wire buffer of the element.
Definition: block.hpp:300
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:26
size_t value_size() const
Definition: block.hpp:455
const Block & wireEncode() const
Encode to a wire format.
Definition: data.hpp:286
Data(const Block &wire)
Create a new Data object from wire encoding.
Definition: data.hpp:49
size_t wireEncode(EncodingImpl< T > &block) const
Fast encoding or block size estimation.
Definition: name.hpp:711
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:265
uint32_t type() const
Definition: block.hpp:320
const Signature & getSignature() const
Definition: data.hpp:450
Component holds a read-only name component value.
const Block & getInfo() const
Definition: signature.hpp:56
void encode()
Encode subblocks into wire buffer.
Definition: block.cpp:298
void setSignatureValue(const Block &value)
Definition: data.hpp:463
uint64_t getIncomingFaceId() const
Definition: data.hpp:484
const Block & getContent() const
Get content Block.
Definition: data.hpp:411
bool operator==(const Data &other) const
Definition: data.hpp:508
Block dataBlock(uint32_t type, const char *data, size_t dataSize)
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.hpp:288
MetaInfo & setType(uint32_t type)
Definition: meta-info.hpp:60
void setIncomingFaceId(uint64_t incomingFaceId)
void setName(const Name &name)
Set name to a copy of the given Name.
Definition: data.hpp:352
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.hpp:523
void wireDecode(const Block &wire)
Definition: name.hpp:746
uint32_t getType() const
Definition: meta-info.hpp:54
void setSignature(const Signature &signature)
Set the signature to a copy of the given signature.
Definition: data.hpp:456
bool hasWire() const
Check if already has wire.
Definition: data.hpp:340
nfd::LocalControlHeader & getLocalControlHeader()
Definition: data.hpp:472
Class representing wire element of the NDN packet.
void setFinalBlockId(const name::Component &finalBlockId)
Definition: data.hpp:404
const time::milliseconds & getFreshnessPeriod() const
Definition: data.hpp:385
A Signature is storage for the signature-related information (info and value) in a Data packet...
Definition: signature.hpp:15
void wireDecode(const Block &wire)
Definition: meta-info.hpp:168