anachro_client/client_io.rs
1//! The Client Io Interface
2//!
3//! This module defines the `ClientIo` trait, which is used for describing
4//! how to send and receive messages "over the wire". The Anachro Protocol
5//! is transport-agnostic, which means you could send messages:
6//!
7//! * Over a packet radio, using the framing provided by the radio
8//! * Over a serial port, using COBS for message framing
9//! * Over a TCP port, using COBS for message framing
10//! * Over a UDP port, using UDP frames
11//! * Over a shared memory interface, using some other framing mechanism
12//! * Literally any other way you can think of to shuffle bytes around
13//!
14//! Implementors of this trait are responsible for serializing the data
15//! appropriately, and potentially buffering multiple packets to be sent
16//! when necessary. Implementors may choose to immediately send and receive,
17//! Or to enqueue/dequeue messages upon request.
18
19use anachro_icd::{arbitrator::Arbitrator, component::Component};
20
21/// The Error type of the ClientIo interface
22#[derive(Debug, PartialEq, Eq)]
23pub enum ClientIoError {
24 /// The ClientIo implementor failed to deserialize an incoming message
25 ParsingError,
26
27 /// The ClientIo implementor does not have any data to give at the moment
28 ///
29 /// TODO: This should probably be removed and we should just return `Ok(None)`
30 NoData,
31
32 /// The ClientIo is unable to send a packet, as the interface is full/busy
33 OutputFull,
34}
35
36/// A trait for defining the IO layer for a given client
37pub trait ClientIo {
38 /// Attempt to receive one message FROM the Arbitrator/Broker, TO the Client
39 fn recv(&mut self) -> Result<Option<Arbitrator>, ClientIoError>;
40
41 /// Attempt to send one message TO the Arbitrator/Broker, FROM the Client
42 fn send(&mut self, msg: &Component) -> Result<(), ClientIoError>;
43}