remove JsonAdapter::first_session (moved into mini-adapter-main.cc) and call_with_lock(): moved into libpEpAdapter.

JSON-123
Roker 4 years ago
parent 549a512732
commit 042bfe038c

@ -26,6 +26,7 @@
#include "server_version.hh"
#include <pEp/keymanagement.h>
#include <pEp/call_with_lock.hh>
#include <pEp/status_to_string.hh> // from libpEpAdapter.
#include <pEp/locked_queue.hh>
@ -80,29 +81,12 @@ PEP_SESSION keyserver_lookup_session = nullptr; // FIXME: what if another adapte
ThreadPtr keyserver_lookup_thread{nullptr, ThreadDeleter};
std::mutex js_mutex;
// TODO: use && and std::forward<> to avoid copying of the arguments.
// It is not relevant, yet, because at the moment we use this function template only
// for init() and release() which have cheap-to-copy pointer parameters only
template<class R, class... Args>
R call_with_lock( R(*fn)(Args...), Args... args)
{
std::lock_guard<std::mutex> L(js_mutex);
return fn(args...);
}
// *sigh* necessary because messageToSend() has no obj pointer anymore. :-(
JsonAdapter* ja_singleton = 0;
} // end of anonymous namespace
PEP_SESSION JsonAdapter::first_session = nullptr;
typedef std::pair<std::string, unsigned> EventListenerKey;
struct EventListenerValue
@ -152,7 +136,7 @@ struct JsonAdapter::Internal
{
stopSync();
if(session)
call_with_lock(&release, session);
pEp::call_with_lock(&release, session);
session=nullptr;
}
@ -248,7 +232,7 @@ struct JsonAdapter::Internal
void* syncThreadRoutine(void* arg)
{
PEP_STATUS status = call_with_lock(&init, &sync_session, &JsonAdapter::messageToSend, &JsonAdapter::injectSyncMsg);
PEP_STATUS status = pEp::call_with_lock(&init, &sync_session, &JsonAdapter::messageToSend, &JsonAdapter::injectSyncMsg);
if (status != PEP_STATUS_OK)
throw std::runtime_error("Cannot init sync_session! status: " + ::pEp::status_to_string(status));
@ -412,7 +396,7 @@ void JsonAdapter::Internal::stopSync()
unregister_sync_callbacks(sync_session);
sync_queue.clear();
call_with_lock(&release, sync_session);
pEp::call_with_lock(&release, sync_session);
sync_session = nullptr;
}
@ -422,7 +406,7 @@ void JsonAdapter::startKeyserverLookup()
if(keyserver_lookup_session)
throw std::runtime_error("KeyserverLookup already started.");
PEP_STATUS status = call_with_lock(&init, &keyserver_lookup_session, &JsonAdapter::messageToSend, &JsonAdapter::injectSyncMsg);
PEP_STATUS status = pEp::call_with_lock(&init, &keyserver_lookup_session, &JsonAdapter::messageToSend, &JsonAdapter::injectSyncMsg);
if(status != PEP_STATUS_OK || keyserver_lookup_session==nullptr)
{
throw std::runtime_error("Cannot create keyserver lookup session! status: " + ::pEp::status_to_string(status));
@ -451,7 +435,7 @@ void JsonAdapter::stopKeyserverLookup()
// there is no unregister_examine_callback() function. hum...
keyserver_lookup_queue.clear();
call_with_lock(&release, keyserver_lookup_session);
pEp::call_with_lock(&release, keyserver_lookup_session);
keyserver_lookup_session = nullptr;
}
@ -544,22 +528,6 @@ void JsonAdapter::prepare_run(const std::string& address, unsigned start_port, u
i->start_port = start_port;
i->end_port = end_port;
if(first_session == nullptr) // okay, we are the 1st:
{
// create a dummy session just to see whether the Engine is functional.
// reason: here we still can log errors to stderr, because prepare_run() is called before daemonize().
PEP_STATUS status = call_with_lock(&init, &first_session, &JsonAdapter::messageToSend, &JsonAdapter::injectSyncMsg);
if(status != PEP_STATUS_OK || first_session==nullptr)
{
const std::string error_msg = "Cannot create first session! PEP_STATUS: " + ::pEp::status_to_string(status) + ".";
std::cerr << error_msg << std::endl; // Log to stderr intentionally, so Enigmail can grab that error message easily.
if( ! i->ignore_session_error)
{
throw std::runtime_error(error_msg);
}
}
}
Log() << "ThreadFunc: thread id " << std::this_thread::get_id() << ". \n Registry: " << to_string( session_registry );
unsigned port_ofs = 0;
@ -598,7 +566,7 @@ void JsonAdapter::threadFunc()
if(q==session_registry.end())
{
i->session = nullptr;
PEP_STATUS status = 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, &JsonAdapter::injectSyncMsg); // 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) + ".";
@ -814,13 +782,6 @@ void JsonAdapter::check_guard() const
}
void JsonAdapter::global_shutdown()
{
call_with_lock(&release, JsonAdapter::first_session);
JsonAdapter::first_session = nullptr;
}
namespace {
std::string to_string(const SessionRegistry& reg)

@ -90,10 +90,6 @@ public:
// will throw logic_error if guard variables contains illegal values, which means: *this is not a valid JsonAdapter object!
void check_guard() const;
// calls release(first_session).
static
void global_shutdown();
//private:
struct Internal;
unsigned long long guard_0;
@ -106,9 +102,6 @@ private:
void threadFunc();
std::exception_ptr initExcept;
// albait not documented, the first PEP_SESSION is special: It must be alive as long as any other PEP_SESSIONS are living.
static PEP_SESSION first_session;
};
// just for debug:

@ -12,6 +12,9 @@
#include <iostream>
#include <boost/program_options.hpp>
#include <pEp/call_with_lock.hh>
#include <pEp/status_to_string.hh>
namespace po = boost::program_options;
bool debug_mode = false;
@ -28,6 +31,10 @@ std::string logfile = "";
unsigned start_port = 4223;
unsigned end_port = 9999;
// albait not documented, the first PEP_SESSION in a process is special:
// It must be alive as long as any other PEP_SESSIONS are living.
static PEP_SESSION first_session = nullptr;
void print_version()
{
@ -112,6 +119,20 @@ try
if( debug_mode == false )
daemonize (!debug_mode, (const uintptr_t) status_handle);
// create a dummy session just to see whether the Engine is functional.
// reason: here we still can log errors to stderr, because prepare_run() is called before daemonize().
PEP_STATUS status = pEp::call_with_lock(&init, &first_session, &JsonAdapter::messageToSend, &JsonAdapter::injectSyncMsg);
if(status != PEP_STATUS_OK || first_session==nullptr)
{
const std::string error_msg = "Cannot create first session! PEP_STATUS: " + ::pEp::status_to_string(status) + ".";
std::cerr << error_msg << std::endl; // Log to stderr intentionally, so Enigmail can grab that error message easily.
if( ! ignore_missing_session)
{
throw std::runtime_error(error_msg);
}
}
JsonAdapter ja;
ja.do_sync( do_sync)
.ignore_session_errors( ignore_missing_session)
@ -145,7 +166,7 @@ try
}
ja.shutdown(nullptr);
ja.Log() << "Good bye. :-)";
JsonAdapter::global_shutdown();
pEp::call_with_lock(&release, first_session);
}
catch(std::exception const& e)
{

Loading…
Cancel
Save