ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
ndn::Name Class Reference

NDN Name class. More...

#include <ndn/name.hpp>

Public Member Functions

 Name ()=default
 Default constructor.
 
uint32_t hash () const
 Compute hash value of the Name.
 
Conversion methods
size_t toUri (char *buf, size_t bufSize) const
 Convert the Name to a URI string.
 
Error encode (uint8_t *buf, size_t bufSize, size_t &encodedLen) const
 Encode the Name to TLV wire format.
 
Component operations
size_t componentCount () const
 Get the number of components.
 
NameComponent component (size_t index) const
 Get the component at a given index.
 
Error appendComponent (std::string_view comp)
 Append a string component.
 
Error appendComponent (const uint8_t *value, size_t len)
 Append a binary component.
 
Comparison methods
int compare (const Name &other) const
 Compare with another Name.
 
bool equals (const Name &other) const
 Check equality with another Name.
 
bool isPrefixOf (const Name &other) const
 Check if this Name is a prefix of another Name.
 
State inspection
bool empty () const
 Check if the Name is empty.
 
const uint8_t * wireValue () const
 Get pointer to the internal buffer.
 
size_t wireLength () const
 Get the length of the internal buffer.
 

Static Public Member Functions

Construction methods
static Result< NamefromUri (std::string_view uri)
 Create a Name from a URI string.
 
static Result< NamefromWire (const uint8_t *buf, size_t len, size_t *bytesRead=nullptr)
 Decode a Name from TLV wire format.
 

Detailed Description

NDN Name class.

A class representing the name of an NDN packet. A name consists of multiple components and can be expressed in URI format (e.g., "/sensor/temperature").

// Create a Name from a URI string
auto result = ndn::Name::fromUri("/sensor/temperature");
if (result.ok()) {
Name& name = result.value;
printf("Components: %zu\n", name.componentCount());
// Output in URI format
char uri[64];
name.toUri(uri, sizeof(uri));
printf("URI: %s\n", uri);
}
// Append components
Name name;
name.appendComponent("test");
name.appendComponent("data");
NDN Name class.
Definition name.hpp:64
Error appendComponent(std::string_view comp)
Append a string component.
Definition name.cpp:228
size_t toUri(char *buf, size_t bufSize) const
Convert the Name to a URI string.
Definition name.cpp:141
size_t componentCount() const
Get the number of components.
Definition name.hpp:133
static Result< Name > fromUri(std::string_view uri)
Create a Name from a URI string.
Definition name.cpp:25

Definition at line 64 of file name.hpp.

Constructor & Destructor Documentation

◆ Name()

ndn::Name::Name ( )
default

Default constructor.

Creates an empty Name.

Member Function Documentation

◆ appendComponent() [1/2]

Error ndn::Name::appendComponent ( const uint8_t *  value,
size_t  len 
)

Append a binary component.

Parameters
valuePointer to component value
lenSize of the component in bytes
Returns
Error::Success on success

Definition at line 232 of file name.cpp.

◆ appendComponent() [2/2]

Error ndn::Name::appendComponent ( std::string_view  comp)

Append a string component.

Parameters
compComponent string to append
Returns
Error::Success on success, Error::BufferTooSmall if buffer is insufficient, Error::TooManyComponents if component limit exceeded

Definition at line 228 of file name.cpp.

Referenced by ndn::Certificate::buildName(), and ndn::Certificate::fromData().

◆ compare()

int ndn::Name::compare ( const Name other) const

Compare with another Name.

Performs lexicographic comparison according to the NDN specification.

Parameters
otherName to compare with
Returns
Negative: this < other, 0: this == other, Positive: this > other

Definition at line 279 of file name.cpp.

References component(), ndn::NameComponent::size, and ndn::NameComponent::value.

Referenced by equals(), and ndn::operator<().

◆ component()

NameComponent ndn::Name::component ( size_t  index) const

Get the component at a given index.

Parameters
indexComponent index (starting from 0)
Returns
NameComponent structure
Note
Behavior is undefined if the index is out of range

Definition at line 219 of file name.cpp.

References ndn::NameComponent::value.

Referenced by ndn::Certificate::buildName(), compare(), ndn::Certificate::fromData(), isPrefixOf(), and toUri().

◆ componentCount()

size_t ndn::Name::componentCount ( ) const
inline

Get the number of components.

Returns
Number of components in the Name

Definition at line 133 of file name.hpp.

Referenced by ndn::Certificate::buildName(), ndn::Fib::findLongestMatch(), and ndn::Certificate::fromData().

