@ -19,6 +19,7 @@ pub enum ClientError {
}
}
pub struct Client {
pub struct Client {
filename : String ,
socket : UnixStream ,
socket : UnixStream ,
// If set, the client will print to stdout the messages sent and
// If set, the client will print to stdout the messages sent and
@ -30,12 +31,25 @@ pub struct Client {
impl Client {
impl Client {
pub fn connect < T : AsRef < str > > ( filename : T , verbose : bool ) -> Result < Self , ClientError > {
pub fn connect < T : AsRef < str > > ( filename : T , verbose : bool ) -> Result < Self , ClientError > {
let filename = filename . as_ref ( ) . to_string ( ) ;
let filename = filename . as_ref ( ) . to_string ( ) ;
let socket = UnixStream ::connect ( filename ) ? ;
let socket = UnixStream ::connect ( & filename ) ? ;
let mut client = Self { socket , verbose } ;
let mut client = Self {
filename ,
socket ,
verbose ,
} ;
client . handshake ( ) ? ;
client . handshake ( ) ? ;
Ok ( client )
Ok ( client )
}
}
pub fn reconnect ( & mut self ) -> Result < ( ) , ClientError > {
if self . verbose {
println! ( "Reconnecting to socket: {}" , self . filename ) ;
}
self . socket = UnixStream ::connect ( & self . filename ) ? ;
self . handshake ( ) ? ;
Ok ( ( ) )
}
fn handshake ( & mut self ) -> Result < ( ) , ClientError > {
fn handshake ( & mut self ) -> Result < ( ) , ClientError > {
self . send ( & json ! ( { "version" : "0.2" } ) ) ? ;
self . send ( & json ! ( { "version" : "0.2" } ) ) ? ;
self . read ( ) . map ( serde_json ::from_value ::< Response > ) ? ? ;
self . read ( ) . map ( serde_json ::from_value ::< Response > ) ? ? ;