diff --git a/server/mini-adapter-impl.cc b/server/mini-adapter-impl.cc index 762b234..02032d0 100644 --- a/server/mini-adapter-impl.cc +++ b/server/mini-adapter-impl.cc @@ -6,6 +6,7 @@ #include #include // from libpEpAdapter. #include +#include namespace pEp { namespace mini { @@ -17,90 +18,34 @@ namespace mini { PEP_SESSION keyserver_lookup_session = nullptr; // FIXME: what if another adapter started it already? ThreadPtr keyserver_lookup_thread; - utility::locked_queue< Sync_event*, &free_Sync_event> sync_queue; - PEP_SESSION sync_session = nullptr; - ThreadPtr sync_thread; - - - int injectSyncMsg(Sync_event* msg, void* /* management */) - { - sync_queue.push_back(msg); - return 0; - } - int injectIdentity(pEp_identity* idy) { keyserver_lookup_queue.push_back(idy); return 0; } - Sync_event* retrieveNextSyncMsg(void* /*management*/, unsigned timeout) - { - Sync_event* msg = nullptr; - if(timeout) - { - const bool success = sync_queue.try_pop_front(msg, std::chrono::seconds(timeout)); - if(!success) - { - // this is timeout occurrence - return new_sync_timeout_event(); - } - }else{ - msg = sync_queue.pop_front(); - } - return msg; - } - pEp_identity* retrieveNextIdentity(void* /*userdata*/) { return keyserver_lookup_queue.pop_front(); } - void* syncThreadRoutine(void* arg) - { - PEP_STATUS status = pEp::call_with_lock(&init, &sync_session, &JsonAdapter::messageToSend, &injectSyncMsg); - if (status != PEP_STATUS_OK) - throw std::runtime_error("Cannot init sync_session! status: " + ::pEp::status_to_string(status)); - - status = register_sync_callbacks(sync_session, - (void*) -1, - &JsonAdapter::notifyHandshake, - &retrieveNextSyncMsg); - if (status != PEP_STATUS_OK) - throw std::runtime_error("Cannot register sync callbacks! status: " + ::pEp::status_to_string(status)); - - status = do_sync_protocol(sync_session, arg); // does the whole work - sync_queue.clear(); // remove remaining messages - return (void*) status; - } + struct dummy_t{}; + dummy_t dummy{}; void startSync() { - sync_queue.clear(); - -// status = attach_sync_session(i->session, i->sync_session); -// if(status != PEP_STATUS_OK) -// throw std::runtime_error("Cannot attach to sync session! status: " + status_to_string(status)); - - sync_thread.reset( new std::thread( syncThreadRoutine, nullptr ) ); + ::pEp::Adapter::startup( + &JsonAdapter::messageToSend, + &JsonAdapter::notifyHandshake, + &dummy + ); } void stopSync() { - // No sync session active - if(sync_session == nullptr) - return; - - sync_queue.push_front(NULL); - sync_thread->join(); - - unregister_sync_callbacks(sync_session); - sync_queue.clear(); - - pEp::call_with_lock(&release, sync_session); - sync_session = nullptr; + ::pEp::Adapter::shutdown(); } @@ -109,7 +54,7 @@ void startKeyserverLookup() if(keyserver_lookup_session) throw std::runtime_error("KeyserverLookup already started."); - PEP_STATUS status = pEp::call_with_lock(&init, &keyserver_lookup_session, &JsonAdapter::messageToSend, &injectSyncMsg); + PEP_STATUS status = pEp::call_with_lock(&init, &keyserver_lookup_session, &JsonAdapter::messageToSend, &::pEp::Adapter::_inject_sync_event); if(status != PEP_STATUS_OK || keyserver_lookup_session==nullptr) { throw std::runtime_error("Cannot create keyserver lookup session! status: " + ::pEp::status_to_string(status)); @@ -150,7 +95,6 @@ int examineIdentity(pEp_identity* idy, void* obj) } - void* keyserverLookupThreadRoutine(void* arg) { PEP_STATUS status = do_keymanagement( @@ -170,7 +114,7 @@ Adapter& Adapter::createInstance() std::thread::id Adapter::get_sync_thread_id() const { - return sync_thread ? sync_thread->get_id() : std::thread::id{} ; + return ::pEp::Adapter::sync_thread_id(); } diff --git a/server/mini-adapter-impl.hh b/server/mini-adapter-impl.hh index d5bd5f4..78f1611 100644 --- a/server/mini-adapter-impl.hh +++ b/server/mini-adapter-impl.hh @@ -5,21 +5,16 @@ #include #include "json-adapter.hh" #include +#include namespace pEp{ namespace mini { - int injectSyncMsg(Sync_event* msg, void* /*management*/ ); - int injectIdentity(pEp_identity* idy); - Sync_event* retrieveNextSyncMsg(void* /*management*/, unsigned timeout); - pEp_identity* retrieveNextIdentity( void* /*management*/); - void* syncThreadRoutine(void* arg); - void startSync(); void stopSync(); @@ -39,7 +34,7 @@ namespace mini { protected: virtual inject_sync_event_t getInjectSyncEvent() const override { - return &::pEp::mini::injectSyncMsg; + return &::pEp::Adapter::_inject_sync_event; } }; diff --git a/server/mini-adapter-main.cc b/server/mini-adapter-main.cc index ac6a628..a408264 100644 --- a/server/mini-adapter-main.cc +++ b/server/mini-adapter-main.cc @@ -15,6 +15,8 @@ #include #include +#include + namespace po = boost::program_options; @@ -125,7 +127,7 @@ try // create a dummy session just to see whether the Engine is functional. // reason: here we still can log errors to stderr, because prepare_run() is called before daemonize(). - PEP_STATUS status = pEp::call_with_lock(&init, &first_session, &JsonAdapter::messageToSend, &pEp::mini::injectSyncMsg); + PEP_STATUS status = pEp::call_with_lock(&init, &first_session, &JsonAdapter::messageToSend, &pEp::Adapter::_inject_sync_event); if(status != PEP_STATUS_OK || first_session==nullptr) { const std::string error_msg = "Cannot create first session! PEP_STATUS: " + ::pEp::status_to_string(status) + ".";