Skip to content

Commit

Permalink
draft API with entity ids aligned with public client api (will be red…
Browse files Browse the repository at this point in the history
…esigned in next iteration, for now just for testing)
  • Loading branch information
rex-schilasky committed Nov 7, 2024
1 parent 72262ed commit cf7f3bc
Show file tree
Hide file tree
Showing 8 changed files with 362 additions and 159 deletions.
55 changes: 26 additions & 29 deletions ecal/core/include/ecal/ecal_client.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 Continental Corporation
* Copyright (C) 2016 - 2024 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -106,47 +106,35 @@ namespace eCAL
ECAL_API bool Destroy();

/**
* @brief Change the host name filter for that client instance
* @brief Get the unique service id's for all matching services
*
* @param host_name_ Host name filter (empty == all hosts)
*
* @return True if successful.
* @return Service id's of all matching services
**/
ECAL_API bool SetHostName(const std::string& host_name_);
ECAL_API std::vector<Registration::SEntityId> GetServiceIDs();

/**
* @brief Call a method of this service, responses will be returned by callback.
* @brief Blocking call specific service method, response will be returned as pair<bool, SServiceReponse>
*
* @param method_name_ Method name.
* @param request_ Request string.
* @param timeout_ Maximum time before operation returns (in milliseconds, -1 means infinite).
* @param entity_id_ Unique service entity (service id, process id, host name).
* @param method_name_ Method name.
* @param request_ Request string.
* @param timeout_ Maximum time before operation returns (in milliseconds, -1 means infinite).
*
* @return True if successful.
* @return success state and service response
**/
ECAL_API bool Call(const std::string& method_name_, const std::string& request_, int timeout_ = -1);
ECAL_API std::pair<bool, SServiceResponse> CallWithResponse(const Registration::SEntityId& entity_id_, const std::string& method_name_, const std::string& request_, int timeout_ = -1);

/**
* @brief Call a method of this service, all responses will be returned in service_response_vec_.
*
* @param method_name_ Method name.
* @param request_ Request string.
* @param timeout_ Maximum time before operation returns (in milliseconds, -1 means infinite).
* @param [out] service_response_vec_ Response vector containing service responses from every called service (null pointer == no response).
*
* @return True if successful.
**/
ECAL_API bool Call(const std::string& method_name_, const std::string& request_, int timeout_, ServiceResponseVecT* service_response_vec_);

/**
* @brief Call a method of this service asynchronously, responses will be returned by callback.
* @brief Blocking call specific service method, using callback
*
* @param entity_id_ Unique service entity (service id, process id, host name).
* @param method_name_ Method name.
* @param request_ Request string.
* @param timeout_ Maximum time before operation returns (in milliseconds, -1 means infinite) - NOT SUPPORTED YET.
* @param timeout_ Maximum time before operation returns (in milliseconds, -1 means infinite).
*
* @return True if successful.
* @return True if successful.
**/
ECAL_API bool CallAsync(const std::string& method_name_, const std::string& request_, int timeout_ = -1);
ECAL_API bool CallWithCallback(const Registration::SEntityId& entity_id_, const std::string& method_name_, const std::string& request_, int timeout_ = -1);

/**
* @brief Add server response callback.
Expand Down Expand Up @@ -190,6 +178,15 @@ namespace eCAL
**/
ECAL_API std::string GetServiceName();

/**
* @brief Check connection state of a specific server connection.
*
* @param entity_id_ Unique service entity (service id, process id, host name).
*
* @return True if connected, false if not.
**/
ECAL_API bool IsConnected(const Registration::SEntityId& entity_id_);

/**
* @brief Check connection state.
*
Expand All @@ -199,6 +196,6 @@ namespace eCAL

protected:
std::shared_ptr<eCAL::CServiceClientImpl> m_service_client_impl;
bool m_created;
std::string m_service_name;
};
}
3 changes: 1 addition & 2 deletions ecal/core/include/ecal/ecal_service_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ namespace eCAL
eCallState call_state; //!< call state (see eCallState)
std::string response; //!< service response
};
using ServiceResponseVecT = std::vector<SServiceResponse>; //!< vector of multiple service responses

/**
* @brief Service method callback function type (low level server interface).
Expand All @@ -71,7 +70,7 @@ namespace eCAL
* @param service_id_ Unique service id (entity id, process id, host name, service name, method name)
* @param service_response_ Service response struct containing the (responding) server informations and the response itself.
**/
using ResponseCallbackT = std::function<void (const Registration::SEntityId&, const struct SServiceResponse &)>;
using ResponseCallbackT = std::function<void (const Registration::SEntityId &, const struct SServiceResponse &)>;

