Browse Source

merge JSON-155 into default branch. :-)

JSON-153_experimental
Roker 2 years ago
parent
commit
ac77e62594
11 changed files with 116 additions and 13 deletions
  1. +1
    -1
      build-windows/pEpJSONServerAdapter/pEpJSONServerAdapter.vcxproj
  2. +1
    -1
      build-windows/pEpJSONServerAdapter/pEpJSONServerAdapterLibrary.vcxproj
  3. +4
    -0
      server/context.hh
  4. +2
    -2
      server/ev_server.cc
  5. +11
    -0
      server/function_map.cc
  6. +42
    -9
      server/function_map.hh
  7. +6
    -0
      server/json-adapter.cc
  8. +2
    -0
      server/json-adapter.hh
  9. +31
    -0
      server/session_registry.cc
  10. +11
    -0
      server/session_registry.hh
  11. +5
    -0
      server/unittest_rpc.cc

+ 1
- 1
build-windows/pEpJSONServerAdapter/pEpJSONServerAdapter.vcxproj View File

@ -55,7 +55,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;_DEBUG;_LIB;DEBUG_ENABLED;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir);$(SolutionDir)pEpJSONServerAdapter\build-windows;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<UndefinePreprocessorDefinitions>%(UndefinePreprocessorDefinitions)</UndefinePreprocessorDefinitions>


+ 1
- 1
build-windows/pEpJSONServerAdapter/pEpJSONServerAdapterLibrary.vcxproj View File

@ -55,7 +55,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;_DEBUG;_LIB;DEBUG_ENABLED;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(SolutionDir);$(SolutionDir)pEpJSONServerAdapter\build-windows\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<UndefinePreprocessorDefinitions>%(UndefinePreprocessorDefinitions)</UndefinePreprocessorDefinitions>


+ 4
- 0
server/context.hh View File

@ -4,6 +4,7 @@
#include <string>
#include "json_spirit/json_spirit_value.h"
#include <map>
#include <pEp/pEpEngine.h>
class Context
{
@ -13,6 +14,9 @@ public:
virtual bool verify_security_token(const std::string& token) const = 0;
virtual void augment(json_spirit::Object& returnObject) = 0;
// Cache a certain function call. See JSON-155.
virtual void cache(const std::string& func_name, const std::function<void(PEP_SESSION)>& fn) = 0;
// store and retrieve other parameters into the context.
// that allows semantic actions based on other function parameters
// KISS: at the moment only "size_t" objects are supported.


+ 2
- 2
server/ev_server.cc View File

@ -87,8 +87,8 @@ const FunctionMap functions = {
// FP( "get_phrase" , new Func<PEP_STATUS, In_Pep_Session, In<Language>, In<int>, Out<char*>> ( &get_phrase) ),
// FP( "get_engine_version", new Func<const char*> ( &get_engine_version) ),
FP( "is_pEp_user" , new Func<PEP_STATUS, In_Pep_Session, In<pEp_identity*>, Out<bool>>( &is_pEp_user) ),
FP( "config_passive_mode", new Func<void, In_Pep_Session, In<bool>>( &config_passive_mode) ),
FP( "config_unencrypted_subject", new Func<void, In_Pep_Session, In<bool>>( &config_unencrypted_subject) ),
FP( "config_passive_mode", new FuncCache<void, In_Pep_Session, In<bool>>( "conf_p_m", &config_passive_mode) ),
FP( "config_unencrypted_subject", new FuncCache<void, In_Pep_Session, In<bool>>( "conf_u_s", &config_unencrypted_subject) ),
FP( "Identity Management API", new Separator),
FP( "get_identity" , new Func<PEP_STATUS, In_Pep_Session, In<c_string>, In<c_string>, Out<pEp_identity*>>( &get_identity) ),


+ 11
- 0
server/function_map.cc View File

@ -23,6 +23,17 @@ FunctionMap::FunctionMap(std::initializer_list<FunctionMap::value_type> il)
}
}
FunctionMap::~FunctionMap()
{
for(auto& elem : v)
{
delete elem.second;
elem.second = nullptr;
}
}
template<>
js::Value Type2String<std::string>::get() { return "String"; }


+ 42
- 9
server/function_map.hh View File

