NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
2 /*
3  * Copyright (c) 2013-2018 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_NAME_COMPONENT_HPP
23 #define NDN_NAME_COMPONENT_HPP
24 
28 #include "ndn-cxx/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 
50 class Component : public Block
51 {
52 public:
53  class Error : public Block::Error
54  {
55  public:
56  using Block::Error::Error;
57  };
58 
59 public: // constructors
64  explicit
66 
73  Component(const Block& wire);
74 
82  Component(uint32_t type, ConstBufferPtr buffer);
83 
91  explicit
93  : Component(tlv::GenericNameComponent, std::move(buffer))
94  {
95  }
96 
100  Component(uint32_t type, const Buffer& buffer)
101  : Component(type, buffer.data(), buffer.size())
102  {
103  }
104 
108  explicit
109  Component(const Buffer& buffer)
110  : Component(tlv::GenericNameComponent, buffer)
111  {
112  }
113 
118  Component(uint32_t type, const uint8_t* value, size_t count);
119 
123  Component(const uint8_t* value, size_t count)
124  : Component(tlv::GenericNameComponent, value, count)
125  {
126  }
127 
136  template<class Iterator>
137  Component(uint32_t type, Iterator first, Iterator last)
138  : Block(makeBinaryBlock(type, first, last))
139  {
140  }
141 
145  template<class Iterator>
146  Component(Iterator first, Iterator last)
147  : Component(tlv::GenericNameComponent, first, last)
148  {
149  }
150 
156  explicit
157  Component(const char* str);
158 
164  explicit
165  Component(const std::string& str);
166 
167 public: // encoding and URI
171  template<encoding::Tag TAG>
172  size_t
173  wireEncode(EncodingImpl<TAG>& encoder) const;
174 
178  const Block&
179  wireEncode() const;
180 
184  void
185  wireDecode(const Block& wire);
186 
194  static Component
195  fromEscapedString(const char* input, size_t beginOffset, size_t endOffset)
196  {
197  return fromEscapedString(std::string(input + beginOffset, input + endOffset));
198  }
199 
204  static Component
205  fromEscapedString(const char* input)
206  {
207  return fromEscapedString(std::string(input));
208  }
209 
214  static Component
215  fromEscapedString(const std::string& input);
216 
224  void
225  toUri(std::ostream& os) const;
226 
234  std::string
235  toUri() const;
236 
237 public: // naming conventions
242  bool
243  isNumber() const;
244 
249  bool
250  isNumberWithMarker(uint8_t marker) const;
251 
256  bool
257  isVersion() const;
258 
263  bool
264  isSegment() const;
265 
270  bool
271  isSegmentOffset() const;
272 
277  bool
278  isTimestamp() const;
279 
284  bool
285  isSequenceNumber() const;
286 
294  uint64_t
295  toNumber() const;
296 
307  uint64_t
308  toNumberWithMarker(uint8_t marker) const;
309 
318  uint64_t
319  toVersion() const;
320 
329  uint64_t
330  toSegment() const;
331 
340  uint64_t
341  toSegmentOffset() const;
342 
352  toTimestamp() const;
353 
362  uint64_t
363  toSequenceNumber() const;
364 
373  static Component
374  fromNumber(uint64_t number);
375 
397  static Component
398  fromNumberWithMarker(uint8_t marker, uint64_t number);
399 
405  static Component
406  fromVersion(uint64_t version);
407 
413  static Component
414  fromSegment(uint64_t segmentNo);
415 
421  static Component
422  fromSegmentOffset(uint64_t offset);
423 
429  static Component
431 
437  static Component
438  fromSequenceNumber(uint64_t seqNo);
439 
440 public: // commonly used TLV-TYPEs
444  bool
445  isGeneric() const;
446 
450  bool
451  isImplicitSha256Digest() const;
452 
456  static Component
458 
462  static Component
463  fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize);
464 
468  bool
469  isParametersSha256Digest() const;
470 
474  static Component
476 
480  static Component
481  fromParametersSha256Digest(const uint8_t* digest, size_t digestSize);
482 
483 public: // operators
484  bool
485  empty() const
486  {
487  return value_size() == 0;
488  }
489 
496  bool
497  equals(const Component& other) const;
498 
509  int
510  compare(const Component& other) const;
511 
518  bool
519  operator==(const Component& other) const
520  {
521  return equals(other);
522  }
523 
529  bool
530  operator!=(const Component& other) const
531  {
532  return !equals(other);
533  }
534 
541  bool
542  operator<=(const Component& other) const
543  {
544  return compare(other) <= 0;
545  }
546 
553  bool
554  operator<(const Component& other) const
555  {
556  return compare(other) < 0;
557  }
558 
565  bool
566  operator>=(const Component& other) const
567  {
568  return compare(other) >= 0;
569  }
570 
577  bool
578  operator>(const Component& other) const
579  {
580  return compare(other) > 0;
581  }
582 
608  Component
609  getSuccessor() const;
610 
611 private:
618  void
619  ensureValid() const;
620 
621  // !!! NOTE TO IMPLEMENTOR !!!
622  //
623  // This class MUST NOT contain any data fields.
624  // Block can be reinterpret_cast'ed as Component type.
625 };
626 
628 
629 inline std::ostream&
630 operator<<(std::ostream& os, const Component& component)
631 {
632  component.toUri(os);
633  return os;
634 }
635 
636 } // namespace name
637 } // namespace ndn
638 
639 #endif // NDN_NAME_COMPONENT_HPP
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
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.
Component(uint32_t type, Iterator first, Iterator last)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from a range.
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
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:316
Component(uint32_t type=tlv::GenericNameComponent)
Construct a NameComponent of TLV-TYPE type, using empty TLV-VALUE.
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
Get pointer to encoded wire.
Definition: block.cpp:289
NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Component)
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
const uint8_t * value() const
Get pointer to TLV-VALUE.
Definition: block.cpp:310
static Component fromEscapedString(const char *input)
Decode NameComponent from a URI component.
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
Component(uint32_t type, const Buffer &buffer)
Construct a NameComponent of TLV-TYPE type, copying TLV-VALUE from buffer.
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.
bool isSegmentOffset() const
Check if the component is segment offset per NDN naming conventions.
bool isParametersSha256Digest() const
Check if the component is ParametersSha256DigestComponent.
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
Component getSuccessor() const
Get the successor of this name component.
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Definition: block.cpp:298
import common constructs for ndn-cxx library internal use
static Component fromEscapedString(const char *input, size_t beginOffset, size_t endOffset)
Decode NameComponent from a URI component.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
bool isSegment() const
Check if the component is segment number per NDN naming conventions.
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.
uint64_t toSegmentOffset() const
Interpret as segment offset component using NDN naming conventions.
Component(const Buffer &buffer)
Construct a GenericNameComponent, copying TLV-VALUE from buffer.
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.
Component(ConstBufferPtr buffer)
Construct a GenericNameComponent, using TLV-VALUE from buffer.
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:195
Represents a name component.
void wireDecode(const Block &wire)
Decode from the wire format.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
Component(Iterator first, Iterator last)
Construct a GenericNameComponent, copying TLV-VALUE from a range.
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.
Component(const uint8_t *value, size_t count)
Construct a GenericNameComponent, copying count bytes at value as TLV-VALUE.
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.
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
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.
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:249
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126