Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ntsa::Host to represent a local name and include and optional transport hint in ntsa::Uri #208

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 66 additions & 2 deletions groups/nts/ntsa/ntsa_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ Host::Host(const ntsa::DomainName& value)
}

Host::Host(const ntsa::IpAddress& value)
: d_type(ntsa::HostType::e_UNDEFINED)
: d_type(ntsa::HostType::e_IP)
{
new (d_ip.buffer()) ntsa::IpAddress(value);
d_type = ntsa::HostType::e_IP;
}

Host::Host(const ntsa::LocalName& value)
: d_type(ntsa::HostType::e_LOCAL_NAME)
{
new (d_localName.buffer()) ntsa::LocalName(value);
}

Host::Host(const Host& other)
Expand All @@ -68,6 +73,9 @@ Host::Host(const Host& other)
case ntsa::HostType::e_IP:
new (d_ip.buffer()) ntsa::IpAddress(other.d_ip.object());
break;
case ntsa::HostType::e_LOCAL_NAME:
new (d_localName.buffer()) ntsa::LocalName(other.d_localName.object());
break;
default:
BSLS_ASSERT(d_type == ntsa::HostType::e_UNDEFINED);
}
Expand All @@ -86,6 +94,9 @@ Host& Host::operator=(const Host& other)
case ntsa::HostType::e_IP:
this->makeIp(other.d_ip.object());
break;
case ntsa::HostType::e_LOCAL_NAME:
this->makeLocalName(other.d_localName.object());
break;
default:
BSLS_ASSERT(other.d_type == ntsa::HostType::e_UNDEFINED);
this->reset();
Expand All @@ -106,6 +117,12 @@ Host& Host::operator=(const ntsa::IpAddress& other)
return *this;
}

Host& Host::operator=(const ntsa::LocalName& other)
{
this->makeLocalName(other);
return *this;
}

Host& Host::operator=(const bslstl::StringRef& text)
{
this->reset();
Expand All @@ -126,6 +143,11 @@ Host& Host::operator=(const bslstl::StringRef& text)
return *this;
}

if (text.size() > 0 && text[0] == '/') {
this->makeLocalName().setValue(text);
return *this;
}

this->reset();

throwAddressInvalidFormat(text);
Expand All @@ -152,6 +174,11 @@ bool Host::parse(const bslstl::StringRef& text)
return true;
}

if (text.size() > 0 && text[0] == '/') {
this->makeLocalName().setValue(text);
return true;
}

this->reset();
return false;
}
Expand Down Expand Up @@ -212,13 +239,43 @@ ntsa::IpAddress& Host::makeIp(const ntsa::IpAddress& value)
return d_ip.object();
}

ntsa::LocalName& Host::makeLocalName()
{
if (d_type == ntsa::HostType::e_LOCAL_NAME) {
d_localName.object().reset();
}
else {
this->reset();
new (d_localName.buffer()) ntsa::LocalName();
d_type = ntsa::HostType::e_LOCAL_NAME;
}

return d_localName.object();
}

ntsa::LocalName& Host::makeLocalName(const ntsa::LocalName& value)
{
if (d_type == ntsa::HostType::e_LOCAL_NAME) {
d_localName.object() = value;
}
else {
this->reset();
new (d_localName.buffer()) ntsa::LocalName(value);
d_type = ntsa::HostType::e_LOCAL_NAME;
}

return d_localName.object();
}

