cleanups of commented code

This commit is contained in:
2025-10-29 16:15:58 +03:00
parent bc300bb3de
commit 6a72ead855
7 changed files with 12 additions and 1094 deletions

View File

@@ -907,148 +907,17 @@ public:
mount_error_t m_err;
MccNetMessage input_msg;
MccNetMessage<handle_message_func_result_t> output_msg;
using output_msg_t = MccNetMessage<handle_message_func_result_t>;
output_msg_t output_msg;
auto ec = parseMessage(command, input_msg);
if (ec) {
output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR, ec);
} else {
output_msg = handleMessage<decltype(output_msg)>(input_msg, mount_ptr);
output_msg = handleMessage<output_msg_t>(input_msg, mount_ptr);
}
return output_msg.byteRepr();
// std::error_code err{};
// if (auto ec = parseMessage(command, input_msg)) {
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR, ec);
// } else {
// if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR)) { // strange!
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, command);
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR)) { // ??!!!
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, command);
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_RESTART_SERVER_STR)) {
// this->restart();
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, command);
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_INIT_STR)) {
// m_err = mount_ptr->initMount();
// if (m_err) {
// err = mcc_deduce_error_code(m_err, MccGenericMountNetworkServerErrorCode::ERROR_MOUNT_INIT);
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_STOP_STR)) {
// m_err = mount_ptr->stopMount();
// if (m_err) {
// err = mcc_deduce_error_code(m_err, MccGenericMountNetworkServerErrorCode::ERROR_MOUNT_STOP);
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_SLEW_STR)) {
// m_err = mount_ptr->slewToTarget(false);
// if (m_err) {
// err = mcc_deduce_error_code(m_err, MccGenericMountNetworkServerErrorCode::ERROR_MOUNT_SLEW);
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_MOVE_STR)) {
// m_err = mount_ptr->slewToTarget(true);
// if (m_err) {
// err = mcc_deduce_error_code(m_err, MccGenericMountNetworkServerErrorCode::ERROR_MOUNT_MOVE);
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_TRACK_STR)) {
// m_err = mount_ptr->trackTarget();
// if (m_err) {
// err = mcc_deduce_error_code(m_err, MccGenericMountNetworkServerErrorCode::ERROR_MOUNT_TRACK);
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_COORDFMT_STR)) {
// auto v = input_msg.paramValue<MccCoordinateSerializer::SerializedCoordFormat>(0);
// if (v) {
// _coordFormat = v.value();
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, command);
// } else {
// err = v.error();
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_COORDPREC_STR)) {
// auto v = input_msg.paramValue<MccCoordinateSerializer::SexagesimalCoordPrec>(0);
// if (v) {
// _coordPrec = v.value();
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, command);
// } else {
// err = v.error();
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_TARGET_STR)) {
// // by default return ICRS coordinates
// MccCelestialPoint cp{.pair_kind = MccCoordPairKind::COORDS_KIND_RADEC_ICRS};
// auto sz = input_msg.paramSize();
// if (sz) { // set or get operation
// auto vc = input_msg.paramValue<MccCelestialPoint>(0); // is it set operation?
// if (vc) { // coordinates are given - set
// operation
// auto m_err = mount_ptr->setPointingTarget(vc.value());
// if (m_err) {
// if (m_err) {
// err = mcc_deduce_error_code(
// m_err, MccGenericMountNetworkServerErrorCode::ERROR_MOUNT_SET_TARGET);
// }
// } else {
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, command);
// }
// } else {
// auto vp = input_msg.paramValue<MccCoordPairKind>(0);
// if (vp) { // coordinate pair kind is given
// cp.pair_kind = vp.value();
// err = coordsFromTelemetryData(*mount_ptr, true, cp);
// if (!err) {
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR,
// MCC_COMMPROTO_KEYWORD_TARGET_STR, cp);
// }
// } else { // invalid command!!!
// err = vp.error();
// }
// }
// } else { // get operation
// err = coordsFromTelemetryData(*mount_ptr, true, cp);
// if (!err) {
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR,
// MCC_COMMPROTO_KEYWORD_TARGET_STR,
// _coordFormat, _coordPrec, cp);
// }
// }
// } else if (input_msg.withKey(MCC_COMMPROTO_KEYWORD_MOUNT_STR)) {
// // by default return ICRS coordinates
// MccCelestialPoint cp{.pair_kind = MccCoordPairKind::COORDS_KIND_RADEC_ICRS};
// if (input_msg.paramSize()) { // ccordinate pair kind is given
// auto vp = input_msg.paramValue<MccCoordPairKind>(0);
// if (vp) { // coordinate pair kind is given
// cp.pair_kind = vp.value();
// err = coordsFromTelemetryData(*mount_ptr, false, cp);
// if (!err) {
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR,
// MCC_COMMPROTO_KEYWORD_MOUNT_STR, cp);
// }
// } else { // invalid command!!!
// err = vp.error();
// }
// } else {
// err = coordsFromTelemetryData(*mount_ptr, false, cp);
// if (!err) {
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR,
// MCC_COMMPROTO_KEYWORD_MOUNT_STR,
// _coordFormat, _coordPrec, cp);
// }
// }
// } else {
// err = std::make_error_code(std::errc::invalid_argument);
// }
// if (err) { // send error description
// output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR, err);
// }
// // else { // send ACK with copy of the input message
// // output_msg.construct(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR, command);
// // }
// }
// return output_msg.byteRepr();
};
}