@ -7,6 +7,7 @@
#include "json_spirit/json_spirit_writer.h"
#include "context.hh"
#include "logger.hh"
#include <type_traits>
// Just for debugging:
@ -147,23 +148,20 @@ public:
typedef typename Return<R>::return_type ReturnType;
virtual ~Func() = default;
virtual bool isSeparator() const noexcept override
{
return false;
}
virtual bool isSeparator() const noexcept override { return false; }
Func() : fn() {}
Func( const std::function<ReturnType(typename Args::c_type ...)>& _f )
explicit Func( const std::function<ReturnType(typename Args::c_type ...)>& _f )
: fn(_f)
{}
Func(const Func<R, Args...>&) = delete;
void operator=(const Func<R, Args...>&) = delete;
std::function<ReturnType(typename Args::c_type ...)> fn;
js::Value call(const js::Array& parameters, Context* context) const override
{
typedef helper<R, 0, sizeof...(Args), Args...> Helper;
if(parameters.size() != Helper::nr_of_input_params)
throw std::runtime_error("Size mismatch: "
"Array has " + std::to_string( parameters.size() ) + " element(s), "
@ -211,6 +209,40 @@ public:
};
template<class R, class... Args>
class FuncCache : public Func<R, Args...>
{
public:
typedef Func<R, Args...> Base;
typedef typename Return<R>::return_type ReturnType;
typedef helper<R, 0, sizeof...(Args), Args...> Helper;
FuncCache(const std::string& _func_name, const std::function<ReturnType(typename Args::c_type ...)>& _f )
: Base(_f)
, func_name(_func_name)
{}
js::Value call(const js::Array& parameters, Context* context) const override
{
Logger Log("FuncCache::call");
typedef std::tuple<typename Args::c_type...> param_tuple_t;
param_tuple_t param_tuple;
// FIXME: Does only work with functions with type: void(PEP_SESSION, T):
const auto p0 = from_json< typename std::tuple_element<1, param_tuple_t>::type >(parameters[0]);
Log << Logger::Debug << "func_name=\"" << func_name << "\", value=" << p0 << ".";
std::function<void(PEP_SESSION)> func = std::bind(Base::fn, std::placeholders::_1, p0);
context->cache(func_name, func);
return Base::call(parameters, context);
}
private:
const std::string func_name;
};
// Just a separating placeholder in the drop-down list. Does not calls anything.
class Separator : public FuncBase
{
@ -237,6 +269,7 @@ public:
const_iterator find(const std::string&) const noexcept;
FunctionMap(std::initializer_list<value_type> il);
~FunctionMap();
private:
FunctionMapBase v;


+ 6
- 0
server/json-adapter.cc View File

@ -347,6 +347,12 @@ void JsonAdapter::augment(json_spirit::Object& returnObject)
}
void JsonAdapter::cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func)
{
i->session_registry->add_to_cache(fn_name, func);
}
js::Array JsonAdapter::pollForEvents(unsigned timeout_seconds)
{
return i->pollForEvents( std::this_thread::get_id(), timeout_seconds);


+ 2
- 0
server/json-adapter.hh View File

@ -74,6 +74,8 @@ public:
virtual void augment(json_spirit::Object& returnObject) override;
virtual void cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func) override;
// returns the version of the JsonAdapter
static
ServerVersion version();


+ 31
- 0
server/session_registry.cc View File

@ -12,6 +12,7 @@ PEP_SESSION SessionRegistry::get(std::thread::id tid)
auto q = m.find(tid);
if(q != m.end())
{
Log.debug("get() returns %p.", (const void*)q->second);
return q->second;
}
@ -22,6 +23,14 @@ PEP_SESSION SessionRegistry::get(std::thread::id tid)
throw std::runtime_error("init() fails: " + pEp::status_to_string(status) );
}
m[tid] = session;
Log.debug("Apply %zu cached config values to new session.", cache.size());
for(const auto& e : cache)
{
Log.debug("\t %s", e.first.c_str());
e.second(session);
}
Log.debug("get() created new session at %p.", (const void*)session);
return session;
}
@ -32,12 +41,34 @@ void SessionRegistry::remove(std::thread::id tid)
const auto q = m.find(tid);
if(q != m.end())
{
Log.debug("remove() session at %p.", (const void*)q->second);
pEp::call_with_lock(&release, q->second);
m.erase(q);
}else{
Log.info("remove(): no session for this thread!");
}
}
void SessionRegistry::for_each(void(*function)(PEP_SESSION))
{
Lock L(_mtx);
Log.debug("for_each() on %zu session.", m.size());
for(const auto& e : m)
{
function(e.second);
}
}
void SessionRegistry::add_to_cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func)
{
Lock L(_mtx);
Log.debug("add_to_cache(\"%s\")", fn_name.c_str());
cache[fn_name] = func;
}
std::string SessionRegistry::to_string() const
{
Lock L(_mtx);


+ 11
- 0
server/session_registry.hh View File

@ -4,7 +4,9 @@
#include <map>
#include <mutex>
#include <thread>
#include <functional>
#include <pEp/pEpEngine.h>
#include "logger.hh"
class SessionRegistry
{
@ -12,6 +14,7 @@ public:
SessionRegistry(messageToSend_t _mts, inject_sync_event_t _ise)
: mts{_mts}
, ise{_ise}
, Log{"SR"}
{}
// calls "init" for the given thread if no PEP_SESSION exists, yet for the given thread
@ -21,12 +24,20 @@ public:
std::size_t size() const { return m.size(); }
bool empty() const { return m.empty(); }
// calls the given function (which might be a lambda or std::function<> or std::bind thingy)
// on each stored session.
void for_each(void(*function)(PEP_SESSION));
void add_to_cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func);
std::string to_string() const;
private:
std::map<std::thread::id, PEP_SESSION> m;
messageToSend_t mts;
inject_sync_event_t ise;
Logger Log;
std::map<std::string, std::function<void(PEP_SESSION)>> cache;
typedef std::recursive_mutex Mutex;
typedef std::unique_lock<Mutex> Lock;


+ 5
- 0
server/unittest_rpc.cc View File

@ -29,6 +29,11 @@ class DummyContext : public Context
public:
virtual bool verify_security_token(const std::string& token) const override { return true; }
virtual void augment(js::Object&) override { /* do nothing */ }
virtual void cache(const std::string& func_name, const std::function<void(PEP_SESSION)>& fn) override
{
// do nothing
}
};


Loading…
Cancel
Save