From 0835cbb5a4be5898ce08d01f11e662009657626a Mon Sep 17 00:00:00 2001 From: Adam Malinowski Date: Thu, 9 Oct 2014 14:44:42 +0200 Subject: [PATCH] Implement agent protocol layer This patch contains implementation of (de)serializer class for agent protocol. Change-Id: I8ac7b5816545bd8e76a50d66a84b8ac686125985 --- src/common/protocol/ProtocolAgent.cpp | 126 ++++++++++++++++++++++++++++++---- src/common/protocol/ProtocolAgent.h | 10 ++- src/common/protocol/ProtocolOpCode.h | 8 +++ 3 files changed, 130 insertions(+), 14 deletions(-) diff --git a/src/common/protocol/ProtocolAgent.cpp b/src/common/protocol/ProtocolAgent.cpp index 7d85d31..7ccec81 100644 --- a/src/common/protocol/ProtocolAgent.cpp +++ b/src/common/protocol/ProtocolAgent.cpp @@ -23,12 +23,16 @@ #include #include -#include #include #include #include #include #include +#include +#include +#include +#include +#include #include "ProtocolAgent.h" @@ -44,6 +48,29 @@ ProtocolPtr ProtocolAgent::clone(void) { return std::make_shared(); } +RequestPtr ProtocolAgent::deserializeActionRequest(void) { + AgentRequestType requestType; + RawBuffer data; + + ProtocolDeserialization::deserialize(m_frameHeader, requestType); + ProtocolDeserialization::deserialize(m_frameHeader, data); + + LOGD("Deserialized AgentActionRequest: requestType [%" PRIu8 "], data lengtgh <%zu>", + requestType, data.size()); + + return std::make_shared(requestType, data, m_frameHeader.sequenceNumber()); +} + +RequestPtr ProtocolAgent::deserializeRegisterRequest(void) { + AgentType agentType; + + ProtocolDeserialization::deserialize(m_frameHeader, agentType); + + LOGD("Deserialized AgentRegisterRequest: agent type <%s>", agentType.c_str()); + + return std::make_shared(agentType, m_frameHeader.sequenceNumber()); +} + RequestPtr ProtocolAgent::extractRequestFromBuffer(BinaryQueuePtr bufferQueue) { ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); @@ -54,15 +81,44 @@ RequestPtr ProtocolAgent::extractRequestFromBuffer(BinaryQueuePtr bufferQueue) { ProtocolDeserialization::deserialize(m_frameHeader, opCode); LOGD("Deserialized opCode [%" PRIu8 "]", opCode); switch (opCode) { - default: - throw InvalidProtocolException(InvalidProtocolException::WrongOpCode); - break; + case OpAgentActionRequest: + return deserializeActionRequest(); + case OpAgentRegisterRequest: + return deserializeRegisterRequest(); + default: + throw InvalidProtocolException(InvalidProtocolException::WrongOpCode); + break; } } return nullptr; } +ResponsePtr ProtocolAgent::deserializeActionResponse(void) { + AgentResponseType responseType; + RawBuffer data; + + ProtocolDeserialization::deserialize(m_frameHeader, responseType); + ProtocolDeserialization::deserialize(m_frameHeader, data); + + LOGD("Deserialized AgentActionResponse: responseType [%" PRIu8 "], data lengtgh <%zu>", + responseType, data.size()); + + return std::make_shared(responseType, data, + m_frameHeader.sequenceNumber()); +} + +ResponsePtr ProtocolAgent::deserializeRegisterResponse(void) { + ProtocolResponseCode result; + + ProtocolDeserialization::deserialize(m_frameHeader, result); + + LOGD("Deserialized AgentRegisterResponse: result [%d]", static_cast(result)); + + return std::make_shared(static_cast(result), + m_frameHeader.sequenceNumber()); +} + ResponsePtr ProtocolAgent::extractResponseFromBuffer(BinaryQueuePtr bufferQueue) { ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); @@ -73,23 +129,67 @@ ResponsePtr ProtocolAgent::extractResponseFromBuffer(BinaryQueuePtr bufferQueue) ProtocolDeserialization::deserialize(m_frameHeader, opCode); LOGD("Deserialized opCode [%" PRIu8 "]", opCode); switch (opCode) { - default: - throw InvalidProtocolException(InvalidProtocolException::WrongOpCode); - break; + case OpAgentActionResponse: + return deserializeActionResponse(); + case OpAgentRegisterResponse: + return deserializeRegisterResponse(); + default: + throw InvalidProtocolException(InvalidProtocolException::WrongOpCode); + break; } } return nullptr; } -void ProtocolAgent::execute(RequestContextPtr context UNUSED, - AgentActionRequestPtr request UNUSED) { - //TODO: implement +void ProtocolAgent::execute(RequestContextPtr context, AgentActionRequestPtr request) { + LOGD("Serializing AgentActionRequest: op [%" PRIu8 "], requestType [%" PRIu8 "], " + "data lengtgh <%zu>", OpAgentActionRequest, request->type(), request->data().size()); + + ProtocolFrame frame = ProtocolFrameSerializer::startSerialization(request->sequenceNumber()); + + ProtocolSerialization::serialize(frame, OpAgentActionRequest); + ProtocolSerialization::serialize(frame, request->type()); + ProtocolSerialization::serialize(frame, request->data()); + + ProtocolFrameSerializer::finishSerialization(frame, *context->responseQueue()); +} + +void ProtocolAgent::execute(RequestContextPtr context, AgentRegisterRequestPtr request) { + LOGD("Serializing AgentRegisterRequest: op [%" PRIu8 "], agent type <%s>", + OpAgentRegisterRequest, request->agentType().c_str()); + + ProtocolFrame frame = ProtocolFrameSerializer::startSerialization(request->sequenceNumber()); + + ProtocolSerialization::serialize(frame, OpAgentRegisterRequest); + ProtocolSerialization::serialize(frame, request->agentType()); + + ProtocolFrameSerializer::finishSerialization(frame, *context->responseQueue()); +} + +void ProtocolAgent::execute(RequestContextPtr context, AgentRegisterResponsePtr response) { + LOGD("Serializing AgentRegisterResponse: op [%" PRIu8 "], response code: [%d]", + OpAgentRegisterResponse, static_cast(response->m_code)); + + ProtocolFrame frame = ProtocolFrameSerializer::startSerialization(response->sequenceNumber()); + + ProtocolSerialization::serialize(frame, OpAgentRegisterResponse); + ProtocolSerialization::serialize(frame, static_cast(response->m_code)); + + ProtocolFrameSerializer::finishSerialization(frame, *context->responseQueue()); } -void ProtocolAgent::execute(RequestContextPtr context UNUSED, - AgentActionResponsePtr request UNUSED) { - //TODO: implement +void ProtocolAgent::execute(RequestContextPtr context, AgentActionResponsePtr response) { + LOGD("Serializing AgentActionResponse: op [%" PRIu8 "], responseType [%" PRIu8 "], " + "data lengtgh <%zu>", OpAgentActionResponse, response->type(), response->data().size()); + + ProtocolFrame frame = ProtocolFrameSerializer::startSerialization(response->sequenceNumber()); + + ProtocolSerialization::serialize(frame, OpAgentActionResponse); + ProtocolSerialization::serialize(frame, response->type()); + ProtocolSerialization::serialize(frame, response->data()); + + ProtocolFrameSerializer::finishSerialization(frame, *context->responseQueue()); } } // namespace Cynara diff --git a/src/common/protocol/ProtocolAgent.h b/src/common/protocol/ProtocolAgent.h index 92fb788..ca28ec7 100644 --- a/src/common/protocol/ProtocolAgent.h +++ b/src/common/protocol/ProtocolAgent.h @@ -42,7 +42,15 @@ public: virtual ResponsePtr extractResponseFromBuffer(BinaryQueuePtr bufferQueue); virtual void execute(RequestContextPtr context, AgentActionRequestPtr request); - virtual void execute(RequestContextPtr context, AgentActionResponsePtr response); + virtual void execute(RequestContextPtr context, AgentActionResponsePtr request); + virtual void execute(RequestContextPtr context, AgentRegisterRequestPtr request); + virtual void execute(RequestContextPtr context, AgentRegisterResponsePtr request); + +private: + RequestPtr deserializeActionRequest(void); + RequestPtr deserializeRegisterRequest(void); + ResponsePtr deserializeActionResponse(void); + ResponsePtr deserializeRegisterResponse(void); }; } // namespace Cynara diff --git a/src/common/protocol/ProtocolOpCode.h b/src/common/protocol/ProtocolOpCode.h index 2b4171c..7bd67ae 100644 --- a/src/common/protocol/ProtocolOpCode.h +++ b/src/common/protocol/ProtocolOpCode.h @@ -44,6 +44,14 @@ enum ProtocolOpCode : uint8_t { OpSetPolicies, OpCodeResponse, OpAdminCheckRequest, + + /** Opcodes 25 - 39 are reserved for future use */ + + /** Agent operations */ + OpAgentRegisterRequest = 40, + OpAgentRegisterResponse, + OpAgentActionRequest, + OpAgentActionResponse, }; } /* namespace Cynara */ -- 2.7.4