Skip to content

Commit

Permalink
Added get_remote_endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed May 7, 2024
1 parent 8cbe187 commit 2bed66b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ namespace eCAL
*
* @return The address that this client session has been created with.
*/
std::string get_address() const;
std::string get_host() const;

/**TODO: Revise documentation
* @brief Get the port that this client session has been created with.
Expand All @@ -248,6 +248,9 @@ namespace eCAL
*/
std::uint16_t get_port() const;

// TODO: Document
asio::ip::tcp::endpoint get_remote_endpoint() const;

/**
* @brief Get the state of this client session.
*
Expand Down
13 changes: 7 additions & 6 deletions ecal/service/ecal_service/src/client_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ namespace eCAL
}
}

State ClientSession::get_state() const { return impl_->get_state(); }
std::uint8_t ClientSession::get_accepted_protocol_version() const { return impl_->get_accepted_protocol_version(); }
int ClientSession::get_queue_size() const { return impl_->get_queue_size(); }
std::string ClientSession::get_address() const { return impl_->get_address(); }
std::uint16_t ClientSession::get_port() const { return impl_->get_port(); }
void ClientSession::stop() { impl_->stop(); }
State ClientSession::get_state() const { return impl_->get_state(); }
std::uint8_t ClientSession::get_accepted_protocol_version() const { return impl_->get_accepted_protocol_version(); }
int ClientSession::get_queue_size() const { return impl_->get_queue_size(); }
std::string ClientSession::get_host() const { return impl_->get_host(); }
asio::ip::tcp::endpoint ClientSession::get_remote_endpoint() const { return impl_->get_remote_endpoint(); }
std::uint16_t ClientSession::get_port() const { return impl_->get_port(); }
void ClientSession::stop() { impl_->stop(); }
} // namespace service
} // namespace eCAL
5 changes: 3 additions & 2 deletions ecal/service/ecal_service/src/client_session_impl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ namespace eCAL
public:
virtual bool async_call_service(const std::shared_ptr<const std::string>& request, const ResponseCallbackT& response_callback) = 0;

virtual std::string get_address() const = 0;
virtual std::uint16_t get_port() const = 0;
virtual std::string get_host() const = 0;
virtual std::uint16_t get_port() const = 0;
virtual asio::ip::tcp::endpoint get_remote_endpoint() const = 0; // TODO: Document

virtual State get_state() const = 0;
virtual std::uint8_t get_accepted_protocol_version() const = 0;
Expand Down
15 changes: 14 additions & 1 deletion ecal/service/ecal_service/src/client_session_impl_v0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ namespace eCAL
// Status API
//////////////////////////////////////

std::string ClientSessionV0::get_address() const
std::string ClientSessionV0::get_host() const
{
const std::lock_guard<std::mutex> chosen_endpoint_lock(chosen_endpoint_mutex_);
return chosen_endpoint_.first;
Expand All @@ -423,6 +423,19 @@ namespace eCAL
return chosen_endpoint_.second;
}

asio::ip::tcp::endpoint ClientSessionV0::get_remote_endpoint() const
{
// form remote endpoint string
{
asio::error_code ec;
const auto endpoint = socket_.remote_endpoint(ec);
if (!ec)
return endpoint;
else
return asio::ip::tcp::endpoint();
}
}

State ClientSessionV0::get_state() const
{
const std::lock_guard<std::mutex> lock(service_state_mutex_);
Expand Down
6 changes: 4 additions & 2 deletions ecal/service/ecal_service/src/client_session_impl_v0.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ namespace eCAL
// Status API
//////////////////////////////////////
public:
std::string get_address() const override;
std::uint16_t get_port() const override;
std::string get_host() const override;
std::uint16_t get_port() const override;
asio::ip::tcp::endpoint get_remote_endpoint() const override; // TODO: Document

State get_state() const override;
std::uint8_t get_accepted_protocol_version() const override;
int get_queue_size() const override;
Expand Down
15 changes: 14 additions & 1 deletion ecal/service/ecal_service/src/client_session_impl_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ namespace eCAL
// Status API
//////////////////////////////////////

std::string ClientSessionV1::get_address() const
std::string ClientSessionV1::get_host() const
{
const std::lock_guard<std::mutex> chosen_endpoint_lock(chosen_endpoint_mutex_);
return chosen_endpoint_.first;
Expand All @@ -549,6 +549,19 @@ namespace eCAL
const std::lock_guard<std::mutex> chosen_endpoint_lock(chosen_endpoint_mutex_);
return chosen_endpoint_.second;
}

asio::ip::tcp::endpoint ClientSessionV1::get_remote_endpoint() const
{
// form remote endpoint string
{
asio::error_code ec;
const auto endpoint = socket_.remote_endpoint(ec);
if (!ec)
return endpoint;
else
return asio::ip::tcp::endpoint();
}
}

State ClientSessionV1::get_state() const
{
Expand Down
6 changes: 4 additions & 2 deletions ecal/service/ecal_service/src/client_session_impl_v1.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ namespace eCAL
// Status API
//////////////////////////////////////
public:
std::string get_address() const override;
std::uint16_t get_port() const override;
std::string get_host() const override;
std::uint16_t get_port() const override;
asio::ip::tcp::endpoint get_remote_endpoint() const override; // TODO: Document

State get_state() const override;
std::uint8_t get_accepted_protocol_version() const override;
int get_queue_size() const override;
Expand Down
11 changes: 8 additions & 3 deletions ecal/service/test/src/ecal_tcp_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1670,8 +1670,9 @@ TEST(ecal_service, Callback_ApiCallsFromCallbacks) // NOLINT
if(client)
{
// We just test if those functions can be called without crashing
auto address = client->get_address();
auto address = client->get_host();
auto port = client->get_port();
auto endpoint = client->get_remote_endpoint();
auto protocol_version = client->get_accepted_protocol_version();
auto queue_size = client->get_queue_size();
auto state = client->get_state();
Expand All @@ -1687,8 +1688,9 @@ TEST(ecal_service, Callback_ApiCallsFromCallbacks) // NOLINT
if (client)
{
// We just test if those functions can be called without crashing
auto address = client->get_address();
auto address = client->get_host();
auto port = client->get_port();
auto endpoint = client->get_remote_endpoint();
auto protocol_version = client->get_accepted_protocol_version();
auto queue_size = client->get_queue_size();
auto state = client->get_state();
Expand Down Expand Up @@ -1813,10 +1815,13 @@ TEST(ecal_service, BackupHost)
EXPECT_EQ(num_client_event_callback_called, 1);

// Check what host the client is connected to
auto connected_host = client->get_address();
auto connected_host = client->get_host();
auto connected_port = client->get_port();
auto endpoint = client->get_remote_endpoint();
EXPECT_EQ(connected_host, "127.0.0.1");
EXPECT_EQ(connected_port, server->get_port());
EXPECT_EQ(endpoint.port(), server->get_port());
EXPECT_EQ(endpoint.address().to_string(), "127.0.0.1");

// Call service and wait for the response
const eCAL::service::ClientSession::ResponseCallbackT response_callback
Expand Down

0 comments on commit 2bed66b

Please sign in to comment.