From 24e13adfe1b94b4e786d52adc793906bb8fb1649 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 20 Aug 2020 14:32:58 +0200 Subject: [PATCH] sync/message_to_send() callback impl in python and using libpEpAdapter / add pEp.start_sync() --- src/pEp/__init__.py | 17 +++- src/pEp/native_pEp/adapter.cc | 2 +- src/pEp/native_pEp/pEpmodule.cc | 141 ++++++++++++--------------- src/pEp/native_pEp/pEpmodule.hh | 5 +- src/pEp/native_pEp/user_interface.cc | 18 ++-- src/pEp/native_pEp/user_interface.hh | 3 +- 6 files changed, 87 insertions(+), 99 deletions(-) diff --git a/src/pEp/__init__.py b/src/pEp/__init__.py index 82b5f39..2cfd9cc 100644 --- a/src/pEp/__init__.py +++ b/src/pEp/__init__.py @@ -11,15 +11,22 @@ from native_pEp import * # import the module import native_pEp +# Executed on module import +def init(): + print(init, "called") + native_pEp._init_after_main_module() +def message_to_send(msg): + """ + message_to_send(msg) + override pEp.message_to_send(msg) with your own implementation + this callback is being called when a p≡p management message needs to be sent + """ + print("message_to_send() - default callback\n") + print("overwrite this method") -# Executed on module import -def init(): - print("init() called") - native_pEp._init() - # Executed when run as script def main(): print("I am being run as a script") diff --git a/src/pEp/native_pEp/adapter.cc b/src/pEp/native_pEp/adapter.cc index 008644b..6bc1c4f 100644 --- a/src/pEp/native_pEp/adapter.cc +++ b/src/pEp/native_pEp/adapter.cc @@ -43,7 +43,7 @@ namespace pEp { case init: ++booked; if (!_session) - status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); +// status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); // status = ::init(&_session, _messageToSend, _inject_sync_event); break; diff --git a/src/pEp/native_pEp/pEpmodule.cc b/src/pEp/native_pEp/pEpmodule.cc index fbd514f..d77dc34 100644 --- a/src/pEp/native_pEp/pEpmodule.cc +++ b/src/pEp/native_pEp/pEpmodule.cc @@ -31,15 +31,20 @@ namespace PythonAdapter { using namespace std; using namespace boost::python; -scope *_scope = NULL; static const char *version_string = "p≡p Python adapter version 0.3"; -void _init() { +void init_before_main_module() { pEpLog("called"); - callback_dispatcher.add(_messageToSend, nullptr, nullptr, nullptr); +} + +// hidden init function, wrapped by hello_world.init() +void _init_after_main_module() { + pEpLog("called"); + callback_dispatcher.add(_messageToSend, notifyHandshake, nullptr, nullptr); Adapter::_messageToSend = CallbackDispatcher::messageToSend; } + void config_passive_mode(bool enable) { ::config_passive_mode(Adapter::session(), enable); @@ -99,37 +104,33 @@ void _throw_status(PEP_STATUS status) } } -PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr) -{ - return PEP_STATUS_OK; -} - PEP_STATUS _messageToSend(::message *msg) { - if (!_scope) - return PEP_SEND_FUNCTION_NOT_REGISTERED; - + pEpLog("called"); try { - object m = _scope->attr("messageToSend"); - call< void >(m.ptr(), Message(msg)); - } - catch (exception& e) { } + object modref = import("pEp"); + object funcref = modref.attr("message_to_send"); + call(funcref.ptr(), Message(msg)); + } catch (exception& e) { } return PEP_STATUS_OK; } -void messageToSend(Message msg) { - throw runtime_error("implement pEp.messageToSend(msg)"); +PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) +{ + pEpLog("called"); + return PEP_STATUS_OK; } -// void do_sync_protocol() -// { -// ::do_sync_protocol(Adapter::session(), nullptr); -// } + +void start_sync() +{ + CallbackDispatcher::start_sync(); +} void shutdown_sync() { - Adapter::shutdown(); + CallbackDispatcher::stop_sync(); } void debug_color(int ansi_color) @@ -142,28 +143,28 @@ void leave_device_group() ::leave_device_group(Adapter::session()); } -// void script_is_implementing_sync() { -// adapter.script_is_implementing_sync(); -// } - bool is_sync_active() { return Adapter::is_sync_running(); } +void testfunc() { + _messageToSend(NULL); +} + BOOST_PYTHON_MODULE(native_pEp) { + init_before_main_module(); - docstring_options doc_options(true, false, false); + // Module init function called by pEp.init() + def("_init_after_main_module", _init_after_main_module); + def("testfunc", &testfunc); + docstring_options doc_options(true, false, false); boost::locale::generator gen; std::locale::global(gen("")); - // Module init function called by pEp.init() - def("_init", _init); - - - _scope = new scope(); +// _scope = new scope(); scope().attr("about") = about(); scope().attr("per_user_directory") = per_user_directory(); scope().attr("per_machine_directory") = per_machine_directory(); @@ -544,14 +545,6 @@ BOOST_PYTHON_MODULE(native_pEp) "\n" "calculate trustwords for two Identities"); - // messageToSend() - - def("messageToSend", &messageToSend, - "messageToSend(msg)\n" - "\n" - "override pEp.messageToSend(msg) with your own implementation\n" - "this callback is being called when a p≡p management message needs to be sent"); - // Sync API enum_("sync_handshake_signal") @@ -566,39 +559,38 @@ BOOST_PYTHON_MODULE(native_pEp) .value("SYNC_NOTIFY_SOLE" , SYNC_NOTIFY_SOLE) .value("SYNC_NOTIFY_IN_GROUP" , SYNC_NOTIFY_IN_GROUP); - auto user_interface_class = class_( - "UserInterface", - "class MyUserInterface(UserInterface):\n" - " def notifyHandshake(self, me, partner):\n" - " ...\n" - "\n" - "p≡p User Interface class\n" - "To be used as a mixin\n" - ) - .def("notifyHandshake", &UserInterface::notifyHandshake, - "notifyHandshake(self, me, partner)\n" - "\n" - " me own identity\n" - " partner identity of communication partner\n" - "\n" - "overwrite this method with an implementation of a handshake dialog") - .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, - boost::python::arg("identities")=object(), - "deliverHandshakeResult(self, result, identities=None)\n" - "\n" - " result -1: cancel, 0: accepted, 1: rejected\n" - " identities list of identities to share or None for all\n" - "\n" - "call to deliver the handshake result of the handshake dialog"); +// auto user_interface_class = class_( +// "UserInterface", +// "class MyUserInterface(UserInterface):\n" +// " def notifyHandshake(self, me, partner):\n" +// " ...\n" +// "\n" +// "p≡p User Interface class\n" +// "To be used as a mixin\n" +// ) +// .def("notifyHandshake", &UserInterface::notifyHandshake, +// "notifyHandshake(self, me, partner)\n" +// "\n" +// " me own identity\n" +// " partner identity of communication partner\n" +// "\n" +// "overwrite this method with an implementation of a handshake dialog") +// .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, +// boost::python::arg("identities")=object(), +// "deliverHandshakeResult(self, result, identities=None)\n" +// "\n" +// " result -1: cancel, 0: accepted, 1: rejected\n" +// " identities list of identities to share or None for all\n" +// "\n" +// "call to deliver the handshake result of the handshake dialog" +// ); -// TODO: Replace with start_sync() -// def("do_sync_protocol", &do_sync_protocol, -// "do_sync_protocol()\n" -// "\n" -// "in case of an explicit sync thread instead of a single threaded\n" -// "implementation call this function in your sync thread\n" -// ); + def("start_sync", &start_sync, + "start_sync()\n" + "\n" + "starts the sync thread" + ); def("shutdown_sync", &shutdown_sync, "shutdown_sync()\n" @@ -615,13 +607,6 @@ BOOST_PYTHON_MODULE(native_pEp) "call this for a grouped device, which should leave\n" ); -// def("script_is_implementing_sync", &script_is_implementing_sync, -// "script_is_implementing_sync()\n" -// "\n" -// "call this in case the Python script is implementing sync to make\n" -// "is_sync_active() working\n" -// ); - def("is_sync_active", &is_sync_active, "is_sync_active()\n" "\n" diff --git a/src/pEp/native_pEp/pEpmodule.hh b/src/pEp/native_pEp/pEpmodule.hh index 71ff9c5..4bfc6fa 100644 --- a/src/pEp/native_pEp/pEpmodule.hh +++ b/src/pEp/native_pEp/pEpmodule.hh @@ -15,11 +15,8 @@ void config_unencrypted_subject(bool enable); void key_reset_user(string user_id, string fpr); void key_reset_all_own_keys(); void _throw_status(PEP_STATUS status); -void messageToSend(Message msg); PEP_STATUS _messageToSend(::message *msg); -PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); -// void do_sync_protocol(); -// extern Adapter adapter; +PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); } // namespace PythonAdapter } // namespace pEp { diff --git a/src/pEp/native_pEp/user_interface.cc b/src/pEp/native_pEp/user_interface.cc index 0cf5c39..a21234d 100644 --- a/src/pEp/native_pEp/user_interface.cc +++ b/src/pEp/native_pEp/user_interface.cc @@ -86,15 +86,15 @@ void UserInterface::deliverHandshakeResult(int result, object identities) _throw_status(status); } -PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) -{ - PEP_rating result; - PEP_STATUS status = - ::get_key_rating_for_user(Adapter::session(), - user_id.c_str(), fpr.c_str(), &result); - _throw_status(status); - return result; -} +//PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) +//{ +// PEP_rating result; +// PEP_STATUS status = +// ::get_key_rating_for_user(Adapter::session(), +// user_id.c_str(), fpr.c_str(), &result); +// _throw_status(status); +// return result; +//} //SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) //{ diff --git a/src/pEp/native_pEp/user_interface.hh b/src/pEp/native_pEp/user_interface.hh index 7fa358d..e222b3c 100644 --- a/src/pEp/native_pEp/user_interface.hh +++ b/src/pEp/native_pEp/user_interface.hh @@ -33,11 +33,10 @@ public: virtual void deliverHandshakeResult(int result, object identities); - PEP_rating get_key_rating_for_user(string user_id, string fpr); +// PEP_rating get_key_rating_for_user(string user_id, string fpr); protected: static PEP_STATUS _notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); - static SYNC_EVENT retrieve_next_sync_event(void *management, unsigned threshold); }; class UserInterface_callback : public UserInterface {