Browse Source

Mutli-threading support for native libs calling python code added

mixing UserInterface replaced with pEp.notify_handshake() to be overwritten (just like message_to_send())
master
heck 5 years ago
parent
commit
73666d8437
  1. 2
      setup.py
  2. 14
      src/pEp/__init__.py
  3. 54
      src/pEp/native_pEp/pEpmodule.cc
  4. 5
      test/codec_doctest.py
  5. 36
      test/pyadpt-81.py

2
setup.py

@ -212,7 +212,7 @@ module_pEp = Extension(
'src/pEp/native_pEp/message.cc', 'src/pEp/native_pEp/message.cc',
'src/pEp/native_pEp/message_api.cc', 'src/pEp/native_pEp/message_api.cc',
'src/pEp/native_pEp/str_attr.cc', 'src/pEp/native_pEp/str_attr.cc',
'src/pEp/native_pEp/user_interface.cc', # 'src/pEp/native_pEp/user_interface.cc',
], ],
) )

14
src/pEp/__init__.py

@ -22,6 +22,20 @@ def message_to_send(msg):
message_to_send(msg) message_to_send(msg)
override pEp.message_to_send(msg) with your own implementation override pEp.message_to_send(msg) with your own implementation
this callback is being called when a pp management message needs to be sent this callback is being called when a pp management message needs to be sent
GIL CAVEAT
"""
print("message_to_send() - default callback\n")
print("overwrite this method")
def notify_handshake(me, partner, signal):
"""
notifyHandshake(self, me, partner)
me own identity
partner identity of communication partner
signal the handshake signal
overwrite this method with an implementation of a handshake dialog
GIL CAVEAT
""" """
print("message_to_send() - default callback\n") print("message_to_send() - default callback\n")
print("overwrite this method") print("overwrite this method")

54
src/pEp/native_pEp/pEpmodule.cc

@ -24,7 +24,7 @@
#include "pEpmodule.hh" #include "pEpmodule.hh"
#include "basic_api.hh" #include "basic_api.hh"
#include "message_api.hh" #include "message_api.hh"
#include "user_interface.hh" //#include "user_interface.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
@ -108,9 +108,13 @@ PEP_STATUS _messageToSend(::message *msg)
{ {
pEpLog("called"); pEpLog("called");
try { try {
PyGILState_STATE gil = PyGILState_Ensure();
pEpLog("GIL Aquired");
object modref = import("pEp"); object modref = import("pEp");
object funcref = modref.attr("message_to_send"); object funcref = modref.attr("message_to_send");
call<void>(funcref.ptr(), Message(msg)); call<void>(funcref.ptr(), Message());
PyGILState_Release(gil);
pEpLog("GIL released");
} catch (exception& e) { } } catch (exception& e) { }
return PEP_STATUS_OK; return PEP_STATUS_OK;
@ -119,6 +123,16 @@ PEP_STATUS _messageToSend(::message *msg)
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal)
{ {
pEpLog("called"); pEpLog("called");
try {
PyGILState_STATE gil = PyGILState_Ensure();
pEpLog("GIL Aquired");
object modref = import("pEp");
object funcref = modref.attr("notify_handshake");
call<void>(funcref.ptr(), me, partner, signal);
PyGILState_Release(gil);
pEpLog("GIL released");
} catch (exception& e) { }
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
@ -152,6 +166,34 @@ void testfunc() {
_messageToSend(NULL); _messageToSend(NULL);
} }
void deliverHandshakeResult(int result, object identities)
{
identity_list *shared_identities = nullptr;
if (identities != boost::python::api::object() && boost::python::len(identities)) {
shared_identities = new_identity_list(nullptr);
if (!shared_identities)
throw bad_alloc();
try {
identity_list *si = shared_identities;
for (int i=0; i < boost::python::len(identities); ++i) {
Identity ident = extract< Identity >(identities[i]);
si = identity_list_add(si, ident);
if (!si)
throw bad_alloc();
}
}
catch (exception& ex) {
free_identity_list(shared_identities);
throw ex;
}
}
PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), (sync_handshake_result) result, shared_identities);
free_identity_list(shared_identities);
_throw_status(status);
}
BOOST_PYTHON_MODULE(native_pEp) BOOST_PYTHON_MODULE(native_pEp)
{ {
init_before_main_module(); init_before_main_module();
@ -585,6 +627,14 @@ BOOST_PYTHON_MODULE(native_pEp)
// "call to deliver the handshake result of the handshake dialog" // "call to deliver the handshake result of the handshake dialog"
// ); // );
def("deliver_handshake_result", &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, def("start_sync", &start_sync,
"start_sync()\n" "start_sync()\n"

5
test/codec_doctest.py

@ -3,14 +3,15 @@
""" """
>>> import pEp >>> import pEp
>>> def messageToSend(msg): >>> def message_to_send(msg):
... m, keys, rating, flags = msg.decrypt() ... m, keys, rating, flags = msg.decrypt()
... try: ... try:
... m.attachments[0].decode() ... m.attachments[0].decode()
... print("decode successfull") ... print("decode successfull")
... except UnicodeDecodeError as e: ... except UnicodeDecodeError as e:
... print("decode failed") ... print("decode failed")
>>> pEp.messageToSend = messageToSend >>> pEp.message_to_send = message_to_send
>>> pEp.myself(pEp.Identity(""))
>>> pEp.key_reset_all_own_keys() >>> pEp.key_reset_all_own_keys()
decode successfull decode successfull
decode successfull decode successfull

36
test/pyadpt-81.py

@ -4,22 +4,44 @@
import pEp import pEp
import time import time
def user_message_to_send(msg): def message_to_send(msg):
print("User defined callback implementation") print("User defined message_to_send() called")
m, keys, rating, flags = msg.decrypt()
try:
print(m.attachments[0].decode())
except UnicodeDecodeError as e:
print("decode failed")
def notify_handshake(me, partner, signal):
print("User defined notify_handshake() called")
print(me)
print(partner)
print(signal)
def start_stop_sync(duration): def start_stop_sync(duration):
pEp.start_sync() pEp.start_sync()
time.sleep(duration) time.sleep(duration)
pEp.shutdown_sync() pEp.shutdown_sync()
alice = pEp.Identity("test@alice.com", "alice", "23")
pEp.myself(alice)
print(alice.fpr)
dir(pEp) dir(pEp)
# test default callback # test default callback
start_stop_sync(1) start_stop_sync(1)
# test user defined callback # test user defined callback
pEp.message_to_send = user_message_to_send pEp.message_to_send = message_to_send
# pEp.notify_handshake = notify_handshake
start_stop_sync(1) start_stop_sync(1)
pEp.start_sync() # pEp.start_sync()
while(True): # while(True):
print("is_sync_active: {}".format(pEp.is_sync_active())) # print("is_sync_active: {}".format(pEp.is_sync_active()))
time.sleep(1) # time.sleep(3)
# pEp.key_reset_all_own_keys()
# time.sleep(3)
Loading…
Cancel
Save