Change Request* to RequestPtr implemented as shared_ptr<Request>
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 20 Jun 2014 17:18:19 +0000 (19:18 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:09 +0000 (14:19 +0200)
Handle case when no response is generated during request execution.

Change-Id: I2ffbcd520dcb910bae7ee342ab432fae457ef437

12 files changed:
src/common/exceptions/NoResponseGeneratedException.h [new file with mode: 0644]
src/service/protocol/Protocol.h
src/service/protocol/ProtocolAdmin.cpp
src/service/protocol/ProtocolAdmin.h
src/service/protocol/ProtocolClient.cpp
src/service/protocol/ProtocolClient.h
src/service/protocol/ProtocolSignal.cpp
src/service/protocol/ProtocolSignal.h
src/service/request/Request.h
src/service/sockets/Descriptor.cpp
src/service/sockets/Descriptor.h
src/service/sockets/SocketManager.cpp

diff --git a/src/common/exceptions/NoResponseGeneratedException.h b/src/common/exceptions/NoResponseGeneratedException.h
new file mode 100644 (file)
index 0000000..9709f68
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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        NoResponseGeneratedException.h
+ * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version     1.0
+ * @brief       Implementation of NoResponseGeneratedException
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_
+
+#include "Exception.h"
+
+#include <exception>
+
+namespace Cynara {
+
+class NoResponseGeneratedException : public Exception {
+public:
+    NoResponseGeneratedException() = default;
+    virtual ~NoResponseGeneratedException() = default;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_NORESPONSEGENERATEDEXCEPTION_H_ */
index 9efae80..282450c 100644 (file)
@@ -37,7 +37,7 @@ public:
     Protocol();
     virtual ~Protocol();
 
-    virtual Request *extractRequestFromBuffer(BinaryQueue &bufferQueue) = 0;
+    virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue) = 0;
 
     virtual void appendResponseToBuffer(CheckResponse &&response);
 };
index c130d8c..bd29828 100644 (file)
@@ -31,8 +31,8 @@ ProtocolAdmin::ProtocolAdmin() {
 ProtocolAdmin::~ProtocolAdmin() {
 }
 
-Request *ProtocolAdmin::extractRequestFromBuffer(BinaryQueue& bufferQueue) {
+RequestPtr ProtocolAdmin::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
     TODO_USE_ME(bufferQueue);
-    return nullptr;
+    return RequestPtr(nullptr);
 }
 } // namespace Cynara
index 3ca0769..3fe459d 100644 (file)
@@ -32,7 +32,7 @@ public:
     ProtocolAdmin();
     virtual ~ProtocolAdmin();
 
-    virtual Request *extractRequestFromBuffer(BinaryQueue &bufferQueue);
+    virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
 };
 
 } // namespace Cynara
index 12dd34f..fb887dd 100644 (file)
@@ -31,9 +31,9 @@ ProtocolClient::ProtocolClient() {
 ProtocolClient::~ProtocolClient() {
 }
 
-Request *ProtocolClient::extractRequestFromBuffer(BinaryQueue& bufferQueue) {
+RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
     TODO_USE_ME(bufferQueue);
-    return nullptr;
+    return RequestPtr(nullptr);
 }
 
 } // namespace Cynara
index f4deae3..19c5d14 100644 (file)
@@ -32,7 +32,7 @@ public:
     ProtocolClient();
     virtual ~ProtocolClient();
 
-    virtual Request *extractRequestFromBuffer(BinaryQueue &bufferQueue);
+    virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
 };
 
 } // namespace Cynara
index e20d13f..676d1d1 100644 (file)
@@ -31,9 +31,9 @@ ProtocolSignal::ProtocolSignal() {
 ProtocolSignal::~ProtocolSignal() {
 }
 
-Request *ProtocolSignal::extractRequestFromBuffer(BinaryQueue& bufferQueue) {
+RequestPtr ProtocolSignal::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
     TODO_USE_ME(bufferQueue);
-    return nullptr;
+    return RequestPtr(nullptr);
 }
 
 } // namespace Cynara
index a540052..c74cf7d 100644 (file)
@@ -32,7 +32,7 @@ public:
     ProtocolSignal();
     virtual ~ProtocolSignal();
 
-    virtual Request *extractRequestFromBuffer(BinaryQueue &bufferQueue);
+    virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
 };
 
 } // namespace Cynara
index 14c7743..7ab5112 100644 (file)
@@ -23,6 +23,8 @@
 #ifndef SRC_SERVICE_REQUEST_REQUEST_H_
 #define SRC_SERVICE_REQUEST_REQUEST_H_
 
+#include <memory>
+
 #include <logic/Logic.h>
 
 namespace Cynara {
@@ -35,6 +37,8 @@ public:
     virtual void execute(Logic *logic, int sourceDesc) = 0;
 };
 
+typedef std::shared_ptr<Request> RequestPtr;
+
 } // namespace Cynara
 
 #endif /* SRC_SERVICE_REQUEST_REQUEST_H_ */
index 2e51ffe..661833d 100644 (file)
@@ -35,7 +35,7 @@ void Descriptor::pushReadBuffer(const RawBuffer &readbuffer) {
     m_readQueue.appendCopy(readbuffer.data(), readbuffer.size());
 }
 
-Request *Descriptor::extractRequest(void) {
+RequestPtr Descriptor::extractRequest(void) {
     return m_protocol->extractRequestFromBuffer(m_readQueue);
 }
 
index 3df54d6..6922562 100644 (file)
@@ -77,7 +77,7 @@ public:
     }
 
     void pushReadBuffer(const RawBuffer &readbuffer);
-    Request *extractRequest(void);
+    RequestPtr extractRequest(void);
 
     RawBuffer &prepareWriteBuffer(void);
 
index 1f955c8..7ce4f08 100644 (file)
@@ -36,6 +36,7 @@
 #include <common.h>
 #include <exceptions/DescriptorNotExistsException.h>
 #include <exceptions/InitException.h>
+#include <exceptions/NoResponseGeneratedException.h>
 #include <exceptions/UnexpectedErrorException.h>
 
 #include <logic/Logic.h>
@@ -215,17 +216,21 @@ bool SocketManager::handleRead(int fd, const RawBuffer &readbuffer) {
     desc.pushReadBuffer(readbuffer);
 
     try {
-        std::unique_ptr<Request> req(nullptr);
+        std::shared_ptr<Request> req(nullptr);
         for (;;) {
-            req.reset(desc.extractRequest());
-            if (req)
+            req = desc.extractRequest();
+            if (!req)
                 break;
 
             LOGD("request extracted");
-            req->execute(Cynara::getLogic(), fd);
+            try {
+                req->execute(Cynara::getLogic(), fd);
 
-            if (desc.hasDataToWrite())
+                if (desc.hasDataToWrite())
                     addWriteSocket(fd);
+            } catch (const NoResponseGeneratedException &ex) {
+                LOGD("no response generated");
+            }
         }
     } catch (const Exception &ex) {
         LOGE("Error handling request <%s>. Closing socket", ex.what());