big refactoring. move all non-library stuff from JsonAdapter into mini-adapter-impl. But injectSyncMsg is still an issue, because the JsonAdapter calls init() and init() has this fn ptr as parameter. :-/

JSON-123
Roker 4 years ago
parent 042bfe038c
commit 70ed7749b7

@ -30,7 +30,7 @@ ifdef BUILD_CONFIG
endif
# These source files shall _not_ be part of the libjson-adapter.a:
NO_SOURCE=mini-adapter-main.cc servertest.cc $(wildcard unittest_*.cc)
NO_SOURCE=$(wildcard mini-adapter-*.cc) servertest.cc $(wildcard unittest_*.cc)
ALL_SOURCE=$(filter-out $(NO_SOURCE),$(wildcard json_spirit/*.cpp))
ALL_SOURCE+= prefix-config.cc
@ -85,10 +85,10 @@ endif
.PHONY: all
all: $(TARGET) $(TARGET_TEST) $(TARGET_GTEST)
$(TARGET): mini-adapter-main.o libjson-adapter.a
$(TARGET): mini-adapter-main.o mini-adapter-impl.o libjson-adapter.a
$(CXX) $(CPPFLAGS) $^ $(LDFLAGS) $(LDLIBS) -o $@
$(TARGET)-static: mini-adapter-main.o libjson-adapter.a
$(TARGET)-static: mini-adapter-main.o mini-adapter-impl.o libjson-adapter.a
$(CXX) $(CPPFLAGS) $^ $(LDFLAGS) $(LDLIBS) -static -o $@
$(TARGET_TEST): servertest.o libjson-adapter.a

@ -75,11 +75,6 @@ using In_Pep_Session = In<PEP_SESSION, ParamFlag::NoInput>;
// these are the pEp functions that are callable by the client
const FunctionMap functions = {
FP( "startKeySync", new Func<void, In<JsonAdapter*,ParamFlag::NoInput>>( &JsonAdapter::startSync) ),
FP( "stopKeySync", new Func<void, In<JsonAdapter*,ParamFlag::NoInput>>( &JsonAdapter::stopSync ) ),
FP( "startKeyserverLookup", new Func<void>( &JsonAdapter::startKeyserverLookup) ),
FP( "stopKeyserverLookup", new Func<void>( &JsonAdapter::stopKeyserverLookup ) ),
// from message_api.h
FP( "Message API", new Separator ),
FP( "encrypt_message", new Func<PEP_STATUS, In_Pep_Session, In<message*>, In<stringlist_t*>, Out<message*>, In<PEP_enc_format>, In<PEP_encrypt_flags_t>>( &encrypt_message ) ),

@ -75,11 +75,6 @@ auto ThreadDeleter = [](std::thread* t)
typedef std::unique_ptr<std::thread, decltype(ThreadDeleter)> ThreadPtr;
typedef std::vector<ThreadPtr> ThreadPool;
// keyserver lookup
utility::locked_queue< pEp_identity*, &free_identity> keyserver_lookup_queue;
PEP_SESSION keyserver_lookup_session = nullptr; // FIXME: what if another adapter started it already?
ThreadPtr keyserver_lookup_thread{nullptr, ThreadDeleter};
// *sigh* necessary because messageToSend() has no obj pointer anymore. :-(
JsonAdapter* ja_singleton = 0;
@ -119,12 +114,6 @@ struct JsonAdapter::Internal
ThreadPool threads;
PEP_SESSION session = nullptr;
// Sync
utility::locked_queue< Sync_event*, &free_Sync_event> sync_queue;
PEP_SESSION sync_session = nullptr;
ThreadPtr sync_thread{nullptr, ThreadDeleter};
explicit Internal()
: Log("JAI")
{}
@ -195,58 +184,7 @@ struct JsonAdapter::Internal
addToArray( param_array, params...);
return makeAndDeliverRequest(msg_name, param_array);
}
int injectSyncMsg(Sync_event* msg)
{
sync_queue.push_back(msg);
return 0;
}
int injectIdentity(pEp_identity* idy)
{
keyserver_lookup_queue.push_back(idy);
return 0;
}
Sync_event* retrieveNextSyncMsg(unsigned timeout)
{
Sync_event* msg = nullptr;
if(timeout)
{
const bool success = sync_queue.try_pop_front(msg, std::chrono::seconds(timeout));
if(!success)
{
// this is timeout occurrence
return new_sync_timeout_event();
}
}else{
msg = sync_queue.pop_front();
}
return msg;
}
pEp_identity* retrieveNextIdentity()
{
return keyserver_lookup_queue.pop_front();
}
void* syncThreadRoutine(void* arg)
{
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));
status = register_sync_callbacks(sync_session,
(void*) this,
JsonAdapter::notifyHandshake,
JsonAdapter::retrieveNextSyncMsg);
if (status != PEP_STATUS_OK)
throw std::runtime_error("Cannot register sync callbacks! status: " + ::pEp::status_to_string(status));
status = do_sync_protocol(sync_session, arg); // does the whole work
sync_queue.clear(); // remove remaining messages
return (void*) status;
}
};
@ -340,131 +278,6 @@ PEP_STATUS JsonAdapter::notifyHandshake(pEp_identity* self, pEp_identity* partne
}
// BEWARE: msg is 1st parameter, obj is second!!!
int JsonAdapter::injectSyncMsg(Sync_event* msg, void* obj)
{
// JsonAdapter* ja = static_cast<JsonAdapter*>(obj);
return ja_singleton->i->injectSyncMsg(msg);
}
Sync_event* JsonAdapter::retrieveNextSyncMsg(void* obj, unsigned timeout)
{
// JsonAdapter* ja = static_cast<JsonAdapter*>(obj);
ja_singleton->check_guard();
return ja_singleton->i->retrieveNextSyncMsg(timeout);
}
void* JsonAdapter::syncThreadRoutine(void* arg)
{
// JsonAdapter* ja = static_cast<JsonAdapter*>(arg);
return ja_singleton->i->syncThreadRoutine(arg);
}
void JsonAdapter::startSync()
{
check_guard();
i->sync_queue.clear();
// status = attach_sync_session(i->session, i->sync_session);
// if(status != PEP_STATUS_OK)
// throw std::runtime_error("Cannot attach to sync session! status: " + status_to_string(status));
i->sync_thread.reset( new std::thread( JsonAdapter::syncThreadRoutine, (void*)this ) );
}
void JsonAdapter::stopSync()
{
check_guard();
i->stopSync();
}
void JsonAdapter::Internal::stopSync()
{
// No sync session active
if(sync_session == nullptr)
return;
sync_queue.push_front(NULL);
sync_thread->join();
unregister_sync_callbacks(sync_session);
sync_queue.clear();
pEp::call_with_lock(&release, sync_session);
sync_session = nullptr;
}
void JsonAdapter::startKeyserverLookup()
{
if(keyserver_lookup_session)
throw std::runtime_error("KeyserverLookup already started.");
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));
}
keyserver_lookup_queue.clear();
status = register_examine_function(keyserver_lookup_session,
JsonAdapter::examineIdentity,
&keyserver_lookup_session // nullptr is not accepted, so any dummy ptr is used here
);
if (status != PEP_STATUS_OK)
throw std::runtime_error("Cannot register keyserver lookup callbacks! status: " + ::pEp::status_to_string(status));
keyserver_lookup_thread.reset( new std::thread( JsonAdapter::keyserverLookupThreadRoutine, &keyserver_lookup_session /* just a dummy */ ) );
}
void JsonAdapter::stopKeyserverLookup()
{
// No keyserver lookup session active
if(keyserver_lookup_session == nullptr)
return;
keyserver_lookup_queue.push_front(NULL);
keyserver_lookup_thread->join();
// there is no unregister_examine_callback() function. hum...
keyserver_lookup_queue.clear();
pEp::call_with_lock(&release, keyserver_lookup_session);
keyserver_lookup_session = nullptr;
}
int JsonAdapter::examineIdentity(pEp_identity* idy, void* obj)
{
// JsonAdapter* ja = static_cast<JsonAdapter*>(obj);
return ja_singleton->i->injectIdentity(idy);
}
pEp_identity* JsonAdapter::retrieveNextIdentity(void* obj)
{
// JsonAdapter* ja = static_cast<JsonAdapter*>(obj);
return ja_singleton->i->retrieveNextIdentity();
}
void* JsonAdapter::keyserverLookupThreadRoutine(void* arg)
{
PEP_STATUS status = do_keymanagement(
&JsonAdapter::retrieveNextIdentity,
arg); // does the whole work
keyserver_lookup_queue.clear();
return (void*) status;
}
JsonAdapter::JsonAdapter()
: guard_0(Guard_0)
, i(new Internal{})
@ -489,7 +302,6 @@ JsonAdapter::~JsonAdapter()
{
check_guard();
Log() << "~JsonAdapter(): " << session_registry.size() << " sessions registered.";
stopSync();
this->shutdown(nullptr);
Log() << "\t After stopSync() and shutdown() there are " << session_registry.size() << " sessions registered.";
delete i;
@ -497,14 +309,6 @@ JsonAdapter::~JsonAdapter()
}
JsonAdapter& JsonAdapter::do_sync(bool _do_sync)
{
check_guard();
i->shall_sync = _do_sync;
return *this;
}
JsonAdapter& JsonAdapter::ignore_session_errors(bool _ig)
{
check_guard();

@ -70,6 +70,7 @@ 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);
@ -84,7 +85,8 @@ public:
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!

