pull file io out of server handler
authoralfadur
Sun, 09 Dec 2018 04:09:38 +0300
changeset 14397 e335b3120f59
parent 14396 42776184fc39
child 14398 c2cd3f64b9f7
pull file io out of server handler
gameServer2/src/server.rs
gameServer2/src/server/core.rs
gameServer2/src/server/handlers/inroom.rs
gameServer2/src/server/io.rs
gameServer2/src/server/network.rs
--- a/gameServer2/src/server.rs	Sun Dec 09 00:53:05 2018 +0100
+++ b/gameServer2/src/server.rs	Sun Dec 09 04:09:38 2018 +0300
@@ -1,5 +1,6 @@
 pub mod core;
 pub mod client;
+pub mod io;
 pub mod room;
 pub mod network;
 pub mod coretypes;
--- a/gameServer2/src/server/core.rs	Sun Dec 09 00:53:05 2018 +0100
+++ b/gameServer2/src/server/core.rs	Sun Dec 09 04:09:38 2018 +0300
@@ -1,6 +1,7 @@
 use slab;
 use crate::utils;
 use super::{
+    io::HWServerIO,
     client::HWClient, room::HWRoom, actions, handlers,
     coretypes::{ClientId, RoomId},
     actions::{Destination, PendingMessage}
@@ -12,24 +13,25 @@
 
 type Slab<T> = slab::Slab<T>;
 
-
 pub struct HWServer {
     pub clients: Slab<HWClient>,
     pub rooms: Slab<HWRoom>,
     pub lobby_id: RoomId,
     pub output: Vec<(Vec<ClientId>, HWServerMessage)>,
     pub removed_clients: Vec<ClientId>,
+    pub io: Box<dyn HWServerIO>
 }
 
 impl HWServer {
-    pub fn new(clients_limit: usize, rooms_limit: usize) -> HWServer {
+    pub fn new(clients_limit: usize, rooms_limit: usize, io: Box<dyn HWServerIO>) -> HWServer {
         let rooms = Slab::with_capacity(rooms_limit);
         let clients = Slab::with_capacity(clients_limit);
         let mut server = HWServer {
             clients, rooms,
             lobby_id: 0,
             output: vec![],
-            removed_clients: vec![]
+            removed_clients: vec![],
+            io
         };
         server.lobby_id = server.add_room();
         server
--- a/gameServer2/src/server/handlers/inroom.rs	Sun Dec 09 00:53:05 2018 +0100
+++ b/gameServer2/src/server/handlers/inroom.rs	Sun Dec 09 04:09:38 2018 +0300
@@ -18,8 +18,7 @@
     utils::is_name_illegal
 };
 use std::{
-    mem::swap, fs::{File, OpenOptions},
-    io::{Read, Write, Result, Error, ErrorKind}
+    mem::swap
 };
 use base64::{encode, decode};
 use super::common::rnd_reply;
@@ -101,18 +100,6 @@
     }
 }
 
-fn read_file(filename: &str) -> Result<String> {
-    let mut reader = File::open(filename)?;
-    let mut result = String::new();
-    reader.read_to_string(&mut result)?;
-    Ok(result)
-}
-
-fn write_file(filename: &str, content: &str) -> Result<()> {
-    let mut writer = OpenOptions::new().create(true).write(true).open(filename)?;
-    writer.write_all(content.as_bytes())
-}
-
 pub fn handle(server: &mut HWServer, client_id: ClientId, room_id: RoomId, message: HWProtocolMessage) {
     use crate::protocol::messages::HWProtocolMessage::*;
     match message {
@@ -315,7 +302,7 @@
         SaveRoom(filename) => {
             if server.clients[client_id].is_admin() {
                 let actions = match server.rooms[room_id].get_saves() {
-                    Ok(text) => match write_file(&filename, &text) {
+                    Ok(text) => match server.io.write_file(&filename, &text) {
                         Ok(_) => vec![server_chat("Room configs saved successfully.".to_string())
                             .send_self().action()],
                         Err(e) => {
@@ -333,7 +320,7 @@
         }
         LoadRoom(filename) => {
             if server.clients[client_id].is_admin() {
-                let actions = match read_file(&filename) {
+                let actions = match server.io.read_file(&filename) {
                     Ok(text) => match server.rooms[room_id].set_saves(&text) {
                         Ok(_) => vec![server_chat("Room configs loaded successfully.".to_string())
                             .send_self().action()],
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gameServer2/src/server/io.rs	Sun Dec 09 04:09:38 2018 +0300
@@ -0,0 +1,49 @@
+use std::{
+    fs::{File, OpenOptions},
+    io::{Read, Write, Result, Error, ErrorKind}
+};
+
+pub trait HWServerIO {
+    fn write_file(&mut self, name: &str, content: &str) -> Result<()>;
+    fn read_file(&mut self, name: &str) -> Result<String>;
+}
+
+pub struct EmptyServerIO {}
+
+impl EmptyServerIO {
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl HWServerIO for EmptyServerIO {
+    fn write_file(&mut self, _name: &str, _content: &str) -> Result<()> {
+        Ok(())
+    }
+
+    fn read_file(&mut self, _name: &str) -> Result<String> {
+        Ok("".to_string())
+    }
+}
+
+pub struct FileServerIO {}
+
+impl FileServerIO {
+    pub fn new() -> Self {
+        Self {}
+    }
+}
+
+impl HWServerIO for FileServerIO {
+    fn write_file(&mut self, name: &str, content: &str) -> Result<()> {
+        let mut writer = OpenOptions::new().create(true).write(true).open(name)?;
+        writer.write_all(content.as_bytes())
+    }
+
+    fn read_file(&mut self, name: &str) -> Result<String> {
+        let mut reader = File::open(name)?;
+        let mut result = String::new();
+        reader.read_to_string(&mut result)?;
+        Ok(result)
+    }
+}
\ No newline at end of file
--- a/gameServer2/src/server/network.rs	Sun Dec 09 00:53:05 2018 +0100
+++ b/gameServer2/src/server/network.rs	Sun Dec 09 04:09:38 2018 +0300
@@ -20,6 +20,7 @@
     protocol::{ProtocolDecoder, messages::*}
 };
 use super::{
+    io::FileServerIO,
     core::{HWServer},
     coretypes::ClientId
 };
@@ -236,7 +237,7 @@
 
 impl NetworkLayer {
     pub fn new(listener: TcpListener, clients_limit: usize, rooms_limit: usize) -> NetworkLayer {
-        let server = HWServer::new(clients_limit, rooms_limit);
+        let server = HWServer::new(clients_limit, rooms_limit, Box::new(FileServerIO::new()));
         let clients = Slab::with_capacity(clients_limit);
         let pending = HashSet::with_capacity(2 * clients_limit);
         let pending_cache = Vec::with_capacity(2 * clients_limit);