aquarium_control/simulator/tcp_communication_error.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
10use crate::launch::channels::AquaChannelError;
11use crate::utilities::channel_content::AquariumSignal;
12use std::io;
13use std::num::ParseFloatError;
14use thiserror::Error;
15
16/// Contains the error definition for communication with the simulator via TCP
17#[derive(Debug, Error)]
18pub enum TcpCommunicationError {
19 /// value indicates that an error has occurred during TCP communication
20 #[allow(unused)]
21 #[error("[{location}] Failed to connect to simulator at {ip_address}:{port}")]
22 ConnectionFailed {
23 location: String,
24 ip_address: String,
25 port: u16,
26 #[source]
27 source: io::Error,
28 },
29
30 /// value indicates that an error has occurred during writing to a TCP stream
31 #[error("[{location}] Writing to stream failed.")]
32 WritingToStreamFailed {
33 location: String,
34 #[source]
35 source: io::Error,
36 },
37
38 /// value indicates that an error has occurred during flushing a TCP stream
39 #[error("[{location}] Flush failed.")]
40 FlushFailed {
41 location: String,
42
43 #[source]
44 source: io::Error,
45 },
46
47 /// value indicates that an error has occurred during reading from a TCP stream
48 #[error("[{location}] Reading from stream failed.")]
49 ReadingFromStreamFailed { location: String, source: io::Error },
50
51 /// value indicates that calling function requested an undefined signal
52 #[error("[{0}] Internal error - illegal signal request {1} to simulator")]
53 IllegalSignalRequestToSimulator(String, AquariumSignal),
54
55 /// value indicates that conversion of response from String to numeric failed
56 #[error("[{location}] Failed to convert response ({last_word}) to f32")]
57 ResponseConversionError {
58 location: String,
59 last_word: String,
60
61 #[source]
62 source: ParseFloatError,
63 },
64
65 /// value indicates that the last word of response is empty
66 #[error("[{0}] Last word of response is empty.")]
67 LastWordOfResponseEmpty(String),
68
69 /// value indicates that the response does not contain any words
70 #[error("[{0}] Response does not contain any words.")]
71 ResponseContainsNoWords(String),
72
73 /// value indicates that sending response value via the channel failed
74 #[error("[{0}] sending response value via channel failed.")]
75 SendingResponseViaChannelFailure(String),
76
77 /// value indicates that channel disconnected unexpectedly
78 #[error("[{0}] Error in trying to receive command from data logger")]
79 ChannelDisconnected(String),
80
81 /// value indicates that receiving from TCP thread failed
82 #[error("{location}: Error occurred when trying to receive response from TCP thread for {requester}.")]
83 ReceivingFromTCPThreadFailed {
84 location: String,
85 requester: String,
86
87 #[source]
88 source: AquaChannelError,
89 },
90
91 /// value indicates that communication to TCP thread failed
92 #[error(
93 "{location}: Error occurred when trying to send request to TCP thread for {requester}."
94 )]
95 SendingToTCPThreadFailed {
96 location: String,
97 requester: String,
98
99 #[source]
100 source: AquaChannelError,
101 },
102
103 /// The TCP stream could not be cloned, which is necessary for creating separate
104 /// read and write handles.
105 #[error("[{location}] Failed to clone the TCP stream.")]
106 StreamCloneFailed {
107 location: String,
108 #[source]
109 source: io::Error,
110 },
111}