@ -0,0 +1,166 @@
#include "mini-adapter-impl.hh"
#include "json-adapter.hh"
#include <thread>
#include <pEp/keymanagement.h>
#include <pEp/call_with_lock.hh>
#include <pEp/status_to_string.hh> // from libpEpAdapter.
#include <pEp/locked_queue.hh>
namespace pEp {
namespace mini {
typedef std::unique_ptr<std::thread> ThreadPtr;
// keyserver lookup
utility::locked_queue< pEp_identity*, &free_identity> keyserver_lookup_queue;
PEP_SESSION keyserver_lookup_session = nullptr; // FIXME: what if another adapter started it already?
ThreadPtr keyserver_lookup_thread;
utility::locked_queue< Sync_event*, &free_Sync_event> sync_queue;
PEP_SESSION sync_session = nullptr;
ThreadPtr sync_thread;
int injectSyncMsg(Sync_event* msg, void* /* management */)
{
sync_queue.push_back(msg);
return 0;
}
int injectIdentity(pEp_identity* idy)
{
keyserver_lookup_queue.push_back(idy);
return 0;
}
Sync_event* retrieveNextSyncMsg(void* /*management*/, unsigned timeout)
{
Sync_event* msg = nullptr;
if(timeout)
{
const bool success = sync_queue.try_pop_front(msg, std::chrono::seconds(timeout));
if(!success)
{
// this is timeout occurrence
return new_sync_timeout_event();
}
}else{
msg = sync_queue.pop_front();
}
return msg;
}
pEp_identity* retrieveNextIdentity(void* /*userdata*/)
{
return keyserver_lookup_queue.pop_front();
}
void* syncThreadRoutine(void* arg)
{
PEP_STATUS status = pEp::call_with_lock(&init, &sync_session, &JsonAdapter::messageToSend, &injectSyncMsg);
if (status != PEP_STATUS_OK)
throw std::runtime_error("Cannot init sync_session! status: " + ::pEp::status_to_string(status));
status = register_sync_callbacks(sync_session,
(void*) -1,
&JsonAdapter::notifyHandshake,
&retrieveNextSyncMsg);
if (status != PEP_STATUS_OK)
throw std::runtime_error("Cannot register sync callbacks! status: " + ::pEp::status_to_string(status));
status = do_sync_protocol(sync_session, arg); // does the whole work
sync_queue.clear(); // remove remaining messages
return (void*) status;
}
void startSync()
{
sync_queue.clear();
// status = attach_sync_session(i->session, i->sync_session);
// if(status != PEP_STATUS_OK)
// throw std::runtime_error("Cannot attach to sync session! status: " + status_to_string(status));
sync_thread.reset( new std::thread( syncThreadRoutine, nullptr ) );
}
void stopSync()
{
// No sync session active
if(sync_session == nullptr)
return;
sync_queue.push_front(NULL);
sync_thread->join();
unregister_sync_callbacks(sync_session);
sync_queue.clear();
pEp::call_with_lock(&release, sync_session);
sync_session = nullptr;
}
void startKeyserverLookup()
{
if(keyserver_lookup_session)
throw std::runtime_error("KeyserverLookup already started.");
PEP_STATUS status = pEp::call_with_lock(&init, &keyserver_lookup_session, &JsonAdapter::messageToSend, &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));
}
keyserver_lookup_queue.clear();
status = register_examine_function(keyserver_lookup_session,
examineIdentity,
&keyserver_lookup_session // nullptr is not accepted, so any dummy ptr is used here
);
if (status != PEP_STATUS_OK)
throw std::runtime_error("Cannot register keyserver lookup callbacks! status: " + ::pEp::status_to_string(status));
keyserver_lookup_thread.reset( new std::thread( &keyserverLookupThreadRoutine, &keyserver_lookup_session /* just a dummy */ ) );
}
void stopKeyserverLookup()
{
// No keyserver lookup session active
if(keyserver_lookup_session == nullptr)
return;
keyserver_lookup_queue.push_front(NULL);
keyserver_lookup_thread->join();
// there is no unregister_examine_callback() function. hum...
keyserver_lookup_queue.clear();
pEp::call_with_lock(&release, keyserver_lookup_session);
keyserver_lookup_session = nullptr;
}
int examineIdentity(pEp_identity* idy, void* obj)
{
// JsonAdapter* ja = static_cast<JsonAdapter*>(obj);
return injectIdentity(idy);
}
void* keyserverLookupThreadRoutine(void* arg)
{
PEP_STATUS status = do_keymanagement(
&retrieveNextIdentity,
arg); // does the whole work
keyserver_lookup_queue.clear();
return (void*) status;
}
} // end of namespace pEp::mini
} // end of namespace pEp

