diff --git a/callback_dispatcher.cc b/callback_dispatcher.cc index 4ffb719..1986f98 100644 --- a/callback_dispatcher.cc +++ b/callback_dispatcher.cc @@ -24,10 +24,14 @@ namespace pEp { return callback_dispatcher._notifyHandshake(me, partner, signal); } - void CallbackDispatcher::add(::messageToSend_t messageToSend, - ::notifyHandshake_t notifyHandshake) + void CallbackDispatcher::add( + ::messageToSend_t messageToSend, + ::notifyHandshake_t notifyHandshake, + proc on_startup, + proc shutdown + ) { - targets.push_back({messageToSend, notifyHandshake}); + targets.push_back({messageToSend, notifyHandshake, on_startup, shutdown}); } void CallbackDispatcher::remove(::messageToSend_t messageToSend) @@ -40,6 +44,31 @@ namespace pEp { } } + void CallbackDispatcher::on_startup() + { + for (auto target : targets) + target.on_startup(); + } + + void CallbackDispatcher::on_shutdown() + { + for (auto target : targets) + target.on_shutdown(); + } + + void CallbackDispatcher::start_sync() + { + pEp::Adapter::startup(CallbackDispatcher::messageToSend, + CallbackDispatcher::notifyHandshake, this, + &CallbackDispatcher::on_startup, + &CallbackDispatcher::on_shutdown); + } + + void CallbackDispatcher::stop_sync() + { + pEp::Adapter::shutdown(); + } + PEP_STATUS CallbackDispatcher::_messageToSend(::message *msg) { for (auto target : targets) { diff --git a/callback_dispatcher.hh b/callback_dispatcher.hh index 2989609..5e8de21 100644 --- a/callback_dispatcher.hh +++ b/callback_dispatcher.hh @@ -1,13 +1,23 @@ #pragma once #include +#include #include "Adapter.hh" namespace pEp { + // use this class when implementing a desktop adapter + // register different interfaces with add() + // then use CallbackDispatcher::start_sync() to start Sync + // and CallbackDispatcher::stop_sync() to stop Sync + class CallbackDispatcher { + using proc = std::function; + struct callbacks { ::messageToSend_t messageToSend; ::notifyHandshake_t notifyHandshake; + proc on_startup; + proc on_shutdown; }; std::vector targets; @@ -16,15 +26,25 @@ namespace pEp { public: CallbackDispatcher(); + void add( + ::messageToSend_t messageToSend, + ::notifyHandshake_t notifyHandshake, + proc on_startup = nullptr, + proc on_shutdown = nullptr + ); + void remove(::messageToSend_t messageToSend); + + void start_sync(); + void stop_sync(); + + protected: + void on_startup(); + void on_shutdown(); + static PEP_STATUS messageToSend(::message *msg); static PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal); - void add(::messageToSend_t messageToSend, - ::notifyHandshake_t notifyHandshake); - void remove(::messageToSend_t messageToSend); - - protected: PEP_STATUS _messageToSend(::message *msg); PEP_STATUS _notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal);