|
|
@@ -8,7 +8,25 @@ use std::net::{TcpListener, TcpStream};
|
|
8
|
8
|
use srv_commands::Command;
|
|
9
|
9
|
use srv_config::Config;
|
|
10
|
10
|
|
|
11
|
|
-fn handle_client(stream: TcpStream, orders: &mut VecDeque<String>) {
|
|
|
11
|
+#[derive(Debug)]
|
|
|
12
|
+pub enum HashServerError {
|
|
|
13
|
+ Parse(srv_commands::ParseError),
|
|
|
14
|
+ Io(std::io::Error),
|
|
|
15
|
+}
|
|
|
16
|
+
|
|
|
17
|
+impl From<std::io::Error> for HashServerError {
|
|
|
18
|
+ fn from(err: std::io::Error) -> HashServerError {
|
|
|
19
|
+ HashServerError::Io(err)
|
|
|
20
|
+ }
|
|
|
21
|
+}
|
|
|
22
|
+
|
|
|
23
|
+impl From<srv_commands::ParseError> for HashServerError {
|
|
|
24
|
+ fn from(err: srv_commands::ParseError) -> HashServerError {
|
|
|
25
|
+ HashServerError::Parse(err)
|
|
|
26
|
+ }
|
|
|
27
|
+}
|
|
|
28
|
+
|
|
|
29
|
+fn handle_client(stream: TcpStream, orders: &mut VecDeque<String>, config:&Config) {
|
|
12
|
30
|
let mut reader = BufReader::new(&stream);
|
|
13
|
31
|
let mut writer = BufWriter::new(&stream);
|
|
14
|
32
|
|
|
|
@@ -20,32 +38,34 @@ fn handle_client(stream: TcpStream, orders: &mut VecDeque<String>) {
|
|
20
|
38
|
break;
|
|
21
|
39
|
}
|
|
22
|
40
|
|
|
23
|
|
- let cmd = srv_commands::parse(&line.trim_right());
|
|
|
41
|
+ let cmd = srv_commands::parse(&line.trim_right()).map_err(HashServerError::Parse);
|
|
24
|
42
|
|
|
25
|
43
|
match cmd {
|
|
26
|
|
- Command::Stage(str) => {
|
|
|
44
|
+ Ok(Command::Stage(str)) => {
|
|
27
|
45
|
orders.push_front(str);
|
|
28
|
46
|
}
|
|
29
|
|
- Command::Retrieve => {
|
|
|
47
|
+ Ok(Command::Retrieve) => {
|
|
30
|
48
|
if orders.is_empty() {
|
|
31
|
49
|
let _ = writer.write(b"No order on stage!\n");
|
|
32
|
50
|
|
|
33
|
51
|
} else {
|
|
34
|
52
|
if let Some(latest_order) = orders.pop_front() {
|
|
35
|
53
|
let _ = writer.write(latest_order.as_bytes());
|
|
36
|
|
- writer.write(b"\n");
|
|
|
54
|
+ let _ = writer.write(b"\n");
|
|
37
|
55
|
}
|
|
38
|
56
|
}
|
|
39
|
57
|
}
|
|
40
|
|
- Command::Control(ref control_string) => {
|
|
41
|
|
- println!("Received Control: {}", control_string);
|
|
|
58
|
+ Ok(Command::Control(ref control_string)) => {
|
|
|
59
|
+ if config.verbosity > 0 {
|
|
|
60
|
+ println!("Received Control: {}", control_string);
|
|
|
61
|
+ }
|
|
42
|
62
|
}
|
|
43
|
|
- Command::ParseError(_) => {
|
|
44
|
|
- println!("Error occurred: {:?}", cmd);
|
|
|
63
|
+ Err(ref e) => {
|
|
|
64
|
+ println!("Error occurred: {:?}", e);
|
|
45
|
65
|
}
|
|
46
|
66
|
}
|
|
47
|
67
|
|
|
48
|
|
- writer.flush();
|
|
|
68
|
+ let _ = writer.flush();
|
|
49
|
69
|
},
|
|
50
|
70
|
_ => {
|
|
51
|
71
|
break;
|
|
|
@@ -73,21 +93,18 @@ pub fn main() {
|
|
73
|
93
|
orders.push_front(String::from("Test1"));
|
|
74
|
94
|
}
|
|
75
|
95
|
|
|
76
|
|
- match TcpListener::bind(host) {
|
|
|
96
|
+ match TcpListener::bind(host).map_err(HashServerError::Io) {
|
|
77
|
97
|
Ok(listener) => {
|
|
78
|
|
- for stream in listener.incoming() {
|
|
79
|
|
- match stream {
|
|
80
|
|
- Ok(stream) => {
|
|
81
|
|
- if c.verbosity > 1 { println!("[DEBUG] New Client connected") }
|
|
82
|
|
- handle_client(stream, &mut orders);
|
|
83
|
|
- if c.verbosity > 1 { println!("[DEBUG] Client diconnected") }
|
|
84
|
|
- }
|
|
85
|
|
- Err(_) => { /* connection failed */ }
|
|
|
98
|
+ for s in listener.incoming() {
|
|
|
99
|
+ if let Ok(stream) = s {
|
|
|
100
|
+ if c.verbosity > 1 { println!("[DEBUG] New Client connected") }
|
|
|
101
|
+ handle_client(stream, &mut orders, &c);
|
|
|
102
|
+ if c.verbosity > 1 { println!("[DEBUG] Client diconnected") }
|
|
86
|
103
|
}
|
|
87
|
104
|
}
|
|
88
|
105
|
}
|
|
89
|
|
- Err(_) => {
|
|
90
|
|
- println!("Error starting server.");
|
|
|
106
|
+ Err(e) => {
|
|
|
107
|
+ println!("{:?}", e);
|
|
91
|
108
|
}
|
|
92
|
109
|
}
|
|
93
|
110
|
}
|