View File

@@ -237,204 +237,6 @@ static constexpr std::array MCC_COMMPROTO_VALID_KEYS_HASH = []<size_t... Is>(std
}(std::make_index_sequence<MCC_COMMPROTO_VALID_KEYS.size()>());
static constexpr size_t MCC_COMMPROTO_KEYWORD_SERVER_ACK_HASH =
mcc::utils::FNV1aHash(MCC_COMMPROTO_KEYWORD_SERVER_ACK_STR);
static constexpr size_t MCC_COMMPROTO_KEYWORD_SERVER_ERROR_HASH =
mcc::utils::FNV1aHash(MCC_COMMPROTO_KEYWORD_SERVER_ERROR_STR);
static constexpr size_t MCC_COMMPROTO_KEYWORD_TARGET_HASH = mcc::utils::FNV1aHash(MCC_COMMPROTO_KEYWORD_TARGET_STR);
static constexpr size_t MCC_COMMPROTO_KEYWORD_MOUNT_HASH = mcc::utils::FNV1aHash(MCC_COMMPROTO_KEYWORD_MOUNT_STR);
template <traits::mcc_char_range T = std::string_view, std::ranges::output_range<T> RT = std::vector<T>>
struct mcc_netmsg_parse_result_t {
size_t keyword_hash;
T keyword;
RT params;
};
// network message parsing result class concept
template <typename T>
concept mcc_netmsg_parse_result_c = requires(T t) {
requires std::same_as<decltype(t.keyword_hash), size_t>; // hash of keyword
requires traits::mcc_char_range<decltype(t.keyword)>; // keyword char-range representation
// a range of parameters char-range representations
requires std::ranges::output_range<decltype(t.params), decltype(t.keyword)>;
};
// the function returns hash of message keyword
// if 'from_server' is true then given network message is considered as a server response, i.e.,
// valid keywords are "ACK" or "ERROR"
//
// the funtions returns false in the case of invalid message format and true otherwise
//
template <traits::mcc_input_char_range IR, mcc_netmsg_parse_result_c ResT>
bool mcc_parse_netmsg(const IR& netmsg, ResT& parse_res, bool from_server = false)
{
if (std::ranges::size(netmsg) == 0) {
return false;
};
auto found = std::ranges::search(netmsg, MCC_COMMPROTO_KEYPARAM_DELIM_SEQ);
if (std::distance(netmsg.begin(), found.begin()) == 0) {
return false;
}
const size_t hash = mcc::utils::FNV1aHash(netmsg.begin(), found.begin());
auto it = std::ranges::find(MCC_COMMPROTO_VALID_KEYS_HASH, hash);
if (it == MCC_COMMPROTO_VALID_KEYS_HASH.end()) {
return false;
}
if (from_server) { // only ACK or ERROR
auto ok = hash == MCC_COMMPROTO_VALID_KEYS_HASH[0] || hash == MCC_COMMPROTO_VALID_KEYS_HASH[1];
if (!ok) {
return false;
}
}
parse_res.keyword_hash = hash;
parse_res.keyword = {netmsg.begin(), found.begin()};
auto pars = netmsg | std::views::drop(std::distance(netmsg.begin(), found.end())) |
std::views::split(MCC_COMMPROTO_PARAMPARAM_DELIM_SEQ);
decltype(parse_res.params) res;
for (auto const& el : pars) { // parameters
std::back_inserter(res) = {el.begin(), el.end()};
}
parse_res.params = std::move(res);
return true;
}
// construct network message
// the function returns false if input keyword is not valid one (see MCC_COMMPROTO_VALID_KEYS)!
template <typename... PTs>
bool mcc_netmsg_construct(traits::mcc_output_char_range auto& msg,
traits::mcc_input_char_range auto const& keyword,
PTs... params)
{
const size_t hash = mcc::utils::FNV1aHash(keyword);
if (!std::ranges::contains(MCC_COMMPROTO_VALID_KEYS_HASH, hash)) {
return false;
}
msg = {keyword.begin(), keyword.end()};
if constexpr (sizeof...(PTs)) {
std::ranges::copy(MCC_COMMPROTO_KEYPARAM_DELIM_SEQ, std::back_inserter(msg));
[&msg]<typename T, typename... Ts>(this auto&& self, const T& par, const Ts&... pars) {
if constexpr (std::is_arithmetic_v<T>) {
std::ranges::copy(std::to_string(par), std::back_inserter(msg));
} else if constexpr (std::convertible_to<T, std::string>) {
std::ranges::copy(static_cast<std::string>(par), std::back_inserter(msg));
} else if constexpr (std::constructible_from<std::string, T>) {
std::ranges::copy(std::string(par), std::back_inserter(msg));
} else if constexpr (traits::mcc_char_range<T>) {
std::ranges::copy(std::string(par.begin(), par.end()), std::back_inserter(msg));
} else if constexpr (std::same_as<T, MccCoordPairKind>) {
std::ranges::copy(mcc_pairkind2str(par), std::back_inserter(msg));
} else {
static_assert(false, "UNSUPPORTED TYPE!!!");
}
if constexpr (sizeof...(Ts)) {
std::ranges::copy(MCC_COMMPROTO_PARAMPARAM_DELIM_SEQ, std::back_inserter(msg));
std::forward<decltype(self)>(self)(pars...);
}
}(params...);
}
return true;
}
// the function convert given network message parsing result class to
// celestial point coordinates according to parsed message parameters.
//
// It is assumed that the coordinates and their type are contained in the consecutive elements of the input array
// starting from the element 'from_idx' (zero-based):
//
// parse_res.params[..., X-COORD, Y-COORD, XY-KIND, ...]
//
// th last parameter 'XY-KIND' can be omitted and, in this case, 'default_kind' is assumed
//
// NOTE: IT IS ASSUMED THAT THE COORDINATES ARE REPRESENTED AS DEGREES EXPRESSED BY THE NUMBER WITH A FLOATING POINT
// OR IN SEXAGESIMAL FORM. IN THE CASE OF SEXAGESIMAL FORM THE TYPE (DEGREES OR HOURS) OF THE COORDINATE
// REPRESENTATION IS DETERMINED BY 'XY-KIND', E.G.:
// parse_res.params[..., "12:34:52.123", "23:43:56.12", "HADEC", ...]
// 'HADEC' STRING FOR 'XY-KIND' DETERMINES THE FIRST COORDINATE (HOUR ANGLE)
// AS AN ANGLE IN HOUR FORM WHILE THE SECOND ONE (DECLINATION) IN DEGREES
//
//
// The function returns false if it can not convert coordinate string to number or the 'XY-KIND' string is invalid
//
bool mcc_netmsg_get_cpoint(mcc_netmsg_parse_result_c auto const& parse_res,
size_t from_idx,
mcc_celestial_point_c auto& cpoint,
MccCoordPairKind default_kind)
requires std::ranges::contiguous_range<decltype(parse_res.keyword)>
{
if (std::ranges::size(parse_res.params) < (from_idx + 2)) {
return false;
}
MccCoordPairKind kind = default_kind;
if (std::ranges::size(parse_res.params) > (from_idx + 2)) {
kind = mcc_str2pairkind(parse_res.params[from_idx + 2]);
if (kind == MccCoordPairKind::COORDS_KIND_GENERIC) {
return false;
}
}
std::optional<double> ang1, ang2;
switch (kind) {
case mcc::MccCoordPairKind::COORDS_KIND_RADEC_ICRS:
case mcc::MccCoordPairKind::COORDS_KIND_RADEC_APP:
case mcc::MccCoordPairKind::COORDS_KIND_HADEC_APP:
ang1 = mcc::utils::parsAngleString(parse_res.params[from_idx], true);
break;
default:
ang1 = mcc::utils::parsAngleString(parse_res.params[from_idx]);
}
if (!ang1) {
return false;
}
ang2 = mcc::utils::parsAngleString(parse_res.params[from_idx + 1]);
if (!ang2) {
return false;
}
if (kind != mcc::MccCoordPairKind::COORDS_KIND_RADEC_ICRS) {
mcc_tp2tp(std::chrono::system_clock::now(), cpoint.time_point);
} else {
// J2000.0: 11:58:55.816 1 January 2000 UTC
auto tp = std::chrono::sys_days(std::chrono::year_month_day(std::chrono::January / std::chrono::day(1) /
std::chrono::year(2000))) +
std::chrono::hours(11) + std::chrono::minutes(58) + std::chrono::milliseconds(55816);
mcc_tp2tp(tp, cpoint.time_point);
}
cpoint.pair_kind = kind;
cpoint.X = MccAngle(ang1.value(), mcc::MccDegreeTag{}); // to radians
cpoint.Y = MccAngle(ang2.value(), mcc::MccDegreeTag{}); // to radians
return true;
}
template <typename T>
concept mcc_netmsg_valid_keys_c = requires(T t) {

View File

@@ -2,7 +2,6 @@
#include <list>
#include "../mcc_ccte_erfa.h"
#include "../mcc_netserver_proto.h"
#include "../mcc_pzone.h"
#include "../mcc_pzone_container.h"
@@ -157,63 +156,5 @@ int main()
std::cout << a1.sexagesimal() << "\n" << a2.sexagesimal() << "\n";
std::cout << "\n\n";
std::string sm{"ACK TARGET 12:23:45.13 -09.23423525 RADEC"};
// std::vector<std::string> sv;
std::vector<std::string_view> sv;
mcc::network::mcc_netmsg_parse_result_t<> p_res;
auto mr = mcc::network::mcc_parse_netmsg(sm, p_res);
std::cout << "MSG: <" << sm << ">\n";
std::cout << "\t" << p_res.keyword_hash << "\n";
std::cout << "\t[" << p_res.keyword << "]\n";
for (auto const& pr : p_res.params) {
std::cout << "\t[" << pr << "]\n";
}
std::cout << "GET CPOINT RET: "
<< mcc::network::mcc_netmsg_get_cpoint(p_res, 1, cp, mcc::MccCoordPairKind::COORDS_KIND_AZZD) << "\n";
std::cout << "CPOINT.X = " << cp.X << ", CPOINT.Y = " << cp.Y << " ("
<< mcc::network::mcc_pairkind2str(cp.pair_kind) << ")\n";
sm = "ERROR ";
mr = mcc::network::mcc_parse_netmsg(sm, p_res);
std::cout << "MSG: <" << sm << ">\n";
sm[0] = 'e';
std::cout << "\t" << p_res.keyword_hash << "\n";
std::cout << "\t[" << p_res.keyword << "]\n";
for (auto const& pr : p_res.params) {
std::cout << "\t[" << pr << "]\n";
}
mr = mcc::network::mcc_netmsg_construct(sm, mcc::network::MCC_COMMPROTO_KEYWORD_TARGET_STR, 12.3456789,
"34:21:56.132", mcc::MccCoordPairKind::COORDS_KIND_AZZD);
std::cout << "\nNETMSG: [" << sm << "] (" << mr << ")\n";
auto tp = std::chrono::sys_days(
std::chrono::year_month_day(std::chrono::January / std::chrono::day(1) / std::chrono::year(2000)))
// + std::chrono::hours(11) + std::chrono::minutes(58) + std::chrono::duration<double>(55.816);
+ std::chrono::hours(11) + std::chrono::minutes(58) + std::chrono::milliseconds(55816);
std::cout << tp << "\n";
// std::cout << std::chrono::system_clock::now() << "\n";
constexpr std::string_view stv{"RADEC"};
constexpr char ccv[] = "RADEC";
// const auto pk = mcc::network::mcc_str2pairkind(stv);
// const auto pk = mcc::network::mcc_str2pairkind(std::string_view{"RADEC"});
// const auto pk = mcc::network::mcc_str2pairkind("RADEC");
const auto pk = mcc::network::mcc_str2pairkind(ccv);
static_assert(pk == mcc::MccCoordPairKind::COORDS_KIND_RADEC_APP);
return 0;
}