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"], diff --git a/src/pyMOOS.cpp b/src/pyMOOS.cpp index 4cddd95..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 = @@ -75,6 +81,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 +127,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 +244,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 +498,31 @@ 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("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." + "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",