Browse Source

messageToSend() and sync test

PYADPT-55
Volker Birk 7 years ago
parent
commit
19983abc21
  1. 13
      src/adapter.cc
  2. 18
      src/adapter.hh
  3. 45
      src/pEpmodule.cc
  4. 2
      src/pEpmodule.hh

13
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);
}
}
}

18
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;
};
}
}

45
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>("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_<pEp::PythonAdapter::Adapter, pEp::PythonAdapter::Adapter_callback, boost::noncopyable>(
"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));

2
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;
}
}

Loading…
Cancel
Save