From 19983abc21ec3c0e30b755d180b3f1ffcfeb5cd9 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sat, 15 Sep 2018 13:55:34 +0200 Subject: [PATCH] messageToSend() and sync test --- src/adapter.cc | 13 ------------- src/adapter.hh | 18 ------------------ src/pEpmodule.cc | 45 ++++++++++++++++++++++++++++----------------- src/pEpmodule.hh | 2 ++ 4 files changed, 30 insertions(+), 48 deletions(-) diff --git a/src/adapter.cc b/src/adapter.cc index 68e94e6..72a476f 100644 --- a/src/adapter.cc +++ b/src/adapter.cc @@ -70,15 +70,6 @@ namespace pEp { return obj; } - PEP_STATUS Adapter::_messageToSend(struct _message *msg) - { - if (!msg) - return PEP_ILLEGAL_VALUE; - - adapter.messageToSend(Message(msg)); - return PEP_STATUS_OK; - } - int Adapter::_inject_sync_event(SYNC_EVENT ev, void *management) { if (is_sync_thread(adapter.session())) { @@ -94,10 +85,6 @@ namespace pEp { } return 0; } - - void Adapter_callback::messageToSend(Message msg) { - call_method< void >(_self, "messageToSend", msg); - } } } diff --git a/src/adapter.hh b/src/adapter.hh index 67c2235..8ae71ff 100644 --- a/src/adapter.hh +++ b/src/adapter.hh @@ -19,10 +19,6 @@ namespace pEp { Adapter(bool unregister_this = false); virtual ~Adapter(); - virtual void messageToSend(Message msg) { - throw runtime_error("override this method"); - } - enum session_action { none = 0, init, @@ -38,7 +34,6 @@ namespace pEp { protected: static PyObject *ui_object(PyObject *value = nullptr); - static PEP_STATUS _messageToSend(struct _message *msg); static int _inject_sync_event(SYNC_EVENT ev, void *management); private: @@ -50,19 +45,6 @@ namespace pEp { friend class UserInterface_callback; }; - - class Adapter_callback : public Adapter { - PyObject *_self; - public: - Adapter_callback(PyObject *self) : _self(self) { } - ~Adapter_callback() { } - - void messageToSend(Message msg); - - // non copyable - Adapter_callback(const Adapter_callback&) = delete; - Adapter_callback& operator= (const Adapter_callback&) = delete; - }; } } diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index 58a283c..dfe9ff5 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -21,6 +21,7 @@ namespace pEp { using namespace std; Adapter adapter(true); + scope *_scope = NULL; static const char *version_string = "p≡p Python adapter version 0.2"; static string about() @@ -45,6 +46,24 @@ namespace pEp { build << setfill('0') << "p≡p 0x" << setw(4) << hex << status; throw runtime_error(build.str()); } + + PEP_STATUS _messageToSend(::message *msg) + { + if (!_scope) + return PEP_SEND_FUNCTION_NOT_REGISTERED; + + try { + object m = _scope->attr("messageToSend"); + call< void >(m.ptr(), Message(msg)); + } + catch (exception& e) { } + + return PEP_STATUS_OK; + } + + void messageToSend(Message msg) { + throw runtime_error("implement pEp.messageToSend(msg)"); + } } } @@ -58,6 +77,7 @@ BOOST_PYTHON_MODULE(pEp) generator gen; std::locale::global(gen("")); + _scope = new scope(); scope().attr("about") = about(); @@ -343,6 +363,14 @@ BOOST_PYTHON_MODULE(pEp) "\n" "configure if sync messages are being kept or automatically removed (default)"); + // messageToSend() + + def("messageToSend", &pEp::PythonAdapter::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") @@ -382,23 +410,6 @@ BOOST_PYTHON_MODULE(pEp) "call to deliver the handshake result of the handshake dialog") ; - auto adapter_class = class_( - "Adapter", - "class MyAdapter(Adapter):\n" - " def messageToSend(self, Message msg):\n" - " ...\n" - "\n" - "p≡p Adapter class\n" - "To be used as a mixin\n" - ) - .def("messageToSend", &pEp::PythonAdapter::Adapter::messageToSend, - "messageToSend(self, msg):\n" - "\n" - " msg message, which has to be send out\n" - "\n" - "overwrite this method with an implementation, which is sending the message\n" - ); - // codecs call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(sync_search)); diff --git a/src/pEpmodule.hh b/src/pEpmodule.hh index 37fc4f5..0ff9959 100644 --- a/src/pEpmodule.hh +++ b/src/pEpmodule.hh @@ -9,6 +9,8 @@ namespace pEp { namespace PythonAdapter { void _throw_status(PEP_STATUS status); + void messageToSend(Message msg); + PEP_STATUS _messageToSend(::message *msg); extern Adapter adapter; } }