aquarium_control/food/feed_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 `Feed` module.
11//!
12//! This module defines the `FeedChannels` struct, which acts as a dedicated "switchboard"
13//! for the `Feed` thread. Its primary purpose is to aggregate all the `mpsc::channel`
14//! senders and receivers that the `Feed` thread needs to communicate with other concurrent
15//! parts of the application, such as the `RelayManager`, `SignalHandler`, and the IPC
16//! `Messaging` thread.
17//!
18//! ## Key Components
19//!
20//! - **`FeedChannels` Struct**: A simple container struct that explicitly declares all the
21//! communication dependencies of the `Feed` thread.
22//!
23//! - **Helper Methods**: Provides convenient wrapper methods like `send_to_relay_manager()`
24//! and `receive_from_relay_manager()`. 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 `FeedChannels` 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 `Feed` thread's communication
34//! dependencies. This makes the overall architecture easier to reason about.
35//!
36//! - **Decoupling**: The main `Feed` 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/// Struct collects the channels for communication between the feed control and the other components.
50pub struct FeedChannels {
51 /// Sender part of the channel for communication to relay manager
52 pub tx_feed_to_relay_manager: AquaSender<InternalCommand>,
53
54 /// Receiver part of the channel for communication from the relay manager
55 pub rx_feed_from_relay_manager: AquaReceiver<bool>,
56
57 /// Sender part of the channel for communication to the signal handler
58 pub tx_feed_to_signal_handler: AquaSender<bool>,
59
60 /// Receiver part of the channel for communication from the signal handler
61 pub rx_feed_from_signal_handler: AquaReceiver<InternalCommand>,
62
63 /// Receiver part of the channel wrapped in Option for communication from the messaging
64 pub rx_feed_from_messaging_opt: Option<AquaReceiver<InternalCommand>>,
65}
66
67impl FeedChannels {
68 /// Sends a command to the relay manager.
69 pub fn send_to_relay_manager(
70 &mut self,
71 command: InternalCommand,
72 ) -> Result<(), AquaChannelError> {
73 self.tx_feed_to_relay_manager.send(command)
74 }
75
76 /// Receives a response from the relay manager.
77 pub fn receive_from_relay_manager(&mut self) -> Result<bool, AquaChannelError> {
78 self.rx_feed_from_relay_manager.recv()
79 }
80
81 /// Sends an acknowledgment to the signal handler.
82 pub fn send_to_signal_handler(&mut self, ack: bool) -> Result<(), AquaChannelError> {
83 self.tx_feed_to_signal_handler.send(ack)
84 }
85}
86
87#[cfg(feature = "debug_channels")]
88impl fmt::Display for FeedChannels {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 writeln!(f, "=== FeedChannels ===")?;
91 writeln!(
92 f,
93 "tx_feed_to_relay_manager: {}",
94 self.tx_feed_to_relay_manager.count
95 )?;
96 writeln!(
97 f,
98 "rx_feed_from_relay_manager: {}",
99 self.rx_feed_from_relay_manager.count
100 )?;
101 writeln!(
102 f,
103 "tx_feed_to_signal_handler: {}",
104 self.tx_feed_to_signal_handler.count
105 )?;
106 writeln!(
107 f,
108 "rx_feed_from_signal_handler: {}",
109 self.rx_feed_from_signal_handler.count
110 )?;
111 if let Some(rx_feed_from_messaging) = self.rx_feed_from_messaging_opt {
112 write!(
113 f,
114 "rx_feed_from_messaging_opt: {}",
115 self.rx_feed_from_messaging.count
116 )?;
117 }
118 Ok(())
119 }
120}
121
122#[cfg(not(feature = "debug_channels"))]
123impl fmt::Display for FeedChannels {
124 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125 write!(
126 f,
127 "Channel counters are not active. Use --features \"debug_channels\" to enable them."
128 )
129 }
130}