*sigh* okay, due to strange Engine's API we have to give a function ptr to JsonAdapter::startup().

JSON-123
Roker 4 years ago
parent 3bea2ea578
commit 9c52f13bcc

@ -75,7 +75,8 @@ typedef std::vector<ThreadPtr> ThreadPool;
// *sigh* necessary because messageToSend() has no obj pointer anymore. :-(
JsonAdapter* ja_singleton = 0;
JsonAdapter* ja_singleton = nullptr;
inject_sync_event_t sync_fn = nullptr; // *sigh* ugly, but the Engine's API requires it.
} // end of anonymous namespace
@ -104,7 +105,6 @@ struct JsonAdapter::Internal
unsigned port = 0;
unsigned request_count = 0;
evutil_socket_t sock = -1;
bool shall_sync = false; // just hold the value from config/command line.
bool running = false;
bool silent = false;
bool ignore_session_error = false;
@ -121,14 +121,11 @@ struct JsonAdapter::Internal
~Internal()
{
stopSync();
if(session)
pEp::call_with_lock(&release, session);
session=nullptr;
}
void stopSync();
static
void requestDone(evhttp_request* req, void* userdata)
{
@ -363,7 +360,7 @@ void JsonAdapter::threadFunc()
if(q==session_registry.end())
{
i->session = nullptr;
PEP_STATUS status = pEp::call_with_lock(&init, &i->session, &JsonAdapter::messageToSend, &JsonAdapter::injectSyncMsg); // release(session) in ThreadDeleter
PEP_STATUS status = pEp::call_with_lock(&init, &i->session, &JsonAdapter::messageToSend, sync_fn); // release(session) in ThreadDeleter
if(status != PEP_STATUS_OK || i->session==nullptr)
{
const std::string error_msg = "Cannot create session! PEP_STATUS: " + ::pEp::status_to_string(status) + ".";
@ -376,11 +373,6 @@ void JsonAdapter::threadFunc()
session_registry.emplace(id, this);
L << Logger::Info << "\tcreated new session for this thread: " << static_cast<void*>(i->session) << ".";
if(i->shall_sync && i->session) // startSync() does not make sense without session.
{
L << Logger::Info << "\tstartSync()...";
startSync();
}
}else{
L << Logger::Info << "\tsession for this thread: " << static_cast<void*>(q->second) << ".";
}
@ -593,6 +585,16 @@ JsonAdapter& JsonAdapter::getInstance()
return *ja_singleton;
}
JsonAdapter& JsonAdapter::startup(inject_sync_event_t se)
{
sync_fn = se;
JsonAdapter& ja = getInstance();
ja.prepare_run("127.0.0.1", 4223, 9999);
ja.run();
return ja;
}
namespace {
std::string to_string(const SessionRegistry& reg)

@ -27,7 +27,6 @@ public:
// these functions shall be called before prepare_run()!
// only if do_sync== true the keysync thread is stared and the keysync callbacks are registered.
JsonAdapter& do_sync(bool _do_sync);
JsonAdapter& ignore_session_errors(bool _ig);
// if called with "false" the JSON Adpapter would no longer deliver HTML and JavaScript files, only handle JSON-RPC requests
@ -68,40 +67,27 @@ public:
static PEP_STATUS messageToSend(message* msg);
static PEP_STATUS notifyHandshake(pEp_identity* self, pEp_identity* partner, sync_handshake_signal signal);
/*
// BEWARE: msg is 1st parameter, obj is second!!!
static int injectSyncMsg(Sync_event* msg, void* obj);
static Sync_event* retrieveNextSyncMsg(void* obj, unsigned timeout);
static void* syncThreadRoutine(void* arg);
void startSync();
void stopSync();
static void startKeyserverLookup();
static void stopKeyserverLookup();
static int examineIdentity(pEp_identity*, void* obj);
static pEp_identity* retrieveNextIdentity(void* obj);
static void* keyserverLookupThreadRoutine(void* arg);
*/
Logger::Stream Log(Logger::Severity s = Logger::Severity::Debug) const;
// will throw logic_error if guard variables contains illegal values, which means: *this is not a valid JsonAdapter object!
void check_guard() const;
static JsonAdapter& getInstance();
// Very ugly: that function ptr has to be known to the JA before any instance is created. -.-
static JsonAdapter& startup(inject_sync_event_t inject_fn);
//private:
struct Internal;
unsigned long long guard_0;
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(); }

Loading…
Cancel
Save