◆ empty()

bool ndn::Name::empty ( ) const
inline

Check if the Name is empty.

Returns
true if there are no components

Definition at line 220 of file name.hpp.

◆ encode()

Error ndn::Name::encode ( uint8_t *  buf,
size_t  bufSize,
size_t &  encodedLen 
) const

Encode the Name to TLV wire format.

Parameters
bufOutput buffer
bufSizeBuffer size
encodedLenStores the number of encoded bytes
Returns
Error::Success on success

Definition at line 186 of file name.cpp.

References ndn::tlv::Name, ndn::TlvEncoder::size(), ndn::varNumberSize(), ndn::TlvEncoder::writeBytes(), ndn::TlvEncoder::writeLength(), and ndn::TlvEncoder::writeType().

Referenced by ndn::Interest::encode().

◆ equals()

bool ndn::Name::equals ( const Name other) const

Check equality with another Name.

Parameters
otherName to compare with
Returns
true if equal

Definition at line 309 of file name.cpp.

References compare().

Referenced by ndn::operator!=(), and ndn::operator==().

◆ fromUri()

Result< Name > ndn::Name::fromUri ( std::string_view  uri)
static

Create a Name from a URI string.

Parses an NDN URI format string and constructs a Name. The "ndn:" prefix is optional.

Parameters
uriURI string (e.g., "/sensor/temperature" or "ndn:/sensor/temperature")
Returns
Name and Error::Success on success, error code on failure
Note
Percent-encoding (XX) is supported.

Definition at line 25 of file name.cpp.

References ndn::NAME_MAX_LENGTH.

Referenced by ndn::Link::addDelegation(), ndn::Interest::addForwardingHint(), ndn::Forwarder::addRoute(), ndn::Forwarder::registerPrefix(), ndn::Certificate::setIdentityName(), ndn::Data::setName(), ndn::Interest::setName(), and ndn::Link::setName().

◆ fromWire()

Result< Name > ndn::Name::fromWire ( const uint8_t *  buf,
size_t  len,
size_t *  bytesRead = nullptr 
)
static

Decode a Name from TLV wire format.

Parameters
bufInput buffer
lenBuffer length
bytesReadPointer to store the number of bytes read (may be nullptr)
Returns
Name and Error::Success on success

Definition at line 84 of file name.cpp.

References ndn::TlvDecoder::current(), ndn::tlv::GenericNameComponent, ndn::tlv::Name, ndn::TlvDecoder::position(), ndn::TlvDecoder::readTlvHeader(), ndn::TlvDecoder::remaining(), and ndn::TlvDecoder::skip().

Referenced by ndn::Link::fromData(), ndn::Data::fromWire(), and ndn::Interest::fromWire().

◆ hash()

uint32_t ndn::Name::hash ( ) const

Compute hash value of the Name.

Returns a 32-bit hash value used for lookups in PIT and CS.

Returns
32-bit hash value

Definition at line 334 of file name.cpp.

◆ isPrefixOf()

bool ndn::Name::isPrefixOf ( const Name other) const

Check if this Name is a prefix of another Name.

Parameters
otherName to compare with
Returns
true if this Name is a prefix of other
Name prefix, full;
prefix.appendComponent("sensor");
full.appendComponent("sensor");
full.appendComponent("temperature");
assert(prefix.isPrefixOf(full)); // true
bool isPrefixOf(const Name &other) const
Check if this Name is a prefix of another Name.
Definition name.cpp:313

Definition at line 313 of file name.cpp.

References component(), ndn::NameComponent::size, and ndn::NameComponent::value.

◆ toUri()

size_t ndn::Name::toUri ( char *  buf,
size_t  bufSize 
) const

Convert the Name to a URI string.

Parameters
bufOutput buffer
bufSizeBuffer size
Returns
Number of characters written (excluding null terminator)

Definition at line 141 of file name.cpp.

References component(), ndn::NameComponent::size, and ndn::NameComponent::value.

◆ wireLength()

size_t ndn::Name::wireLength ( ) const
inline

Get the length of the internal buffer.

Returns
Length in bytes of the TLV-encoded Name Value portion

Definition at line 232 of file name.hpp.

◆ wireValue()

const uint8_t * ndn::Name::wireValue ( ) const
inline

Get pointer to the internal buffer.

Returns
Pointer to the TLV-encoded Name Value portion

Definition at line 226 of file name.hpp.


The documentation for this class was generated from the following files: