From f078dee7209ae5d2a6d308fe96fc51b62b606f90 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sat, 15 Jun 2019 14:55:32 +0200 Subject: [PATCH] multithreaded implementation as an alternative --- src/pEpmodule.cc | 12 ++++++++++++ src/pEpmodule.hh | 1 + test/sync_handshake.py | 17 ++++++++++++++++- test/sync_test.py | 15 +++++++++++---- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index c506353..eb6cb53 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -103,6 +103,11 @@ namespace pEp { void messageToSend(Message msg) { throw runtime_error("implement pEp.messageToSend(msg)"); } + + void do_sync_protocol() + { + ::do_sync_protocol(adapter.session(), nullptr); + } } } @@ -490,6 +495,13 @@ BOOST_PYTHON_MODULE(pEp) "call to deliver the handshake result of the handshake dialog") ; + def("do_sync_protocol", &pEp::PythonAdapter::do_sync_protocol, + "do_sync_protocol()\n" + "\n" + "in case of an explicit sync thread instead of a single threaded\n" + "implementation call this function in your sync thread\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 c89609b..af575d3 100644 --- a/src/pEpmodule.hh +++ b/src/pEpmodule.hh @@ -16,6 +16,7 @@ namespace pEp { void _throw_status(PEP_STATUS status); void messageToSend(Message msg); PEP_STATUS _messageToSend(::message *msg); + void do_sync_protocol(); extern Adapter adapter; } } diff --git a/test/sync_handshake.py b/test/sync_handshake.py index d259814..1517917 100644 --- a/test/sync_handshake.py +++ b/test/sync_handshake.py @@ -41,6 +41,7 @@ except: inbox = pathlib.Path("..") / "TestInbox" device_name = "" output = print +multithreaded = False DONT_TRIGGER_SYNC = 0x200 SYNC_HANDSHAKE_ACCEPTED = 0 @@ -131,7 +132,17 @@ def run(name, color=None): me = pEp.Identity("alice@peptest.ch", name + " of Alice Neuman", name) pEp.myself(me) pEp.messageToSend = messageToSend - ui = UserInterface() + + if multithreaded: + from threading import Thread + def sync_thread(): + ui = UserInterface() + pEp.do_sync_protocol() + ui_thread = Thread(target=sync_thread) + ui_thread.start() + else: + ui_thread = None + ui = UserInterface() try: while not the_end: @@ -163,6 +174,9 @@ if __name__=="__main__": help="accept device group (default)") optParser.add_option("-E", "--end-on", dest="notifications", help="end test on these notifications") + optParser.add_option("-j", "--multi-threaded", action="store_true", + dest="multithreaded", + help="use multithreaded instead of single threaded implementation") options, args = optParser.parse_args() if not options.exec_for: @@ -174,5 +188,6 @@ if __name__=="__main__": except TypeError: end_on = (end_on,) + multithreaded = options.multithreaded run(options.exec_for, options.color) diff --git a/test/sync_test.py b/test/sync_test.py index 26322c3..e4d0dc8 100644 --- a/test/sync_test.py +++ b/test/sync_test.py @@ -20,7 +20,7 @@ import shutil import pathlib -def test_for(path, color=None, end_on=None): +def test_for(path, color=None, end_on=None, mt=False): cwd = os.getcwd(); os.chdir(path) os.environ["HOME"] = os.getcwd() @@ -29,6 +29,7 @@ def test_for(path, color=None, end_on=None): import sync_handshake if end_on: sync_handshake.end_on = end_on + sync_handshake.multithreaded = mt sync_handshake.run(path, color) os.chdir(cwd) @@ -91,6 +92,9 @@ if __name__ == "__main__": help="end test on these notifications") optParser.add_option("-3", "--third-device", action="store_true", dest="third", help="start Pad as third device") + optParser.add_option("-j", "--multi-threaded", action="store_true", + dest="multithreaded", + help="use multithreaded instead of single threaded implementation") options, args = optParser.parse_args() if options.cleanall: @@ -154,10 +158,13 @@ if __name__ == "__main__": try: None in end_on except TypeError: end_on = (end_on,) - Phone = Process(target=test_for, args=("Phone", "red", end_on)) - Laptop = Process(target=test_for, args=("Laptop", "green", end_on)) + Phone = Process(target=test_for, args=("Phone", "red", end_on, + options.multithreaded)) + Laptop = Process(target=test_for, args=("Laptop", "green", end_on, + options.multithreaded)) if options.third: - Pad = Process(target=test_for, args=("Pad", "cyan", end_on)) + Pad = Process(target=test_for, args=("Pad", "cyan", end_on, + options.multithreaded)) Phone.start() Laptop.start()