Add a separate client binary, with some common code to the server.

The cargo workspace thing was a PITA to figure out, but it seems to be working.  Could maybe be simplified?
This commit is contained in:
Barry Pederson
2024-11-02 15:58:35 -07:00
parent 46ad77f19e
commit 076e48f457
16 changed files with 380 additions and 255 deletions
+34
View File
@@ -0,0 +1,34 @@
use crate::handler::FileHandler;
use common::{Request, RESPONSE_NO, RESPONSE_OK};
use std::io::{Error, Write};
use std::os::unix::net::UnixStream;
pub struct Server {
stream: UnixStream,
handler: FileHandler,
}
impl Server {
pub fn new(stream: UnixStream, handler: FileHandler) -> Result<Server, Error> {
Ok(Server { stream, handler })
}
fn communicate(&mut self) -> Result<(), Error> {
let request = Request::from_stream(&mut self.stream)?;
println!("userid: {}", request.userid);
if self.handler.check_auth(&request) {
self.stream.write_all(&RESPONSE_OK)?
} else {
self.stream.write_all(&RESPONSE_NO)?
}
Ok(())
}
pub fn handle_client(&mut self) {
if let Err(error) = self.communicate() {
eprint!("{error}");
}
}
}