Skip to content

Commit

Permalink
First implementation of Publisher config
Browse files Browse the repository at this point in the history
  • Loading branch information
ashariff-11 committed Jul 29, 2024
1 parent 622c97d commit 3189cda
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 35 deletions.
6 changes: 2 additions & 4 deletions lang/python/nanobind_core/samples/minimal_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
# define the server method "foo" function
def foo_req_callback(method_name, req_type, resp_type, request):
print("'DemoService' method '{}' called with {}".format(method_name, request))
#return True #, bytes("thank you for calling foo :-)", "ascii")
return 0, "pong"
return True, bytes("thank you for calling foo :-)", "ascii")

# define the server method "ping" function
def ping_req_callback(method_name, req_type, resp_type, request):
Expand All @@ -49,8 +48,7 @@ def main():
server = ecal_core.ServiceServer("DemoService")

# define the server methods and connect them to the callbacks
server.add_method_callback("foo", "string", "string", foo_req_callback)
server.add_method_callback("ping", "ping_type", "pong_type", ping_req_callback)
server.add_method_callback("ping", "ping_type", "pong_type", "ping_req_callback", ping_req_callback)

# idle
while(ecal_core.ok()):
Expand Down
34 changes: 27 additions & 7 deletions lang/python/nanobind_core/src/modules/module_publisher_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,30 @@

void AddPublisherConfigStructToModule(nanobind::module_& module)
{
// Struct eCAL::SDataTypeInformation
nanobind::class_<eCAL::CNBPublisherConfigStruct>(module, "PublisherConfiguration");
// .def(nanobind::init<>())
// .def_rw("name", &eCAL::CNBDataTypeInformation::name)
// .def_rw("encoding", &eCAL::CNBDataTypeInformation::encoding)
// .def_rw("descriptor", &eCAL::CNBDataTypeInformation::descriptor);
}
nanobind::class_<eCAL::Publisher::SHM::CNBSHMConfiguration>(module, "SHMConfiguration")
.def(nanobind::init<>())
.def_rw("enable", &eCAL::Publisher::SHM::CNBSHMConfiguration::enable)
.def_rw("zero_copy_mode", &eCAL::Publisher::SHM::CNBSHMConfiguration::zero_copy_mode)
.def_rw("acknowledge_timeout_ms", &eCAL::Publisher::SHM::CNBSHMConfiguration::acknowledge_timeout_ms)
.def_rw("memfile_min_size_bytes", &eCAL::Publisher::SHM::CNBSHMConfiguration::memfile_min_size_bytes)
.def_rw("memfile_reserve_percent", &eCAL::Publisher::SHM::CNBSHMConfiguration::memfile_reserve_percent)
.def_rw("memfile_buffer_count", &eCAL::Publisher::SHM::CNBSHMConfiguration::memfile_buffer_count);

nanobind::class_<eCAL::Publisher::UDP::CNBUDPConfiguration>(module, "UDPConfiguration")
.def(nanobind::init<>())
.def_rw("enable", &eCAL::Publisher::UDP::CNBUDPConfiguration::enable)
.def_rw("loopback", &eCAL::Publisher::UDP::CNBUDPConfiguration::loopback)
.def_rw("sndbuf_size_bytes", &eCAL::Publisher::UDP::CNBUDPConfiguration::sndbuf_size_bytes);

nanobind::class_<eCAL::Publisher::TCP::CNBTCPConfiguration>(module, "TCPConfiguration")
.def(nanobind::init<>())
.def_rw("enable", &eCAL::Publisher::TCP::CNBTCPConfiguration::enable);

