浏览代码

Nearly finished. Error handling and tests missing.

Joshua Rutschmann 7 年前
父节点
当前提交
d0a0200486
共有 5 个文件被更改,包括 71 次插入41 次删除
  1. 0
    1
      hw9/task2/Cargo.toml
  2. 35
    34
      hw9/task2/src/main.rs
  3. 1
    0
      hw9/task2/srv-config/Cargo.toml
  4. 0
    0
      hw9/task2/srv-config/src/cli.yml
  5. 35
    6
      hw9/task2/srv-config/src/lib.rs

+ 0
- 1
hw9/task2/Cargo.toml 查看文件

@@ -4,7 +4,6 @@ version = "0.1.0"
4 4
 authors = ["Joshua Rutschmann <joshua.rutschmann@gmx.de>"]
5 5
 
6 6
 [dependencies]
7
-clap = {version = "~2.29.0", features = ["yaml"]}
8 7
 srv-commands = { path = "srv-commands" }
9 8
 srv-config = { path = "srv-config" } 
10 9
 

+ 35
- 34
hw9/task2/src/main.rs 查看文件

@@ -1,17 +1,17 @@
1
-#[macro_use]
2
-extern crate clap;
3 1
 extern crate srv_commands;
2
+extern crate srv_config;
4 3
 
5
-use clap::App;
6
-use std::io::{BufReader, BufRead};
4
+
5
+use std::io::{BufReader, BufRead, BufWriter, Write};
7 6
 use std::collections::VecDeque;
8 7
 use std::net::{TcpListener, TcpStream};
9 8
 use srv_commands::Command;
9
+use srv_config::Config;
10 10
 
