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())
PYADPT-81
heck 1 year ago
parent
commit
73666d8437
5 changed files with 99 additions and 12 deletions
  1. +1
    -1
      setup.py
  2. +14
    -0
      src/pEp/__init__.py
  3. +52
    -2
      src/pEp/native_pEp/pEpmodule.cc
  4. +3
    -2
      test/codec_doctest.py
  5. +29
    -7
      test/pyadpt-81.py

+ 1
- 1
setup.py View File

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


+ 14
- 0
src/pEp/__init__.py View File

@ -22,6 +22,20 @@ 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
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("overwrite this method")


+ 52
- 2
src/pEp/native_pEp/pEpmodule.cc View File

@ -24,7 +24,7 @@
#include "pEpmodule.hh"
#include "basic_api.hh"
#include "message_api.hh"
#include "user_interface.hh"
//#include "user_interface.hh"
namespace pEp {
namespace PythonAdapter {
@ -108,9 +108,13 @@ PEP_STATUS _messageToSend(::message *msg)
{
pEpLog("called");
try {
PyGILState_STATE gil = PyGILState_Ensure();
pEpLog("GIL Aquired");
object modref = import("pEp");
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) { }
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)
{
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;
}
@ -152,6 +166,34 @@ void testfunc() {
_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)
{
init_before_main_module();
@ -585,6 +627,14 @@ BOOST_PYTHON_MODULE(native_pEp)
// "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,
"start_sync()\n"


+ 3
- 2
test/codec_doctest.py View File

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


+ 29
- 7
test/pyadpt-81.py View File

@ -4,22 +4,44 @@
import pEp
import time
def user_message_to_send(msg):
print("User defined callback implementation")
def message_to_send(msg):
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):
pEp.start_sync()
time.sleep(duration)
pEp.shutdown_sync()
alice = pEp.Identity("test@alice.com", "alice", "23")
pEp.myself(alice)
print(alice.fpr)
dir(pEp)
# test default callback
start_stop_sync(1)
# 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)
pEp.start_sync()
while(True):
print("is_sync_active: {}".format(pEp.is_sync_active()))
time.sleep(1)
# pEp.start_sync()
# while(True):
# print("is_sync_active: {}".format(pEp.is_sync_active()))
# time.sleep(3)
# pEp.key_reset_all_own_keys()
# time.sleep(3)

Loading…
Cancel
Save