瀏覽代碼

Tests running. Fixed task1.

Joshua Rutschmann 7 年之前
父節點
當前提交
928c5b4f0b

+ 1
- 1
hw9/task1/src/lib.rs 查看文件

@@ -1,5 +1,5 @@
1 1
 pub fn parse(message: &str) -> Result<Command, ParseError> {
2
-    let m: String = String::from(message);
2
+    let m: String = String::from(message.trim_right());
3 3
     let mut line = m.lines();
4 4
     match line.next() {
5 5
         Some(x) => {

+ 8
- 8
hw9/task1/tests/task1.rs 查看文件

@@ -16,33 +16,33 @@ mod tests {
16 16
 
17 17
     #[test]
18 18
     fn not_known_command_returns_correct_command() {
19
-        assert_eq!(
20
-            parse("Hello"),
21
-            Err(ParseError(ErrorType::UnknownCommand))
22
-        )
19
+        assert_eq!(parse("Hello\n"), Err(ParseError(ErrorType::UnknownCommand)))
23 20
     }
24 21
 
25 22
     #[test]
26 23
     fn stage_returns_correct_command() {
27
-        assert_eq!(parse("STAGE Hello"), Ok(Command::Stage("Hello".to_string())))
24
+        assert_eq!(
25
+            parse("STAGE Hello\n"),
26
+            Ok(Command::Stage("Hello".to_string()))
27
+        )
28 28
     }
29 29
 
30 30
     #[test]
31 31
     fn control_returns_correct_command() {
32 32
         assert_eq!(
33
-            parse("CONTROL Hello"),
33
+            parse("CONTROL Hello\n"),
34 34
             Ok(Command::Control("Hello".to_string()))
35 35
         )
36 36
     }
37 37
 
38 38
     #[test]
39 39
     fn retrieve_returns_correct_command() {
40
-        assert_eq!(parse("RETRIEVE"), Ok(Command::Retrieve))
40
+        assert_eq!(parse("RETRIEVE\n"), Ok(Command::Retrieve))
41 41
     }
42 42
 
43 43
     #[test]
44 44
     fn retrieve_with_arguments_returns_correct_command() {
45
-        assert_eq!(parse("RETRIEVE Hello"), Ok(Command::Retrieve))
45
+        assert_eq!(parse("RETRIEVE Hello\n"), Ok(Command::Retrieve))
46 46
     }
47 47
 
48 48
 }

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

@@ -5,6 +5,6 @@ authors = ["Joshua Rutschmann <joshua.rutschmann@gmx.de>"]
5 5
 
6 6
 [dependencies]
7 7
 srv-commands = { path = "srv-commands" }
8
-srv-config = { path = "srv-config" } 
8
+srv-config = { path = "srv-config" }
9 9
 
10 10
 [workspace]

+ 44
- 33
hw9/task2/src/main.rs 查看文件

@@ -1,12 +1,12 @@
1 1
 extern crate srv_commands;
2 2
 extern crate srv_config;
3 3
 
4
-
5 4
 use std::io::{BufReader, BufRead, BufWriter, Write};
6 5
 use std::collections::VecDeque;
7 6
 use std::net::{TcpListener, TcpStream};
8 7
 use srv_commands::Command;
9 8
 use srv_config::Config;
9
+use HashServerError::Parse;
10 10
 
11 11
 #[derive(Debug)]
12 12
 pub enum HashServerError {
@@ -26,10 +26,10 @@ impl From<srv_commands::ParseError> for HashServerError {
26 26
     }
27 27
 }
28 28
 
29
-fn handle_client(stream: TcpStream, orders: &mut VecDeque<String>, config:&Config) {    
30
-    let mut reader = BufReader::new(&stream);
31
-	let mut writer = BufWriter::new(&stream);
32
-	
29
+fn handle_client(stream: &TcpStream, orders: &mut VecDeque<String>, v: bool) {
30
+    let mut reader = BufReader::new(stream);
31
+    let mut writer = BufWriter::new(stream);
32
+
33 33
     loop {
34 34
         let mut line = String::new();
35 35
         match reader.read_line(&mut line) {
@@ -38,8 +38,8 @@ fn handle_client(stream: TcpStream, orders: &mut VecDeque<String>, config:&Confi
38 38
                     break;
39 39
                 }
40 40
 
41
-                let cmd = srv_commands::parse(&line.trim_right()).map_err(HashServerError::Parse);
42
-                
41
+                let cmd = srv_commands::parse(&line).map_err(HashServerError::Parse);
42
+
43 43
                 match cmd {
44 44
                     Ok(Command::Stage(str)) => {
45 45
                         orders.push_front(str);
@@ -47,64 +47,75 @@ fn handle_client(stream: TcpStream, orders: &mut VecDeque<String>, config:&Confi
47 47
                     Ok(Command::Retrieve) => {
48 48
                         if orders.is_empty() {
49 49
                             let _ = writer.write(b"No order on stage!\n");
50
-							
50
+
51 51
                         } else {
52
-							if let Some(latest_order) = orders.pop_front() {
53
-								let _ = writer.write(latest_order.as_bytes());
54
-								let _ = writer.write(b"\n");
55
-							}
52
+                            if let Some(latest_order) = orders.pop_front() {
53
+                                let _ = writer.write(latest_order.as_bytes());
54
+                                let _ = writer.write(b"\n");
55
+                            }
56 56
                         }
57 57
                     }
58 58
                     Ok(Command::Control(ref control_string)) => {
59
-                        if config.verbosity > 0 {
59
+                        if v {
60 60
                             println!("Received Control: {}", control_string);
61 61
                         }
62 62
                     }
63
-                    Err(ref e) => {
63
+                    Err(Parse(e)) => {
64 64
                         println!("Error occurred: {:?}", e);
65 65
                     }
66
+                    _ => {}
66 67
                 }
67
-				
68
-				let _ = writer.flush();
69
-            },
68
+
69
+                let _ = writer.flush();
70
+            }
70 71
             _ => {
71 72
                 break;
72
-            },
73
+            }
73 74
         }
74 75
     }
75 76
 }
76 77
 
77 78
 pub fn main() {
78
-    
79
-	let c = Config::load();
79
+
80
+    let c = Config::load();
80 81
 
81 82
     if c.verbosity > 0 {
82 83
         println!("Starting Multi Hash Server 0.1:");
83
-        println!("verbosity: {} | address: {} | port: {} | test-mode: {}", c.verbosity, c.address, c.port, c.testing);
84
+        println!(
85
+            "verbosity: {} | address: {} | port: {} | test-mode: {}",
86
+            c.verbosity,
87
+            c.address,
88
+            c.port,
89
+            c.testing
90
+        );
84 91
     }
85 92
 
86 93
     let host = format!("{}:{}", c.address, c.port);
87 94
 
88
-	let mut orders:VecDeque<String> = VecDeque::new(); 
89
-	
90
-	if c.testing {
91
-		orders.push_front(String::from("Test3"));
92
-		orders.push_front(String::from("Test2"));
93
-		orders.push_front(String::from("Test1"));
94
-	}
95
-	
95
+    let mut orders: VecDeque<String> = VecDeque::new();
96
+
97
+    if c.testing {
98
+        orders.push_front(String::from("Test3"));
99
+        orders.push_front(String::from("Test2"));
100
+        orders.push_front(String::from("Test1"));
101
+    }
102
+
96 103
     match TcpListener::bind(host).map_err(HashServerError::Io) {
97 104
         Ok(listener) => {
98 105
             for s in listener.incoming() {
99 106
                 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") }
107
+                    if c.verbosity > 1 {
108
+                        println!("[DEBUG] New Client connected")
109
+                    }
110
+                    handle_client(&stream, &mut orders, c.verbosity > 0);
111
+                    if c.verbosity > 1 {
112
+                        println!("[DEBUG] Client diconnected")
113
+                    }
103 114
                 }
104 115
             }
105 116
         }
106 117
         Err(e) => {
107
-            println!("{:?}", e);
118
+            println!("Failed to start the MultiHashServer: {:?}", e);
108 119
         }
109 120
     }
110 121
 }

+ 1
- 1
hw9/task2/srv-commands/src/lib.rs 查看文件

@@ -1,5 +1,5 @@
1 1
 pub fn parse(message: &str) -> Result<Command, ParseError> {
2
-    let m: String = String::from(message);
2
+    let m: String = String::from(message.trim_right());
3 3
     let mut line = m.lines();
4 4
     match line.next() {
5 5
         Some(x) => {

+ 48
- 0
hw9/task2/srv-commands/tests/srv-commands.rs 查看文件

@@ -0,0 +1,48 @@
1
+#[cfg(test)]
2
+
3
+mod tests {
4
+
5
+    extern crate task1;
6
+
7
+    use self::task1::Command;
8
+    use self::task1::ErrorType;
9
+    use self::task1::parse;
10
+    use self::task1::ParseError;
11
+
12
+    #[test]
13
+    fn empty_returns_correct_command() {
14
+        assert_eq!(parse("\n"), Err(ParseError(ErrorType::EmptyString)))
15
+    }
16
+
17
+    #[test]
18
+    fn not_known_command_returns_correct_command() {
19
+        assert_eq!(parse("Hello\n"), Err(ParseError(ErrorType::UnknownCommand)))
20
+    }
21
+
22
+    #[test]
23
+    fn stage_returns_correct_command() {
24
+        assert_eq!(
25
+            parse("STAGE Hello\n"),
26
+            Ok(Command::Stage("Hello".to_string()))
27
+        )
28
+    }
29
+
30
+    #[test]
31
+    fn control_returns_correct_command() {
32
+        assert_eq!(
33
+            parse("CONTROL Hello\n"),
34
+            Ok(Command::Control("Hello".to_string()))
35
+        )
36
+    }
37
+
38
+    #[test]
39
+    fn retrieve_returns_correct_command() {
40
+        assert_eq!(parse("RETRIEVE\n"), Ok(Command::Retrieve))
41
+    }
42
+
43
+    #[test]
44
+    fn retrieve_with_arguments_returns_correct_command() {
45
+        assert_eq!(parse("RETRIEVE Hello\n"), Ok(Command::Retrieve))
46
+    }
47
+
48
+}

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

@@ -4,4 +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"]}
7
+clap = {version = "~2.29.0", features = ["yaml"]}

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

@@ -4,33 +4,33 @@ extern crate clap;
4 4
 use clap::App;
5 5
 
6 6
 pub struct Config {
7
-	pub address:String,
8
-	pub port:String,
9
-	pub verbosity:u64,
10
-	pub testing:bool,
7
+    pub address: String,
8
+    pub port: String,
9
+    pub verbosity: u64,
10
+    pub testing: bool,
11 11
 }
12 12
 
13 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;
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 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
-}
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
+}

Loading…
取消
儲存