Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] service client use datainfo #1876

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions ecal/core/include/ecal/ecal_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,8 @@ namespace eCAL
*
* @return True if succeeded, false if not.
**/

// TODO: Provide new MethodCallbackT type using SServiceMethodInformation instead "MethodCallbackT = std::function<int(const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_)>"

ECAL_API_EXPORTED_MEMBER
bool SetMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodCallbackT& callback_);
bool SetMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodInfoCallbackT& callback_);

/**
* @brief Remove method callback.
Expand Down
13 changes: 12 additions & 1 deletion ecal/core/include/ecal/ecal_service_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace eCAL
using ServiceResponseVecT = std::vector<SServiceResponse>; //!< vector of multiple service responses (deprecated)

/**
* @brief Service method callback function type (low level server interface).
* @brief Service method callback function type (low level server interface). (deprecated)
*
* @param method_ The method name.
* @param req_type_ The type of the method request.
Expand All @@ -66,6 +66,17 @@ namespace eCAL
**/
using MethodCallbackT = std::function<int(const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_)>;

/**
* @brief Service method callback function type (low level server interface).
*
* @param method_ The method name.
* @param req_type_ The type of the method request.
* @param resp_type_ The type of the method response.
* @param request_ The request.
* @param response_ The response returned from the method call.
**/
using MethodInfoCallbackT = std::function<int(const std::string& method_, const SDataTypeInformation& req_type_, const SDataTypeInformation& resp_type_, const std::string& request_, std::string& response_)>;

