diff --git a/candid_client/Cargo.toml b/candid_client/Cargo.toml index 4683936..133eed6 100644 --- a/candid_client/Cargo.toml +++ b/candid_client/Cargo.toml @@ -12,3 +12,4 @@ keywords = ["CAN", "SocketCAN"] [dev-dependencies] clap = "^2.32" +rand = "*" diff --git a/candid_client/examples/send.rs b/candid_client/examples/send.rs new file mode 100644 index 0000000..7bc93bf --- /dev/null +++ b/candid_client/examples/send.rs @@ -0,0 +1,35 @@ +use candid_client::*; + +use rand::prelude::*; + +use clap::{App, Arg}; + +fn main() { + let matches = App::new("CANdid Client") + .version("0.1.0") + .author("Alex van de Sandt ") + .about("A client to send messages to a CANdid server") + .arg( + Arg::with_name("addr") + .value_name("ADDRESS") + .help("The address:port to connect to") + .takes_value(true) + .required(true), + ) + .get_matches(); + + let addr = matches.value_of("addr").unwrap(); + + let mut client = CandidConnection::new(addr).expect("Couldn't connect to server"); + + for i in 1..10 { + let data = random::().to_ne_bytes(); + println!("Sending {}, {:?}", i as u32, data); + client.write_frame(i, data).unwrap(); + } + + for line in client.lines() { + let line = line.unwrap(); + println!("Received: \"{}\"", line); + } +} diff --git a/candid_client/src/candid_connection.rs b/candid_client/src/candid_connection.rs index a40d2c6..22b8d01 100644 --- a/candid_client/src/candid_connection.rs +++ b/candid_client/src/candid_connection.rs @@ -1,9 +1,10 @@ use std::io::prelude::*; -use std::io::{BufReader, Lines}; +use std::io::{BufReader, BufWriter, Lines}; use std::net::{TcpStream, ToSocketAddrs}; pub struct CandidConnection { - stream: BufReader, + read_stream: BufReader, + write_stream: BufWriter, } /// A connection to a CANdid server. Simply contains a `TcpStream` wrapped by a @@ -15,31 +16,38 @@ impl CandidConnection { let stream = TcpStream::connect(addr)?; Ok(CandidConnection { - stream: BufReader::new(stream), + read_stream: BufReader::new(stream.try_clone().unwrap()), + write_stream: BufWriter::new(stream.try_clone().unwrap()), }) } /// Reads a single CAN frame sent by the server. pub fn read_frame(&mut self) -> Result { let mut buffer = String::new(); - self.stream.read_line(&mut buffer)?; + self.read_stream.read_line(&mut buffer)?; Ok(buffer) } + pub fn write_frame(&mut self, id: u32, data: [u8; 8]) -> Result<(), std::io::Error> { + self.write_stream.write(&id.to_be_bytes())?; + self.write_stream.write(&data)?; + Ok(()) + } + /// Unwraps the internal `BufReader` and returns the raw `TcpStream`. /// /// Note that any leftover data in the buffer will be lost. pub fn into_raw_stream(self) -> TcpStream { - self.stream.into_inner() + self.read_stream.into_inner() } /// Unwraps this `CandidConnection` and returns the underlying `BufReader`. pub fn into_buffer(self) -> BufReader { - self.stream + self.read_stream } /// Returns an iterator over the raw lines form the underlying `TcpStream`. pub fn lines(self) -> Lines> { - self.stream.lines() + self.read_stream.lines() } }