Add manager for check contexts 01/29801/10
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Mon, 3 Nov 2014 10:21:25 +0000 (11:21 +0100)
committerAdam Malinowski <a.malinowsk2@partner.samsung.com>
Sat, 15 Nov 2014 04:35:15 +0000 (05:35 +0100)
This class will be used by service logic to create and handle check
contexts.

Change-Id: I8c1f1265336dd8b5a428ed254083d1e508579a2e

src/service/CMakeLists.txt
src/service/logic/Logic.h
src/service/request/CheckRequestManager.cpp [new file with mode: 0644]
src/service/request/CheckRequestManager.h [new file with mode: 0644]

index 675a866..d4dd32e 100644 (file)
@@ -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
     )
index fdf338e..35226d7 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <main/pointers.h>
 #include <plugin/PluginManager.h>
+#include <request/CheckRequestManager.h>
 #include <request/pointers.h>
 #include <request/RequestTaker.h>
 
@@ -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 (file)
index 0000000..8090f47
--- /dev/null
@@ -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 <a.malinowsk2@partner.samsung.com>
+ * @version     1.0
+ * @brief       Definition of CheckRequestManager class
+ */
+
+#include <cstdint>
+
+#include <exceptions/UnexpectedErrorException.h>
+#include <log/log.h>
+#include <request/RequestContext.h>
+
+#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<CheckContext>(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 (file)
index 0000000..f71f8f1
--- /dev/null
@@ -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 <a.malinowsk2@partner.samsung.com>
+ * @version     1.0
+ * @brief       Declaration of CheckRequestManager class
+ */
+
+#ifndef SRC_SERVICE_AGENT_CHECKREQUESTMANAGER_H_
+#define SRC_SERVICE_AGENT_CHECKREQUESTMANAGER_H_
+
+#include <functional>
+#include <map>
+
+#include <containers/BinaryQueue.h>
+#include <request/CheckContext.h>
+#include <types/ProtocolFields.h>
+
+#include <cynara-plugin.h>
+
+namespace Cynara {
+
+class CheckRequestManager {
+public:
+    typedef std::map<ProtocolFrameSequenceNumber, CheckContextPtr> CheckContexts;
+    typedef std::function<void(const CheckContextPtr &checkContextPtr)> 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<LinkId, CheckContexts> m_checks;
+};
+
+} // namespace Cynara
+
+#endif /* SRC_SERVICE_AGENT_CHECKREQUESTMANAGER_H_ */