Rename server.rs to handler.rs for clarity, since this is responsible for handling one interaction with a client
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user