Skip to content

Commit

Permalink
transport: minor refactoring
Browse files Browse the repository at this point in the history
Change-Id: Ieebf3d58e3094b60978857eb35934e1980734fc8
  • Loading branch information
Pesa committed Dec 10, 2023
1 parent 39af9f8 commit b0fe59f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 42 deletions.
38 changes: 19 additions & 19 deletions ndn-cxx/transport/tcp-transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,31 @@ TcpTransport::create(const std::string& uri)
std::pair<std::string, std::string>
TcpTransport::getSocketHostAndPortFromUri(const std::string& uriString)
{
// Default host and port.
std::string host = "localhost";
std::string port = "6363";

if (uriString.empty()) {
return {host, port};
}

try {
const FaceUri uri(uriString);

const auto& scheme = uri.getScheme();
if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
NDN_THROW(Error("Cannot create TcpTransport from \"" + scheme + "\" URI"));
// Use host and port from the provided URI, if valid.
if (!uriString.empty()) {
try {
const FaceUri uri(uriString);

const auto& scheme = uri.getScheme();
if (scheme != "tcp" && scheme != "tcp4" && scheme != "tcp6") {
NDN_THROW(Error("Cannot create TcpTransport from \"" + scheme + "\" URI"));
}

if (!uri.getHost().empty()) {
host = uri.getHost();
}
if (!uri.getPort().empty()) {
port = uri.getPort();
}
}

if (!uri.getHost().empty()) {
host = uri.getHost();
}
if (!uri.getPort().empty()) {
port = uri.getPort();
catch (const FaceUri::Error& error) {
NDN_THROW_NESTED(Error(error.what()));
}
}
catch (const FaceUri::Error& error) {
NDN_THROW_NESTED(Error(error.what()));
}

return {host, port};
}
Expand Down
42 changes: 19 additions & 23 deletions ndn-cxx/transport/unix-transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,29 @@ UnixTransport::~UnixTransport() = default;
std::string
UnixTransport::getSocketNameFromUri(const std::string& uriString)
{
// Assume the default nfd.sock location.
#ifdef __linux__
std::string path = "/run/nfd.sock";
#else
std::string path = "/var/run/nfd.sock";
#endif // __linux__

if (uriString.empty()) {
return path;
}

try {
const FaceUri uri(uriString);

if (uri.getScheme() != "unix") {
NDN_THROW(Error("Cannot create UnixTransport from \"" + uri.getScheme() + "\" URI"));
// Use path from the provided URI, if valid.
if (!uriString.empty()) {
try {
const FaceUri uri(uriString);
if (uri.getScheme() != "unix") {
NDN_THROW(Error("Cannot create UnixTransport from \"" + uri.getScheme() + "\" URI"));
}
if (!uri.getPath().empty()) {
return uri.getPath();
}
}

if (!uri.getPath().empty()) {
path = uri.getPath();
catch (const FaceUri::Error& error) {
NDN_THROW_NESTED(Error(error.what()));
}
}
catch (const FaceUri::Error& error) {
NDN_THROW_NESTED(Error(error.what()));
}

return path;
// Otherwise, use the default nfd.sock location.
return
#ifdef __linux__
"/run/nfd.sock";
#else
"/var/run/nfd.sock";
#endif // __linux__
}

shared_ptr<UnixTransport>
Expand Down

0 comments on commit b0fe59f

Please sign in to comment.