Skip to main content

plato_mud/transport/
memory.rs

1//! PLATO MUD Engine — Shared Memory Transport
2//!
3//! Multi-process on same host. In-process channel for now.
4
5extern crate alloc;
6
7use alloc::collections::VecDeque;
8use alloc::string::String;
9
10use crate::transport::{Result, Transport, TransportError};
11use crate::types::{FluxTransference, TransportConfig};
12
13/// Shared memory transport — uses an in-process queue
14pub struct MemoryTransport {
15    connected: bool,
16    inbox: VecDeque<FluxTransference>,
17    // In a real implementation, this would use shared memory segments
18    // or Unix domain sockets for multi-process communication
19}
20
21impl Default for MemoryTransport {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl MemoryTransport {
28    pub fn new() -> Self {
29        Self {
30            connected: false,
31            inbox: VecDeque::new(),
32        }
33    }
34
35    /// Inject a flux into the inbox (simulating reception from another process)
36    pub fn inject(&mut self, flux: FluxTransference) {
37        self.inbox.push_back(flux);
38    }
39}
40
41impl Transport for MemoryTransport {
42    fn connect(&mut self, _config: &TransportConfig) -> Result<()> {
43        self.connected = true;
44        Ok(())
45    }
46
47    fn send_flux(&mut self, _flux: &FluxTransference) -> Result<()> {
48        if !self.connected {
49            return Err(TransportError::NotConnected);
50        }
51        // In shared memory, "send" means write to shared segment
52        // For now, we just acknowledge the send
53        Ok(())
54    }
55
56    fn recv_flux(&mut self) -> Result<FluxTransference> {
57        if !self.connected {
58            return Err(TransportError::NotConnected);
59        }
60        self.inbox
61            .pop_front()
62            .ok_or(TransportError::ReceiveFailed(String::from(
63                "No pending flux",
64            )))
65    }
66
67    fn disconnect(&mut self) -> Result<()> {
68        self.connected = false;
69        self.inbox.clear();
70        Ok(())
71    }
72
73    fn is_connected(&self) -> bool {
74        self.connected
75    }
76}
77
78// ─── Stub transports for std feature ────────────────────────────────────────
79// TCP, WebSocket, MQTT, Serial, CAN adapters are stub implementations
80// that compile under the std feature but don't require actual hardware.
81
82#[cfg(feature = "std")]
83pub mod tcp {
84    use crate::transport::{Result, Transport, TransportError};
85    use crate::types::{FluxTransference, TransportConfig};
86
87    /// TCP transport adapter (enterprise, LAN)
88    pub struct TcpTransport {
89        connected: bool,
90        address: String,
91        port: u16,
92    }
93
94    impl Default for TcpTransport {
95        fn default() -> Self {
96            Self::new()
97        }
98    }
99
100    impl TcpTransport {
101        pub fn new() -> Self {
102            Self {
103                connected: false,
104                address: String::new(),
105                port: 0,
106            }
107        }
108    }
109
110    impl Transport for TcpTransport {
111        fn connect(&mut self, config: &TransportConfig) -> Result<()> {
112            self.address = config.address.clone();
113            self.port = config.port;
114            // In a real implementation: TcpStream::connect()
115            self.connected = true;
116            Ok(())
117        }
118
119        fn send_flux(&mut self, _flux: &FluxTransference) -> Result<()> {
120            if !self.connected {
121                return Err(TransportError::NotConnected);
122            }
123            // In a real implementation: serialize and write to TcpStream
124            Ok(())
125        }
126
127        fn recv_flux(&mut self) -> Result<FluxTransference> {
128            if !self.connected {
129                return Err(TransportError::NotConnected);
130            }
131            Err(TransportError::ReceiveFailed(
132                "TCP recv not yet implemented".into(),
133            ))
134        }
135
136        fn disconnect(&mut self) -> Result<()> {
137            self.connected = false;
138            Ok(())
139        }
140
141        fn is_connected(&self) -> bool {
142            self.connected
143        }
144    }
145}
146
147#[cfg(feature = "std")]
148pub mod websocket {
149    use crate::transport::{Result, Transport, TransportError};
150    use crate::types::{FluxTransference, TransportConfig};
151
152    /// WebSocket transport adapter (browser dashboard)
153    pub struct WebSocketTransport {
154        connected: bool,
155    }
156
157    impl Default for WebSocketTransport {
158        fn default() -> Self {
159            Self::new()
160        }
161    }
162
163    impl WebSocketTransport {
164        pub fn new() -> Self {
165            Self { connected: false }
166        }
167    }
168
169    impl Transport for WebSocketTransport {
170        fn connect(&mut self, _config: &TransportConfig) -> Result<()> {
171            self.connected = true;
172            Ok(())
173        }
174        fn send_flux(&mut self, _flux: &FluxTransference) -> Result<()> {
175            if !self.connected {
176                return Err(TransportError::NotConnected);
177            }
178            Ok(())
179        }
180        fn recv_flux(&mut self) -> Result<FluxTransference> {
181            if !self.connected {
182                return Err(TransportError::NotConnected);
183            }
184            Err(TransportError::ReceiveFailed(
185                "WebSocket recv not yet implemented".into(),
186            ))
187        }
188        fn disconnect(&mut self) -> Result<()> {
189            self.connected = false;
190            Ok(())
191        }
192        fn is_connected(&self) -> bool {
193            self.connected
194        }
195    }
196}
197
198#[cfg(feature = "std")]
199pub mod mqtt {
200    use crate::transport::{Result, Transport, TransportError};
201    use crate::types::{FluxTransference, TransportConfig};
202
203    /// MQTT transport adapter (IoT, cloud)
204    pub struct MqttTransport {
205        connected: bool,
206        topic: String,
207    }
208
209    impl Default for MqttTransport {
210        fn default() -> Self {
211            Self::new()
212        }
213    }
214
215    impl MqttTransport {
216        pub fn new() -> Self {
217            Self {
218                connected: false,
219                topic: String::new(),
220            }
221        }
222    }
223
224    impl Transport for MqttTransport {
225        fn connect(&mut self, config: &TransportConfig) -> Result<()> {
226            self.topic = config.options.get("topic").cloned().unwrap_or_default();
227            self.connected = true;
228            Ok(())
229        }
230        fn send_flux(&mut self, _flux: &FluxTransference) -> Result<()> {
231            if !self.connected {
232                return Err(TransportError::NotConnected);
233            }
234            Ok(())
235        }
236        fn recv_flux(&mut self) -> Result<FluxTransference> {
237            if !self.connected {
238                return Err(TransportError::NotConnected);
239            }
240            Err(TransportError::ReceiveFailed(
241                "MQTT recv not yet implemented".into(),
242            ))
243        }
244        fn disconnect(&mut self) -> Result<()> {
245            self.connected = false;
246            Ok(())
247        }
248        fn is_connected(&self) -> bool {
249            self.connected
250        }
251    }
252}
253
254#[cfg(feature = "std")]
255pub mod serial {
256    use crate::transport::{Result, Transport, TransportError};
257    use crate::types::{FluxTransference, TransportConfig};
258
259    /// Serial/UART transport adapter (embedded, debug)
260    pub struct SerialTransport {
261        connected: bool,
262        port_name: String,
263        baud_rate: u32,
264    }
265
266    impl Default for SerialTransport {
267        fn default() -> Self {
268            Self::new()
269        }
270    }
271
272    impl SerialTransport {
273        pub fn new() -> Self {
274            Self {
275                connected: false,
276                port_name: String::new(),
277                baud_rate: 115200,
278            }
279        }
280    }
281
282    impl Transport for SerialTransport {
283        fn connect(&mut self, config: &TransportConfig) -> Result<()> {
284            self.port_name = config.address.clone();
285            self.baud_rate = config
286                .options
287                .get("baud")
288                .and_then(|b| b.parse().ok())
289                .unwrap_or(115200);
290            self.connected = true;
291            Ok(())
292        }
293        fn send_flux(&mut self, _flux: &FluxTransference) -> Result<()> {
294            if !self.connected {
295                return Err(TransportError::NotConnected);
296            }
297            Ok(())
298        }
299        fn recv_flux(&mut self) -> Result<FluxTransference> {
300            if !self.connected {
301                return Err(TransportError::NotConnected);
302            }
303            Err(TransportError::ReceiveFailed(
304                "Serial recv not yet implemented".into(),
305            ))
306        }
307        fn disconnect(&mut self) -> Result<()> {
308            self.connected = false;
309            Ok(())
310        }
311        fn is_connected(&self) -> bool {
312            self.connected
313        }
314    }
315}
316
317#[cfg(feature = "std")]
318pub mod can_bus {
319    use crate::transport::{Result, Transport, TransportError};
320    use crate::types::{FluxTransference, TransportConfig};
321
322    /// CAN bus transport adapter (automotive, industrial)
323    pub struct CanTransport {
324        connected: bool,
325        interface: String,
326    }
327
328    impl Default for CanTransport {
329        fn default() -> Self {
330            Self::new()
331        }
332    }
333
334    impl CanTransport {
335        pub fn new() -> Self {
336            Self {
337                connected: false,
338                interface: String::new(),
339            }
340        }
341    }
342
343    impl Transport for CanTransport {
344        fn connect(&mut self, config: &TransportConfig) -> Result<()> {
345            self.interface = config.address.clone();
346            self.connected = true;
347            Ok(())
348        }
349        fn send_flux(&mut self, _flux: &FluxTransference) -> Result<()> {
350            if !self.connected {
351                return Err(TransportError::NotConnected);
352            }
353            Ok(())
354        }
355        fn recv_flux(&mut self) -> Result<FluxTransference> {
356            if !self.connected {
357                return Err(TransportError::NotConnected);
358            }
359            Err(TransportError::ReceiveFailed(
360                "CAN recv not yet implemented".into(),
361            ))
362        }
363        fn disconnect(&mut self) -> Result<()> {
364            self.connected = false;
365            Ok(())
366        }
367        fn is_connected(&self) -> bool {
368            self.connected
369        }
370    }
371}