Browse Source

Module Adapter - Improve internal terminology only

pull/15/head
heck 2 years ago
parent
commit
6c8ad91cc1
  1. 73
      src/Adapter.cc
  2. 21
      src/Adapter.hh

73
src/Adapter.cc

@ -10,21 +10,24 @@
#include "group_manager_api.h" #include "group_manager_api.h"
thread_local pEp::Adapter::Session pEp::Adapter::session{};
namespace pEp { namespace pEp {
namespace Adapter { namespace Adapter {
::SYNC_EVENT _cb_dequeue_next_sync_event(void *management, unsigned threshold); ::SYNC_EVENT _cb_retrieve_next_sync_event_dequeue_next_sync_event(
void *management,
unsigned threshold);
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
// SESSION // SESSION
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
// the single thread-local instance of class Session
thread_local Session session{};
std::mutex mut{}; std::mutex mut{};
SyncModes Session::_sync_mode{ SyncModes::Async }; SyncModes Session::_cfg_sync_mode{ SyncModes::Async };
bool Session::_adapter_manages_sync_thread{ false }; bool Session::_cfg_adapter_manages_sync_thread{ false };
::messageToSend_t Session::_cb_messageToSend{ nullptr }; ::messageToSend_t Session::_cfg_cb_messageToSend{ nullptr };
::notifyHandshake_t Session::_cb_notifyHandshake{ nullptr }; ::notifyHandshake_t Session::_cfg_cb_notifyHandshake{ nullptr };
::inject_sync_event_t Session::_cb_handle_sync_event_from_engine{ nullptr }; ::inject_sync_event_t Session::_cfg_cb_inject_sync_event{ nullptr };
bool Session::_is_initialized{ false }; bool Session::_is_initialized{ false };
Session::Session() Session::Session()
@ -62,10 +65,10 @@ namespace pEp {
bool adapter_manages_sync_thread) bool adapter_manages_sync_thread)
{ {
// cache the values for sync-thread session creation // cache the values for sync-thread session creation
_cb_messageToSend = messageToSend; _cfg_cb_messageToSend = messageToSend;
_cb_notifyHandshake = notifyHandshake; _cfg_cb_notifyHandshake = notifyHandshake;
_sync_mode = sync_mode; _cfg_sync_mode = sync_mode;
_adapter_manages_sync_thread = adapter_manages_sync_thread; _cfg_adapter_manages_sync_thread = adapter_manages_sync_thread;
::adapter_group_init(); ::adapter_group_init();
_is_initialized = true; _is_initialized = true;
} }
@ -74,25 +77,31 @@ namespace pEp {
{ {
std::lock_guard<std::mutex> lock(mut); std::lock_guard<std::mutex> lock(mut);
if (_sync_mode == SyncModes::Sync) { if (_cfg_sync_mode == SyncModes::Sync) {
_cb_handle_sync_event_from_engine = _cb_pass_sync_event_to_engine; _cfg_cb_inject_sync_event = _cb_inject_sync_event_do_sync_protocol_step;
} }
if (_sync_mode == SyncModes::Async) { if (_cfg_sync_mode == SyncModes::Async) {
_cb_handle_sync_event_from_engine = _cb_enqueue_sync_event; _cfg_cb_inject_sync_event = _cb_inject_sync_event_enqueue_sync_event;
} }
// create // create
::PEP_SESSION session_{ nullptr }; ::PEP_SESSION session_{ nullptr };
::PEP_STATUS status = ::init(&session_, _cb_messageToSend,
_cb_handle_sync_event_from_engine, _ensure_passphrase); ::PEP_STATUS status = ::init(
&session_,
_cfg_cb_messageToSend,
_cfg_cb_inject_sync_event,
_ensure_passphrase);
throw_status(status); throw_status(status);
// _cb_retrieve_next_sync_event is only being called by ::do_sync_protocol()
// which is only being used in async operation using queue
status = ::register_sync_callbacks( status = ::register_sync_callbacks(
session_, session_,
nullptr, nullptr,
_cb_notifyHandshake, _cfg_cb_notifyHandshake,
_cb_dequeue_next_sync_event); _cb_retrieve_next_sync_event_dequeue_next_sync_event);
throw_status(status); throw_status(status);
_session = SessionPtr{ session_, ::release }; _session = SessionPtr{ session_, ::release };
@ -108,7 +117,7 @@ namespace pEp {
// public // public
::PEP_SESSION Session::operator()() ::PEP_SESSION Session::operator()()
{ {
if(!_is_initialized) { if (!_is_initialized) {
throw std::runtime_error( throw std::runtime_error(
"libpEpAdapter: No session! Before first use, call session::initialize()"); "libpEpAdapter: No session! Before first use, call session::initialize()");
} else { } else {
@ -119,6 +128,12 @@ namespace pEp {
return _session.get(); return _session.get();
} }
// public/static
bool Session::adapter_manages_sync_thread()
{
return _cfg_adapter_manages_sync_thread;
}
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
// SYNC // SYNC
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
@ -132,7 +147,9 @@ namespace pEp {
} }
// private // private
int _cb_pass_sync_event_to_engine(::SYNC_EVENT ev, void *management) // must be assigneble to ::inject_sync_event_t _cb_inject_sync_event;
// so, must be of type ::inject_sync_event_t
int _cb_inject_sync_event_do_sync_protocol_step(::SYNC_EVENT ev, void *management)
{ {
if (ev != nullptr) { if (ev != nullptr) {
::do_sync_protocol_step(session(), ev); ::do_sync_protocol_step(session(), ev);
@ -141,7 +158,9 @@ namespace pEp {
} }
// public (json adapter needs it, but should use Session mgmt from libpEpAdapter eventually) // public (json adapter needs it, but should use Session mgmt from libpEpAdapter eventually)
int _cb_enqueue_sync_event(::SYNC_EVENT ev, void *management) // must be assigneble to ::inject_sync_event_t _cb_inject_sync_event;
// so, must be of type ::inject_sync_event_t
int _cb_inject_sync_event_enqueue_sync_event(::SYNC_EVENT ev, void *management)
{ {
try { try {
if (ev == nullptr) { if (ev == nullptr) {
@ -156,8 +175,10 @@ namespace pEp {
return 0; return 0;
} }
// public // private
::SYNC_EVENT _cb_dequeue_next_sync_event(void *management, unsigned threshold) // callback for pEpEngine retrieve_next_sync_event
// so, must be of type ::retrieve_next_sync_event_t
::SYNC_EVENT _cb_retrieve_next_sync_event_dequeue_next_sync_event(void *management, unsigned threshold)
{ {
::SYNC_EVENT syncEvent = nullptr; ::SYNC_EVENT syncEvent = nullptr;
const bool success = sync_evt_q.try_pop_front(syncEvent, std::chrono::seconds(threshold)); const bool success = sync_evt_q.try_pop_front(syncEvent, std::chrono::seconds(threshold));
@ -179,7 +200,7 @@ namespace pEp {
void inject_sync_shutdown() void inject_sync_shutdown()
{ {
pEpLog("called"); pEpLog("called");
_cb_enqueue_sync_event(nullptr, nullptr); _cb_inject_sync_event_enqueue_sync_event(nullptr, nullptr);
} }
void start_sync() void start_sync()

21
src/Adapter.hh

@ -15,12 +15,15 @@
namespace pEp { namespace pEp {
namespace Adapter { namespace Adapter {
// Used to define if sync events are being processes
// sync (single-threaded) or async (multi-threaded)
enum class SyncModes { enum class SyncModes {
Sync, Sync,
Async Async
}; };
// The thread-local pEp-session // pEp-Session manager
// handles lifecycle (create/destroy) and configuratiin of a PEP_SESSION
// CAVEAT: there is a default constructor Sesssion(), // CAVEAT: there is a default constructor Sesssion(),
// BUT // BUT
// the session object needs to be initialized in order to be usable. // the session object needs to be initialized in order to be usable.
@ -28,9 +31,6 @@ namespace pEp {
// are being created in the lifetime of a process // are being created in the lifetime of a process
// A Session object is not copyable/assignable // A Session object is not copyable/assignable
// TODO: remove initialize() and do that in the ctor's
// remove release() and do that in the destructor
class Session { class Session {
public: public:
Session(); Session();
@ -70,7 +70,7 @@ namespace pEp {
void release(); void release();
// returns the PEP_SESSION handle // returns the managed PEP_SESSION
PEP_SESSION operator()(); PEP_SESSION operator()();
bool adapter_manages_sync_thread() bool adapter_manages_sync_thread()
@ -92,15 +92,16 @@ namespace pEp {
SessionPtr _session = nullptr; SessionPtr _session = nullptr;
static SyncModes _sync_mode; static SyncModes _cfg_sync_mode;
static bool _adapter_manages_sync_thread; static bool _cfg_adapter_manages_sync_thread;
static ::messageToSend_t _cb_messageToSend; static ::messageToSend_t _cfg_cb_messageToSend;
static ::notifyHandshake_t _cb_notifyHandshake; static ::notifyHandshake_t _cfg_cb_notifyHandshake;
static ::inject_sync_event_t _cb_handle_sync_event_from_engine; static ::inject_sync_event_t _cfg_cb_inject_sync_event;
static bool _is_initialized; static bool _is_initialized;
}; };
// the single thread-local instance of class Session
extern thread_local Session session; extern thread_local Session session;
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------

Loading…
Cancel
Save