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
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_ */
*/
#include <common.h>
+#include <memory>
#include "ProtocolAdmin.h"
namespace Cynara {
ProtocolAdmin::~ProtocolAdmin() {
}
+ProtocolPtr ProtocolAdmin::clone(void) {
+ return std::make_shared<ProtocolAdmin>();
+}
+
RequestPtr ProtocolAdmin::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
TODO_USE_ME(bufferQueue);
return RequestPtr(nullptr);
ProtocolAdmin();
virtual ~ProtocolAdmin();
+ virtual ProtocolPtr clone(void);
+
virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue);
};
*/
#include <common.h>
+#include <memory>
#include "ProtocolClient.h"
namespace Cynara {
ProtocolClient::~ProtocolClient() {
}
+ProtocolPtr ProtocolClient::clone(void) {
+ return std::make_shared<ProtocolClient>();
+}
+
RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
TODO_USE_ME(bufferQueue);
return RequestPtr(nullptr);
ProtocolClient();
virtual ~ProtocolClient();
+ virtual ProtocolPtr clone(void);
+
virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue);
};
*/
#include <common.h>
+#include <memory>
#include "ProtocolSignal.h"
namespace Cynara {
ProtocolSignal::~ProtocolSignal() {
}
+ProtocolPtr ProtocolSignal::clone(void) {
+ return std::make_shared<ProtocolSignal>();
+}
+
RequestPtr ProtocolSignal::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
TODO_USE_ME(bufferQueue);
return RequestPtr(nullptr);
ProtocolSignal();
virtual ~ProtocolSignal();
+ virtual ProtocolPtr clone(void);
+
virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue);
};
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");
}
}
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);
}