diff --git a/candid_server/src/can_reader.rs b/candid_server/src/can_reader.rs index a084b46..46a911a 100644 --- a/candid_server/src/can_reader.rs +++ b/candid_server/src/can_reader.rs @@ -3,12 +3,14 @@ use std::thread; use socketcan::{CANFrame, CANSocket}; +/// Reads from the opened CAN socket and relays each frame to each stored client pub struct CANReader { can_socket: Arc>, clients: Arc>>>, } impl CANReader { + /// Creates a new `CANReader` from the given socket. Spawns one thread for reading frames pub fn new(socket: CANSocket) -> (CANReader, thread::JoinHandle<()>) { let reader = CANReader { can_socket: Arc::new(Mutex::new(socket)), @@ -42,10 +44,12 @@ impl CANReader { (reader, handle) } + /// Registers a client with this `CANReader` pub fn add_client(&self, channel: mpsc::Sender) { &self.clients.lock().unwrap().push(channel); } + /// Returns a copy of the underlying `CANsocket` pub fn get_socket(&self) -> Arc> { Arc::clone(&self.can_socket) } diff --git a/candid_server/src/client_handler.rs b/candid_server/src/client_handler.rs index b364a9b..3e4abd3 100644 --- a/candid_server/src/client_handler.rs +++ b/candid_server/src/client_handler.rs @@ -7,6 +7,8 @@ use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt}; use socketcan::{CANFrame, CANSocket}; +/// Spawns two threads: one to read incoming frames from the client (and write them to the +/// CANSocket), and one to read incoming frames from the `CANReader` (and write them to the client) pub fn new_client_handler( mut connection: TcpStream, can_interface: String,