diff --git a/asibfm700/asibfm700_servocontroller.cpp b/asibfm700/asibfm700_servocontroller.cpp index b62bbb7..41c2ed6 100644 --- a/asibfm700/asibfm700_servocontroller.cpp +++ b/asibfm700/asibfm700_servocontroller.cpp @@ -106,6 +106,9 @@ AsibFM700ServoController::error_t AsibFM700ServoController::hardwareInit() AsibFM700ServoController::error_t AsibFM700ServoController::hardwareSetState(hardware_state_t state) { + static thread_local coordval_pair_t cvalpair{.X{0.0, 0.0}, .Y{0.0, 0.0}}; + static thread_local coordpair_t cpair{.X = 0.0, .Y = 0.0}; + // time point from sidservo library is 'double' number represented UNIXTIME with // microseconds/nanoseconds precision double tp = std::chrono::duration(state.time_point.time_since_epoch()).count(); @@ -113,9 +116,16 @@ AsibFM700ServoController::error_t AsibFM700ServoController::hardwareSetState(har std::lock_guard lock{*_setStateMutex}; // according to"SiTech protocol notes" X is DEC-axis and Y is HA-axis - coordval_pair_t cvalpair{.X{.val = state.Y, .t = tp}, .Y{.val = state.X, .t = tp}}; - coordpair_t cpair{.X = state.Y, .Y = state.X}; - // coordpair_t cpair{.X = state.Y, .Y = state.X + mcc::MccAngle(1.0_degs)}; + // coordval_pair_t cvalpair{.X{.val = state.Y, .t = tp}, .Y{.val = state.X, .t = tp}}; + // coordpair_t cpair{.X = state.Y, .Y = state.X}; + // coordpair_t cpair{.X = state.Y + mcc::MccAngle(1.0_degs), .Y = state.X + mcc::MccAngle(1.0_degs)}; + + + cvalpair.X = {.val = state.Y, .t = tp}; + cvalpair.Y = {.val = state.X, .t = tp}; + + cpair.X = state.Y; + cpair.Y = state.X; // correctTo is asynchronous function!!! // diff --git a/mcc/mcc_netserver.h b/mcc/mcc_netserver.h index 59f7adb..8d8f8ca 100644 --- a/mcc/mcc_netserver.h +++ b/mcc/mcc_netserver.h @@ -188,7 +188,7 @@ public: std::stringstream st; st << std::this_thread::get_id(); - logInfo(std::format("Create generic network server instance (thread ID = {})", st.str())); + logInfo(std::format("Create MccGenericNetworkServer class instance (thread ID = {})", st.str())); } // MccNetworkServer(asio::io_context& ctx, const handle_message_func_t& func, LoggerT logger = MccNullLogger{}) @@ -208,6 +208,10 @@ public: logInfo(std::format("Delete generic network server instance (thread ID = {}) ...", st.str())); stopListening(); + + _sessionThreadPool.stop(); + _sessionThreadPool.join(); + disconnectClients(); } @@ -585,8 +589,15 @@ public: _stopSignal.async_wait([this](std::error_code, int signo) { logInfo(std::format("Stop signal was received (signo = {})", signo)); - stopListening(); - disconnectClients(); + // _handleMessageFunc(MCC_COMMPROTO_KEYWORD_STOP_STR); + // std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + // stopListening(); + + // _sessionThreadPool.stop(); + // _sessionThreadPool.join(); + + // disconnectClients(); _asioContext.stop(); }); @@ -913,10 +924,21 @@ class MccGenericMountNetworkServer : public MccGenericNetworkServer public: using typename base_t::handle_message_func_result_t; + using base_t::logDebug; + using base_t::logError; + using base_t::logInfo; + using base_t::logTrace; + using base_t::logWarn; + template MccGenericMountNetworkServer(asio::io_context& ctx, MountT& mount, LoggerCtorArgsTs&&... log_args) : base_t(ctx, {}, std::forward(log_args)...) { + std::stringstream st; + st << std::this_thread::get_id(); + + logInfo(std::format("Create MccGenericMountNetworkServer class instance (thread ID = {})", st.str())); + // to avoid possible compiler optimization (one needs to catch 'mount' strictly by reference) auto* mount_ptr = &mount; @@ -939,15 +961,27 @@ public: return output_msg.template byteRepr(); }; + + // special functor (used in the destructor) + _stopMountFunc = [mount_ptr]() { mount_ptr->stopMount(); }; } - virtual ~MccGenericMountNetworkServer() {} + virtual ~MccGenericMountNetworkServer() + { + std::stringstream st; + st << std::this_thread::get_id(); + + _stopMountFunc(); + + logInfo(std::format("Delete MccGenericMountNetworkServer class instance (thread ID = {})", st.str())); + } protected: MccCoordinateSerializer::SerializedCoordFormat _coordFormat{ MccCoordinateSerializer::SerializedCoordFormat::CFMT_SGM}; MccCoordinateSerializer::SexagesimalCoordPrec _coordPrec{2, 1}; + std::function _stopMountFunc{}; template RESULT_MSG_T handleMessage(const INPUT_MSG_T& input_msg, MountT* mount_ptr)