diff --git a/candid_client/src/candid_connection.rs b/candid_client/src/candid_connection.rs new file mode 100644 index 0000000..fa5ef3f --- /dev/null +++ b/candid_client/src/candid_connection.rs @@ -0,0 +1,16 @@ +use std::io::BufReader; +use std::net::{TcpStream, ToSocketAddrs}; + +pub struct CandidConnection { + stream: BufReader, +} + +impl CandidConnection { + pub fn new(addr: A) -> Result { + let stream = TcpStream::connect(addr)?; + + Ok(CandidConnection { + stream: BufReader::new(stream), + }) + } +} diff --git a/candid_client/src/lib.rs b/candid_client/src/lib.rs new file mode 100644 index 0000000..e9ef90e --- /dev/null +++ b/candid_client/src/lib.rs @@ -0,0 +1 @@ +pub mod candid_connection; diff --git a/candid_client/src/main.rs b/candid_client/src/main.rs index c37239b..ef9e709 100644 --- a/candid_client/src/main.rs +++ b/candid_client/src/main.rs @@ -4,6 +4,8 @@ use std::net::TcpStream; use clap::{App, Arg}; +extern crate candid_client; + fn main() { let matches = App::new("CANdid Client") .version("0.1.0") -- 2.51.2 From ed023bf3ad65191e3a7f5cffbe6f8b331ca94d5a Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Sat, 2 Mar 2019 14:15:42 -0500 Subject: [PATCH 2/7] Add read_frame, into_raw_stream, and lines functions --- candid_client/src/candid_connection.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/candid_client/src/candid_connection.rs b/candid_client/src/candid_connection.rs index fa5ef3f..da35b47 100644 --- a/candid_client/src/candid_connection.rs +++ b/candid_client/src/candid_connection.rs @@ -1,4 +1,5 @@ -use std::io::BufReader; +use std::io::prelude::*; +use std::io::{BufReader, Lines}; use std::net::{TcpStream, ToSocketAddrs}; pub struct CandidConnection { @@ -13,4 +14,20 @@ impl CandidConnection { stream: BufReader::new(stream), }) } + + pub fn read_frame(&mut self) -> Result { + let mut buffer = String::new(); + + self.stream.read_line(&mut buffer)?; + + Ok(buffer) + } + + pub fn into_raw_stream(self) -> TcpStream { + self.stream.into_inner() + } + + pub fn lines(self) -> Lines> { + self.stream.lines() + } } -- 2.51.2 From 73124bccc1965bb4bdb5bfbc47174eb743617d25 Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Sat, 2 Mar 2019 15:18:32 -0500 Subject: [PATCH 3/7] Add documentation to functions --- candid_client/src/candid_connection.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/candid_client/src/candid_connection.rs b/candid_client/src/candid_connection.rs index da35b47..a40d2c6 100644 --- a/candid_client/src/candid_connection.rs +++ b/candid_client/src/candid_connection.rs @@ -6,7 +6,11 @@ pub struct CandidConnection { stream: BufReader, } +/// A connection to a CANdid server. Simply contains a `TcpStream` wrapped by a +/// `BufReader`. Handles the communication with a server by reading one can +/// frame at a time from a TCP stream. impl CandidConnection { + /// Creates a new CandidConnection with a server designated at `addr` pub fn new(addr: A) -> Result { let stream = TcpStream::connect(addr)?; @@ -15,18 +19,26 @@ impl CandidConnection { }) } + /// 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)?; - Ok(buffer) } + /// 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() } + /// Unwraps this `CandidConnection` and returns the underlying `BufReader`. + pub fn into_buffer(self) -> BufReader { + self.stream + } + + /// Returns an iterator over the raw lines form the underlying `TcpStream`. pub fn lines(self) -> Lines> { self.stream.lines() } -- 2.51.2 From 0cdd3b5405b9422bb8aec13a61629aac7640b755 Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Sat, 2 Mar 2019 15:45:03 -0500 Subject: [PATCH 4/7] Export CandidConnection to public API --- candid_client/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/candid_client/src/lib.rs b/candid_client/src/lib.rs index e9ef90e..a0887aa 100644 --- a/candid_client/src/lib.rs +++ b/candid_client/src/lib.rs @@ -1 +1,3 @@ pub mod candid_connection; + +pub use candid_connection::CandidConnection; -- 2.51.2 From 707d9fab8d9f08fb64a48e1fb22d7a7bfc3bfaa7 Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Sat, 2 Mar 2019 15:45:19 -0500 Subject: [PATCH 5/7] Use CandidConnection in main function --- candid_client/src/main.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/candid_client/src/main.rs b/candid_client/src/main.rs index ef9e709..33909b1 100644 --- a/candid_client/src/main.rs +++ b/candid_client/src/main.rs @@ -1,11 +1,7 @@ -use std::io::prelude::*; -use std::io::BufReader; -use std::net::TcpStream; +use candid_client::*; use clap::{App, Arg}; -extern crate candid_client; - fn main() { let matches = App::new("CANdid Client") .version("0.1.0") @@ -22,15 +18,10 @@ fn main() { let addr = matches.value_of("addr").unwrap(); - if let Ok(stream) = TcpStream::connect(addr) { - println!("Connected to the server!"); - let reader = BufReader::new(stream); + let client = CandidConnection::new(addr).expect("Couldn't connect to server"); - for line in reader.lines() { - let line = line.unwrap(); - println!("Received: \"{}\"", line); - } - } else { - println!("Couldn't connect to server..."); + for line in client.lines() { + let line = line.unwrap(); + println!("Received: \"{}\"", line); } } -- 2.51.2 From ba1dca7f39dd54dc9e37c18dc47dee9fa76e28e0 Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Sat, 2 Mar 2019 16:07:03 -0500 Subject: [PATCH 6/7] Move main to an example --- candid_client/{src/main.rs => examples/echo.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename candid_client/{src/main.rs => examples/echo.rs} (100%) diff --git a/candid_client/src/main.rs b/candid_client/examples/echo.rs similarity index 100% rename from candid_client/src/main.rs rename to candid_client/examples/echo.rs -- 2.51.2 From 389e2401c15951e2469af677279d36998a52a972 Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Sat, 2 Mar 2019 16:09:00 -0500 Subject: [PATCH 7/7] Move `clap` to a dev dependency It is now only needed for the `echo` example --- candid_client/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/candid_client/Cargo.toml b/candid_client/Cargo.toml index f10acf6..e677107 100644 --- a/candid_client/Cargo.toml +++ b/candid_client/Cargo.toml @@ -4,5 +4,5 @@ version = "0.1.0" authors = ["Alex van de Sandt "] edition = "2018" -[dependencies] +[dev-dependencies] clap = "^2.32"