redis_imitate/network/
server.rs1use crate::config::config::Config;
6use crate::network::connection::Connection;
7use crate::commands::executor::CommandExecutor;
8use crate::storage::memory::MemoryStorage;
9
10use std::net::{TcpListener, TcpStream};
11use std::io;
12use threadpool::ThreadPool;
13use std::sync::{Arc, Mutex};
14
15pub struct Server {
16 pub config: Arc<Config>,
17 thread_pool: ThreadPool,
18 storage: Arc<Mutex<MemoryStorage>>,
19}
20
21impl Server {
29
30 pub fn new(config: Config) -> Self {
32 let config = Arc::new(config);
33 let thread_pool = ThreadPool::new(config.max_connections);
34 let storage = Arc::new(Mutex::new(MemoryStorage::new()));
35 Server { config, thread_pool, storage }
36 }
37
38 pub fn run(&self) -> io::Result<()> {
45 let address = format!("{}:{}", self.config.host, self.config.port);
46 let listener = TcpListener::bind(&address)?;
47 println!("Server is running on {}", address);
48
49 for stream in listener.incoming() {
50 match stream {
51 Ok(stream) => {
52 let storage = Arc::clone(&self.storage);
53 self.thread_pool.execute(move || {
54 let executor = Arc::new(CommandExecutor::new(storage));
55 if let Err(e) = handle_client(stream, executor) {
56 eprintln!("Error handling client: {}", e);
57 }
58 });
59 }
60 Err(e) => eprintln!("Connection failed: {}", e),
61 }
62 }
63
64 Ok(())
65 }
66}
67
68fn handle_client(stream: TcpStream, executor: Arc<CommandExecutor>) -> io::Result<()> {
70 let mut connection = Connection::new(stream, executor);
71 connection.process()
72}