e64323fa60ef62994e51436d957bdea23fcf2baa
[crypto_lab1.git] / src / main.rs
1 extern crate openssl;
2
3 use std::io;
4 use std::os;
5
6 mod crypto;
7 mod command;
8 mod client;
9 mod server;
10
11 /*
12 TODO
13 * Comment stocker les clefs? à quels critères doivent elle répondre?
14 *
15 */
16
17 const PORT: u16 = 4221;
18
19 fn print_usage() {
20 println!("{} <genkey> | ...", os::args()[0]);
21 }
22
23 fn main() {
24 let args = os::args();
25
26 if args.iter().any(|a| a.as_slice() == "--help" || a.as_slice() == "-h") {
27 print_usage();
28 } else if args.len() > 1 && args[1].as_slice() == "genkey" {
29 match crypto::generate_key(256 / 8) {
30 Ok(key) => println!("key: {}", key),
31 Err(e) => println!("Unable to generate a key. Error: {}", e)
32 }
33 } else {
34 match server::Server::new(PORT) {
35 Ok(mut server) => {
36 println!("Server started");
37
38 match client::Client::new("127.0.0.1", PORT) {
39 Ok(mut client) => {
40 client.start_tests();
41 client.close();
42 },
43 Err(e) => {
44 println!("Unable to create a client. Error: {}", e);
45 return
46 }
47 }
48
49 println!("Press any key to quit");
50 io::stdin().read_line().ok().expect("Failed to read line");
51 server.close().ok().expect("Failed to close the server");
52 },
53 Err(e) => println!("Unable to create a new server. Error: {}", e)
54 }
55 }
56 }