using ServiceMethodInformationMapT = std::map<std::string, SServiceMethodInformation>;
}
52 changes: 24 additions & 28 deletions ecal/core/src/service/ecal_clientgate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,43 +54,37 @@ namespace eCAL
if (!m_created) return;

// destroy all remaining clients
const std::shared_lock<std::shared_timed_mutex> lock(m_client_set_sync);
for (const auto& client : m_client_set)
{
client->Stop();
}
const std::shared_lock<std::shared_timed_mutex> lock(m_service_client_map_sync);
m_service_client_map.clear();

m_created = false;
}

bool CClientGate::Register(CServiceClientImpl* client_)
bool CClientGate::Register(const std::string& service_name_, const std::shared_ptr<CServiceClientImpl>& client_)
{
if (!m_created) return(false);

// register internal client
const std::unique_lock<std::shared_timed_mutex> lock(m_client_set_sync);
m_client_set.insert(client_);
const std::unique_lock<std::shared_timed_mutex> lock(m_service_client_map_sync);
m_service_client_map.emplace(std::pair<std::string, std::shared_ptr<CServiceClientImpl>>(service_name_, client_));

return(true);
}

bool CClientGate::Unregister(CServiceClientImpl* client_)
bool CClientGate::Unregister(const std::string& service_name_, const std::shared_ptr<CServiceClientImpl>& client_)
{
if (!m_created) return(false);
bool ret_state(false);
bool ret_state = false;

// unregister internal service
const std::unique_lock<std::shared_timed_mutex> lock(m_client_set_sync);
for (auto iter = m_client_set.begin(); iter != m_client_set.end();)
const std::unique_lock<std::shared_timed_mutex> lock(m_service_client_map_sync);
auto res = m_service_client_map.equal_range(service_name_);
for (auto iter = res.first; iter != res.second; ++iter)
{
if (*iter == client_)
if (iter->second == client_)
{
iter = m_client_set.erase(iter);
m_service_client_map.erase(iter);
ret_state = true;
}
else
{
iter++;
break;
}
}

Expand Down Expand Up @@ -130,13 +124,15 @@ namespace eCAL

// inform matching clients
{
const std::shared_lock<std::shared_timed_mutex> lock(m_client_set_sync);
for (const auto& iter : m_client_set)
const std::shared_lock<std::shared_timed_mutex> lock(m_service_client_map_sync);
auto res = m_service_client_map.equal_range(service.sname);
for (ServiceNameServiceImplMapT::const_iterator iter = res.first; iter != res.second; ++iter)
{
if (iter->GetServiceName() == service.sname)
{
iter->RegisterService(service.key, service);
}
Registration::SEntityId service_entity;
service_entity.entity_id = service.sid;
service_entity.process_id = service.pid;
service_entity.host_name = service.hname;
iter->second->RegisterService(service_entity, service);
}
}
}
Expand All @@ -146,10 +142,10 @@ namespace eCAL
if (!m_created) return;

// read service registrations
std::shared_lock<std::shared_timed_mutex> const lock(m_client_set_sync);
for (const auto& service_client_impl : m_client_set)
const std::shared_lock<std::shared_timed_mutex> lock(m_service_client_map_sync);
for (const auto& iter : m_service_client_map)
{
reg_sample_list_.push_back(service_client_impl->GetRegistration());
reg_sample_list_.push_back(iter.second->GetRegistration());
}
}
}
11 changes: 6 additions & 5 deletions ecal/core/src/service/ecal_clientgate.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <ecal/ecal_callback.h>

#include <atomic>
#include <map>
#include <set>
#include <shared_mutex>
#include <string>
Expand All @@ -48,8 +49,8 @@ namespace eCAL
void Start();
void Stop();

bool Register (CServiceClientImpl* client_);
bool Unregister(CServiceClientImpl* client_);
bool Register (const std::string& service_name_, const std::shared_ptr<CServiceClientImpl>& client_);
bool Unregister(const std::string& service_name_, const std::shared_ptr<CServiceClientImpl>& client_);

void ApplyServiceRegistration(const Registration::Sample& ecal_sample_);

Expand All @@ -58,9 +59,9 @@ namespace eCAL
protected:
static std::atomic<bool> m_created;

using ServiceNameServiceImplSetT = std::set<CServiceClientImpl *>;
std::shared_timed_mutex m_client_set_sync;
ServiceNameServiceImplSetT m_client_set;
using ServiceNameServiceImplMapT = std::multimap<std::string, std::shared_ptr<CServiceClientImpl>>;
std::shared_timed_mutex m_service_client_map_sync;
ServiceNameServiceImplMapT m_service_client_map;

using ConnectedMapT = Util::CExpirationMap<std::string, SServiceAttr>;
std::shared_timed_mutex m_service_register_map_sync;
Expand Down
Loading

0 comments on commit cf7f3bc

Please sign in to comment.