spread the client_id to all relevant code places... *sigh* the whole design looks quite ugly now to me. :-/

JSON-156
Roker 3 years ago
parent 3f9a4c93b0
commit 001655c2e1

@ -22,7 +22,7 @@ bool Context::verify_security_token(const std::string& token) const
// Cache a certain function call. See JSON-155.
void Context::cache(const std::string& func_name, const std::function<void(PEP_SESSION)>& fn)
{
ja->cache(func_name, fn);
ja->cache(client_id(), func_name, fn);
}

@ -10,11 +10,15 @@ class JsonAdapterBase;
class Context
{
public:
Context(JsonAdapterBase* _ja) : ja{_ja} {}
Context(JsonAdapterBase* _ja, const std::string& _cid)
: ja{_ja}, cid{_cid}
{}
Context(const Context&) = delete;
void operator=(const Context&) = delete;
const std::string& client_id() const { return cid; }
// delegate call to the 'ja' member
bool verify_security_token(const std::string& token) const ;
@ -27,11 +31,11 @@ public:
// KISS: at the moment only "size_t" objects are supported.
void store(int position, size_t value);
size_t retrieve(int position);
void clear();
private:
std::map<int, size_t> obj_store;
JsonAdapterBase* ja;
const std::string& cid;
};
#endif // JSON_ADAPTER_CONTEXT_HH

@ -150,15 +150,15 @@ struct JsonAdapter::Internal
};
PEP_SESSION JsonAdapter::getSessionForThread()
PEP_SESSION JsonAdapter::getSessionForThread(const std::string& client_id)
{
const auto id = std::this_thread::get_id();
return JsonAdapter::getInstance().i->session_registry->get(id);
const auto thread_id = std::this_thread::get_id();
return JsonAdapter::getInstance().i->session_registry->get(thread_id, client_id);
}
In_Pep_Session::In_Pep_Session(const js::Value& v, Context*, unsigned)
: Base( JsonAdapter::getSessionForThread() )
In_Pep_Session::In_Pep_Session(const js::Value& v, Context* ctx, unsigned)
: Base( JsonAdapter::getSessionForThread(ctx->client_id()) )
{}
@ -343,9 +343,9 @@ bool JsonAdapter::verify_security_token(const std::string& s) const
}
void JsonAdapter::cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func)
void JsonAdapter::cache(const std::string& client_id, const std::string& fn_name, const std::function<void(PEP_SESSION)>& func)
{
i->session_registry->add_to_cache(fn_name, func);
i->session_registry->add_to_cache(client_id, fn_name, func);
}

@ -21,7 +21,7 @@ public:
virtual bool verify_security_token(const std::string& s) const = 0;
// Cache a certain function call. See JSON-155.
virtual void cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func) = 0;
virtual void cache(const std::string& client_id, const std::string& fn_name, const std::function<void(PEP_SESSION)>& func) = 0;
};
@ -87,14 +87,14 @@ public:
// returns 'true' if 's' is the security token created by the function above.
virtual bool verify_security_token(const std::string& s) const override;
virtual void cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func) override;
virtual void cache(const std::string& client_id, const std::string& fn_name, const std::function<void(PEP_SESSION)>& func) override;
// returns the version of the JsonAdapter
static
ServerVersion version();
// returns the PEP_SESSION registered for the current thread
static PEP_SESSION getSessionForThread();
static PEP_SESSION getSessionForThread(const std::string& client_id);
static PEP_STATUS messageToSend(message* msg);
static PEP_STATUS notifyHandshake(pEp_identity* self, pEp_identity* partner, sync_handshake_signal signal);

@ -131,7 +131,7 @@ js::Object call(const FunctionMap& fm, const js::Object& request, JsonAdapterBas
DEBUG_OUT(L, "method_name=\"" + method_name + "\"\n"
"params=" + js::write(params) );
Context context{ja};
Context context{ja, client_id_s};
const js::Value result = fn->second->call(p, &context);
DEBUG_OUT(L, "result=" + js::write(result, js::raw_utf8) );

@ -6,7 +6,7 @@
// creates a PEP_SESSION if none yet exists for the given thread
PEP_SESSION SessionRegistry::get(std::thread::id tid)
PEP_SESSION SessionRegistry::get(std::thread::id tid, const std::string& client_id)
{
Lock L(_mtx);
@ -24,8 +24,10 @@ 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)
const auto& cache_for_client = cache[client_id];
Log.debug("Apply %zu cached config values for client_id \"%s\" to new session.", cache_for_client.size(), client_id.c_str());
for(const auto& e : cache_for_client)
{
Log.debug("\t %s", e.first.c_str());
e.second(session);
@ -62,11 +64,11 @@ void SessionRegistry::for_each(void(*function)(PEP_SESSION))
}
void SessionRegistry::add_to_cache(const std::string& fn_name, const std::function<void(PEP_SESSION)>& func)
void SessionRegistry::add_to_cache(const std::string& client_id, 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;
Log.debug("add_to_cache(\"%s\", \"%s\")", client_id.c_str(), fn_name.c_str());
cache[client_id][fn_name] = func;
}

@ -18,7 +18,7 @@ public:
{}
// calls "init" for the given thread if no PEP_SESSION exists, yet for the given thread
PEP_SESSION get(std::thread::id tid = std::this_thread::get_id());
PEP_SESSION get(std::thread::id tid, const std::string& client_id);
void remove(std::thread::id tid = std::this_thread::get_id());
std::size_t size() const { return m.size(); }
@ -28,7 +28,7 @@ public:
// 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);
void add_to_cache(const std::string& client_id, const std::string& fn_name, const std::function<void(PEP_SESSION)>& func);
std::string to_string() const;
@ -37,7 +37,11 @@ private:
messageToSend_t mts;
inject_sync_event_t ise;
Logger Log;
std::map<std::string, std::function<void(PEP_SESSION)>> cache;
typedef
std::map<std::string, std::function<void(PEP_SESSION)>> cache_per_client_t;
std::map<std::string, cache_per_client_t> cache;
typedef std::recursive_mutex Mutex;
typedef std::unique_lock<Mutex> Lock;

@ -30,7 +30,7 @@ class DummyAdapter : public JsonAdapterBase
public:
virtual bool verify_security_token(const std::string& token) const override { return true; }
virtual void cache(const std::string& func_name, const std::function<void(PEP_SESSION)>& fn) override
virtual void cache(const std::string& client_id, const std::string& func_name, const std::function<void(PEP_SESSION)>& fn) override
{
// do nothing
}

Loading…
Cancel
Save