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

Feature/nanobind core #1682

Open
wants to merge 4 commits into
base: feature/nanobind-core
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
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
4 changes: 4 additions & 0 deletions lang/python/nanobind_core/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ nanobind_add_module(
modules/module_server.h
modules/module_subscriber.cpp
modules/module_subscriber.h
modules/module_subscriber_config.cpp
modules/module_subscriber_config.h
modules/module_util.cpp
modules/module_util.h

Expand All @@ -46,6 +48,8 @@ nanobind_add_module(
wrappers/wrapper_server.h
wrappers/wrapper_subscriber.cpp
wrappers/wrapper_subscriber.h
wrappers/wrapper_subscriber_config.cpp
wrappers/wrapper_subscriber_config.h

nanobind_core.cpp
)
Expand Down
36 changes: 28 additions & 8 deletions lang/python/nanobind_core/src/modules/module_publisher_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/**
* @brief Add ServiceClient class to nanobind module
* @brief Add Publisher config structs to nanobind module
**/


Expand All @@ -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);
47 changes: 47 additions & 0 deletions lang/python/nanobind_core/src/modules/module_subscriber_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* ========================= eCAL LICENSE =================================
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

/**
* @brief Add ServiceClient class to nanobind module
**/


#include <modules/module_subscriber_config.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'modules/module_subscriber_config.h' file not found [clang-diagnostic-error]

#include <modules/module_subscriber_config.h>
         ^

#include <wrappers/wrapper_subscriber_config.h>

void AddSubscriberConfigStructToModule(nanobind::module_& module)
{
nanobind::class_<eCAL::Subscriber::SHM::CNBSHMConfiguration>(module, "SHMConfiguration")
.def(nanobind::init<>())
.def_rw("enable", &eCAL::Subscriber::SHM::CNBSHMConfiguration::enable);

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

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

nanobind::class_<eCAL::Subscriber::CNBSubscriberConfiguration>(module, "SubscriberConfiguration")
.def(nanobind::init<>())
.def_rw("shm", &eCAL::Subscriber::CNBSubscriberConfiguration::shm)
.def_rw("udp", &eCAL::Subscriber::CNBSubscriberConfiguration::udp)
.def_rw("tcp", &eCAL::Subscriber::CNBSubscriberConfiguration::tcp);
}
46 changes: 46 additions & 0 deletions lang/python/nanobind_core/src/modules/module_subscriber_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ========================= eCAL LICENSE =================================
*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ========================= eCAL LICENSE =================================
*/

/**
* @file wrapper_subscriber_config.h
* @brief Nanobind wrapper for structs of Subscriber config
**/

#pragma once

#include <nanobind/nanobind.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'nanobind/nanobind.h' file not found [clang-diagnostic-error]

#include <nanobind/nanobind.h>
         ^

#include <nanobind/operators.h>
#include <ecal/ecal.h>
#include <nanobind/operators.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: duplicate include [readability-duplicate-include]

lang/python/nanobind_core/src/modules/module_subscriber_config.h:28:

- #include <ecal/ecal.h>
- #include <nanobind/operators.h>
+ #include <ecal/ecal.h>

#include <nanobind/stl/string.h>
#include <nanobind/stl/shared_ptr.h>
#include <nanobind/stl/tuple.h>
#include <cstdint>
#include <chrono>
#include <memory>
#include <string>
#include <cstddef>
#include <ecal/ecal_types.h>

/**
* @brief Function to Add Nanobind module
*
* @param module The nanobind module variable
**/
void AddSubscriberConfigStructToModule(nanobind::module_& module);
4 changes: 4 additions & 0 deletions lang/python/nanobind_core/src/nanobind_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
#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_subscriber_config.h>
#include <modules/module_util.h>


Expand All @@ -51,6 +53,8 @@ NB_MODULE(nanobind_core, m) {
AddPublisherClassToModule(m);
AddClientClassToModule(m);
AddServerClassToModule(m);
AddPublisherConfigStructToModule(m);
AddSubscriberConfigStructToModule(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{};
//}
}
65 changes: 50 additions & 15 deletions lang/python/nanobind_core/src/wrappers/wrapper_publisher_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

/**
* @file wrapper_datatypeinfo.h
* @brief Nanobind wrapper SDataTypeInformation struct
* @file wrapper_publisher_config.h
* @brief Nanobind wrapper for structs of Publisher config
**/

#pragma once
Expand All @@ -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
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: constructor does not initialize these fields: enable, zero_copy_mode, acknowledge_timeout_ms, memfile_min_size_bytes, memfile_reserve_percent, memfile_buffer_count [cppcoreguidelines-pro-type-member-init]

lang/python/nanobind_core/src/wrappers/wrapper_publisher_config.h:44:

-                 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
+                 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

lang/python/nanobind_core/src/wrappers/wrapper_publisher_config.h:48:

-                 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)
+                 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)

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
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: constructor does not initialize these fields: enable, loopback, sndbuf_size_bytes [cppcoreguidelines-pro-type-member-init]

lang/python/nanobind_core/src/wrappers/wrapper_publisher_config.h:58:

-                 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)
+                 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)

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
Loading
Loading