Skip to content

Commit

Permalink
Fixed a few things to satisfy tests, but there is still a lot to do
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Jun 30, 2023
1 parent 51abace commit 080f1d9
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 10 deletions.
11 changes: 10 additions & 1 deletion ecal/core/src/ecal_globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,18 @@ namespace eCAL
// start destruction
if (monitoring_instance) monitoring_instance->Destroy();
if (timegate_instance) timegate_instance->Destroy();

// The order here is EXTREMELY important! First, the actual service
// implementation must be stopped (->Service Manager), then the
// clientgate/servicegat. The callbacks in the service implementation carry
// raw pointers to the gate's functions, so we must make sure that everything
// has been executed, before we delete the gates.
//
// TODO: just make it a share pointer... it will be soooo much better.
eCAL::service::ServiceManager::instance()->stop();
if (clientgate_instance) clientgate_instance->Destroy();
if (servicegate_instance) servicegate_instance->Destroy();
eCAL::service::ServiceManager::instance()->stop();

if (pubgate_instance) pubgate_instance->Destroy();
if (subgate_instance) subgate_instance->Destroy();
if (registration_receiver_instance) registration_receiver_instance->Destroy();
Expand Down
20 changes: 17 additions & 3 deletions ecal/core/src/service/ecal_service_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,23 @@ namespace eCAL
// Event callback
// TODO: Make an actual implementation
const eCAL::service::ClientSession::EventCallbackT event_callback
= []
(eCAL_Client_Event /*event*/, const std::string& /*message*/) -> void
{};
= [this, service_ = iter] // TODO: using the this pointer here is extremely unsafe, as it actually forces us to manage the lifetime of this object
(eCAL_Client_Event event, const std::string& message) -> void
{

// TODO: I have no idea why, but for some reason the event callbacks of the actual connetions are not even used. The connect / disconnect callbacks are executed whenever a new connection is found, and not when the client has actually connected or disconnected. I am preserving the previous behavior.

//const std::lock_guard<std::mutex> callback_map_lock(this->m_event_callback_map_sync);
//auto callback_it = this->m_event_callback_map.find(event);
//if (callback_it != m_event_callback_map.end())
//{
// SClientEventCallbackData sdata;
// sdata.type = event;
// sdata.time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
// sdata.attr = service_;
// (callback_it->second)(m_service_name.c_str(), &sdata);
//}
};

// Only connect via V0 protocol / V0 port, if V1 port is not available
const auto protocol_version = (iter.tcp_port_v1 != 0 ? iter.version : 0);
Expand Down
4 changes: 2 additions & 2 deletions ecal/core/src/service/ecal_service_server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ namespace eCAL

// Create callback functions
const eCAL::service::Server::EventCallbackT event_callback
= [this](eCAL_Server_Event event, const std::string& message)
= [this](eCAL_Server_Event event, const std::string& message) // TODO: incorporating the this pointer here is very unsafe, as it would actually force us to manage the memory of "this", too
{
this->EventCallback(event, message);
};

const eCAL::service::Server::ServiceCallbackT service_callback
= [this](const std::shared_ptr<const std::string>& request, const std::shared_ptr<std::string>& response) -> int
= [this](const std::shared_ptr<const std::string>& request, const std::shared_ptr<std::string>& response) -> int // TODO: incorporating the this pointer here is very unsafe, as it would actually force us to manage the memory of "this", too
{
// TODO: I can change the signature of the RequestCallback to use shared_ptr
return this->RequestCallback(*request, *response);
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/service/ecal_service_singleton_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace eCAL
// Member variables
////////////////////////////////////////////////////////////
private:
static const size_t num_io_threads = 4;
static constexpr size_t num_io_threads = 4;

std::mutex singleton_mutex;

Expand Down
14 changes: 13 additions & 1 deletion ecal/service/src/server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,19 @@ namespace eCAL
int ServerImpl::get_connection_count() const
{
const std::lock_guard<std::mutex> session_list_lock(session_list_mutex_);
return static_cast<int>(session_list_.size());
int connection_count = 0;

// Iterate over all sessions and count the ones that are still alive
for (const auto& session : session_list_)
{
auto session_ptr = session.lock();
if (session_ptr && (session_ptr->get_state() != eCAL::service::State::FAILED))
{
++connection_count;
}
}

return connection_count;
}

std::uint16_t ServerImpl::get_port() const
Expand Down
3 changes: 3 additions & 0 deletions ecal/service/src/server_session_impl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "ecal/cimpl/ecal_callback_cimpl.h"

#include <ecal/service/server_session_types.h>
#include <ecal/service/state.h>

namespace eCAL
{
Expand Down Expand Up @@ -80,6 +81,8 @@ namespace eCAL
virtual void start() = 0;
virtual void stop() = 0;

virtual eCAL::service::State get_state() const = 0;

/////////////////////////////////////
// Member variables
/////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions ecal/service/src/server_session_impl_v0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ namespace eCAL
}
}

eCAL::service::State ServerSessionV0::get_state() const
{
return state_;
}

void ServerSessionV0::handle_read(const asio::error_code& ec, size_t bytes_transferred, const std::shared_ptr<std::string>& request)
{
ECAL_SERVICE_LOG_DEBUG_VERBOSE(logger_, "[" + get_connection_info_string(socket_) + "] " + "Received " + std::to_string(bytes_transferred) + " bytes.");
Expand Down
4 changes: 3 additions & 1 deletion ecal/service/src/server_session_impl_v0.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ namespace eCAL
void start() override;
void stop() override;

eCAL::service::State get_state() const override;

private:
void handle_read(const asio::error_code& ec, size_t bytes_transferred, const std::shared_ptr<std::string>& request);

Expand All @@ -84,7 +86,7 @@ namespace eCAL
private:
const LoggerT logger_;

State state_;
std::atomic<State> state_;

enum { max_length = 64 * 1024 };
char data_[max_length]{};
Expand Down
5 changes: 5 additions & 0 deletions ecal/service/src/server_session_impl_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ namespace eCAL
}
}

eCAL::service::State ServerSessionV1::get_state() const
{
return state_;
}

void ServerSessionV1::receive_handshake_request()
{
ECAL_SERVICE_LOG_DEBUG(logger_, "[" + get_connection_info_string(socket_) + "] " + "Waiting for protocol handshake request...");
Expand Down
4 changes: 3 additions & 1 deletion ecal/service/src/server_session_impl_v1.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ namespace eCAL
void start() override;
void stop() override;

eCAL::service::State get_state() const override;

private:
void receive_handshake_request();
void send_handshake_response();
Expand All @@ -87,7 +89,7 @@ namespace eCAL
static constexpr std::uint8_t MIN_SUPPORTED_PROTOCOL_VERSION = 1;
static constexpr std::uint8_t MAX_SUPPORTED_PROTOCOL_VERSION = 1;

State state_;
std::atomic<State> state_;
std::uint8_t accepted_protocol_version_;

const LoggerT logger_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

#include "atomic_signalable.h"

// TODO: Test the is_connected() function of the server. It should return false, directly after the disconnected callback has been fired.

eCAL::service::LoggerT critical_logger(const std::string& node_name)
{
return [node_name](const eCAL::service::LogLevel log_level, const std::string& message)
Expand Down

0 comments on commit 080f1d9

Please sign in to comment.