Skip to content

Commit

Permalink
Service V1 protocol now supports a list of hosts to connect to
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed May 3, 2024
1 parent 1c56cae commit eaca193
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 116 deletions.
15 changes: 8 additions & 7 deletions ecal/service/ecal_service/include/ecal/service/client_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
#include <cstdint>
#include <map>
#include <memory>

#include <ecal/service/client_session.h>
#include <mutex>
#include <string>
#include <utility>
#include <vector>

#include <ecal/service/client_session.h>

namespace eCAL
{
Expand Down Expand Up @@ -141,16 +143,15 @@ namespace eCAL
* stopped from this central place.
*
* @param protocol_version The protocol version to use for the client session. If 0, the legacy buggy protocol will be used.
* @param address The address of the server to connect to
* @param address The address of the server to connect to TODO: Update documentation
* @param port The port of the server to connect to
* @param event_callback The callback, that will be called, when the client has connected to the server or disconnected from it. The callback will be executed in the io_context thread.
*
* @return A shared_ptr to the newly created ClientSession instance
*/
std::shared_ptr<ClientSession> create_client(std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const ClientSession::EventCallbackT& event_callback);
std::shared_ptr<ClientSession> create_client(std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const ClientSession::EventCallbackT& event_callback);

/**
* @brief Returns the number of managed client sessions
Expand Down
48 changes: 22 additions & 26 deletions ecal/service/ecal_service/include/ecal/service/client_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,43 +133,39 @@ namespace eCAL
*
* @param io_context The io_context to use for the session and all callbacks.
* @param protocol_version The protocol version to use for the session. When this is 0, the legacy buggy protocol is used.
* @param address The address of the server to connect to. May be an IP or a Hostname, IPv6 is supported.
* @param address The address of the server to connect to. May be an IP or a Hostname, IPv6 is supported. TODO: Update documentation
* @param port The port of the server to connect to.
* @param event_callback The callback to be called when the session's state changes, i.e. when the session successfully connected to a server or disconnected from it.
* @param logger The logger to use for logging.
* @param delete_callback The callback to be called when the session is deleted. This is useful for the eCAL::service::ClientManager to keep track of the number of active sessions.
*
* @return The new ClientSession instance as a shared_ptr.
*/
static std::shared_ptr<ClientSession> create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const LoggerT& logger
, const DeleteCallbackT& delete_callback);
static std::shared_ptr<ClientSession> create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const LoggerT& logger
, const DeleteCallbackT& delete_callback);

static std::shared_ptr<ClientSession> create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const LoggerT& logger = default_logger("Service Client"));
static std::shared_ptr<ClientSession> create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const LoggerT& logger = default_logger("Service Client"));

static std::shared_ptr<ClientSession> create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const DeleteCallbackT& delete_callback);
static std::shared_ptr<ClientSession> create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const DeleteCallbackT& delete_callback);

protected:
ClientSession(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const LoggerT& logger);
ClientSession(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const LoggerT& logger);

public:
// Delete copy constructor and assignment operator
Expand Down
9 changes: 4 additions & 5 deletions ecal/service/ecal_service/src/client_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ namespace eCAL
///////////////////////////////////////////////////////
// Public API
///////////////////////////////////////////////////////
std::shared_ptr<ClientSession> ClientManager::create_client(std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const ClientSession::EventCallbackT& event_callback)
std::shared_ptr<ClientSession> ClientManager::create_client(std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const ClientSession::EventCallbackT& event_callback)
{
const std::lock_guard<std::mutex> lock(client_manager_mutex_);
if (stopped_)
Expand All @@ -74,7 +73,7 @@ namespace eCAL
}
};

auto client = ClientSession::create(io_context_, protocol_version, address, port, event_callback, logger_, deleter);
auto client = ClientSession::create(io_context_, protocol_version, server_list, event_callback, logger_, deleter);
sessions_.emplace(client.get(), client);
return client;
}
Expand Down
57 changes: 27 additions & 30 deletions ecal/service/ecal_service/src/client_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,54 @@ namespace eCAL
{
namespace service
{
std::shared_ptr<ClientSession> ClientSession::create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const LoggerT& logger
, const DeleteCallbackT& delete_callback)
std::shared_ptr<ClientSession> ClientSession::create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const LoggerT& logger
, const DeleteCallbackT& delete_callback)
{
auto deleter = [delete_callback](ClientSession* session)
{
delete_callback(session);
delete session; // NOLINT(cppcoreguidelines-owning-memory)
};

return std::shared_ptr<ClientSession>(new ClientSession(io_context, protocol_version, address, port, event_callback, logger), deleter);
return std::shared_ptr<ClientSession>(new ClientSession(io_context, protocol_version, server_list, event_callback, logger), deleter);
}

std::shared_ptr<ClientSession> ClientSession::create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const LoggerT& logger)
std::shared_ptr<ClientSession> ClientSession::create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const LoggerT& logger)
{
return std::shared_ptr<ClientSession>(new ClientSession(io_context, protocol_version, address, port, event_callback, logger));
return std::shared_ptr<ClientSession>(new ClientSession(io_context, protocol_version, server_list, event_callback, logger));
}

std::shared_ptr<ClientSession> ClientSession::create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const DeleteCallbackT& delete_callback)
std::shared_ptr<ClientSession> ClientSession::create(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const DeleteCallbackT& delete_callback)
{
return ClientSession::create(io_context, protocol_version, address, port, event_callback, default_logger("Service Client"), delete_callback);
return ClientSession::create(io_context, protocol_version, server_list, event_callback, default_logger("Service Client"), delete_callback);
}

ClientSession::ClientSession(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::string& address
, std::uint16_t port
, const EventCallbackT& event_callback
, const LoggerT& logger)
ClientSession::ClientSession(const std::shared_ptr<asio::io_context>& io_context
, std::uint8_t protocol_version
, const std::vector<std::pair<std::string, std::uint16_t>>& server_list
, const EventCallbackT& event_callback
, const LoggerT& logger)
{
if (protocol_version == 0)
{
impl_ = ClientSessionV0::create(io_context, address, port, event_callback, logger);
// TODO: Enable V0 protocol again
//impl_ = ClientSessionV0::create(io_context, address, port, event_callback, logger);
}
else
{
impl_ = ClientSessionV1::create(io_context, address, port, event_callback, logger);
impl_ = ClientSessionV1::create(io_context, server_list, event_callback, logger);
}
}

Expand Down
Loading

0 comments on commit eaca193

Please sign in to comment.