//Ask cynara service
PolicyResult result(PredefinedPolicyType::DENY);
try {
- RequestPtr request = std::make_shared<CheckRequest>(PolicyKey(client, user, privilege));
+ // todo handle sequence number correctly
+ ProtocolFrameSequenceNumber sequenceNumber = 0;
+ RequestPtr request = std::make_shared<CheckRequest>(PolicyKey(client, user, privilege), sequenceNumber);
ResponsePtr response = m_socketClient->askCynaraServer(request);
if (!response) {
LOGW("Disconnected by cynara server.");
${COMMON_PATH}/log/log.cpp
${COMMON_PATH}/protocol/ProtocolAdmin.cpp
${COMMON_PATH}/protocol/ProtocolClient.cpp
+ ${COMMON_PATH}/protocol/ProtocolFrame.cpp
+ ${COMMON_PATH}/protocol/ProtocolFrameHeader.cpp
+ ${COMMON_PATH}/protocol/ProtocolFrameSerializer.cpp
${COMMON_PATH}/protocol/ProtocolSerialization.cpp
${COMMON_PATH}/protocol/ProtocolSignal.cpp
${COMMON_PATH}/request/CheckRequest.cpp
* Binary queue auto pointer
*/
class BinaryQueue;
-typedef std::auto_ptr<BinaryQueue> BinaryQueueAutoPtr;
+typedef std::shared_ptr<BinaryQueue> BinaryQueuePtr;
/**
* Binary stream implemented as constant size bucket list
--- /dev/null
+/*
+ * 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 InvalidProtocolException.h
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Implementation of InvalidProtocolException
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_INVALIDPROTOCOLEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_INVALIDPROTOCOLEXCEPTION_H_
+
+#include <exception>
+#include <sstream>
+
+#include "Exception.h"
+
+namespace Cynara {
+
+class InvalidProtocolException : public Exception {
+public:
+ enum ExceptionType {
+ InvalidSignature,
+ WrongOpCode,
+ Other
+ };
+
+private:
+ std::string m_whatMessage;
+ ExceptionType m_exceptionType;
+
+public:
+ InvalidProtocolException(ExceptionType exceptionType) :
+ m_exceptionType(exceptionType) {
+ switch(m_exceptionType) {
+ case InvalidSignature:
+ m_whatMessage = "No valid signature found";
+ break;
+ case WrongOpCode:
+ m_whatMessage = "Wrong request code";
+ break;
+ case Other:
+ m_whatMessage = "Unknown problem";
+ break;
+ }
+
+ }
+
+ virtual ~InvalidProtocolException() = default;
+
+ virtual const char *what(void) const noexcept {
+ return m_whatMessage.c_str();
+ }
+
+ ExceptionType exceptionTyp(void) const {
+ return m_exceptionType;
+ }
+};
+
+} // namespace Cynara
+
+#endif /* SRC_COMMON_EXCEPTIONS_INVALIDPROTOCOLEXCEPTION_H_ */
#include <memory>
#include <containers/BinaryQueue.h>
+#include <protocol/ProtocolFrameHeader.h>
#include <request/pointers.h>
#include <request/RequestTaker.h>
#include <response/pointers.h>
virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue) = 0;
virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue) = 0;
+
+ ProtocolFrameHeader &frameHeader(void) {
+ return m_frameHeader;
+ }
+
+protected:
+ ProtocolFrameHeader m_frameHeader;
};
} // namespace Cynara
/*
* @file ProtocolClient.cpp
* @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
* @version 1.0
* @brief This file implements protocol class for communication with client
*/
#include <common.h>
#include <memory>
+
+#include <exceptions/InvalidProtocolException.h>
+#include <exceptions/OutOfDataException.h>
+#include <protocol/ProtocolFrameSerializer.h>
+#include <protocol/ProtocolOpCode.h>
+#include <protocol/ProtocolSerialization.h>
+#include <request/CheckRequest.h>
+#include <request/RequestContext.h>
+#include <types/PolicyKey.h>
+
#include "ProtocolClient.h"
namespace Cynara {
return std::make_shared<ProtocolClient>();
}
+RequestPtr ProtocolClient::deserializeCheckRequest(ProtocolFrameHeader &frame) {
+ std::string clientId, userId, privilegeId;
+ ProtocolDeserialization::deserialize(frame, clientId);
+ ProtocolDeserialization::deserialize(frame, userId);
+ ProtocolDeserialization::deserialize(frame, privilegeId);
+ return std::make_shared<CheckRequest>(PolicyKey(clientId, userId, privilegeId),
+ frame.sequenceNumber());
+}
+
RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueue &bufferQueue) {
- TODO_USE_ME(bufferQueue);
- return RequestPtr(nullptr);
+ ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue);
+
+ if (m_frameHeader.isFrameComplete()) {
+ ProtocolOpCode requestId;
+
+ m_frameHeader.resetState();
+ ProtocolDeserialization::deserialize(m_frameHeader, requestId);
+ switch (requestId) {
+ case OpCheckPolicy:
+ return deserializeCheckRequest(m_frameHeader);
+ default:
+ throw InvalidProtocolException(InvalidProtocolException::WrongOpCode);
+ break;
+ }
+ }
+
+ return nullptr;
}
ResponsePtr ProtocolClient::extractResponseFromBuffer(BinaryQueue &bufferQueue) {
return ResponsePtr(nullptr);
}
+void ProtocolClient::execute(RequestContextPtr context, CheckRequestPtr request) {
+ ProtocolFramePtr frame = ProtocolFrameSerializer::startSerialization(request->sequenceNumber());
+
+ ProtocolSerialization::serialize(*frame, OpCheckPolicy);
+ ProtocolSerialization::serialize(*frame, request->key().client().value());
+ ProtocolSerialization::serialize(*frame, request->key().user().value());
+ ProtocolSerialization::serialize(*frame, request->key().privilege().value());
+
+ ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue());
+}
+
} // namespace Cynara
#ifndef SRC_COMMON_PROTOCOL_PROTOCOLCLIENT_H_
#define SRC_COMMON_PROTOCOL_PROTOCOLCLIENT_H_
+#include <protocol/ProtocolFrameHeader.h>
+#include <request/pointers.h>
+
#include "Protocol.h"
namespace Cynara {
virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue);
virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue);
+
+ virtual void execute(RequestContextPtr context, CheckRequestPtr request);
+
+private:
+ RequestPtr deserializeCheckRequest(ProtocolFrameHeader &frame);
};
} // namespace Cynara
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolFrame.cpp
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Implementation of ProtocolFrame class.
+ */
+
+#include <string.h>
+
+#include "ProtocolFrame.h"
+
+namespace Cynara {
+
+ProtocolFrame::ProtocolFrame(ProtocolFrameHeaderPtr frameHeader, BinaryQueuePtr data) :
+ m_frameHeader(frameHeader), m_frameBodyContent(data) {
+}
+
+void ProtocolFrame::read(size_t num, void *bytes) {
+ m_frameBodyContent->flattenConsume(bytes, num);
+}
+
+void ProtocolFrame::write(size_t num, const void *bytes) {
+ m_frameBodyContent->appendCopy(bytes, num);
+ m_frameHeader->increaseFrameLength(num);
+}
+
+} /* namespace Cynara */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolFrame.h
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Header for ProtocolFrame class.
+ */
+
+#ifndef SRC_COMMON_PROTOCOL_PROTOCOLFRAME_H_
+#define SRC_COMMON_PROTOCOL_PROTOCOLFRAME_H_
+
+#include <cstddef>
+#include <memory>
+
+#include <containers/BinaryQueue.h>
+#include <protocol/ProtocolFrameHeader.h>
+#include <protocol/ProtocolSerialization.h>
+
+namespace Cynara {
+
+class ProtocolFrameSerializer;
+
+class ProtocolFrame: public IStream {
+
+public:
+ ProtocolFrame(ProtocolFrameHeaderPtr frameHeader, BinaryQueuePtr headerContent);
+ virtual ~ProtocolFrame() = default;
+
+ ProtocolFrameHeaderPtr frameHeader(void) {
+ return m_frameHeader;
+ }
+
+ virtual void read(size_t num, void *bytes);
+ virtual void write(size_t num, const void *bytes);
+
+private:
+ ProtocolFrameHeaderPtr m_frameHeader;
+ BinaryQueuePtr m_frameBodyContent;
+
+ BinaryQueue &bodyContent(void) {
+ return *m_frameBodyContent;
+ }
+
+ friend class ProtocolFrameSerializer;
+};
+
+typedef std::shared_ptr<ProtocolFrame> ProtocolFramePtr;
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_PROTOCOL_PROTOCOLFRAME_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolFrameHeader.cpp
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Implementation of protocol frame header (de)serializer class.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "ProtocolFrameHeader.h"
+
+namespace Cynara {
+
+const ProtocolFrameSignature ProtocolFrameHeader::m_signature = "CPv1";
+
+ProtocolFrameHeader::ProtocolFrameHeader(BinaryQueuePtr headerContent) :
+ m_frameHeaderContent(headerContent), m_frameLength(0), m_sequenceNumber(0),
+ m_headerComplete(false), m_bodyComplete(false) {
+}
+
+void ProtocolFrameHeader::read(size_t num, void *bytes) {
+ m_frameHeaderContent->flattenConsume(bytes, num);
+}
+
+void ProtocolFrameHeader::write(size_t num, const void *bytes) {
+ m_frameHeaderContent->appendCopy(bytes, num);
+}
+
+} /* namespace Cynara */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolFrameHeader.h
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Header file for protocol frame header (de)serializer class.
+ */
+
+#ifndef SRC_COMMON_PROTOCOL_PROTOCOLFRAMEHEADER_H_
+#define SRC_COMMON_PROTOCOL_PROTOCOLFRAMEHEADER_H_
+
+#include <cstddef>
+
+#include <containers/BinaryQueue.h>
+#include <protocol/ProtocolSerialization.h>
+#include <types/ProtocolFields.h>
+
+namespace Cynara {
+
+class ProtocolFrameSerializer;
+
+class ProtocolFrameHeader: public IStream {
+private:
+ static const ProtocolFrameSignature m_signature;
+ static size_t frameHeaderLength(void) {
+ return m_signature.size() +
+ sizeof(ProtocolFrameLength) +
+ sizeof(ProtocolFrameSequenceNumber);
+ }
+
+public:
+ ProtocolFrameHeader(BinaryQueuePtr headerContent = nullptr);
+ virtual ~ProtocolFrameHeader() = default;
+
+ virtual void read(size_t num, void *bytes);
+ virtual void write(size_t num, const void *bytes);
+
+ ProtocolFrameSequenceNumber sequenceNumber(void) {
+ return m_sequenceNumber;
+ }
+
+ bool isHeaderComplete(void) {
+ return m_headerComplete;
+ }
+
+ bool isFrameComplete(void) {
+ return m_headerComplete && m_bodyComplete;
+ }
+
+ void resetState(void) {
+ m_headerComplete = false;
+ m_bodyComplete = false;
+ }
+
+ ProtocolFrameLength frameLength(void) {
+ return m_frameLength;
+ }
+
+private:
+ BinaryQueuePtr m_frameHeaderContent;
+ ProtocolFrameLength m_frameLength;
+ ProtocolFrameSequenceNumber m_sequenceNumber;
+ bool m_headerComplete;
+ bool m_bodyComplete;
+
+ void setSequenceNumber(ProtocolFrameSequenceNumber sequenceNumber) {
+ m_sequenceNumber = sequenceNumber;
+ }
+
+ void increaseFrameLength(ProtocolFrameLength size) {
+ m_frameLength += size;
+ }
+
+ BinaryQueue &headerContent(void) {
+ return *m_frameHeaderContent;
+ }
+
+ void setHeaderContent(BinaryQueuePtr headerContent) {
+ m_frameHeaderContent = headerContent;
+ }
+
+ void setHeaderComplete(void) {
+ m_headerComplete = true;
+ }
+
+ void setBodyComplete(void) {
+ m_bodyComplete = true;
+ }
+
+ friend class ProtocolFrame;
+ friend class ProtocolFrameSerializer;
+};
+
+typedef std::shared_ptr<ProtocolFrameHeader> ProtocolFrameHeaderPtr;
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_PROTOCOL_PROTOCOLFRAMEHEADER_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolSerializer.cpp
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Implementation of protocol frame (de)serializer class.
+ */
+
+#include <exceptions/InvalidProtocolException.h>
+#include <exceptions/OutOfDataException.h>
+
+#include "ProtocolFrameSerializer.h"
+
+namespace Cynara {
+
+void ProtocolFrameSerializer::deserializeHeader(ProtocolFrameHeader &frameHeader,
+ BinaryQueue &data) {
+ if (!frameHeader.isHeaderComplete()) {
+ if ((data.size() < ProtocolFrameHeader::frameHeaderLength())) {
+ return;
+ }
+
+ frameHeader.setHeaderContent(BinaryQueuePtr(&data, [=] (BinaryQueue *) {}));
+
+ ProtocolFrameSignature signature;
+ ProtocolDeserialization::deserialize(frameHeader, frameHeader.m_signature.length(),
+ signature);
+ if (ProtocolFrameHeader::m_signature != signature) {
+ throw InvalidProtocolException(InvalidProtocolException::InvalidSignature);
+ }
+
+ ProtocolDeserialization::deserialize(frameHeader, frameHeader.m_frameLength);
+ ProtocolDeserialization::deserialize(frameHeader, frameHeader.m_sequenceNumber);
+
+ frameHeader.setHeaderComplete();
+ }
+
+ if (data.size() >= (frameHeader.frameLength() - ProtocolFrameHeader::frameHeaderLength())) {
+ frameHeader.setBodyComplete();
+ }
+}
+
+ProtocolFramePtr ProtocolFrameSerializer::startSerialization(ProtocolFrameSequenceNumber sequenceNumber) {
+ BinaryQueuePtr headerQueue = std::make_shared<BinaryQueue>();
+ BinaryQueuePtr bodyQueue = std::make_shared<BinaryQueue>();
+ ProtocolFrameHeaderPtr header = std::make_shared<ProtocolFrameHeader>(headerQueue);
+ header->setSequenceNumber(sequenceNumber);
+ header->increaseFrameLength(ProtocolFrameHeader::frameHeaderLength());
+ return std::make_shared<ProtocolFrame>(header, bodyQueue);
+}
+
+void ProtocolFrameSerializer::finishSerialization(ProtocolFramePtr frame, BinaryQueue &data) {
+ ProtocolFrameHeader &frameHeader = *frame->frameHeader();
+ ProtocolSerialization::serializeNoSize(frameHeader, ProtocolFrameHeader::m_signature);
+ ProtocolSerialization::serialize(frameHeader, frameHeader.m_frameLength);
+ ProtocolSerialization::serialize(frameHeader, frameHeader.m_sequenceNumber);
+
+ data.appendMoveFrom(frameHeader.headerContent());
+ data.appendMoveFrom(frame->bodyContent());
+}
+
+} /* namespace Cynara */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolFrameSerializer.h
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Header file for protocol frame (de)serialization.
+ */
+
+#ifndef SRC_COMMON_PROTOCOL_PROTOCOLFRAMESERIALIZER_H_
+#define SRC_COMMON_PROTOCOL_PROTOCOLFRAMESERIALIZER_H_
+
+#include <containers/BinaryQueue.h>
+#include <protocol/ProtocolFrame.h>
+
+namespace Cynara {
+
+class ProtocolFrameSerializer {
+
+public:
+ static void deserializeHeader(ProtocolFrameHeader &frameHeader, BinaryQueue &data);
+ static ProtocolFramePtr startSerialization(ProtocolFrameSequenceNumber sequenceNumber);
+ static void finishSerialization(ProtocolFramePtr frame, BinaryQueue &data);
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_PROTOCOL_PROTOCOLFRAMESERIALIZER_H_ */
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolOpCode.h
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Decalaration of protocol frame operation codes.
+ */
+
+#ifndef SRC_COMMON_TYPES_PROTOCOLOPCODE_H_
+#define SRC_COMMON_TYPES_PROTOCOLOPCODE_H_
+
+#include <cstddef>
+
+namespace Cynara {
+
+enum ProtocolOpCode : uint8_t {
+ /** Client operations */
+ OpCheckPolicy = 0,
+
+ /** Opcodes 1 - 19 are reserved for future use */
+
+ /** Admin operations */
+ OpInsertPolicy = 20,
+ OpDeletePolicy,
+ OpListPolicies,
+ OpBeginTransaction,
+ OpEndTransaction
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_TYPES_PROTOCOLOPCODE_H_ */
/**
* @file ProtocolSerialization.h
* @author Tomasz Swierczek (t.swierczek@samsung.com)
+ * @author Adam Malinowski (a.malinowsk2@samsung.com)
* @version 1.0
* @brief Interfaces and templates used for data serialization.
*/
#ifndef SRC_COMMON_PROTOCOL_PROTOCOLSERIALIZATION_H_
#define SRC_COMMON_PROTOCOL_PROTOCOLSERIALIZATION_H_
-#include <string>
-#include <vector>
+#include <endian.h>
#include <list>
#include <map>
#include <memory>
+#include <string>
+#include <vector>
+
+#include <protocol/ProtocolOpCode.h>
namespace Cynara {
// Abstract data stream buffer
stream.write(sizeof(*value), value);
}
- // unsigned int
- static void serialize(IStream &stream, const unsigned value) {
- stream.write(sizeof(value), &value);
+ // unsigned 16-bit int
+ static void serialize(IStream &stream, const uint16_t value) {
+ uint16_t _value = htole16(value);
+ stream.write(sizeof(value), &_value);
}
- static void serialize(IStream &stream, const unsigned* const value) {
- stream.write(sizeof(*value), value);
+ static void serialize(IStream &stream, const uint16_t * const value) {
+ uint16_t _value = htole16(*value);
+ stream.write(sizeof(*value), &_value);
}
- // int
- static void serialize(IStream &stream, const int value) {
- stream.write(sizeof(value), &value);
+ // 16-bit int
+ static void serialize(IStream &stream, const int16_t value) {
+ int16_t _value = htole16(value);
+ stream.write(sizeof(value), &_value);
}
- static void serialize(IStream &stream, const int * const value) {
- stream.write(sizeof(*value), value);
+ static void serialize(IStream &stream, const int16_t * const value) {
+ int16_t _value = htole16(*value);
+ stream.write(sizeof(*value), &_value);
+ }
+
+ // unsigned 32-bit int
+ static void serialize(IStream &stream, const uint32_t value) {
+ uint32_t _value = htole32(value);
+ stream.write(sizeof(value), &_value);
+ }
+ static void serialize(IStream &stream, const uint32_t * const value) {
+ uint32_t _value = htole32(*value);
+ stream.write(sizeof(*value), &_value);
+ }
+
+ // 32-bit int
+ static void serialize(IStream &stream, const int32_t value) {
+ int32_t _value = htole32(value);
+ stream.write(sizeof(value), &_value);
+ }
+ static void serialize(IStream &stream, const int32_t * const value) {
+ int32_t _value = htole32(*value);
+ stream.write(sizeof(*value), &_value);
}
// bool
stream.write(sizeof(*value), value);
}
+ // ProtocolOpCode
+ static void serialize(IStream &stream, const ProtocolOpCode value) {
+ stream.write(sizeof(value), &value);
+ }
+ static void serialize(IStream &stream, const ProtocolOpCode * const value) {
+ stream.write(sizeof(*value), value);
+ }
+
// std::string
static void serialize(IStream &stream, const std::string &str) {
int length = str.size();
stream.write(sizeof(length), &length);
stream.write(length, str->c_str());
}
+ static void serializeNoSize(IStream &stream, const std::string &str) {
+ int length = str.size();
+ stream.write(length, str.c_str());
+ }
+ static void serializeNoSize(IStream &stream, const std::string * const str) {
+ int length = str->size();
+ stream.write(length, str->c_str());
+ }
// STL templates
stream.read(sizeof(*value), value);
}
- // unsigned int
- static void deserialize(IStream &stream, unsigned &value) {
+ // 16-bit int
+ static void deserialize(IStream &stream, int16_t &value) {
stream.read(sizeof(value), &value);
+ value = le16toh(value);
}
- static void deserialize(IStream &stream, unsigned *&value) {
- value = new unsigned;
+ static void deserialize(IStream &stream, int16_t *&value) {
+ value = new int16_t;
stream.read(sizeof(*value), value);
+ value = le16toh(value);
}
- // int
- static void deserialize(IStream &stream, int &value) {
+ // unsigned 16-bit int
+ static void deserialize(IStream &stream, uint16_t &value) {
stream.read(sizeof(value), &value);
+ value = le16toh(value);
}
- static void deserialize(IStream &stream, int *&value) {
- value = new int;
+ static void deserialize(IStream &stream, uint16_t *&value) {
+ value = new uint16_t;
stream.read(sizeof(*value), value);
+ value = le16toh(value);
+ }
+
+ // 32-bit int
+ static void deserialize(IStream &stream, int32_t &value) {
+ stream.read(sizeof(value), &value);
+ value = le32toh(value);
+ }
+ static void deserialize(IStream &stream, int32_t *&value) {
+ value = new int32_t;
+ stream.read(sizeof(*value), value);
+ value = le32toh(value);
+ }
+
+ // unsigned 32-bit int
+ static void deserialize(IStream &stream, uint32_t &value) {
+ stream.read(sizeof(value), &value);
+ value = le32toh(value);
+ }
+ static void deserialize(IStream &stream, uint32_t *&value) {
+ value = new uint32_t;
+ stream.read(sizeof(*value), value);
+ value = le32toh(value);
}
// bool
stream.read(sizeof(*value), value);
}
+ // PrtocolOpCode
+ static void deserialize(IStream &stream, ProtocolOpCode &value) {
+ stream.read(sizeof(value), &value);
+ }
+ static void deserialize(IStream &stream, ProtocolOpCode *&value) {
+ value = new ProtocolOpCode;
+ stream.read(sizeof(*value), value);
+ }
+
// std::string
static void deserialize(IStream &stream, std::string &str) {
int length;
stream.read(sizeof(length), &length);
- char *buf = new char[length + 1];
- stream.read(length, buf);
- buf[length] = 0;
- str = std::string(buf);
- delete[] buf;
+ str.resize(length);
+ stream.read(length, &str[0]);
}
static void deserialize(IStream &stream, std::string *&str) {
int length;
stream.read(sizeof(length), &length);
- char *buf = new char[length + 1];
- stream.read(length, buf);
- buf[length] = 0;
- str = new std::string(buf);
- delete[] buf;
+ str = new std::string(length, '\0');
+ stream.read(length, &str[0]);
+ }
+ static void deserialize(IStream &stream, int length, std::string &str) {
+ str.resize(length);
+ stream.read(length, &str[0]);
+ }
+ static void deserialize(IStream &stream, int length, std::string *&str) {
+ str = new std::string(length, '\0');
+ stream.read(length, &str[0]);
}
// STL templates
PolicyKey m_key;
public:
- CheckRequest(const PolicyKey &key) : m_key(key) {
+ CheckRequest(const PolicyKey &key, ProtocolFrameSequenceNumber sequenceNumber) :
+ Request(sequenceNumber), m_key(key) {
}
virtual ~CheckRequest() = default;
#include <request/pointers.h>
#include <request/RequestTaker.h>
+#include <types/ProtocolFields.h>
namespace Cynara {
class Request {
public:
- Request() = default;
+ Request(ProtocolFrameSequenceNumber sequenceNumber) : m_sequenceNumber(sequenceNumber) {
+ }
virtual ~Request() = default;
virtual void execute(RequestPtr self, RequestTakerPtr taker,
- RequestContextPtr context) const = 0;
+ RequestContextPtr context) const = 0;
+
+ ProtocolFrameSequenceNumber sequenceNumber(void) const {
+ return m_sequenceNumber;
+ }
+
+private:
+ ProtocolFrameSequenceNumber m_sequenceNumber;
};
} // namespace Cynara
/*
* @file RequestTaker.cpp
* @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
* @version 1.0
* @brief This file implements RequestTaker class
*/
#include <attributes/attributes.h>
#include <exceptions/NotImplementedException.h>
+#include <request/CheckRequest.h>
+#include <request/RequestContext.h>
#include "RequestTaker.h"
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
+ * 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 ProtocolFields.h
+ * @author Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @version 1.0
+ * @brief Types definition for protocol frame fields.
+ */
+
+#ifndef SRC_COMMON_TYPES_PROTOCOLFIELDS_H_
+#define SRC_COMMON_TYPES_PROTOCOLFIELDS_H_
+
+#include <string>
+#include <cstddef>
+
+namespace Cynara {
+
+typedef std::string ProtocolFrameSignature;
+typedef uint32_t ProtocolFrameLength;
+typedef uint16_t ProtocolFrameSequenceNumber;
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_TYPES_PROTOCOLFIELDS_H_ */