@ -0,0 +1,34 @@
#ifndef MINI_ADAPTER_IMPL_HH
#define MINI_ADAPTER_IMPL_HH
#include <pEp/keymanagement.h>
#include <pEp/sync_api.h>
namespace pEp{
namespace mini {
int injectSyncMsg(Sync_event* msg, void* /*management*/ );
int injectIdentity(pEp_identity* idy);
Sync_event* retrieveNextSyncMsg(void* /*management*/, unsigned timeout);
pEp_identity* retrieveNextIdentity( void* /*management*/);
void* syncThreadRoutine(void* arg);
void startSync();
void stopSync();
void startKeyserverLookup();
void stopKeyserverLookup();
int examineIdentity(pEp_identity* idy, void* obj);
void* keyserverLookupThreadRoutine(void* arg);
} // end of namespace pEp::mini
} // end of namespace pEp
#endif // MINI_ADAPTER_IMPL_HH

@ -1,4 +1,6 @@
#include "main.hh"
#include "mini-adapter-impl.hh"
#include "ev_server.hh"
#include "prefix-config.hh"
#include "json-adapter.hh"
@ -48,6 +50,7 @@ void print_version()
std::ostream* my_logfile = nullptr;
std::shared_ptr<std::ostream> real_logfile;
int main(int argc, char** argv)
try
{
@ -121,7 +124,7 @@ try
// 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);
PEP_STATUS status = pEp::call_with_lock(&init, &first_session, &JsonAdapter::messageToSend, &pEp::mini::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) + ".";
@ -134,8 +137,7 @@ try
JsonAdapter ja;
ja.do_sync( do_sync)
.ignore_session_errors( ignore_missing_session)
ja.ignore_session_errors( ignore_missing_session)
.deliver_html( !no_html )
;
/*

Loading…
Cancel
Save