nanobind::class_<eCAL::Publisher::CNBPublisherConfiguration>(module, "PublisherConfiguration")
.def(nanobind::init<>())
.def_rw("shm", &eCAL::Publisher::CNBPublisherConfiguration::shm)
.def_rw("udp", &eCAL::Publisher::CNBPublisherConfiguration::udp)
.def_rw("tcp", &eCAL::Publisher::CNBPublisherConfiguration::tcp)
.def_rw("share_topic_type", &eCAL::Publisher::CNBPublisherConfiguration::share_topic_type)
.def_rw("share_topic_description", &eCAL::Publisher::CNBPublisherConfiguration::share_topic_description);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
*
* @param module The nanobind module variable
**/
void AddDataTypeInfoStructToModule(nanobind::module_& module);
void AddPublisherConfigStructToModule(nanobind::module_& module);
2 changes: 2 additions & 0 deletions lang/python/nanobind_core/src/nanobind_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <modules/module_core.h>
#include <modules/module_datatypeinfo.h>
#include <modules/module_publisher.h>
#include <modules/module_publisher_config.h>
#include <modules/module_server.h>
#include <modules/module_subscriber.h>
#include <modules/module_util.h>
Expand All @@ -51,6 +52,7 @@ NB_MODULE(nanobind_core, m) {
AddPublisherClassToModule(m);
AddClientClassToModule(m);
AddServerClassToModule(m);
AddPublisherConfigStructToModule(m);

AddCoreFuncToModule(m);
AddUtilFuncToModule(m);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ namespace eCAL
info.name = nb_info.name;
info.encoding = nb_info.encoding;
info.descriptor = std::string(nb_info.descriptor.c_str(), nb_info.descriptor.size());

return info;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@

namespace eCAL
{
// TODO @Ariff
Publisher::Configuration convert(const CNBPublisherConfigStruct& nb_config)
{
return Publisher::Configuration{};
Publisher::CNBPublisherConfiguration::CNBPublisherConfiguration()
{
Publisher::Configuration::Configuration();
}

// TODO @Ariff
// Publisher::Configuration convert(const CNBPublisherConfigStruct& nb_config)
// {
// return Publisher::Configuration{};
//}
}
61 changes: 48 additions & 13 deletions lang/python/nanobind_core/src/wrappers/wrapper_publisher_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,58 @@
#include <stdint.h>
#include <string>
#include <cstddef>
#include <ecal/ecal_os.h>
#include <ecal/types/ecal_custom_data_types.h>

#include <ecal/config/publisher.h>
#include <nanobind/nanobind.h>

namespace eCAL
{
// TODO @Ariff
class CNBPublisherConfigStruct
namespace Publisher
{
};

/**
* @brief Convert function for SDataTypeInformation
*
* @param nb_info CNBDataTypeInformation struct
*
* @return SDataTypeInformation struct after convertion
**/
Publisher::Configuration convert(const CNBPublisherConfigStruct& nb_config);
}
namespace SHM
{
struct CNBSHMConfiguration
{
bool enable; //!< enable layer
bool zero_copy_mode; //!< enable zero copy shared memory transport mode
unsigned int acknowledge_timeout_ms; /*!< force connected subscribers to send acknowledge event after processing the message
the publisher send call is blocked on this event with this timeout (0 == no handshake) */
Types::ConstrainedInteger<4096, 4096> memfile_min_size_bytes; //!< default memory file size for new publisher
Types::ConstrainedInteger<50, 1, 100> memfile_reserve_percent; //!< dynamic file size reserve before recreating memory file if topic size changes
Types::ConstrainedInteger<1, 1> memfile_buffer_count; //!< maximum number of used buffers (needs to be greater than 1, default = 1)
};
}

namespace UDP
{
struct CNBUDPConfiguration
{
bool enable; //!< enable layer
bool loopback; //!< enable to receive udp messages on the same local machine
Types::ConstrainedInteger<5242880, 1024> sndbuf_size_bytes; //!< udp send buffer size in bytes (default 5MB)
};
}

namespace TCP
{
struct CNBTCPConfiguration
{
bool enable; //!< enable layer
};
}

struct CNBPublisherConfiguration
{
CNBPublisherConfiguration();

SHM::Configuration shm;
UDP::Configuration udp;
TCP::Configuration tcp;

bool share_topic_type; //!< share topic type via registration
bool share_topic_description; //!< share topic description via registration
};
}
}
12 changes: 7 additions & 5 deletions lang/python/nanobind_core/src/wrappers/wrapper_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ namespace eCAL

CNBSrvServer::CNBSrvServer(const std::string& service_name) : CServiceServer(service_name) { }

bool CNBSrvServer::WrapAddMethodCB(const std::string& nb_method, const std::string& nb_req_type, const std::string& nb_resp_type, nanobind::callable callback_)
bool CNBSrvServer::WrapAddMethodCB(const std::string& nb_method, const std::string& nb_req_type, const std::string& nb_resp_type, const std::string& nb_request, nanobind::callable callback_)
{
assert(IsConnected());
{
std::lock_guard<std::mutex> callback_lock(m_python_method_callback_mutex);
m_python_method_callback = callback_;
}
auto Servercallback = std::bind(&CNBSrvServer::MethodCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5);
auto const Servercallback = std::bind(&CNBSrvServer::MethodCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5);
return(CNBSrvServer::AddMethodCallback(nb_method, nb_req_type, nb_resp_type, Servercallback));
}

Expand All @@ -68,11 +68,13 @@ namespace eCAL

try {
nanobind::gil_scoped_acquire g2;
auto result = fn_callback(method_, req_type_, resp_type_, request);
nanobind::callable func_call;
auto result = fn_callback(method_, req_type_, resp_type_, func_call);
// do some check if object holds a tuple if (!result.is_type<nanobind::tuple>)
nanobind::tuple result_tuple = nanobind::cast<nanobind::tuple>(result);
response = nanobind::cast<std::string>(result[1]);
int nb_int = nanobind::cast<int>(result[0]);
response = nanobind::cast<std::string>(result_tuple[1]);
// request = nanobind::cast<std::string>(result_tuple[2]);
int nb_int = nanobind::cast<int>(result_tuple[0]);
return nb_int;
}
catch (const nanobind::python_error& e) {
Expand Down
3 changes: 2 additions & 1 deletion lang/python/nanobind_core/src/wrappers/wrapper_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ namespace eCAL
* @param nb_method Service method name
* @param nb_req_type Service method request type.
* @param nb_resp_type Service method response type.
* @param nb_request Name of request
* @param callback_ The callback function to add.
*
* @return True if succeeded, false if not.
**/
bool WrapAddMethodCB(const std::string& nb_method, const std::string& nb_req_type, const std::string& nb_resp_type, nanobind::callable callback_);
bool WrapAddMethodCB(const std::string& nb_method, const std::string& nb_req_type, const std::string& nb_resp_type, const std::string& nb_request, nanobind::callable callback_);

/**
* @brief Wrapper for Add callback function for service client events.
Expand Down

0 comments on commit 3189cda

Please sign in to comment.