Skip to content

Commit

Permalink
[core] Combine TopicID in Pubs/Sub-Event Callback struct, and SMethod…
Browse files Browse the repository at this point in the history
…InfoID in Server/Client Callback struct.
  • Loading branch information
KerstinKeller committed Jan 17, 2025
1 parent a77ae1f commit cb9ef18
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 35 deletions.
38 changes: 22 additions & 16 deletions ecal/core/include/ecal/ecal_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ namespace eCAL
**/
struct SPubEventCallbackData
{
ePublisherEvent type{ ePublisherEvent::none }; //!< publisher event type
long long time{ 0 }; //!< publisher event time in µs
long long clock{ 0 }; //!< publisher event clock
SDataTypeInformation tdatatype; //!< datatype description of the connected subscriber (for pub_event_update_connection only)
Registration::STopicId publisher_id; //!< ID of the publisher triggering the event
ePublisherEvent type{ ePublisherEvent::none }; //!< publisher event type
long long time{ 0 }; //!< publisher event time in µs
long long clock{ 0 }; //!< publisher event clock
SDataTypeInformation tdatatype; //!< datatype description of the connected subscriber (for pub_event_update_connection only)
};

/**
Expand All @@ -165,17 +166,18 @@ namespace eCAL
* @param topic_id_ The topic id struct of the received message.
* @param data_ Event callback data structure with the event specific information.
**/
using PubEventCallbackT = std::function<void(const Registration::STopicId& topic_id_, const SPubEventCallbackData& data_)>;
using PubEventCallbackT = std::function<void(const SPubEventCallbackData& data_)>;

/**
* @brief eCAL subscriber event callback struct.
**/
struct SSubEventCallbackData
{
eSubscriberEvent type{ eSubscriberEvent::none }; //!< subscriber event type
long long time{ 0 }; //!< subscriber event time in µs
long long clock{ 0 }; //!< subscriber event clock
SDataTypeInformation tdatatype; //!< topic information of the connected subscriber (for sub_event_update_connection only)
Registration::STopicId subscriber_id; //!< ID of the publisher triggering the event
eSubscriberEvent type{ eSubscriberEvent::none }; //!< subscriber event type
long long time{ 0 }; //!< subscriber event time in µs
long long clock{ 0 }; //!< subscriber event clock
SDataTypeInformation tdatatype; //!< topic information of the connected subscriber (for sub_event_update_connection only)
};

/**
Expand All @@ -184,15 +186,17 @@ namespace eCAL
* @param topic_id_ The topic id struct of the received message.
* @param data_ Event callback data structure with the event specific information.
**/
using SubEventCallbackT = std::function<void(const Registration::STopicId& topic_id_, const SSubEventCallbackData& data_)>;
using SubEventCallbackT = std::function<void(const SSubEventCallbackData& data_)>;

/**
* @brief eCAL client event callback struct.
**/
struct SClientEventCallbackData
{
eClientEvent type{ eClientEvent::none }; //!< event type
long long time = 0; //!< event time in µs
// TODO: shouldn't this be a SServiceId? No methods involved?
Registration::SServiceMethodId client_id; //!< ID of the client triggering the event
eClientEvent type{ eClientEvent::none }; //!< event type
long long time = 0; //!< event time in µs
};

/**
Expand All @@ -201,15 +205,17 @@ namespace eCAL
* @param service_id_ The service id struct of the connection that triggered the event.
* @param data_ Event callback data structure with the event specific information.
**/
using ClientEventCallbackT = std::function<void(const Registration::SServiceMethodId& service_id_, const SClientEventCallbackData& data_)>;
using ClientEventCallbackT = std::function<void(const SClientEventCallbackData& data_)>;

/**
* @brief eCAL server event callback struct.
**/
struct SServerEventCallbackData
{
eServerEvent type{ eServerEvent::none }; //!< event type
long long time = 0; //!< event time in µs
// TODO: shouldn't this be a SServiceId? No methods involved?
Registration::SServiceMethodId server_id; //!< ID of the server triggering the event
eServerEvent type{ eServerEvent::none }; //!< event type
long long time = 0; //!< event time in µs
};

