Handle client and agent disconnection 05/29805/12
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Mon, 3 Nov 2014 13:57:13 +0000 (14:57 +0100)
committerAdam Malinowski <a.malinowsk2@partner.samsung.com>
Sat, 15 Nov 2014 04:40:03 +0000 (05:40 +0100)
This patch handles situations where client and/or agent connection
to cynara service is closed.

Change-Id: I410b6da96102b6ae16442e90dbbb1e867490287a

src/service/logic/Logic.cpp
src/service/logic/Logic.h

index 4cf78eb..8f51af5 100644 (file)
@@ -63,6 +63,7 @@
 #include "Logic.h"
 
 namespace Cynara {
+
 Logic::Logic() {
 }
 
@@ -307,8 +308,17 @@ void Logic::execute(RequestContextPtr context, SetPoliciesRequestPtr request) {
                             request->sequenceNumber()));
 }
 
-void Logic::contextClosed(RequestContextPtr context UNUSED) {
-    //We don't care now, but we will
+void Logic::contextClosed(RequestContextPtr context) {
+    LOGD("context closed");
+
+    LinkId linkId = context->responseQueue();
+
+    m_agentManager->cleanupAgent(linkId, [&](const AgentTalkerPtr &talker) -> void {
+                                 handleAgentTalkerDisconnection(talker); });
+
+    m_checkRequestManager.cancelRequests(linkId,
+                                         [&](const CheckContextPtr &checkContextPtr) -> void {
+                                         handleClientDisconnection(checkContextPtr); });
 }
 
 void Logic::onPoliciesChanged(void) {
@@ -317,4 +327,29 @@ void Logic::onPoliciesChanged(void) {
     //todo remove all saved contexts (if there will be any saved contexts)
 }
 
+void Logic::handleAgentTalkerDisconnection(const AgentTalkerPtr &agentTalkerPtr) {
+    CheckContextPtr checkContextPtr = m_checkRequestManager.getContext(agentTalkerPtr);
+    if (checkContextPtr == nullptr) {
+        LOGE("No matching check context for agent talker.");
+        return;
+    }
+
+    if (!checkContextPtr->cancelled() && checkContextPtr->m_requestContext->responseQueue()) {
+        PolicyResult result(PredefinedPolicyType::DENY);
+        checkContextPtr->m_requestContext->returnResponse(checkContextPtr->m_requestContext,
+                std::make_shared<CheckResponse>(result, checkContextPtr->m_checkId));
+    }
+
+    m_checkRequestManager.removeRequest(checkContextPtr);
+}
+
+void Logic::handleClientDisconnection(const CheckContextPtr &checkContextPtr) {
+    LOGD("Handle client disconnection");
+
+    if (!checkContextPtr->cancelled()) {
+        checkContextPtr->cancel();
+        checkContextPtr->m_agentTalker->cancel();
+    }
+}
+
 } // namespace Cynara
index 57a862c..33c5701 100644 (file)
@@ -91,6 +91,9 @@ private:
                 const PluginData &agentData, const RequestContextPtr &request,
                 const ServicePluginInterfacePtr &plugin);
 
+    void handleAgentTalkerDisconnection(const AgentTalkerPtr &agentTalkerPtr);
+    void handleClientDisconnection(const CheckContextPtr &checkContextPtr);
+
     void onPoliciesChanged(void);
 };