From fabe257134bcd544deeb68de52ca6ed0eb0a0da4 Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Mon, 3 Nov 2014 11:21:25 +0100 Subject: [PATCH] Add manager for check contexts This class will be used by service logic to create and handle check contexts. Change-Id: I8c1f1265336dd8b5a428ed254083d1e508579a2e --- src/service/CMakeLists.txt | 1 + src/service/logic/Logic.h | 2 + src/service/request/CheckRequestManager.cpp | 91 +++++++++++++++++++++++++++++ src/service/request/CheckRequestManager.h | 61 +++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 src/service/request/CheckRequestManager.cpp create mode 100644 src/service/request/CheckRequestManager.h diff --git a/src/service/CMakeLists.txt b/src/service/CMakeLists.txt index 675a866..d4dd32e 100644 --- a/src/service/CMakeLists.txt +++ b/src/service/CMakeLists.txt @@ -24,6 +24,7 @@ SET(CYNARA_SOURCES ${CYNARA_SERVICE_PATH}/logic/Logic.cpp ${CYNARA_SERVICE_PATH}/main/Cynara.cpp ${CYNARA_SERVICE_PATH}/main/main.cpp + ${CYNARA_SERVICE_PATH}/request/CheckRequestManager.cpp ${CYNARA_SERVICE_PATH}/sockets/Descriptor.cpp ${CYNARA_SERVICE_PATH}/sockets/SocketManager.cpp ) diff --git a/src/service/logic/Logic.h b/src/service/logic/Logic.h index fdf338e..35226d7 100644 --- a/src/service/logic/Logic.h +++ b/src/service/logic/Logic.h @@ -28,6 +28,7 @@ #include
#include +#include #include #include @@ -74,6 +75,7 @@ public: private: AgentManagerPtr m_agentManager; + CheckRequestManager m_checkRequestManager; PluginManagerPtr m_pluginManager; StoragePtr m_storage; SocketManagerPtr m_socketManager; diff --git a/src/service/request/CheckRequestManager.cpp b/src/service/request/CheckRequestManager.cpp new file mode 100644 index 0000000..8090f47 --- /dev/null +++ b/src/service/request/CheckRequestManager.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/service/request/CheckRequestManager.cpp + * @author Adam Malinowski + * @version 1.0 + * @brief Definition of CheckRequestManager class + */ + +#include + +#include +#include +#include + +#include "CheckRequestManager.h" + +namespace Cynara { + +CheckContextPtr CheckRequestManager::createContext(const PolicyKey &key, + const RequestContextPtr &request, + ProtocolFrameSequenceNumber checkId, + const ServicePluginInterfacePtr &plugin, + const AgentTalkerPtr &agentTalkerPtr) { + + CheckContextPtr checkPtr = std::make_shared(key, request, checkId, plugin, + agentTalkerPtr); + if (m_checks[request->responseQueue()].insert(std::make_pair(checkId, checkPtr)).second) { + return checkPtr; + } + + return CheckContextPtr(); +} + +CheckContextPtr CheckRequestManager::getContext(const LinkId &linkId, + ProtocolFrameSequenceNumber checkId) { + const auto checkMap = m_checks.find(linkId); + if (checkMap == m_checks.end()) { + return CheckContextPtr(); + } + + const auto checkContext = checkMap->second.find(checkId); + return checkContext != checkMap->second.end() ? checkContext->second : CheckContextPtr(); +} + +CheckContextPtr CheckRequestManager::getContext(const AgentTalkerPtr &talker) { + for (const auto &checkMap : m_checks) { + for (const auto &check : checkMap.second) { + if (check.second->m_agentTalker == talker) { + return check.second; + } + } + } + return nullptr; +} + +void CheckRequestManager::removeRequest(const CheckContextPtr &checkContextPtr) { + m_checks[checkContextPtr->m_requestContext->responseQueue()].erase(checkContextPtr->m_checkId); + auto it = m_checks.find(checkContextPtr->m_requestContext->responseQueue()); + if (it->second.empty()) { + m_checks.erase(it); + } +} + +void CheckRequestManager::cancelRequests(const LinkId &linkId, CancelRequestFunction cancelFunction) +{ + auto checkMap = m_checks.find(linkId); + if (checkMap == m_checks.end()) + return; + + if (cancelFunction) { + for (auto p : checkMap->second) { + cancelFunction(p.second); + } + } +} + +} // namespace Cynara diff --git a/src/service/request/CheckRequestManager.h b/src/service/request/CheckRequestManager.h new file mode 100644 index 0000000..f71f8f1 --- /dev/null +++ b/src/service/request/CheckRequestManager.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/service/request/CheckRequestManager.h + * @author Adam Malinowski + * @version 1.0 + * @brief Declaration of CheckRequestManager class + */ + +#ifndef SRC_SERVICE_AGENT_CHECKREQUESTMANAGER_H_ +#define SRC_SERVICE_AGENT_CHECKREQUESTMANAGER_H_ + +#include +#include + +#include +#include +#include + +#include + +namespace Cynara { + +class CheckRequestManager { +public: + typedef std::map CheckContexts; + typedef std::function CancelRequestFunction; + + CheckRequestManager() {} + ~CheckRequestManager() {} + + CheckContextPtr createContext(const PolicyKey &key, const RequestContextPtr &request, + ProtocolFrameSequenceNumber checkId, + const ServicePluginInterfacePtr &plugin, + const AgentTalkerPtr &agentTalkerPtr); + void removeRequest(const CheckContextPtr &checkContextPtr); + + CheckContextPtr getContext(const LinkId &linkId, ProtocolFrameSequenceNumber checkId); + CheckContextPtr getContext(const AgentTalkerPtr &talker); + void cancelRequests(const LinkId &linkId, CancelRequestFunction cancelFunction); + +private: + std::map m_checks; +}; + +} // namespace Cynara + +#endif /* SRC_SERVICE_AGENT_CHECKREQUESTMANAGER_H_ */ -- 2.7.4