aquarium_control/watchmen/petting.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/// Provides the concrete implementation for "petting" a system watchdog.
10///
11/// This struct doesn't hold any state; its purpose is to implement the `PettingTrait`
12/// for writing heartbeat messages to a watchdog file.
13pub struct Petting {}
14
15impl Petting {
16 /// Creates a new `Petting` instance.
17 ///
18 /// This constructor initializes the watchdog petting mechanism.
19 /// As the `Petting` struct is stateless, it simply returns an empty instance.
20 ///
21 /// # Returns
22 /// A new `Petting` struct.
23 pub fn new() -> Petting {
24 Petting {}
25 }
26}
27
28/// Defines the interface for interacting with a system watchdog.
29///
30/// This trait provides a method for sending a "heartbeat" signal,
31/// typically by writing to a specific file, to inform a watchdog
32/// that the application is still alive and functioning.
33pub trait PettingTrait {
34 /// Sends a heartbeat message to the system watchdog.
35 ///
36 /// This method's implementation is responsible for performing the
37 /// actual "petting" action, usually by writing a predefined string
38 /// to a designated watchdog file.
39 ///
40 /// # Arguments
41 /// * `filename` - The path to the watchdog file or device.
42 /// * `watchdog_heartbeat` - The specific string message representing the heartbeat.
43 fn pet(&mut self, filename: &str, watchdog_heartbeat: &str);
44}
45
46impl PettingTrait for Petting {
47 /// Writes the heartbeat message to the specified watchdog file.
48 ///
49 /// This concrete implementation of `pet` uses `std::fs::write` to write the
50 /// `watchdog_heartbeat` string to the `watchdog_filename`. The result of the
51 /// file write operation is intentionally ignored, as the system watchdog
52 /// typically performs its own checks to determine if the heartbeat was received.
53 ///
54 /// # Arguments
55 /// * `watchdog_filename` - The path to the watchdog file.
56 /// * `watchdog_heartbeat` - The string message to write as the heartbeat.
57 fn pet(&mut self, watchdog_filename: &str, watchdog_heartbeat: &str) {
58 let _ = std::fs::write(watchdog_filename, watchdog_heartbeat);
59 }
60}