NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
name-component.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_NAME_COMPONENT_HPP
23 #define NDN_NAME_COMPONENT_HPP
24 
25 #include "common.hpp"
26 #include "encoding/block.hpp"
28 #include "util/time.hpp"
29 
30 namespace ndn {
31 namespace name {
32 
34 static const uint8_t SEGMENT_MARKER = 0x00;
36 static const uint8_t SEGMENT_OFFSET_MARKER = 0xFB;
38 static const uint8_t VERSION_MARKER = 0xFD;
40 static const uint8_t TIMESTAMP_MARKER = 0xFC;
42 static const uint8_t SEQUENCE_NUMBER_MARKER = 0xFE;
43 
47 class Component : public Block
48 {
49 public:
53  class Error : public Block::Error
54  {
55  public:
56  explicit
57  Error(const std::string& what)
58  : Block::Error(what)
59  {
60  }
61  };
62 
66  Component();
67 
76  Component(const Block& wire);
77 
87  explicit
88  Component(const ConstBufferPtr& buffer);
89 
97  explicit
98  Component(const Buffer& buffer);
99 
108  Component(const uint8_t* buffer, size_t bufferSize);
109 
121  template<class Iterator>
122  Component(Iterator first, Iterator last);
123 
133  explicit
134  Component(const char* str);
135 
145  explicit
146  Component(const std::string& str);
147 
151  template<encoding::Tag TAG>
152  size_t
153  wireEncode(EncodingImpl<TAG>& encoder) const;
154 
158  const Block&
159  wireEncode() const;
160 
164  void
165  wireDecode(const Block& wire);
166 
181  static Component
182  fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset);
183 
189  static Component
190  fromEscapedString(const char* escapedString)
191  {
192  return fromEscapedString(escapedString, 0, std::char_traits<char>::length(escapedString));
193  }
194 
200  static Component
201  fromEscapedString(const std::string& escapedString)
202  {
203  return fromEscapedString(escapedString.c_str(), 0, escapedString.size());
204  }
205 
216  void
217  toEscapedString(std::ostream& os) const)
218  {
219  return toUri(os);
220  }
221 
232  std::string
233  toEscapedString() const)
234  {
235  return toUri();
236  }
237 
245  void
246  toUri(std::ostream& os) const;
247 
255  std::string
256  toUri() const;
257 
259 
264  bool
265  isNumber() const;
266 
271  bool
272  isNumberWithMarker(uint8_t marker) const;
273 
278  bool
279  isVersion() const;
280 
285  bool
286  isSegment() const;
287 
292  bool
293  isSegmentOffset() const;
294 
299  bool
300  isTimestamp() const;
301 
306  bool
307  isSequenceNumber() const;
308 
310 
318  uint64_t
319  toNumber() const;
320 
331  uint64_t
332  toNumberWithMarker(uint8_t marker) const;
333 
342  uint64_t
343  toVersion() const;
344 
353  uint64_t
354  toSegment() const;
355 
364  uint64_t
365  toSegmentOffset() const;
366 
376  toTimestamp() const;
377 
386  uint64_t
387  toSequenceNumber() const;
388 
390 
399  static Component
400  fromNumber(uint64_t number);
401 
423  static Component
424  fromNumberWithMarker(uint8_t marker, uint64_t number);
425 
431  static Component
432  fromVersion(uint64_t version);
433 
439  static Component
440  fromSegment(uint64_t segmentNo);
441 
447  static Component
448  fromSegmentOffset(uint64_t offset);
449 
455  static Component
457 
463  static Component
464  fromSequenceNumber(uint64_t seqNo);
465 
467 
471  bool
472  isGeneric() const;
473 
477  bool
478  isImplicitSha256Digest() const;
479 
483  static Component
485 
489  static Component
490  fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
491 
493 
494  bool
495  empty() const
496  {
497  return !hasValue() || value_size() == 0;
498  }
499 
500  Component
501  getSuccessor() const;
502 
509  bool
510  equals(const Component& other) const
511  {
512  if (value_size() != other.value_size())
513  return false;
514  if (value_size() == 0 /* == other.value_size()*/)
515  return true;
516 
517  // somehow, behavior is wrong on OSX 10.9 when component is empty
518  // (probably some bug in STL...)
519  return std::equal(value_begin(), value_end(), other.value_begin());
520  }
521 
532  int
533  compare(const Component& other) const;
534 
541  bool
542  operator==(const Component& other) const
543  {
544  return equals(other);
545  }
546 
552  bool
553  operator!=(const Component& other) const
554  {
555  return !equals(other);
556  }
557 
564  bool
565  operator<=(const Component& other) const
566  {
567  return compare(other) <= 0;
568  }
569 
576  bool
577  operator<(const Component& other) const
578  {
579  return compare(other) < 0;
580  }
581 
588  bool
589  operator>=(const Component& other) const
590  {
591  return compare(other) >= 0;
592  }
593 
600  bool
601  operator>(const Component& other) const
602  {
603  return compare(other) > 0;
604  }
605 
606  // !!! NOTE TO IMPLEMENTOR !!!
607  //
608  // This class MUST NOT contain any data fields.
609  // Block can be reinterpret_cast'ed as Component type.
610 };
611 
612 inline std::ostream&
613 operator<<(std::ostream& os, const Component& component)
614 {
615  component.toUri(os);
616  return os;
617 }
618 
619 template<class Iterator>
620 inline
621 Component::Component(Iterator first, Iterator last)
622  : Block(makeBinaryBlock(tlv::NameComponent, first, last))
623 {
624 }
625 
626 } // namespace name
627 } // namespace ndn
628 
629 #endif // NDN_NAME_COMPONENT_HPP
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Convert *this by escaping characters according to the NDN URI Scheme.
bool isSequenceNumber() const
Check if the component is sequence number per NDN naming conventions.
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
bool operator<=(const Component &other) const
Check if the *this is less than or equal to the other in NDN canonical ordering.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
static Component fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset)
Create name::Component by decoding the escapedString between beginOffset and endOffset according to t...
std::ostream & operator<<(std::ostream &os, const Component &component)
bool operator>=(const Component &other) const
Check if the *this is greater or equal than the other in NDN canonical ordering.
bool operator==(const Component &other) const
Check if this is the same component as other.
size_t value_size() const
Definition: block.cpp:529
bool isNumberWithMarker(uint8_t marker) const
Check if the component is NameComponentWithMarker per NDN naming conventions.
static Component fromTimestamp(const time::system_clock::TimePoint &timePoint)
Create sequence number component using NDN naming conventions.
const uint8_t * wire() const
Definition: block.cpp:495
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
DEPRECATED(void toEscapedString(std::ostream &os) const)
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
Copyright (c) 2013-2015 Regents of the University of California.
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
static const uint8_t SEGMENT_OFFSET_MARKER
Segment offset marker for NDN naming conventions.
bool operator>(const Component &other) const
Check if the *this is greater than the other in NDN canonical ordering.
bool isNumber() const
Check if the component is nonNegativeInteger.
Buffer::const_iterator value_begin() const
Definition: block.hpp:352
bool isSegmentOffset() const
Check if the component is segment offset per NDN naming conventions.
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
Component getSuccessor() const
static Component fromEscapedString(const char *escapedString)
Create name::Component by decoding the escapedString according to the NDN URI Scheme.
static Component fromEscapedString(const std::string &escapedString)
Create name::Component by decoding the escapedString according to the NDN URI Scheme.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block type type with value from a buffer value of size length.
bool isSegment() const
Check if the component is segment number per NDN naming conventions.
Component()
Create a new name::Component with an empty value.
bool operator!=(const Component &other) const
Check if this is not the same component as other.
void toUri(std::ostream &os) const
Write *this to the output stream, escaping characters according to the NDN URI Scheme.
Buffer::const_iterator value_end() const
Definition: block.hpp:358
uint64_t toSegmentOffset() const
Interpret as segment offset component using NDN naming conventions.
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
bool equals(const Component &other) const
Check if this is the same component as other.
const Block & wireEncode() const
Encode to a wire format.
uint64_t toNumber() const
Interpret this name component as nonNegativeInteger.
static const uint8_t VERSION_MARKER
Version marker for NDN naming conventions.
time_point TimePoint
Definition: time.hpp:78
Component holds a read-only name component value.
static Component fromImplicitSha256Digest(const ConstBufferPtr &digest)
Create ImplicitSha256DigestComponent component.
void wireDecode(const Block &wire)
Decode from the wire format.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
static const uint8_t SEGMENT_MARKER
Segment marker for NDN naming conventions.
bool isTimestamp() const
Check if the component is timestamp per NDN naming conventions.
static Component fromVersion(uint64_t version)
Create version component using NDN naming conventions.
bool operator<(const Component &other) const
Check if the *this is less than the other in NDN canonical ordering.
static const uint8_t SEQUENCE_NUMBER_MARKER
Sequence number marker for NDN naming conventions.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
bool isGeneric() const
Check if the component is GenericComponent.
bool isVersion() const
Check if the component is version per NDN naming conventions.
bool hasValue() const
Check if the Block has value block (no type and length are encoded)
Definition: block.cpp:514
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
DEPRECATED(std::string toEscapedString() const)
Convert *this by escaping characters according to the NDN URI Scheme.
time::system_clock::TimePoint toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
static const uint8_t TIMESTAMP_MARKER
Timestamp marker for NDN naming conventions.
Class representing a general-use automatically managed/resized buffer.
Definition: buffer.hpp:44
Error that can be thrown from name::Component.
Error(const std::string &what)