use std::io::Error;
use crate::communication::client_handler::{ClientHandler, ClientHandlerImpl};
use crate::method::method_manager::MethodManager;
use crate::net::tcp_server::TcpServer;
pub struct CommunicationManagerImpl {
tcp_server: Box<dyn TcpServer>,
method_manager: Box<dyn MethodManager>,
}
pub trait CommunicationManager {
fn run(&mut self);
fn handle_client(&self) -> Result<(), Error>;
}
impl CommunicationManagerImpl {
pub fn new(tcp_server: Box<dyn TcpServer>, method_manager: Box<dyn MethodManager>) -> Self {
Self {
tcp_server,
method_manager,
}
}
}
impl CommunicationManager for CommunicationManagerImpl {
fn run(&mut self) {
self.tcp_server.bind();
}
fn handle_client(&self) -> Result<(), Error> {
let reader_writer = self.tcp_server.get_tcp_stream()?;
let mut handler = ClientHandlerImpl::new(
reader_writer.0,
reader_writer.1,
&self.method_manager,
);
loop {
if handler.handle().is_err() {
println!("Client Connection end.");
break;
}
}
Ok(())
}
}
unsafe impl Send for CommunicationManagerImpl {}
unsafe impl Sync for CommunicationManagerImpl {}