Rename server.rs to handler.rs for clarity, since this is responsible for handling one interaction with a client

This commit is contained in:
Barry Pederson
2024-11-03 10:31:31 -08:00
parent 1a66c8ede5
commit 7cd2ba3364
3 changed files with 7 additions and 7 deletions
+34
View File
@@ -0,0 +1,34 @@
use crate::repository::PasswordDirectory;
use common::{Request, RESPONSE_NO, RESPONSE_OK};
use std::io::{Error, Write};
use std::os::unix::net::UnixStream;
pub struct Handler {
stream: UnixStream,
handler: PasswordDirectory,
}
impl Handler {
pub fn new(stream: UnixStream, handler: PasswordDirectory) -> Result<Handler, Error> {
Ok(Handler { 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}");
}
}
}