make the JsonAdapter a proper singleton: private default c'tor. mutexed getInstance() method instead.

JSON-123
Roker 4 years ago
parent 70ed7749b7
commit 3bea2ea578

@ -13,8 +13,6 @@
#include <functional>
#include <tuple>
#include <mutex>
#include "json-adapter.hh"
#include "daemonize.hh"
#include "pEp-types.hh"
@ -283,11 +281,6 @@ JsonAdapter::JsonAdapter()
, i(new Internal{})
, guard_1(Guard_1)
{
if(!ja_singleton)
{
ja_singleton = this;
}
i->eventBase.reset(event_base_new());
if (!i->eventBase)
throw std::runtime_error("Failed to create new base_event.");
@ -586,6 +579,20 @@ void JsonAdapter::check_guard() const
}
std::recursive_mutex get_instance_mutex;
JsonAdapter& JsonAdapter::getInstance()
{
std::lock_guard<std::recursive_mutex> L(get_instance_mutex);
if(!ja_singleton)
{
ja_singleton = new JsonAdapter();
}
return *ja_singleton;
}
namespace {
std::string to_string(const SessionRegistry& reg)

@ -12,8 +12,6 @@
class JsonAdapter : public Context
{
public:
// creates an instance of the JSON adapter. It tries to bind the first available port in the given range
JsonAdapter();
// calls shutdown() on the instance if it is still running().
virtual ~JsonAdapter();
@ -98,7 +96,12 @@ public:
Internal* i; // pimpl for stable interface.
unsigned long long guard_1;
static JsonAdapter& getInstance();
private:
// creates the one and only instance of the JSON adapter.
JsonAdapter();
static
void staticThreadFunc(JsonAdapter* that) { that->threadFunc(); }

@ -135,8 +135,7 @@ try
}
}
JsonAdapter ja;
JsonAdapter& ja = JsonAdapter::getInstance();
ja.ignore_session_errors( ignore_missing_session)
.deliver_html( !no_html )
;

Loading…
Cancel
Save