/**
Expand All @@ -218,6 +224,6 @@ namespace eCAL
* @param service_id_ The service id struct of the connection that triggered the event.
* @param data_ Event callback data structure with the event specific information.
**/
using ServerEventCallbackT = std::function<void(const Registration::SServiceMethodId& service_id_, const struct SServerEventCallbackData& data_)>;
using ServerEventCallbackT = std::function<void(const struct SServerEventCallbackData& data_)>;
}
}
4 changes: 2 additions & 2 deletions ecal/core/src/pubsub/ecal_publisher_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,15 +744,15 @@ namespace eCAL
data.clock = 0;
data.tdatatype = data_type_info_;

Registration::STopicId topic_id;
Registration::STopicId& topic_id = data.publisher_id;
topic_id.topic_id.entity_id = subscription_info_.entity_id;
topic_id.topic_id.process_id = subscription_info_.process_id;
topic_id.topic_id.host_name = subscription_info_.host_name;
topic_id.topic_name = m_attributes.topic_name;
const std::lock_guard<std::mutex> lock(m_event_id_callback_mutex);

// call event callback
m_event_id_callback(topic_id, data);
m_event_id_callback(data);
}

// deprecated event handling with topic name
Expand Down
9 changes: 5 additions & 4 deletions ecal/core/src/pubsub/ecal_subscriber_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,15 +793,15 @@ namespace eCAL
data.clock = 0;
data.tdatatype = data_type_info_;

Registration::STopicId topic_id;
Registration::STopicId& topic_id = data.subscriber_id;
topic_id.topic_id.entity_id = publication_info_.entity_id;
topic_id.topic_id.process_id = publication_info_.process_id;
topic_id.topic_id.host_name = publication_info_.host_name;
topic_id.topic_name = m_attributes.topic_name;
const std::lock_guard<std::mutex> lock(m_event_id_callback_mutex);

// call event callback
m_event_id_callback(topic_id, data);
m_event_id_callback(data);
}

// deprecated event handling with topic name
Expand Down Expand Up @@ -914,12 +914,13 @@ namespace eCAL
data.time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
data.clock = current_clock_;

Registration::STopicId topic_id;
// TODO: Event data not filled correctly here! We should have information about the TopicID of the connection where data was dropped.
Registration::STopicId& topic_id = data.subscriber_id;
topic_id.topic_name = m_attributes.topic_name;
const std::lock_guard<std::mutex> lock(m_event_id_callback_mutex);

// call event callback
m_event_id_callback(topic_id, data);
m_event_id_callback(data);
}

// deprecated event handling with topic name
Expand Down
6 changes: 5 additions & 1 deletion ecal/core/src/service/ecal_service_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,12 @@ namespace eCAL

SClientEventCallbackData callback_data;
callback_data.type = event_type_;
// TODO: is it correct to use the system time here? do we not need some kind of eCAL time???
callback_data.time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
m_event_callback(service_id_, callback_data);

// TODO: We would like to avoid copying here!
callback_data.client_id = service_id_;
m_event_callback(callback_data);
}

std::shared_ptr<CServiceClientImpl::SResponseData> CServiceClientImpl::PrepareInitialResponse(const SClient& client_, const std::string& method_name_)
Expand Down
12 changes: 6 additions & 6 deletions ecal/core/src/service/ecal_service_client_v5_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ namespace eCAL
Logging::Log(Logging::log_level_debug1, "v5::CServiceClientImpl: Creating service client with name: " + service_name_);

