aquarium_control/watchmen/
watchdog_channels.rs

1/* Copyright 2025 Uwe Martin
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8*/
9use crate::launch::channels::{AquaChannelError, AquaReceiver, AquaSender};
10use crate::utilities::channel_content::InternalCommand;
11use std::fmt;
12
13pub struct WatchDogChannels {
14    pub tx_watchdog_to_signal_handler: AquaSender<bool>,
15
16    pub rx_watchdog_from_signal_handler: AquaReceiver<InternalCommand>,
17
18    pub rx_watchdog_from_messaging_opt: Option<AquaReceiver<InternalCommand>>,
19}
20
21impl WatchDogChannels {
22    /// Sends an acknowledgment to the signal handler.
23    ///
24    /// This wrapper function sends a boolean acknowledgment and increments the
25    /// associated debug counter if the `debug_channels` feature is enabled and
26    /// the send operation is successful.
27    #[allow(unused)]
28    pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
29        self.tx_watchdog_to_signal_handler.send(ack)
30    }
31
32    /// Receives a command from the signal handler (non-blocking).
33    ///
34    /// This wrapper function performs a non-blocking receive-operation for a command and increments
35    /// the associated debug counter if the `debug_channels` feature is enabled and a
36    /// message is successfully received.
37    #[allow(unused)]
38    pub fn receive_from_signal_handler(&mut self) -> Result<InternalCommand, AquaChannelError> {
39        self.rx_watchdog_from_signal_handler.try_recv()
40    }
41
42    /// Receives a command from the messaging module, if the channel exists (non-blocking).
43    ///
44    /// This wrapper function performs a non-blocking receive-operation and increments the
45    /// associated debug counter if the `debug_channels` feature is enabled and a
46    /// message is successfully received.
47    #[allow(unused)]
48    pub fn receive_from_messaging(&mut self) -> Option<Result<InternalCommand, AquaChannelError>> {
49        self.rx_watchdog_from_messaging_opt
50            .as_mut()
51            .map(|mut rx| rx.try_recv())
52    }
53}
54
55#[cfg(feature = "debug_channels")]
56impl fmt::Display for WatchDogChannels {
57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58        writeln!(f, "=== WatchDogChannels ===")?;
59        writeln!(
60            f,
61            "tx_watchdog_to_signal_handler: {}",
62            self.tx_watchdog_to_signal_handler.count
63        )?;
64        writeln!(
65            f,
66            "rx_watchdog_from_signal_handler: {}",
67            self.rx_watchdog_from_signal_handler.count
68        )?;
69        if self.rx_watchdog_from_messaging_opt.is_some() {
70            write!(
71                f,
72                "rx_watchdog_from_messaging_opt: {}",
73                self.rx_watchdog_from_messaging_opt.as_ref().unwrap().count
74            )?;
75        }
76        Ok(())
77    }
78}
79
80#[cfg(not(feature = "debug_channels"))]
81impl fmt::Display for WatchDogChannels {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        write!(
84            f,
85            "Channel counters are not active. Use --features \"debug_channels\" to enable them."
86        )
87    }
88}