Clone protocol objects
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Thu, 26 Jun 2014 20:07:09 +0000 (22:07 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:10 +0000 (14:19 +0200)
Add clone method to Protocol class and derivatives.
Use cloned Protocol object for every new socket (created after
accept on listening socket).

Protocol derived classes handle serialization and deserialization
of Requests and Responses. Protocol objects are kept in Descriptor
objects for every open socket in form of shared pointers.  All
binary communication through a socket is translated by related
Protocol.
On the server side of Cynara Protocols are used as ResponseTakers
(they consume Responses ready to send back to clients). Pointers
to Protocols as ResponseTakers are kept in RequestContext as weak
pointers. RequestContext may be (will be in the future) saved
by Plugin in order to run some UI and return Response later.
Meanwhile a socket can be closed. In such a case weak pointer shall
lose his reference to Protocol object, which was destroyed in
Descriptor::close() method. Before returning Response to
ResponseTaker, validation of weak pointer is made in
RequestContext::cynaraResponse().

Change-Id: I5a6af197357c8ff641aa1162fd30c01bbe725786

src/common/protocol/Protocol.h
src/common/protocol/ProtocolAdmin.cpp
src/common/protocol/ProtocolAdmin.h
src/common/protocol/ProtocolClient.cpp
src/common/protocol/ProtocolClient.h
src/common/protocol/ProtocolSignal.cpp
src/common/protocol/ProtocolSignal.h
src/service/sockets/SocketManager.cpp

index b0f5c53..56ee51e 100644 (file)
 
 namespace Cynara {
 
+class Protocol;
+typedef std::shared_ptr<Protocol> ProtocolPtr;
+
 class Protocol : public RequestTaker, public ResponseTaker {
 public:
     Protocol() = default;
     virtual ~Protocol() = default;
 
+    virtual ProtocolPtr clone(void) = 0;
+
     virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue) = 0;
     virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue) = 0;
 };
 
-typedef std::shared_ptr<Protocol> ProtocolPtr;
-
 } // namespace Cynara
 
 #endif /* SRC_COMMON_PROTOCOL_PROTOCOL_H_ */
index 0a949f4..d40dcf6 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <common.h>
+#include <memory>
 #include "ProtocolAdmin.h"
 
 namespace Cynara {
@@ -31,6 +32,10 @@ ProtocolAdmin::ProtocolAdmin() {
 ProtocolAdmin::~ProtocolAdmin() {
 }
 
+ProtocolPtr ProtocolAdmin::clone(void) {
+    return std::make_shared<ProtocolAdmin>();
+}
+
 RequestPtr ProtocolAdmin::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
     TODO_USE_ME(bufferQueue);
     return RequestPtr(nullptr);
index eb5d644..ccd2843 100644 (file)
@@ -32,6 +32,8 @@ public:
     ProtocolAdmin();
     virtual ~ProtocolAdmin();
 
+    virtual ProtocolPtr clone(void);
+
     virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
     virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue);
 };
index 4528679..4e98d53 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <common.h>
+#include <memory>
 #include "ProtocolClient.h"
 
 namespace Cynara {
@@ -31,6 +32,10 @@ ProtocolClient::ProtocolClient() {
 ProtocolClient::~ProtocolClient() {
 }
 
+ProtocolPtr ProtocolClient::clone(void) {
+    return std::make_shared<ProtocolClient>();
+}
+
 RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
     TODO_USE_ME(bufferQueue);
     return RequestPtr(nullptr);
index 3f25d22..0b5cdb9 100644 (file)
@@ -32,6 +32,8 @@ public:
     ProtocolClient();
     virtual ~ProtocolClient();
 
+    virtual ProtocolPtr clone(void);
+
     virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
     virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue);
 };
index 9a69648..9a30455 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <common.h>
+#include <memory>
 #include "ProtocolSignal.h"
 
 namespace Cynara {
@@ -31,6 +32,10 @@ ProtocolSignal::ProtocolSignal() {
 ProtocolSignal::~ProtocolSignal() {
 }
 
+ProtocolPtr ProtocolSignal::clone(void) {
+    return std::make_shared<ProtocolSignal>();
+}
+
 RequestPtr ProtocolSignal::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
     TODO_USE_ME(bufferQueue);
     return RequestPtr(nullptr);
index a5efdc4..bd14478 100644 (file)
@@ -32,6 +32,8 @@ public:
     ProtocolSignal();
     virtual ~ProtocolSignal();
 
+    virtual ProtocolPtr clone(void);
+
     virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
     virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue);
 };
index 4c8442e..e1633d9 100644 (file)
@@ -71,8 +71,8 @@ void SocketManager::init(void) {
     const mode_t clientSocketUMask(0);
     const mode_t adminSocketUMask(0077);
 
-    createDomainSocket(ProtocolPtr(new ProtocolClient), clientSocketPath, clientSocketUMask);
-    createDomainSocket(ProtocolPtr(new ProtocolAdmin), adminSocketPath, adminSocketUMask);
+    createDomainSocket(std::make_shared<ProtocolClient>(), clientSocketPath, clientSocketUMask);
+    createDomainSocket(std::make_shared<ProtocolAdmin>(), adminSocketPath, adminSocketUMask);
     // todo create signal descriptor
     LOGI("SocketManger init done");
 }
@@ -197,9 +197,9 @@ void SocketManager::readyForAccept(int fd) {
     }
     LOGD("Accept on sock [%d]. New client socket opened [%d]", fd, client);
 
-    auto &description = createDescriptor(client);
-    description.setListen(false);
-    description.setProtocol(m_fds[fd].protocol());
+    auto &desc = createDescriptor(client);
+    desc.setListen(false);
+    desc.setProtocol(m_fds[fd].protocol()->clone());
     addReadSocket(client);
     LOGD("SocketManger readyForAccept on fd [%d] done", fd);
 }