// Define the event callback to pass to CServiceClient
v6::ClientEventCallbackT event_callback = [this](const Registration::SServiceMethodId& service_id_, const v6::SClientEventCallbackData& data_)
v6::ClientEventCallbackT event_callback = [this](const v6::SClientEventCallbackData& data_)
{
Logging::Log(Logging::log_level_debug2, "v5::CServiceClientImpl: Event callback triggered for event type: " + to_string(data_.type));

Expand All @@ -110,11 +110,11 @@ namespace eCAL
SClientEventCallbackData event_data;
event_data.type = data_.type;
event_data.time = data_.time;
event_data.attr.hname = service_id_.service_id.host_name;
event_data.attr.sname = service_id_.service_name;
event_data.attr.pid = service_id_.service_id.process_id;
event_data.attr.sid = service_id_.service_id.entity_id;
callback->second(service_id_.service_name.c_str(), &event_data);
event_data.attr.hname = data_.client_id.service_id.host_name;
event_data.attr.sname = data_.client_id.service_name;
event_data.attr.pid = data_.client_id.service_id.process_id;
event_data.attr.sid = data_.client_id.service_id.entity_id;
callback->second(data_.client_id.service_name.c_str(), &event_data);
}
};

Expand Down
5 changes: 4 additions & 1 deletion ecal/core/src/service/ecal_service_server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ namespace eCAL
SServerEventCallbackData callback_data;
callback_data.type = event_type_;
callback_data.time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
m_event_callback(service_id_, callback_data);

// TODO: we should try to avoid a copy here
callback_data.server_id = service_id_;
m_event_callback(callback_data);
}
}
4 changes: 2 additions & 2 deletions ecal/core/src/service/ecal_service_server_v5_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace eCAL
}

// Define the event callback to pass to CServiceClient
v6::ServerEventCallbackT event_callback = [this](const Registration::SServiceMethodId& service_id_, const v6::SServerEventCallbackData& data_)
v6::ServerEventCallbackT event_callback = [this](const v6::SServerEventCallbackData& data_)
{
Logging::Log(Logging::log_level_debug2, "v5::CServiceServerImpl: Event callback triggered for event type: " + to_string(data_.type));

Expand All @@ -73,7 +73,7 @@ namespace eCAL
SServerEventCallbackData event_data;
event_data.type = data_.type;
event_data.time = data_.time;
callback->second(service_id_.service_name.c_str(), &event_data);
callback->second(data_.server_id.service_name.c_str(), &event_data);
}
};

Expand Down
6 changes: 3 additions & 3 deletions ecal/tests/cpp/clientserver_test/src/clientserver_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ TEST(core_cpp_clientserver, ClientConnectEvent)
// client event callback for connect events
atomic_signalable<int> event_connected_fired (0);
atomic_signalable<int> event_disconnected_fired(0);
auto event_callback = [&](const eCAL::Registration::SServiceMethodId& /*service_id_*/, const struct eCAL::SClientEventCallbackData& data_)
auto event_callback = [&](const struct eCAL::SClientEventCallbackData& data_)
{
switch (data_.type)
{
Expand Down Expand Up @@ -181,7 +181,7 @@ TEST(core_cpp_clientserver, ServerConnectEvent)
// server event callback for connect events
atomic_signalable<int> event_connected_fired (0);
atomic_signalable<int> event_disconnected_fired(0);
auto event_callback = [&](const eCAL::Registration::SServiceMethodId& /*service_id_*/, const struct eCAL::SServerEventCallbackData& data_) -> void
auto event_callback = [&](const struct eCAL::SServerEventCallbackData& data_) -> void
{
switch (data_.type)
{
Expand Down Expand Up @@ -384,7 +384,7 @@ TEST(core_cpp_clientserver, ClientServerBaseCallbackTimeout)

// event callback for timeout event
std::atomic<int> timeout_fired(0);
auto event_callback = [&](const eCAL::Registration::SServiceMethodId& /*service_id_*/, const struct eCAL::SClientEventCallbackData& data_) -> void
auto event_callback = [&](const struct eCAL::SClientEventCallbackData& data_) -> void
{
if (data_.type == eCAL::eClientEvent::timeout)
{
Expand Down

0 comments on commit cb9ef18

Please sign in to comment.