re-define SessionRegistry completely.

JSON-143
Roker 2020-05-14 00:18:58 +02:00
parent c4af4df721
commit d9ad778178
2 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,46 @@
#include "session_registry.hh"
#include <sstream>
#include <pEp/call_with_lock.hh>
#include <pEp/status_to_string.hh>
// calls "init" for the given thread
PEP_SESSION SessionRegistry::add(std::thread::id tid)
{
Lock L(_mtx);
if(m.count(tid) > 0)
{
std::stringstream ss; ss << tid;
throw std::runtime_error("There is already a session for thread " + ss.str() + "!");
}
PEP_SESSION session = nullptr;
PEP_STATUS status = pEp::call_with_lock(&init, &session, mts, ise);
if(status != PEP_STATUS_OK)
{
throw std::runtime_error("init() fails: " + pEp::status_to_string(status) );
}
m[tid] = session;
return session;
}
void SessionRegistry::remove(std::thread::id tid)
{
Lock L(_mtx);
const auto q = m.find(tid);
if(q != m.end())
{
pEp::call_with_lock(&release, q->second);
m.erase(q);
}
}
PEP_SESSION SessionRegistry::get(std::thread::id tid) const
{
Lock L(_mtx);
auto q = m.find(tid);
return (q==m.end()) ? nullptr : q->second;
}

View File

@ -0,0 +1,32 @@
#ifndef JSON_ADAPTER_SESSION_REGISTRY_HH
#define JSON_ADAPTER_SESSION_REGISTRY_HH
#include <map>
#include <mutex>
#include <thread>
#include <pEp/pEpEngine.h>
class SessionRegistry
{
public:
SessionRegistry(messageToSend_t _mts, inject_sync_event_t _ise)
: mts{_mts}
, ise{_ise}
{}
// calls "init" for the given thread
PEP_SESSION add(std::thread::id tid = std::this_thread::get_id());
void remove(std::thread::id tid = std::this_thread::get_id());
PEP_SESSION get(std::thread::id tid = std::this_thread::get_id()) const;
private:
std::map<std::thread::id, PEP_SESSION> m;
messageToSend_t mts;
inject_sync_event_t ise;
typedef std::recursive_mutex Mutex;
typedef std::unique_lock<Mutex> Lock;
mutable Mutex _mtx;
};
#endif // JSON_ADAPTER_SESSION_REGISTRY_HH