From bfe56f194a65060ca8f6c9b00d8c61c68784511f Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Tue, 3 Apr 2018 16:11:06 +0100 Subject: [PATCH] protocol: Definitions for DML messages --- include/ki/protocol/dml/Message.h | 38 +++++++++++++++++ include/ki/protocol/dml/MessageBuilder.h | 38 +++++++++++++++++ include/ki/protocol/dml/MessageManager.h | 40 ++++++++++++++++++ include/ki/protocol/dml/MessageModule.h | 51 +++++++++++++++++++++++ include/ki/protocol/dml/MessageTemplate.h | 35 ++++++++++++++++ 5 files changed, 202 insertions(+) create mode 100644 include/ki/protocol/dml/Message.h create mode 100644 include/ki/protocol/dml/MessageBuilder.h create mode 100644 include/ki/protocol/dml/MessageManager.h create mode 100644 include/ki/protocol/dml/MessageModule.h create mode 100644 include/ki/protocol/dml/MessageTemplate.h diff --git a/include/ki/protocol/dml/Message.h b/include/ki/protocol/dml/Message.h new file mode 100644 index 0000000..397b848 --- /dev/null +++ b/include/ki/protocol/dml/Message.h @@ -0,0 +1,38 @@ +#pragma once +#include "../../util/Serializable.h" +#include "../../dml/Record.h" +#include + +namespace ki +{ +namespace protocol +{ +namespace dml +{ + class Message : public util::Serializable + { + public: + Message(uint8_t service_id = 0, uint8_t type = 0); + virtual ~Message() = default; + + uint8_t get_service_id() const; + void set_service_id(uint8_t service_id); + + uint8_t get_type() const; + void set_type(uint8_t type); + + ki::dml::Record &get_record(); + const ki::dml::Record &get_record() const; + void set_record(const ki::dml::Record &record); + + void write_to(std::ostream &ostream) const final; + void read_from(std::istream &istream) final; + size_t get_size() const final; + private: + uint8_t m_service_id; + uint8_t m_type; + ki::dml::Record *m_record; + }; +} +} +} \ No newline at end of file diff --git a/include/ki/protocol/dml/MessageBuilder.h b/include/ki/protocol/dml/MessageBuilder.h new file mode 100644 index 0000000..2b2e134 --- /dev/null +++ b/include/ki/protocol/dml/MessageBuilder.h @@ -0,0 +1,38 @@ +#pragma once +#include "Message.h" +#include + +namespace ki +{ +namespace protocol +{ +namespace dml +{ + class MessageBuilder + { + public: + MessageBuilder(uint8_t service_id = 0, uint8_t type = 0); + + MessageBuilder &set_service_id(uint8_t service_id); + MessageBuilder &set_message_type(uint8_t type); + MessageBuilder &use_template_record(const ki::dml::Record &record); + + template + MessageBuilder &set_field_value(std::string name, ValueT value) + { + auto *field = m_message->get_record().get_field(name); + if (!field) + { + // TODO: Exceptions + } + field->set_value(value); + return *this; + } + + Message *get_message() const; + private: + Message *m_message; + }; +} +} +} \ No newline at end of file diff --git a/include/ki/protocol/dml/MessageManager.h b/include/ki/protocol/dml/MessageManager.h new file mode 100644 index 0000000..be3281e --- /dev/null +++ b/include/ki/protocol/dml/MessageManager.h @@ -0,0 +1,40 @@ +#pragma once +#include "MessageModule.h" +#include "MessageBuilder.h" +#include "../../dml/Record.h" +#include + +namespace ki +{ +namespace protocol +{ +namespace dml +{ + class MessageManager + { + public: + MessageManager(); + ~MessageManager(); + + static MessageManager &get_singleton(); + + const MessageModule &load_module(std::string filepath); + const MessageModule &get_module(uint8_t service_id) const; + const MessageModule &get_module(std::string protocol_type) const; + + MessageBuilder &build_message(uint8_t service_id, uint8_t message_type) const; + MessageBuilder &build_message(uint8_t service_id, std::string message_name) const; + MessageBuilder &build_message(std::string service_type, uint8_t message_type) const; + MessageBuilder &build_message(std::string service_type, std::string message_name) const; + + const Message *from_binary(std::istream &istream); + private: + static MessageManager *g_instance; + + MessageModuleList m_modules; + MessageModuleServiceIdMap m_service_id_map; + MessageModuleProtocolTypeMap m_protocol_type_map; + }; +} +} +} diff --git a/include/ki/protocol/dml/MessageModule.h b/include/ki/protocol/dml/MessageModule.h new file mode 100644 index 0000000..137cc42 --- /dev/null +++ b/include/ki/protocol/dml/MessageModule.h @@ -0,0 +1,51 @@ +#pragma once +#include "MessageTemplate.h" +#include +#include +#include +#include + +namespace ki +{ +namespace protocol +{ +namespace dml +{ + class MessageModule + { + public: + MessageModule(uint8_t service_id, std::string protocol_type); + ~MessageModule(); + + uint8_t get_service_id() const; + void set_service_id(uint8_t service_id); + + std::string get_protocol_type() const; + void set_protocol_type(std::string protocol_type); + + std::string get_protocol_desription() const; + void set_protocol_description(std::string protocol_description); + + const MessageTemplate &add_message_template(std::string name, uint8_t type, + ki::dml::Record *record); + const MessageTemplate &get_message_template(uint8_t type); + const MessageTemplate &get_message_template(std::string name); + + MessageBuilder &build_message(uint8_t message_type) const; + MessageBuilder &build_message(std::string message_name) const; + private: + uint8_t m_service_id; + std::string m_protocol_type; + std::string m_protocol_description; + + std::vector m_templates; + std::map m_message_name_map; + std::map m_message_type_map; + }; + + typedef std::vector MessageModuleList; + typedef std::map MessageModuleServiceIdMap; + typedef std::map MessageModuleProtocolTypeMap; +} +} +} diff --git a/include/ki/protocol/dml/MessageTemplate.h b/include/ki/protocol/dml/MessageTemplate.h new file mode 100644 index 0000000..ba8b744 --- /dev/null +++ b/include/ki/protocol/dml/MessageTemplate.h @@ -0,0 +1,35 @@ +#pragma once +#include "../../dml/Record.h" +#include "MessageBuilder.h" +#include + +namespace ki +{ +namespace protocol +{ +namespace dml +{ + class MessageTemplate + { + public: + MessageTemplate(std::string name, uint8_t type, ki::dml::Record *record); + ~MessageTemplate(); + + std::string get_name() const; + void set_name(std::string name); + + uint8_t get_type() const; + void set_type(uint8_t type); + + const ki::dml::Record &get_record() const; + void set_record(ki::dml::Record *record); + + MessageBuilder &build_message(); + private: + std::string m_name; + uint8_t m_type; + ki::dml::Record *m_record; + }; +} +} +} \ No newline at end of file