Browse Source

Adapter.hxx: add documentation

pull/8/head
heck 4 years ago
parent
commit
21e4496fd2
  1. 40
      src/Adapter.hxx

40
src/Adapter.hxx

@ -26,6 +26,16 @@ namespace pEp {
static std::exception_ptr _ex; static std::exception_ptr _ex;
static std::atomic_bool register_done{false}; static std::atomic_bool register_done{false};
/*
* Sync Thread
* 1. Execute registered startup function
* 2. Create session for the sync thread (registers: messageToSend, _inject_sync_event, _ensure_passphrase)
* 3. register_sync_callbacks() (registers: _notifyHandshake, _retrieve_next_sync_event)
* 4. Enter Sync Event Dispatching Loop (do_sync_protocol())
* 5. unregister_sync_callbacks()
* 6. Release the session
* 7. Execute registered shutdown function
*/
template<class T> template<class T>
void sync_thread(T *obj, function<void(T *)> _startup, function<void(T *)> _shutdown) void sync_thread(T *obj, function<void(T *)> _startup, function<void(T *)> _shutdown)
{ {
@ -33,13 +43,17 @@ namespace pEp {
_ex = nullptr; _ex = nullptr;
assert(_messageToSend); assert(_messageToSend);
assert(_notifyHandshake); assert(_notifyHandshake);
// 1. Execute registered startup function
if (obj && _startup) { if (obj && _startup) {
_startup(obj); _startup(obj);
} }
pEpLog("creating session"); pEpLog("creating session for the sync thread");
// 2. Create session for the sync thread
session(); session();
// 3. register_sync_callbacks()
{ {
// TODO: Do we need to use a passphraseWrap here??? // TODO: Do we need to use a passphraseWrap here???
pEpLog("register_sync_callbacks()"); pEpLog("register_sync_callbacks()");
@ -50,6 +64,8 @@ namespace pEp {
_retrieve_next_sync_event); _retrieve_next_sync_event);
pEpLog("register_sync_callbacks() return:" << status); pEpLog("register_sync_callbacks() return:" << status);
// Convert status into exception and store it
// set register_done AFTER that
try { try {
throw_status(status); throw_status(status);
register_done.store(true); register_done.store(true);
@ -61,17 +77,30 @@ namespace pEp {
} }
pEpLog("sync protocol loop started"); pEpLog("sync protocol loop started");
// 4. Enter Sync Event Dispatching Loop (do_sync_protocol())
::do_sync_protocol(session(), (void *)obj); ::do_sync_protocol(session(), (void *)obj);
pEpLog("sync protocol loop ended"); pEpLog("sync protocol loop ended");
// 5. unregister_sync_callbacks()
unregister_sync_callbacks(session()); unregister_sync_callbacks(session());
// 6. Release the session
// TODO: Maybe do that AFTER shutdown?
session(release); session(release);
// 7. Execute registered shutdown function
if (obj && _shutdown) { if (obj && _shutdown) {
_shutdown(obj); _shutdown(obj);
} }
} }
/*
* Sync Thread Startup
* 1. ensure session for the main thread (registers: messageToSend, _inject_sync_event, _ensure_passphrase)
* 2. Start the sync thread
* 3. Defer execution until sync thread register_sync_callbacks() has returned
* 4. Throw pending exception from the sync thread
*/
template<class T> template<class T>
void startup( void startup(
::messageToSend_t messageToSend, ::messageToSend_t messageToSend,
@ -88,19 +117,22 @@ namespace pEp {
if (notifyHandshake) { if (notifyHandshake) {
_notifyHandshake = notifyHandshake; _notifyHandshake = notifyHandshake;
} }
pEpLog("creating session"); pEpLog("ensure session for the main thread");
// 1. ensure session for the main thread (registers: messageToSend, _inject_sync_event, _ensure_passphrase)
session(); session();
if (!_sync_thread.joinable()) { if (!_sync_thread.joinable()) {
register_done.store(false); register_done.store(false);
pEpLog("creating sync-thread"); pEpLog("creating sync-thread");
// 2. Start the sync thread
_sync_thread = std::thread(sync_thread<T>, obj, _startup, _shutdown); _sync_thread = std::thread(sync_thread<T>, obj, _startup, _shutdown);
// 3. Defer execution until sync thread register_sync_callbacks() has returned
while (register_done.load() == false) { while (register_done.load() == false) {
pEpLog("waiting for sync-thread to init..."); pEpLog("waiting for sync-thread to init...");
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
// 4. Throw pending exception from the sync thread
if (_ex) { if (_ex) {
pEpLog("exception pending, rethrowing"); pEpLog("exception pending, rethrowing");
std::rethrow_exception(_ex); std::rethrow_exception(_ex);
@ -110,4 +142,4 @@ namespace pEp {
} // namespace Adapter } // namespace Adapter
} // namespace pEp } // namespace pEp
#endif // LIBPEPADAPTER_ADAPTER_HXX #endif //LIBPEPADAPTER_ADAPTER_HXX
Loading…
Cancel
Save