Browse Source

sync/message_to_send() callback impl in python and using libpEpAdapter / add pEp.start_sync()

PYADPT-81
heck 5 years ago
parent
commit
24e13adfe1
  1. 17
      src/pEp/__init__.py
  2. 2
      src/pEp/native_pEp/adapter.cc
  3. 141
      src/pEp/native_pEp/pEpmodule.cc
  4. 5
      src/pEp/native_pEp/pEpmodule.hh
  5. 18
      src/pEp/native_pEp/user_interface.cc
  6. 3
      src/pEp/native_pEp/user_interface.hh

17
src/pEp/__init__.py

@ -11,15 +11,22 @@ from native_pEp import *
# import the module # import the module
import native_pEp 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 pp 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 # Executed when run as script
def main(): def main():
print("I am being run as a script") print("I am being run as a script")

2
src/pEp/native_pEp/adapter.cc

@ -43,7 +43,7 @@ namespace pEp {
case init: case init:
++booked; ++booked;
if (!_session) 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); // status = ::init(&_session, _messageToSend, _inject_sync_event);
break; break;

141
src/pEp/native_pEp/pEpmodule.cc

@ -31,15 +31,20 @@ namespace PythonAdapter {
using namespace std; using namespace std;
using namespace boost::python; using namespace boost::python;
scope *_scope = NULL;
static const char *version_string = "p≡p Python adapter version 0.3"; static const char *version_string = "p≡p Python adapter version 0.3";
void _init() { void init_before_main_module() {
pEpLog("called"); 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; Adapter::_messageToSend = CallbackDispatcher::messageToSend;
} }
void config_passive_mode(bool enable) void config_passive_mode(bool enable)
{ {
::config_passive_mode(Adapter::session(), 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) PEP_STATUS _messageToSend(::message *msg)
{ {
if (!_scope) pEpLog("called");
return PEP_SEND_FUNCTION_NOT_REGISTERED;
try { try {
object m = _scope->attr("messageToSend"); object modref = import("pEp");
call< void >(m.ptr(), Message(msg)); object funcref = modref.attr("message_to_send");
} call<void>(funcref.ptr(), Message(msg));
catch (exception& e) { } } catch (exception& e) { }
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
void messageToSend(Message msg) { PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
throw runtime_error("implement pEp.messageToSend(msg)"); {
pEpLog("called");
return PEP_STATUS_OK;
} }
// void do_sync_protocol()
// { void start_sync()
// ::do_sync_protocol(Adapter::session(), nullptr); {
// } CallbackDispatcher::start_sync();
}
void shutdown_sync() void shutdown_sync()
{ {
Adapter::shutdown(); CallbackDispatcher::stop_sync();
} }
void debug_color(int ansi_color) void debug_color(int ansi_color)
@ -142,28 +143,28 @@ void leave_device_group()
::leave_device_group(Adapter::session()); ::leave_device_group(Adapter::session());
} }
// void script_is_implementing_sync() {
// adapter.script_is_implementing_sync();
// }
bool is_sync_active() bool is_sync_active()
{ {
return Adapter::is_sync_running(); return Adapter::is_sync_running();
} }
void testfunc() {
_messageToSend(NULL);
}
BOOST_PYTHON_MODULE(native_pEp) 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; boost::locale::generator gen;
std::locale::global(gen("")); std::locale::global(gen(""));
// Module init function called by pEp.init() // _scope = new scope();
def("_init", _init);
_scope = new scope();
scope().attr("about") = about(); scope().attr("about") = about();
scope().attr("per_user_directory") = per_user_directory(); scope().attr("per_user_directory") = per_user_directory();
scope().attr("per_machine_directory") = per_machine_directory(); scope().attr("per_machine_directory") = per_machine_directory();
@ -544,14 +545,6 @@ BOOST_PYTHON_MODULE(native_pEp)
"\n" "\n"
"calculate trustwords for two Identities"); "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 // Sync API
enum_<sync_handshake_signal>("sync_handshake_signal") enum_<sync_handshake_signal>("sync_handshake_signal")
@ -566,40 +559,39 @@ BOOST_PYTHON_MODULE(native_pEp)
.value("SYNC_NOTIFY_SOLE" , SYNC_NOTIFY_SOLE) .value("SYNC_NOTIFY_SOLE" , SYNC_NOTIFY_SOLE)
.value("SYNC_NOTIFY_IN_GROUP" , SYNC_NOTIFY_IN_GROUP); .value("SYNC_NOTIFY_IN_GROUP" , SYNC_NOTIFY_IN_GROUP);
auto user_interface_class = class_<UserInterface, UserInterface_callback, boost::noncopyable>( // auto user_interface_class = class_<UserInterface, UserInterface_callback, boost::noncopyable>(
"UserInterface", // "UserInterface",
"class MyUserInterface(UserInterface):\n" // "class MyUserInterface(UserInterface):\n"
" def notifyHandshake(self, me, partner):\n" // " def notifyHandshake(self, me, partner):\n"
" ...\n" // " ...\n"
"\n" // "\n"
"p≡p User Interface class\n" // "p≡p User Interface class\n"
"To be used as a mixin\n" // "To be used as a mixin\n"
) // )
.def("notifyHandshake", &UserInterface::notifyHandshake, // .def("notifyHandshake", &UserInterface::notifyHandshake,
"notifyHandshake(self, me, partner)\n" // "notifyHandshake(self, me, partner)\n"
"\n" // "\n"
" me own identity\n" // " me own identity\n"
" partner identity of communication partner\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" // "\n"
// "in case of an explicit sync thread instead of a single threaded\n" // "overwrite this method with an implementation of a handshake dialog")
// "implementation call this function in your sync thread\n" // .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"
// ); // );
def("start_sync", &start_sync,
"start_sync()\n"
"\n"
"starts the sync thread"
);
def("shutdown_sync", &shutdown_sync, def("shutdown_sync", &shutdown_sync,
"shutdown_sync()\n" "shutdown_sync()\n"
"\n" "\n"
@ -615,13 +607,6 @@ BOOST_PYTHON_MODULE(native_pEp)
"call this for a grouped device, which should leave\n" "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, def("is_sync_active", &is_sync_active,
"is_sync_active()\n" "is_sync_active()\n"
"\n" "\n"

5
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_user(string user_id, string fpr);
void key_reset_all_own_keys(); void key_reset_all_own_keys();
void _throw_status(PEP_STATUS status); void _throw_status(PEP_STATUS status);
void messageToSend(Message msg);
PEP_STATUS _messageToSend(::message *msg); PEP_STATUS _messageToSend(::message *msg);
PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal);
// void do_sync_protocol();
// extern Adapter adapter;
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp {

18
src/pEp/native_pEp/user_interface.cc

@ -86,15 +86,15 @@ void UserInterface::deliverHandshakeResult(int result, object identities)
_throw_status(status); _throw_status(status);
} }
PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) //PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr)
{ //{
PEP_rating result; // PEP_rating result;
PEP_STATUS status = // PEP_STATUS status =
::get_key_rating_for_user(Adapter::session(), // ::get_key_rating_for_user(Adapter::session(),
user_id.c_str(), fpr.c_str(), &result); // user_id.c_str(), fpr.c_str(), &result);
_throw_status(status); // _throw_status(status);
return result; // return result;
} //}
//SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) //SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold)
//{ //{

3
src/pEp/native_pEp/user_interface.hh

@ -33,11 +33,10 @@ public:
virtual void deliverHandshakeResult(int result, object identities); 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: protected:
static PEP_STATUS _notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); 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 { class UserInterface_callback : public UserInterface {

Loading…
Cancel
Save