/**
* @brief Service response callback function type (low level client interface). (deprecated)
*
Expand Down
9 changes: 8 additions & 1 deletion ecal/core/src/service/ecal_service_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,18 @@ namespace eCAL
const auto& method_information = method_information_pair.second;

Service::Method method;
method.mname = method_name;
method.mname = method_name;

// old type and descriptor fields
method.req_type = method_information.request_type.name;
method.req_desc = method_information.request_type.descriptor;
method.resp_type = method_information.response_type.name;
method.resp_desc = method_information.response_type.descriptor;

// new type and descriptor fields
method.req_datatype = method_information.request_type;
method.resp_datatype = method_information.response_type;

{
const auto& call_count_iter = m_method_call_count_map.find(method_name);
if (call_count_iter != m_method_call_count_map.end())
Expand Down
2 changes: 1 addition & 1 deletion ecal/core/src/service/ecal_service_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace eCAL
return *this;
}

bool CServiceServer::SetMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodCallbackT& callback_)
bool CServiceServer::SetMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodInfoCallbackT& callback_)
{
if (m_service_server_impl == nullptr) return false;
return m_service_server_impl->AddMethodCallback(method_, method_info_, callback_);
Expand Down
34 changes: 29 additions & 5 deletions ecal/core/src/service/ecal_service_server_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace eCAL
Stop();
}

bool CServiceServerImpl::AddMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodCallbackT& callback_)
bool CServiceServerImpl::AddMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodInfoCallbackT& callback_)
{
#ifndef NDEBUG
Logging::Log(log_level_debug1, "CServiceServerImpl::AddMethodCallback: Adding method callback for method: " + method_);
Expand All @@ -77,10 +77,18 @@ namespace eCAL
if (iter != m_method_map.end())
{
Logging::Log(log_level_warning, "CServiceServerImpl::AddMethodCallback: Method already exists, updating callback: " + method_);

// old type and descriptor fields
iter->second.method.req_type = method_info_.request_type.name;
iter->second.method.req_desc = method_info_.request_type.descriptor;
iter->second.method.resp_type = method_info_.response_type.name;
iter->second.method.resp_desc = method_info_.response_type.descriptor;

// new type and descriptor fields
iter->second.method.req_datatype = method_info_.request_type;
iter->second.method.resp_datatype = method_info_.response_type;

// callback
iter->second.callback = callback_;
}
else
Expand All @@ -89,12 +97,21 @@ namespace eCAL
Logging::Log(log_level_debug1, "CServiceServerImpl::AddMethodCallback: Registering new method: " + method_);
#endif
SMethod method;
method.method.mname = method_;
// method name
method.method.mname = method_;

// old type and descriptor fields
method.method.req_type = method_info_.request_type.name;
method.method.req_desc = method_info_.request_type.descriptor;
method.method.resp_type = method_info_.response_type.name;
method.method.resp_desc = method_info_.response_type.descriptor;
method.callback = callback_;

// new type and descriptor fields
method.method.req_datatype = method_info_.request_type;
method.method.resp_datatype = method_info_.response_type;

// callback
method.callback = callback_;
m_method_map[method_] = method;
}

Expand Down Expand Up @@ -295,11 +312,18 @@ namespace eCAL
for (const auto& iter : m_method_map)
{
Service::Method method;
method.mname = iter.first;
method.mname = iter.first;

// old type and descriptor fields
method.req_type = iter.second.method.req_type;
method.req_desc = iter.second.method.req_desc;
method.resp_type = iter.second.method.resp_type;
method.resp_desc = iter.second.method.resp_desc;

// new type and descriptor fields
method.req_datatype = iter.second.method.req_datatype;
method.resp_datatype = iter.second.method.resp_datatype;

method.call_count = iter.second.method.call_count;
service.methods.push_back(method);
}
Expand Down Expand Up @@ -392,7 +416,7 @@ namespace eCAL
// execute method (outside lock guard)
const std::string& request_s = request.request;
std::string response_s;
const int service_return_state = method.callback(method.method.mname, method.method.req_type, method.method.resp_type, request_s, response_s);
const int service_return_state = method.callback(method.method.mname, method.method.req_datatype, method.method.resp_datatype, request_s, response_s);

// set method call state 'executed'
response_header.state = Service::eMethodCallState::executed;
Expand Down
6 changes: 3 additions & 3 deletions ecal/core/src/service/ecal_service_server_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace eCAL
public:
~CServiceServerImpl();

bool AddMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodCallbackT& callback_);
bool AddMethodCallback(const std::string& method_, const SServiceMethodInformation& method_info_, const MethodInfoCallbackT& callback_);
bool RemoveMethodCallback(const std::string& method_);

// Check connection state of a specific service
Expand Down Expand Up @@ -104,8 +104,8 @@ namespace eCAL
// Server method map and synchronization
struct SMethod
{
Service::Method method;
MethodCallbackT callback;
Service::Method method;
MethodInfoCallbackT callback;
};

using MethodMapT = std::map<std::string, SMethod>;
Expand Down
13 changes: 12 additions & 1 deletion ecal/core/src/service/ecal_service_server_v5_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,18 @@ namespace eCAL
method_info.request_type.name = req_type_;
method_info.response_type.name = resp_type_;

return m_service_server_impl->SetMethodCallback(method_, method_info, callback_);
MethodInfoCallbackT callback =
rex-schilasky marked this conversation as resolved.
Show resolved Hide resolved
[req_type_, resp_type_, callback_](
const std::string& method,
const SDataTypeInformation& req_type_info,
const SDataTypeInformation& resp_type_info,
const std::string& request,
std::string& response) -> int
{
return callback_(method, req_type_info.name, resp_type_info.name, request, response);
};

return m_service_server_impl->SetMethodCallback(method_, method_info, callback);
}

bool CServiceServerImpl::RemMethodCallback(const std::string& method_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <chrono>
#include <thread>

int OnHello(const std::string& /*method_*/, const std::string& /*req_type_*/, const std::string& /*resp_type_*/, const std::string& /*request_*/, std::string& /*response_*/)
int OnHello(const std::string& /*method_*/, const eCAL::SDataTypeInformation& /*req_type_*/, const eCAL::SDataTypeInformation& /*resp_type_*/, const std::string& /*request_*/, std::string& /*response_*/)
{
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <thread>

// method callback
int OnMethodCallback(const std::string& method_, const std::string& /*req_type_*/, const std::string& /*resp_type_*/, const std::string& request_, std::string& response_)
int OnMethodCallback(const std::string& method_, const eCAL::SDataTypeInformation& /*req_type_*/, const eCAL::SDataTypeInformation& /*resp_type_*/, const std::string& request_, std::string& response_)
{
std::cout << "Method called : " << method_ << std::endl;
std::cout << "Request : " << request_ << std::endl << std::endl;
Expand Down
27 changes: 14 additions & 13 deletions ecal/tests/cpp/clientserver_test/src/clientserver_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,19 @@ namespace
typedef std::vector<std::shared_ptr<eCAL::CServiceClient>> ClientVecT;

#if DO_LOGGING
void PrintRequest(const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_)
void PrintRequest(const std::string& method_, const eCAL::SDataTypeInformation& req_type_, const eCAL::SDataTypeInformation& resp_type_, const std::string& request_)
{
std::cout << "------ REQUEST -------" << std::endl;
std::cout << "Method name : " << method_ << std::endl;
std::cout << "Method request type : " << req_type_ << std::endl;
std::cout << "Method response type : " << resp_type_ << std::endl;
std::cout << "Method request type : " << resp_type_ << std::endl;
std::cout << "Method request : " << request_ << std::endl;
std::cout << "Method name : " << method_ << std::endl;
std::cout << "Method request type name : " << req_type_.name << std::endl;
std::cout << "Method request type encoding : " << req_type_.encoding << std::endl;
std::cout << "Method response type name : " << resp_type_.name << std::endl;
std::cout << "Method response type encoding : " << resp_type_.encoding << std::endl;
std::cout << "Method request : " << request_ << std::endl;
std::cout << std::endl;
}
#else
void PrintRequest(const std::string& /*method_*/, const std::string& /*req_type_*/, const std::string& /*resp_type_*/, const std::string& /*request_*/)
void PrintRequest(const std::string& /*method_*/, const eCAL::SDataTypeInformation& /*req_type_*/, const eCAL::SDataTypeInformation& /*resp_type_*/, const std::string& /*request_*/)
{
}
#endif
Expand Down Expand Up @@ -258,7 +259,7 @@ TEST(core_cpp_clientserver, ClientServerBaseCallback)
// method callback function
std::atomic<int> methods_executed(0);
std::atomic<int> method_process_time(0);
auto method_callback = [&](const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_) -> int
auto method_callback = [&](const std::string& method_, const eCAL::SDataTypeInformation& req_type_, const eCAL::SDataTypeInformation& resp_type_, const std::string& request_, std::string& response_) -> int
{
eCAL::Process::SleepMS(method_process_time);
PrintRequest(method_, req_type_, resp_type_, request_);
Expand Down Expand Up @@ -363,7 +364,7 @@ TEST(core_cpp_clientserver, ClientServerBaseCallbackTimeout)
// method callback function
std::atomic<int> methods_executed(0);
std::atomic<int> method_process_time(0);
auto method_callback = [&](const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_) -> int
auto method_callback = [&](const std::string& method_, const eCAL::SDataTypeInformation& req_type_, const eCAL::SDataTypeInformation& resp_type_, const std::string& request_, std::string& response_) -> int
{
eCAL::Process::SleepMS(method_process_time);
PrintRequest(method_, req_type_, resp_type_, request_);
Expand Down Expand Up @@ -519,7 +520,7 @@ TEST(core_cpp_clientserver, ClientServerBaseAsyncCallback)

// method callback function
std::atomic<int> methods_executed(0);
auto method_callback = [&](const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_) -> int
auto method_callback = [&](const std::string& method_, const eCAL::SDataTypeInformation& req_type_, const eCAL::SDataTypeInformation& resp_type_, const std::string& request_, std::string& response_) -> int
{
PrintRequest(method_, req_type_, resp_type_, request_);
response_ = "I answered on " + request_;
Expand Down Expand Up @@ -593,7 +594,7 @@ TEST(core_cpp_clientserver, ClientServerBaseAsync)
atomic_signalable<int> num_service_callbacks_finished(0);
int service_callback_time_ms(0);

auto method_callback = [&](const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_) -> int
auto method_callback = [&](const std::string& method_, const eCAL::SDataTypeInformation& req_type_, const eCAL::SDataTypeInformation& resp_type_, const std::string& request_, std::string& response_) -> int
{
eCAL::Process::SleepMS(service_callback_time_ms);
PrintRequest(method_, req_type_, resp_type_, request_);
Expand Down Expand Up @@ -703,7 +704,7 @@ TEST(core_cpp_clientserver, ClientServerBaseBlocking)

// method callback function
std::atomic<int> methods_executed(0);
auto method_callback = [&](const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_) -> int
auto method_callback = [&](const std::string& method_, const eCAL::SDataTypeInformation& req_type_, const eCAL::SDataTypeInformation& resp_type_, const std::string& request_, std::string& response_) -> int
{
PrintRequest(method_, req_type_, resp_type_, request_);
response_ = "I answer on " + request_;
Expand Down Expand Up @@ -800,7 +801,7 @@ TEST(core_cpp_clientserver, NestedRPCCall)

// request callback function
std::atomic<int> methods_executed(0);
auto method_callback = [&](const std::string& method_, const std::string& req_type_, const std::string& resp_type_, const std::string& request_, std::string& response_) -> int
auto method_callback = [&](const std::string& method_, const eCAL::SDataTypeInformation& req_type_, const eCAL::SDataTypeInformation& resp_type_, const std::string& request_, std::string& response_) -> int
{
PrintRequest(method_, req_type_, resp_type_, request_);
response_ = "I answer on " + request_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ TEST_P(ServicesTestFixture, ServiceExpiration)
{
// create service
eCAL::CServiceServer service("foo::service");
service.SetMethodCallback("foo::method", { { "foo::req_type", "foo::req_desc" }, { "foo::resp_type", "foo::resp_desc" } }, eCAL::MethodCallbackT());
service.SetMethodCallback("foo::method", { { "foo::req_type", "foo::req_desc" }, { "foo::resp_type", "foo::resp_desc" } }, eCAL::MethodInfoCallbackT());

// let's register
eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS);
Expand Down Expand Up @@ -116,7 +116,7 @@ TEST_P(ServicesTestFixture, GetServiceIDs)
service_method_info.request_type.descriptor = "foo::req_desc";
service_method_info.response_type.name = "foo::resp_type";
service_method_info.response_type.descriptor = "foo::resp_desc";
service.SetMethodCallback("method", service_method_info, eCAL::MethodCallbackT());
service.SetMethodCallback("method", service_method_info, eCAL::MethodInfoCallbackT());

// let's register
eCAL::Process::SleepMS(2 * CMN_REGISTRATION_REFRESH_MS);
Expand Down
Loading