bsl::string Host::text() const
{
switch (d_type) {
case ntsa::HostType::e_DOMAIN_NAME:
return d_domainName.object().text();
case ntsa::HostType::e_IP:
return d_ip.object().text();
case ntsa::HostType::e_LOCAL_NAME:
return d_localName.object().value();
default:
return "";
}
Expand All @@ -235,6 +292,8 @@ bool Host::equals(const Host& other) const
return d_domainName.object() == other.d_domainName.object();
case ntsa::HostType::e_IP:
return d_ip.object().equals(other.d_ip.object());
case ntsa::HostType::e_LOCAL_NAME:
return d_localName.object().equals(other.d_localName.object());
default:
return true;
}
Expand All @@ -251,6 +310,8 @@ bool Host::less(const Host& other) const
return d_domainName.object().less(other.d_domainName.object());
case ntsa::HostType::e_IP:
return d_ip.object().less(other.d_ip.object());
case ntsa::HostType::e_LOCAL_NAME:
return d_localName.object().less(other.d_localName.object());
default:
return true;
}
Expand All @@ -267,6 +328,9 @@ bsl::ostream& Host::print(bsl::ostream& stream,
case ntsa::HostType::e_IP:
d_ip.object().print(stream, level, spacesPerLevel);
break;
case ntsa::HostType::e_LOCAL_NAME:
d_localName.object().print(stream, level, spacesPerLevel);
break;
default:
BSLS_ASSERT(d_type == ntsa::HostType::e_UNDEFINED);
stream << "UNDEFINED";
Expand Down
58 changes: 45 additions & 13 deletions groups/nts/ntsa/ntsa_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ BSLS_IDENT("$Id: $")

#include <ntsa_domainname.h>
#include <ntsa_hosttype.h>
#include <ntsa_localname.h>
#include <ntsa_ipaddress.h>
#include <ntscfg_platform.h>
#include <ntsscm_version.h>
Expand Down Expand Up @@ -78,22 +79,27 @@ class Host
union {
bsls::ObjectBuffer<ntsa::DomainName> d_domainName;
bsls::ObjectBuffer<ntsa::IpAddress> d_ip;
bsls::ObjectBuffer<ntsa::LocalName> d_localName;
};

ntsa::HostType::Value d_type;

public:
/// Create a new address having an undefined type.
/// Create a new host having an undefined type.
Host();

/// Create a new address having a "domain name" representation having
/// the specified 'value'.
/// Create a new host having a "domain name" representation having the
/// specified 'value'.
explicit Host(const ntsa::DomainName& value);

/// Create a new address having a "ip" representation having the
/// specified 'value'.
/// Create a new host having a "ip" representation having the specified
/// 'value'.
explicit Host(const ntsa::IpAddress& value);

/// Create a new host having a "local name" representation having the
/// specified 'value'.
explicit Host(const ntsa::LocalName& value);

/// Create a new address parsed from the specified 'text'
/// representation.
explicit Host(const bslstl::StringRef& text);
Expand All @@ -117,6 +123,10 @@ class Host
/// Return a reference to this modifiable object.
Host& operator=(const ntsa::IpAddress& other);

/// Assign the value of the specified 'other' object to this object. Return
/// a reference to this modifiable object.
Host& operator=(const ntsa::LocalName& other);

/// Set the value of the object from the specified 'text'.
Host& operator=(const bslstl::StringRef& text);

Expand Down Expand Up @@ -147,27 +157,45 @@ class Host
/// representation.
ntsa::IpAddress& makeIp(const ntsa::IpAddress& value);

/// Select the "local name" address representation. Return a reference to
/// the modifiable representation.
ntsa::LocalName& makeLocalName();

/// Select the "local name" address representation initially having the
/// specified 'value'. Return a reference to the modifiable representation.
ntsa::LocalName& makeLocalName(const ntsa::LocalName& value);

/// Return a reference to the modifiable "domain name" address
/// representation. The behavior is undefined unless 'isDomainName()'
/// is true.
/// representation. The behavior is undefined unless 'isDomainName()' is
/// true.
ntsa::DomainName& domainName();

/// Return a reference to the modifiable "ip" address representation.
/// The behavior is undefined unless 'isIp()' is true.
/// Return a reference to the modifiable "ip" address representation. The
/// behavior is undefined unless 'isIp()' is true.
ntsa::IpAddress& ip();

/// Return a reference to the modifiable "local name" address
/// representation. The behavior is undefined unless 'isLocalName()' is
/// true.
ntsa::LocalName& localName();

/// Return the textual representation of this object.
bsl::string text() const;

/// Return a reference to the non-modifiable "domain name" address
/// representation. The behavior is undefined unless 'isDomainName()'
/// is true.
/// representation. The behavior is undefined unless 'isDomainName()' is
/// true.
const ntsa::DomainName& domainName() const;

/// Return a reference to the non-modifiable "ip" address
/// representation. The behavior is undefined unless 'isIp()' is true.
/// Return a reference to the non-modifiable "ip" address representation.
/// The behavior is undefined unless 'isIp()' is true.
const ntsa::IpAddress& ip() const;

/// Return a reference to the non-modifiable "local name" address
/// representation. The behavior is undefined unless 'isLocalName()' is
/// true.
const ntsa::LocalName& localName() const;

/// Return the type of the address representation.
ntsa::HostType::Value type() const;

Expand All @@ -183,6 +211,10 @@ class Host
/// selected, otherwise return false.
bool isIp() const;

/// Return true if the "local name" address representation is currently
/// selected, otherwise return false.
bool isLocalName() const;

/// Return true if this object has the same value as the specified
/// 'other' object, otherwise return false.
bool equals(const Host& other) const;
Expand Down
8 changes: 8 additions & 0 deletions groups/nts/ntsa/ntsa_hosttype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ int HostType::fromInt(HostType::Value* result, int number)
case HostType::e_UNDEFINED:
case HostType::e_DOMAIN_NAME:
case HostType::e_IP:
case HostType::e_LOCAL_NAME:
*result = static_cast<HostType::Value>(number);
return 0;
default:
Expand All @@ -51,6 +52,10 @@ int HostType::fromString(HostType::Value* result,
*result = e_IP;
return 0;
}
if (bdlb::String::areEqualCaseless(string, "LOCAL_NAME")) {
*result = e_LOCAL_NAME;
return 0;
}

return -1;
}
Expand All @@ -67,6 +72,9 @@ const char* HostType::toString(HostType::Value value)
case e_IP: {
return "IP";
} break;
case e_LOCAL_NAME: {
return "LOCAL_NAME";
} break;
}

BSLS_ASSERT(!"invalid enumerator");
Expand Down
5 changes: 4 additions & 1 deletion groups/nts/ntsa/ntsa_hosttype.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ struct HostType {
e_DOMAIN_NAME = 1,

/// The host type is an IP address
e_IP = 2
e_IP = 2,

/// The host type is a local name.
e_LOCAL_NAME = 3
};

/// Return the string representation exactly matching the enumerator
Expand Down
Loading
Loading