From 95108c995a7705cbb89769cd6f0beb6fa8545741 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Mon, 3 Nov 2014 14:45:03 +0100 Subject: [PATCH] Handle check request with agent usage This patch introduces sending request to agent and storing context for future. Change-Id: I8187b4c5e66daa155b485b5ff6b9710de27f6345 --- src/service/agent/AgentManager.cpp | 2 +- src/service/logic/Logic.cpp | 50 ++++++++++++++++++++++++++++++++------ src/service/logic/Logic.h | 6 +++-- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/src/service/agent/AgentManager.cpp b/src/service/agent/AgentManager.cpp index ee91e64..d551790 100644 --- a/src/service/agent/AgentManager.cpp +++ b/src/service/agent/AgentManager.cpp @@ -77,7 +77,7 @@ AgentTalkerPtr AgentManager::createTalker(const AgentType &agentType) { return talker; } } catch (const std::out_of_range &) { - LOGE("Proper agent not found: <%s>", agentType.c_str()); + LOGE("Required agent is not registered: <%s>", agentType.c_str()); } return AgentTalkerPtr(); diff --git a/src/service/logic/Logic.cpp b/src/service/logic/Logic.cpp index 7227ef9..dfb968a 100644 --- a/src/service/logic/Logic.cpp +++ b/src/service/logic/Logic.cpp @@ -21,8 +21,11 @@ */ #include +#include +#include #include +#include #include #include #include @@ -95,14 +98,20 @@ void Logic::execute(RequestContextPtr context, CancelRequestPtr request) { void Logic::execute(RequestContextPtr context, CheckRequestPtr request) { PolicyResult result(PredefinedPolicyType::DENY); - if (check(context, request->key(), result)) { + if (check(context, request->key(), request->sequenceNumber(), result)) { context->returnResponse(context, std::make_shared(result, request->sequenceNumber())); } } -bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key, - PolicyResult& result) { +bool Logic::check(const RequestContextPtr &context, const PolicyKey &key, + ProtocolFrameSequenceNumber checkId, PolicyResult &result) { + + if (m_checkRequestManager.getContext(context->responseQueue(), checkId)) { + LOGE("Check request for checkId: [%" PRIu16 "] is already processing", checkId); + return false; + } + result = m_storage->checkPolicy(key); switch (result.policyType()) { @@ -114,9 +123,19 @@ bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key, return true; } + return pluginCheck(context, key, checkId, result); +} + +bool Logic::pluginCheck(const RequestContextPtr &context, const PolicyKey &key, + ProtocolFrameSequenceNumber checkId, PolicyResult &result) { + + LOGD("Trying to check policy: <%s> in plugin.", key.toString().c_str()); + ExternalPluginPtr plugin = m_pluginManager->getPlugin(result.policyType()); if (!plugin) { - throw PluginNotFoundException(result); + LOGE("Plugin not found for policy: [0x%x]", result.policyType()); + result = PolicyResult(PredefinedPolicyType::DENY); + return true; } ServicePluginInterfacePtr servicePlugin = @@ -134,12 +153,27 @@ bool Logic::check(RequestContextPtr context UNUSED, const PolicyKey &key, switch (ret) { case ServicePluginInterface::PluginStatus::ANSWER_READY: return true; - case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY: - //todo send request to agent - //context should be saved in plugin in order to return answer when ready + case ServicePluginInterface::PluginStatus::ANSWER_NOTREADY: { + result = PolicyResult(PredefinedPolicyType::DENY); + AgentTalkerPtr agentTalker = m_agentManager->createTalker(requiredAgent); + if (!agentTalker) { + LOGE("Required agent talker for: <%s> could not be created.", + requiredAgent.c_str()); + return true; + } + + if (!m_checkRequestManager.createContext(key, context, checkId, servicePlugin, + agentTalker)) { + LOGE("Check context for checkId: [%" PRIu16 "] could not be created.", + checkId); + m_agentManager->removeTalker(agentTalker); + return true; + } + agentTalker->send(pluginData); + } return false; default: - throw PluginErrorException(key); + throw PluginErrorException(key); // This 'throw' should be removed or handled properly. } } diff --git a/src/service/logic/Logic.h b/src/service/logic/Logic.h index 35226d7..a90c08c 100644 --- a/src/service/logic/Logic.h +++ b/src/service/logic/Logic.h @@ -80,8 +80,10 @@ private: StoragePtr m_storage; SocketManagerPtr m_socketManager; - bool check(RequestContextPtr context, const PolicyKey &key, PolicyResult& result); - + bool check(const RequestContextPtr &context, const PolicyKey &key, + ProtocolFrameSequenceNumber checkId, PolicyResult &result); + bool pluginCheck(const RequestContextPtr &context, const PolicyKey &key, + ProtocolFrameSequenceNumber checkId, PolicyResult &result); void onPoliciesChanged(void); }; -- 2.7.4