From 4869ec1135b5ebb15bd0fbed7a8afe44be0500f3 Mon Sep 17 00:00:00 2001 From: torsten-pf Date: Fri, 19 Apr 2024 12:14:45 +0200 Subject: [PATCH 1/3] Added on_disconnect callback and newlines in doc strings --- src/pyMOOS.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/src/pyMOOS.cpp b/src/pyMOOS.cpp index 4cddd95..299dbe4 100644 --- a/src/pyMOOS.cpp +++ b/src/pyMOOS.cpp @@ -75,6 +75,17 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient { return true; } + static bool on_disconnect_delegate(void * pParam) { + MOOS::AsyncCommsWrapper * pMe = + static_cast (pParam); + return pMe->on_disconnect(); + } + bool SetOnDisconnectCallback(py::object func) { + BASE::SetOnDisconnectCallBack(on_disconnect_delegate, this); + on_disconnect_object_ = func; + return true; + } + bool Close(bool nice){ bool bResult = false; @@ -110,6 +121,26 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient { } + bool on_disconnect() { + + bool bResult = false; + + PyGILState_STATE gstate = PyGILState_Ensure(); + try { + py::object result = on_disconnect_object_(); + bResult = py::bool_(result); + } catch (const py::error_already_set& e) { + PyGILState_Release(gstate); + throw pyMOOSException( + "OnDisconnect:: caught an exception thrown in python callback"); + } + + PyGILState_Release(gstate); + + return bResult; + + } + bool SetOnMailCallback(py::object func) { BASE::SetOnMailCallBack(on_mail_delegate, this); on_mail_object_ = func; @@ -207,6 +238,7 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient { /** callback functions (stored) */ py::object on_connect_object_; + py::object on_disconnect_object_; py::object on_mail_object_; /** close connection flag */ @@ -460,21 +492,28 @@ PYBIND11_MODULE(pymoos, m) { "Fetch incoming mail as a vector.") .def("set_on_connect_callback", &MOOS::AsyncCommsWrapper::SetOnConnectCallback, - "Set the user supplied on_connect callback. This callback, " - "when set, will be invoked when a connection to the server " - "is made. It is a good plan to register for notifications " + "Set the user supplied on_connect callback. This callback,\n" + "when set, will be invoked when a connection to the server\n" + "is made. It is a good plan to register for notifications\n" + "of variables in this callback.", + py::arg("func")) + .def("set_on_disconnect_callback", + &MOOS::AsyncCommsWrapper::SetOnDisconnectCallback, + "Set the user supplied on_disconnect callback. This callback,\n" + "when set, will be invoked when the connection to the server\n" + "is lost. It is a good plan to register for notifications\n" "of variables in this callback.", py::arg("func")) .def("set_on_mail_callback", &MOOS::AsyncCommsWrapper::SetOnMailCallback, - "Set the user supplied on_mail callback. If you want rapid " + "Set the user supplied on_mail callback. If you want rapid\n" "response, use V10 active callbacks.", py::arg("func")) .def("notify_binary", &MOOS::AsyncCommsWrapper::NotifyBinary, "Notify binary data. (specific to pymoos.)", py::arg("name"), py::arg("binary_data"), py::arg("time")=-1) .def("add_active_queue", &MOOS::AsyncCommsWrapper::AddActiveQueue, - "Register a custom callback for a particular message." + "Register a custom callback for a particular message.\n" "This will be called in it's own thread.", py::arg("queue_name"), py::arg("func")) .def("remove_message_route_to_active_queue", From a4368301f2205dc63730317df06599a82642fc08 Mon Sep 17 00:00:00 2001 From: torsten-pf Date: Tue, 23 Apr 2024 10:41:47 +0200 Subject: [PATCH 2/3] Added comms.server_request() to do a MOOSDB server request --- src/pyMOOS.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/pyMOOS.cpp b/src/pyMOOS.cpp index 299dbe4..aa1b819 100644 --- a/src/pyMOOS.cpp +++ b/src/pyMOOS.cpp @@ -63,6 +63,12 @@ class AsyncCommsWrapper : public MOOS::MOOSAsyncCommClient { dfTime); return BASE::Post(M); } + /* do a MOOSDB server request */ + bool ServerRequest(const std::string& sKey, double dfTime) { + CMOOSMsg M(MOOS_SERVER_REQUEST, sKey, dfTime); + M.m_sOriginatingCommunity = GetCommunityName(); + return BASE::Post(M); + } static bool on_connect_delegate(void * pParam) { MOOS::AsyncCommsWrapper * pMe = @@ -512,6 +518,9 @@ PYBIND11_MODULE(pymoos, m) { .def("notify_binary", &MOOS::AsyncCommsWrapper::NotifyBinary, "Notify binary data. (specific to pymoos.)", py::arg("name"), py::arg("binary_data"), py::arg("time")=-1) + .def("server_request", &MOOS::AsyncCommsWrapper::ServerRequest, + "Do a MOOSDB server request. (specific to pymoos.)", + py::arg("key"), py::arg("time")=-1) .def("add_active_queue", &MOOS::AsyncCommsWrapper::AddActiveQueue, "Register a custom callback for a particular message.\n" "This will be called in it's own thread.", From 5fec8e719658eb2c2f2892eba524400bca67c5d7 Mon Sep 17 00:00:00 2001 From: torsten-pf Date: Wed, 8 May 2024 10:11:13 +0200 Subject: [PATCH 3/3] Update setup.py Add local pybind11 directory to path to find the pybind11 package during installation / wheel creation --- setup.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 05cb458..09749d9 100644 --- a/setup.py +++ b/setup.py @@ -1,17 +1,19 @@ from setuptools import setup from pathlib import Path - -# Available at setup time due to pyproject.toml -from pybind11.setup_helpers import Pybind11Extension, build_ext -from pybind11 import get_cmake_dir - -import sys +import sys, os __version__ = "2022.1" this_directory = Path(__file__).parent long_description = (this_directory / "README.md").read_text() +# append local pybind11 directory to find the required files +sys.path.append(os.path.join(this_directory, 'pybind11')) + +# Available at setup time due to pyproject.toml (not working on older systems) +from pybind11.setup_helpers import Pybind11Extension, build_ext +from pybind11 import get_cmake_dir + ext_modules = [ Pybind11Extension("pymoos", ["src/pyMOOS.cpp"],