aquarium_control/mineral/
balling_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*/
9
10//! A central container for all inter-thread communication channels used by the `Balling` module.
11//!
12//! This module defines the `BallingChannels` struct, which acts as a dedicated "switchboard"
13//! for the `Balling` dosing thread. Its primary purpose is to aggregate all the `mpsc::channel`
14//! senders and receivers that the `Balling` thread needs to communicate with other concurrent
15//! parts of the application, such as the `RelayManager`, `SignalHandler`, `ScheduleCheck`,
16//! and the IPC `Messaging` thread.
17//!
18//! ## Key Components
19//!
20//! - **`BallingChannels` Struct**: A simple container struct that explicitly declares all the
21//!   communication dependencies of the `Balling` thread.
22//!
23//! - **Helper Methods**: Provides convenient wrapper methods like `send_to_relay_manager()`
24//!   and `receive_from_schedule_check()`. These methods offer a clean, consistent API for
25//!   channel operations and abstract away the direct use of the sender/receiver fields.
26//!
27//! ## Design and Architecture
28//!
29//! The `BallingChannels` struct is a key part of the application's inter-thread
30//! communication strategy, promoting clean and decoupled code.
31//!
32//! - **Centralization**: By gathering all necessary channels into a single struct, it
33//!   provides a clear and explicit declaration of the `Balling` thread's communication
34//!   dependencies. This makes the overall architecture easier to reason about.
35//!
36//! - **Decoupling**: The main `Balling` thread logic doesn't need to manage a list of
37//!   individual channels. It just needs this struct, which it can use to send and
38//!   receive messages. This separates the core control logic from the communication
39//!   infrastructure.
40//!
41//! - **Testability**: The struct's channels can be used by mock implementation during
42//!   unit testing, allowing for isolated testing of the `Feed` thread's logic without
43//!   needing to run the entire application.
44
45use crate::launch::channels::{AquaChannelError, AquaReceiver, AquaSender};
46use crate::utilities::channel_content::InternalCommand;
47use std::fmt;
48
49/// Container for the channels used by Balling for inter-thread communication
50pub struct BallingChannels {
51    /// sender part of the channel for communication to relay manager
52    pub tx_balling_to_relay_manager: AquaSender<InternalCommand>,
53
54    /// receiver part of the channel for communication from the relay manager
55    pub rx_balling_from_relay_manager: AquaReceiver<bool>,
56
57    /// sender part of the channel for communication to the signal handler
58    pub tx_balling_to_signal_handler: AquaSender<bool>,
59
60    /// receiver part of the channel for communication from the signal handler
61    pub rx_balling_from_signal_handler: AquaReceiver<InternalCommand>,
62
63    /// sender part of the channel for communication to the schedule checker
64    pub tx_balling_to_schedule_check: AquaSender<InternalCommand>,
65
66    /// receiver part of the channel for communication from the schedule checker
67    pub rx_balling_from_schedule_check: AquaReceiver<bool>,
68
69    /// receiver part of the channel communication wrapped in Option from Messaging
70    pub rx_balling_from_messaging_opt: Option<AquaReceiver<InternalCommand>>,
71}
72
73impl BallingChannels {
74    /// Sends a command to the relay manager.
75    pub fn send_to_relay_manager(
76        &mut self,
77        command: InternalCommand,
78    ) -> Result<(), AquaChannelError> {
79        self.tx_balling_to_relay_manager.send(command)
80    }
81
82    /// Receives a response from the relay manager.
83    pub fn receive_from_relay_manager(&mut self) -> Result<bool, AquaChannelError> {
84        self.rx_balling_from_relay_manager.recv()
85    }
86
87    /// Sends an acknowledgment to the signal handler.
88    pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
89        self.tx_balling_to_signal_handler.send(ack)
90    }
91
92    /// Sends a command to the schedule checker.
93    pub fn send_to_schedule_check(
94        &mut self,
95        command: InternalCommand,
96    ) -> Result<(), AquaChannelError> {
97        self.tx_balling_to_schedule_check.send(command)
98    }
99
100    /// Receives a response from the schedule checker.
101    pub fn receive_from_schedule_check(&mut self) -> Result<bool, AquaChannelError> {
102        self.rx_balling_from_schedule_check.recv()
103    }
104}
105
106#[cfg(feature = "debug_channels")]
107impl fmt::Display for BallingChannels {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        writeln!(f, "=== BallingChannels ===")?;
110        writeln!(
111            f,
112            "tx_balling_to_relay_manager: {}",
113            self.tx_balling_to_relay_manager.count
114        )?;
115        writeln!(
116            f,
117            "rx_balling_from_relay_manager: {}",
118            self.rx_balling_from_relay_manager.count
119        )?;
120        writeln!(
121            f,
122            "tx_balling_to_signal_handler: {}",
123            self.tx_balling_to_signal_handler.count
124        )?;
125        writeln!(
126            f,
127            "rx_balling_from_signal_handler: {}",
128            self.rx_balling_from_signal_handler.count
129        )?;
130        writeln!(
131            f,
132            "tx_balling_to_schedule_check: {}",
133            self.tx_balling_to_schedule_check.count
134        )?;
135        writeln!(
136            f,
137            "rx_balling_from_schedule_check: {}",
138            self.rx_balling_from_schedule_check.count
139        )?;
140        if let Some(rx_balling_from_messaging) = self.rx_balling_from_messaging_opt {
141            write!(
142                f,
143                "rx_balling_from_messaging_opt: {}",
144                rx_balling_from_messaging.count
145            )?;
146        }
147        Ok(())
148    }
149}
150
151#[cfg(not(feature = "debug_channels"))]
152impl fmt::Display for BallingChannels {
153    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154        write!(
155            f,
156            "Channel counters are not active. Use --features \"debug_channels\" to enable them."
157        )
158    }
159}