11
-fn handle_client(stream: TcpStream) {
12
-    println!("New Client connected");
13
-    let mut orders:VecDeque<Command> = VecDeque::new(); 
14
-    let mut reader = BufReader::new(stream);
11
+fn handle_client(stream: TcpStream, orders: &mut VecDeque<String>) {    
12
+    let mut reader = BufReader::new(&stream);
13
+	let mut writer = BufWriter::new(&stream);
14
+	
15 15
     loop {
16 16
         let mut line = String::new();
17 17
         match reader.read_line(&mut line) {
@@ -20,18 +20,21 @@ fn handle_client(stream: TcpStream) {
20 20
                     break;
21 21
                 }
22 22
 
23
-                let cmd = srv_commands::parse(&line); 
24
-                println!("Command: {:?}", cmd);
23
+                let cmd = srv_commands::parse(&line.trim_right());
25 24
                 
26 25
                 match cmd {
27
-                    Command::Stage(_) => {
28
-                        orders.push_front(cmd);
26
+                    Command::Stage(str) => {
27
+                        orders.push_front(str);
29 28
                     }
30 29
                     Command::Retrieve => {
31 30
                         if orders.is_empty() {
32
-                            println!("No order on stage!");
31
+                            let _ = writer.write(b"No order on stage!\n");
32
+							
33 33
                         } else {
34
-                            println!("{:?}", orders.pop_front());
34
+							if let Some(latest_order) = orders.pop_front() {
35
+								let _ = writer.write(latest_order.as_bytes());
36
+								writer.write(b"\n");
37
+							}
35 38
                         }
36 39
                     }
37 40
                     Command::Control(ref control_string) => {
@@ -41,45 +44,43 @@ fn handle_client(stream: TcpStream) {
41 44
                         println!("Error occurred: {:?}", cmd);
42 45
                     }
43 46
                 }
47
+				
48
+				writer.flush();
44 49
             },
45 50
             _ => {
46 51
                 break;
47 52
             },
48 53
         }
49 54
     }
50
-
51
-    println!("Client diconnected");
52 55
 }
53 56
 
54 57
 pub fn main() {
55
-    // Lade Commandline Einstellungen aus YAML-Datei
56
-    let yaml = load_yaml!("cli.yml");
57
-    let matches = App::from_yaml(yaml).get_matches();
58
-    let address = matches.value_of("address").unwrap_or("127.0.0.1");
59
-    let port = matches.value_of("port").unwrap_or("7878");
60
-    let verbosity = matches.occurrences_of("verbose");
61
-    let mut testing = false;
62
-
63
-    // Falls das Unterkommando timings angegeben wurde aktiviere die Zeitmessung.
64
-    if let Some(ref sub_command) = matches.subcommand {
65
-        if sub_command.name.eq("test") {
66
-            testing = true;
67
-        }
68
-    }
58
+    
59
+	let c = Config::load();
69 60
 
70
-    if verbosity > 0 {
61
+    if c.verbosity > 0 {
71 62
         println!("Starting Multi Hash Server 0.1:");
72
-        println!("verbosity: {} | address: {} | port: {} | test-mode: {}", verbosity, address, port, testing);
63
+        println!("verbosity: {} | address: {} | port: {} | test-mode: {}", c.verbosity, c.address, c.port, c.testing);
73 64
     }
74 65
 
75
-    let host = format!("{}:{}", address, port);
66
+    let host = format!("{}:{}", c.address, c.port);
76 67
 
68
+	let mut orders:VecDeque<String> = VecDeque::new(); 
69
+	
70
+	if c.testing {
71
+		orders.push_front(String::from("Test3"));
72
+		orders.push_front(String::from("Test2"));
73
+		orders.push_front(String::from("Test1"));
74
+	}
75
+	
77 76
     match TcpListener::bind(host) {
78 77
         Ok(listener) => {
79 78
             for stream in listener.incoming() {
80 79
                 match stream {
81 80
                     Ok(stream) => {
82
-						handle_client(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") }
83 84
                     }
84 85
                     Err(_) => { /* connection failed */ }
85 86
                 }

+ 1
- 0
hw9/task2/srv-config/Cargo.toml 查看文件

@@ -4,3 +4,4 @@ version = "0.1.0"
4 4
 authors = ["Joshua Rutschmann <joshua.rutschmann@gmx.de>"]
5 5
 
6 6
 [dependencies]
7
+clap = {version = "~2.29.0", features = ["yaml"]}

hw9/task2/src/cli.yml → hw9/task2/srv-config/src/cli.yml 查看文件


+ 35
- 6
hw9/task2/srv-config/src/lib.rs 查看文件

@@ -1,7 +1,36 @@
1
-#[cfg(test)]
2
-mod tests {
3
-    #[test]
4
-    fn it_works() {
5
-        assert_eq!(2 + 2, 4);
6
-    }
1
+#[macro_use]
2
+extern crate clap;
3
+
4
+use clap::App;
5
+
6
+pub struct Config {
7
+	pub address:String,
8
+	pub port:String,
9
+	pub verbosity:u64,
10
+	pub testing:bool,
7 11
 }
12
+
13
+impl Config {
14
+	pub fn load() -> Self {
15
+		let yaml = load_yaml!("cli.yml");
16
+		let matches = App::from_yaml(yaml).get_matches();
17
+		let address = matches.value_of("address").unwrap_or("127.0.0.1");
18
+		let port = matches.value_of("port").unwrap_or("7878");
19
+		let verbosity = matches.occurrences_of("verbose");
20
+		let mut testing = false;
21
+
22
+		// Falls das Unterkommando timings angegeben wurde aktiviere die Zeitmessung.
23
+		if let Some(ref sub_command) = matches.subcommand {
24
+			if sub_command.name.eq("test") {
25
+				testing = true;
26
+			}
27
+		}
28
+		
29
+		Config{
30
+			address:address.to_string(),
31
+			port:port.to_string(),
32
+			verbosity:verbosity,
33
+			testing:testing,
34
+		}
35
+	}
36
+}

正在加载...
取消
保存