Change request execution path
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Wed, 25 Jun 2014 19:18:06 +0000 (21:18 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:09 +0000 (14:19 +0200)
Add RequestTaker - interface for objects executing requests.
Derive Logic from RequestTaker.
Implement CheckRequest execution in Logic.
Swap const references to Request derivatives with shared pointers.
Add pointers.h file to gather all request related shared pointers.
Remove Request.cpp file (all methods inlined).
Fix SocketManager request execution.
Remove not any more needed NoResponseGeneratedException class.
Add temporary impl. of method requestTaker() to SocketManager.

Change-Id: I67526fb1f68cc90c5340572445ce63068bed03ea

12 files changed:
src/service/CMakeLists.txt
src/service/logic/Logic.cpp
src/service/logic/Logic.h
src/service/protocol/Protocol.h
src/service/request/CheckRequest.cpp
src/service/request/CheckRequest.h
src/service/request/Request.h
src/service/request/RequestTaker.cpp [moved from src/service/request/Request.cpp with 73% similarity]
src/service/request/RequestTaker.h [moved from src/common/exceptions/NoResponseGeneratedException.h with 60% similarity]
src/service/request/pointers.h [new file with mode: 0644]
src/service/sockets/SocketManager.cpp
src/service/sockets/SocketManager.h

index d7511d5..d1e9972 100644 (file)
@@ -27,7 +27,7 @@ SET(CYNARA_SOURCES
     ${CYNARA_SERVICE_PATH}/protocol/ProtocolSignal.cpp
     ${CYNARA_SERVICE_PATH}/protocol/ResponseTaker.cpp
     ${CYNARA_SERVICE_PATH}/request/CheckRequest.cpp
-    ${CYNARA_SERVICE_PATH}/request/Request.cpp
+    ${CYNARA_SERVICE_PATH}/request/RequestTaker.cpp
     ${CYNARA_SERVICE_PATH}/sockets/Descriptor.cpp
     ${CYNARA_SERVICE_PATH}/sockets/SocketManager.cpp
     ${CYNARA_SERVICE_PATH}/storage/InMemoryStorageBackend.cpp
index c230ab8..9aa2e30 100644 (file)
@@ -25,6 +25,8 @@
 #include <exceptions/PluginNotFoundException.h>
 
 #include <main/Cynara.h>
+#include <request/CheckRequest.h>
+#include <response/CheckResponse.h>
 #include <storage/Storage.h>
 
 #include "Logic.h"
@@ -36,24 +38,33 @@ Logic::Logic() {
 Logic::~Logic() {
 }
 
-PolicyResult Logic::check(const RequestContext &context UNUSED, const PolicyKey &key) {
-    PolicyResult result = Cynara::getStorage()->checkPolicy(key);
+void Logic::execute(const RequestContext &context, CheckRequestPtr request) {
+    PolicyResult result(PredefinedPolicyType::DENY);
+    if (check(context, request->key(), result)) {
+        context.returnResponse(CheckResponse(result));
+    }
+}
+
+bool Logic::check(const RequestContext &context UNUSED, const PolicyKey &key,
+                  PolicyResult& result) {
+    result = Cynara::getStorage()->checkPolicy(key);
 
     switch (result.policyType()) {
         case PredefinedPolicyType::ALLOW :
             LOGD("check of policy key <%s> returned ALLOW", key.toString().c_str());
-            return result;
+            return true;
         case PredefinedPolicyType::DENY :
             LOGD("check of policy key <%s> returned DENY", key.toString().c_str());
-            return result;
+            return true;
     }
     //todo pass question to proper plugin that:
-    //  1) might throw NoResponseGeneratedException when answer has to be waited for (UI)
-    //  2) might return PolicyResult
+    //  1) returns false when answer has to be waited for (UI)
+    //  2) returns true if Response is to be generated
     // In case 1) context should be saved in plugin in order to return answer when ready.
 
     //in case no proper plugin is found
     throw PluginNotFoundException(result);
+    return false;
 }
 
 } // namespace Cynara
index 3f8626f..362b47c 100644 (file)
 #include <types/PolicyKey.h>
 #include <types/PolicyResult.h>
 
+#include <request/pointers.h>
 #include <request/RequestContext.h>
+#include <request/RequestTaker.h>
 
 namespace Cynara {
 
-class Logic {
+class Logic : public RequestTaker {
 public:
     Logic();
-    ~Logic();
+    virtual ~Logic();
+
+    virtual void execute(const RequestContext &context, CheckRequestPtr request);
+
+private:
+    bool check(const RequestContext &context, const PolicyKey &key, PolicyResult& result);
 
-    PolicyResult check(const RequestContext &context, const PolicyKey &key);
 };
 
 } // namespace Cynara
index 21c6426..f296c3f 100644 (file)
@@ -28,8 +28,7 @@
 #include <common.h>
 
 #include <protocol/ResponseTaker.h>
-#include <request/Request.h>
-#include <response/CheckResponse.h>
+#include <request/pointers.h>
 
 namespace Cynara {
 
index a7edfef..8bcbf65 100644 (file)
  * @brief       This file implements check request class
  */
 
-#include <common.h>
-#include <types/PolicyResult.h>
-
-#include <logic/Logic.h>
-#include <main/Cynara.h>
-#include <request/RequestContext.h>
+#include <memory>
 
 #include "CheckRequest.h"
 
 namespace Cynara {
 
-CheckRequest::CheckRequest(const PolicyKey &key) : m_key(key) {
-}
-
-void CheckRequest::execute(const RequestContext &context) {
-    PolicyResult result = Cynara::getLogic()->check(context, m_key);
-    context.returnResponse(CheckResponse(result));
+void CheckRequest::execute(RequestPtr self, RequestTaker &taker,
+                           const RequestContext &context) const {
+    taker.execute(context, std::dynamic_pointer_cast<CheckRequest>(self));
 }
 
 } // namespace Cynara
index 4e1b62c..54a8acf 100644 (file)
 
 #include <types/PolicyKey.h>
 
+#include <request/pointers.h>
 #include <request/Request.h>
 #include <request/RequestContext.h>
+#include <request/RequestTaker.h>
 
 namespace Cynara {
 
@@ -35,10 +37,16 @@ private:
     PolicyKey m_key;
 
 public:
-    CheckRequest(const PolicyKey &key);
+    CheckRequest(const PolicyKey &key) : m_key(key) {
+    }
+
     virtual ~CheckRequest() = default;
 
-    virtual void execute(const RequestContext &context);
+    const PolicyKey &key(void) const {
+        return m_key;
+    }
+
+    virtual void execute(RequestPtr self, RequestTaker &taker, const RequestContext &context) const;
 };
 
 } // namespace Cynara
index 9671510..8df08b9 100644 (file)
 #ifndef SRC_SERVICE_REQUEST_REQUEST_H_
 #define SRC_SERVICE_REQUEST_REQUEST_H_
 
-#include <memory>
-
-#include "RequestContext.h"
+#include <request/pointers.h>
+#include <request/RequestContext.h>
+#include <request/RequestTaker.h>
 
 namespace Cynara {
 
 class Request {
 public:
-    Request();
-    virtual ~Request();
+    Request() = default;
+    virtual ~Request() = default;
 
-    virtual void execute(const RequestContext &context) = 0;
+    virtual void execute(RequestPtr self, RequestTaker &taker,
+                         const RequestContext &context) const = 0;
 };
 
-typedef std::shared_ptr<Request> RequestPtr;
-
 } // namespace Cynara
 
 #endif /* SRC_SERVICE_REQUEST_REQUEST_H_ */
similarity index 73%
rename from src/service/request/Request.cpp
rename to src/service/request/RequestTaker.cpp
index 152447f..e56f4aa 100644 (file)
  *    limitations under the License.
  */
 /*
- * @file        Request.cpp
+ * @file        RequestTaker.cpp
  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  * @version     1.0
- * @brief       This file implements request base class
+ * @brief       This file implements RequestTaker class
  */
 
-#include "Request.h"
+#include <exceptions/NotImplementedException.h>
 
-namespace Cynara {
+#include "RequestTaker.h"
 
-Request::Request() {
-}
+namespace Cynara {
 
-Request::~Request() {
+void RequestTaker::execute(const RequestContext &context UNUSED, CheckRequestPtr request UNUSED) {
+    throw NotImplementedException();
 }
 
 } // namespace Cynara
similarity index 60%
rename from src/common/exceptions/NoResponseGeneratedException.h
rename to src/service/request/RequestTaker.h
index 9709f68..5564ece 100644 (file)
  *    limitations under the License.
  */
 /*
- * @file        NoResponseGeneratedException.h
+ * @file        RequestTaker.h
  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  * @version     1.0
- * @brief       Implementation of NoResponseGeneratedException
+ * @brief       This file defines RequestTaker class
  */
 
-#ifndef SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_
-#define SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_
+#ifndef SRC_SERVICE_REQUEST_REQUESTTAKER_H_
+#define SRC_SERVICE_REQUEST_REQUESTTAKER_H_
 
-#include "Exception.h"
-
-#include <exception>
+#include <request/pointers.h>
+#include <request/RequestContext.h>
 
 namespace Cynara {
 
-class NoResponseGeneratedException : public Exception {
+class RequestTaker {
 public:
-    NoResponseGeneratedException() = default;
-    virtual ~NoResponseGeneratedException() = default;
+    RequestTaker() = default;
+    virtual ~RequestTaker() = default;
+
+    virtual void execute(const RequestContext &context, CheckRequestPtr request);
 };
 
-} /* namespace Cynara */
+} // namespace Cynara
 
-#endif /* SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_ */
+#endif /* SRC_SERVICE_REQUEST_REQUESTTAKER_H_ */
diff --git a/src/service/request/pointers.h b/src/service/request/pointers.h
new file mode 100644 (file)
index 0000000..79f1539
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * 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        pointers.h
+ * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       This file defines request base class
+ */
+
+#ifndef SRC_SERVICE_REQUEST_POINTERS_H_
+#define SRC_SERVICE_REQUEST_POINTERS_H_
+
+#include <memory>
+
+namespace Cynara {
+
+class Request;
+typedef std::shared_ptr<Request> RequestPtr;
+
+class CheckRequest;
+typedef std::shared_ptr<CheckRequest> CheckRequestPtr;
+
+} // namespace Cynara
+
+#endif /* SRC_SERVICE_REQUEST_POINTERS_H_ */
index a8078fc..aa7cb51 100644 (file)
@@ -36,7 +36,6 @@
 #include <common.h>
 #include <exceptions/DescriptorNotExistsException.h>
 #include <exceptions/InitException.h>
-#include <exceptions/NoResponseGeneratedException.h>
 #include <exceptions/UnexpectedErrorException.h>
 
 #include <logic/Logic.h>
@@ -44,6 +43,8 @@
 #include <protocol/ProtocolAdmin.h>
 #include <protocol/ProtocolClient.h>
 #include <protocol/ProtocolSignal.h>
+#include <request/pointers.h>
+#include <request/RequestContext.h>
 #include <stdexcept>
 
 #include "SocketManager.h"
@@ -219,16 +220,16 @@ bool SocketManager::handleRead(int fd, const RawBuffer &readbuffer) {
 
     try {
         while(true) {
+            //try extract request from binary data received on socket
             auto req = desc.extractRequest();
-            if (!req)
+            if (!req)   // not enough data to build request yet
                 break;
-
             LOGD("request extracted");
-            try {
-                req->execute(RequestContext(fd, desc.responseTaker(), desc.writeQueue()));
-            } catch (const NoResponseGeneratedException &ex) {
-                LOGD("no response generated");
-            }
+
+            //build context
+            RequestContext context(fd, desc.responseTaker(), desc.writeQueue());
+            //pass request to request taker
+            req->execute(req, requestTaker(), context);
         }
     } catch (const Exception &ex) {
         LOGE("Error handling request <%s>. Closing socket", ex.what());
@@ -352,4 +353,9 @@ void SocketManager::removeWriteSocket(int fd) {
     FD_CLR(fd, &m_writeSet);
 }
 
+RequestTaker &SocketManager::requestTaker(void) {
+    //todo change
+    return *Cynara::getLogic();
+}
+
 } // namespace Cynara
index 9d6522e..03e7ca9 100644 (file)
@@ -30,7 +30,7 @@
 #include <common.h>
 
 #include <protocol/Protocol.h>
-#include <request/RequestContext.h>
+#include <request/RequestTaker.h>
 #include "Descriptor.h"
 
 namespace Cynara {
@@ -74,6 +74,8 @@ private:
     void removeReadSocket(int fd);
     void addWriteSocket(int fd);
     void removeWriteSocket(int fd);
+
+    RequestTaker &requestTaker(void);
 };
 
 } // namespace Cynara