This commit is contained in:
2024-11-20 12:23:50 +03:00
parent ae6fbf18ca
commit 319276845a
2 changed files with 127 additions and 101 deletions

View File

@@ -291,6 +291,27 @@ protected:
};
/* ADC client-server network protocol definitions */
namespace constants
{
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_ACK{"ACK"};
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_SET{"SET"};
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_GET{"GET"};
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_CMD{"CMD"};
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_ERR{"ERR"};
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_HELLO{"HELLO"};
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_DEVICE{"DEVICE"};
static constexpr std::string_view ADC_DEVICE_NETPROTO_KEY_NAMES{"NAMES"};
static constexpr std::array ADC_DEVICE_NETPROTO_VALID_KEY{
ADC_DEVICE_NETPROTO_KEY_ACK, ADC_DEVICE_NETPROTO_KEY_SET, ADC_DEVICE_NETPROTO_KEY_GET,
ADC_DEVICE_NETPROTO_KEY_CMD, ADC_DEVICE_NETPROTO_KEY_ERR, ADC_DEVICE_NETPROTO_KEY_HELLO,
ADC_DEVICE_NETPROTO_KEY_DEVICE, ADC_DEVICE_NETPROTO_KEY_NAMES};
} // namespace constants
template <traits::adc_char_range ByteSeqT,
const char KEY_VALUE_DELIM[] = constants::ADC_DEFAULT_KEY_VALUE_DELIMITER1,
const char VALUE_DELIM[] = constants::ADC_DEFAULT_VALUE_DELIMITER,
@@ -301,20 +322,7 @@ class AdcDeviceProtoMessage
using base_t = AdcKeyValueMessage<ByteSeqT, KEY_VALUE_DELIM, VALUE_DELIM, COMPOSITE_VALUE_DELIM>;
public:
static constexpr std::string_view ACK_KEY{"ACK"};
static constexpr std::string_view SET_KEY{"SET"};
static constexpr std::string_view GET_KEY{"GET"};
static constexpr std::string_view CMD_KEY{"CMD"};
static constexpr std::string_view ERR_KEY{"ERR"};
static constexpr std::string_view HELLO_KEY{"HELLO"};
static constexpr std::string_view DEVICE_KEY{"DEVICE"};
static constexpr std::string_view NAMES_KEY{"NAMES"};
static constexpr std::array VALID_KEY{ACK_KEY, SET_KEY, GET_KEY, CMD_KEY,
ERR_KEY, HELLO_KEY, DEVICE_KEY, NAMES_KEY};
typedef std::array<size_t, VALID_KEY.size()> keyword_hash_array_t;
typedef std::array<size_t, constants::ADC_DEVICE_NETPROTO_VALID_KEY.size()> keyword_hash_array_t;
enum KEY_IDX : size_t {
ACK_KEY_IDX,
@@ -324,7 +332,8 @@ public:
ERR_KEY_IDX,
HELLO_KEY_IDX,
DEVICE_KEY_IDX,
NAMES_KEY_IDX
NAMES_KEY_IDX,
INVALID_KEY_IDX
};
private: // include here to allow clang compilation
@@ -333,9 +342,9 @@ private: // include here to allow clang compilation
template <size_t... I>
static constexpr auto computeKeywordHashesImpl(std::index_sequence<I...>)
{
return keyword_hash_array_t{utils::AdcFNV1aHash(VALID_KEY[I])...};
return keyword_hash_array_t{utils::AdcFNV1aHash(constants::ADC_DEVICE_NETPROTO_VALID_KEY[I])...};
}
template <typename Indices = std::make_index_sequence<VALID_KEY.size()>>
template <typename Indices = std::make_index_sequence<constants::ADC_DEVICE_NETPROTO_VALID_KEY.size()>>
static constexpr auto computeKeywordHashes()
{
return computeKeywordHashesImpl(Indices{});
@@ -350,6 +359,9 @@ private: // include here to allow clang compilation
bool isKey(size_t idx) const
{
if (idx >= INVALID_KEY_IDX)
return false;
return _keyHash == KEY_HASHES[idx];
}
@@ -452,9 +464,11 @@ public:
}
using base_t::setKeyValue; // import to public area
void ack()
{
base_t::setKey(ACK_KEY);
base_t::setKey(constants::ADC_DEVICE_NETPROTO_KEY_ACK);
keyHash();
}
@@ -463,7 +477,7 @@ public:
template <typename ParT, typename... ParTs>
void ack(const ParT& param, const ParTs&... params)
{
base_t::setKeyValue(ACK_KEY, param, params...);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_ACK, param, params...);
keyHash();
}
@@ -474,7 +488,7 @@ public:
requires(!traits::adc_input_char_range<ParT>)
void ack(const ParT& param)
{
base_t::setKeyValue(ACK_KEY, param);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_ACK, param);
keyHash();
}
@@ -483,7 +497,7 @@ public:
template <traits::adc_tuple_like ParT>
void ack(const ParT& param)
{
base_t::setKeyValue(ACK_KEY, param);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_ACK, param);
keyHash();
}
@@ -492,8 +506,8 @@ public:
template <traits::adc_input_char_range AttrNameT, typename ValueT, typename... ValueTs>
void set(AttrNameT&& attr_name, ValueT&& value, ValueTs&&... values)
{
base_t::setKeyValue(SET_KEY, std::forward<AttrNameT>(attr_name), std::forward<ValueT>(value),
std::forward<ValueTs>(values)...);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_SET, std::forward<AttrNameT>(attr_name),
std::forward<ValueT>(value), std::forward<ValueTs>(values)...);
keyHash();
}
@@ -501,7 +515,7 @@ public:
template <traits::adc_input_char_range AttrNameT>
void get(AttrNameT&& attr_name)
{
base_t::setKeyValue(GET_KEY, std::forward<AttrNameT>(attr_name));
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_GET, std::forward<AttrNameT>(attr_name));
keyHash();
}
@@ -509,14 +523,14 @@ public:
template <traits::adc_input_char_range CmdNameT>
void cmd(CmdNameT&& cmd_name)
{
base_t::setKeyValue(CMD_KEY, std::forward<CmdNameT>(cmd_name));
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_CMD, std::forward<CmdNameT>(cmd_name));
keyHash();
}
void err(const std::error_code& ec)
{
base_t::setKeyValue(ERR_KEY, ec.value(), ec.category().name(), ec.message());
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_ERR, ec.value(), ec.category().name(), ec.message());
keyHash();
}
@@ -524,7 +538,8 @@ public:
template <traits::adc_input_char_range SenderNameT, typename... ParamTs>
void hello(SenderNameT&& name, ParamTs&&... params)
{
base_t::setKeyValue(HELLO_KEY, std::forward<SenderNameT>(name), std::forward<ParamTs>(params)...);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_HELLO, std::forward<SenderNameT>(name),
std::forward<ParamTs>(params)...);
keyHash();
}
@@ -532,14 +547,14 @@ public:
template <traits::adc_input_char_range DevNameT>
void device(DevNameT&& dev_name)
{
base_t::setKeyValue(DEVICE_KEY, std::forward<DevNameT>(dev_name));
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_DEVICE, std::forward<DevNameT>(dev_name));
keyHash();
}
void names()
{
base_t::setKey(NAMES_KEY);
base_t::setKey(constants::ADC_DEVICE_NETPROTO_KEY_NAMES);
keyHash();
}
@@ -547,7 +562,7 @@ public:
template <typename DevNameT, typename... DevNameTs>
void names(const DevNameT& dev_name, const DevNameTs&... dev_names)
{
base_t::setKeyValue(NAMES_KEY, dev_name, dev_names...);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_NAMES, dev_name, dev_names...);
keyHash();
}
@@ -556,7 +571,7 @@ public:
requires(!traits::adc_input_char_range<std::ranges::range_value_t<R>>)
void names(const R& dev_names)
{
base_t::setKeyValue(NAMES_KEY, dev_names);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_NAMES, dev_names);
keyHash();
}
@@ -564,7 +579,7 @@ public:
template <traits::adc_tuple_like T>
void names(const T& dev_names)
{
base_t::setKeyValue(NAMES_KEY, dev_names);
base_t::setKeyValue(constants::ADC_DEVICE_NETPROTO_KEY_NAMES, dev_names);
keyHash();
}