use std::{
io::{self, Write},
os::unix::net::UnixStream,
};
use crate::{error::OhNo, protocol::request::Request, server::resources::Paths};
#[derive(Debug)]
pub struct Client {
stream: UnixStream,
}
impl Client {
pub fn connect(paths: &Paths) -> Result<Self, OhNo> {
let stream = UnixStream::connect(paths.socket_path())
.map_err(|err| OhNo::CannotConnectToServer { err })?;
log::debug!("connected to KTS");
Ok(Self { stream })
}
pub fn init_session(paths: &Paths, session: impl Into<String>) -> Result<(), OhNo> {
let mut client = Self::connect(paths)?;
client.send(&Request::init_session(session))
}
pub fn send(&mut self, req: &Request) -> Result<(), OhNo> {
let bytes = serde_json::to_string(req).map_err(|err| OhNo::CannotSendRequest {
err: err.to_string(),
})?;
loop {
let r = self.stream.write_all(bytes.as_bytes());
match r {
Err(err) => match err.kind() {
io::ErrorKind::Interrupted => continue,
_ => {
return Err(OhNo::CannotSendRequest {
err: err.to_string(),
});
}
},
_ => return Ok(()),
}
}
}
}