protocol: Definitions for DML messages

This commit is contained in:
Joshua Scott 2018-04-03 16:11:06 +01:00
parent eb7e5ade63
commit 3cb082b1af
5 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,38 @@
#pragma once
#include "../../util/Serializable.h"
#include "../../dml/Record.h"
#include <iostream>
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;
};
}
}
}

View File

@ -0,0 +1,38 @@
#pragma once
#include "Message.h"
#include <string>
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 <typename ValueT>
MessageBuilder &set_field_value(std::string name, ValueT value)
{
auto *field = m_message->get_record().get_field<ValueT>(name);
if (!field)
{
// TODO: Exceptions
}
field->set_value(value);
return *this;
}
Message *get_message() const;
private:
Message *m_message;
};
}
}
}

View File

@ -0,0 +1,40 @@
#pragma once
#include "MessageModule.h"
#include "MessageBuilder.h"
#include "../../dml/Record.h"
#include <string>
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;
};
}
}
}

View File

@ -0,0 +1,51 @@
#pragma once
#include "MessageTemplate.h"
#include <cstdint>
#include <string>
#include <vector>
#include <map>
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<MessageTemplate *> m_templates;
std::map<std::string, MessageTemplate *> m_message_name_map;
std::map<uint8_t, MessageTemplate *> m_message_type_map;
};
typedef std::vector<MessageModule *> MessageModuleList;
typedef std::map<uint8_t, MessageModule *> MessageModuleServiceIdMap;
typedef std::map<std::string, MessageModule *> MessageModuleProtocolTypeMap;
}
}
}

View File

@ -0,0 +1,35 @@
#pragma once
#include "../../dml/Record.h"
#include "MessageBuilder.h"
#include <string>
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;
};
}
}
}