X-Git-Url: http://git.euphorik.ch/?p=crypto_lab1.git;a=blobdiff_plain;f=lab1_rust%2Fsrc%2Fend_point.rs;h=83967dac4640651ecce039c4ecc277da7d58ccfe;hp=97104c686e6118eec4a142a0a3ff174679208ef6;hb=d7f0bb987b21e93a5798403d294f7905151682f7;hpb=9cdc634f2ce4d0e88f4d3af1d6f555a5449da189 diff --git a/lab1_rust/src/end_point.rs b/lab1_rust/src/end_point.rs index 97104c6..83967da 100644 --- a/lab1_rust/src/end_point.rs +++ b/lab1_rust/src/end_point.rs @@ -1,9 +1,11 @@ use std::io; use std::io::{ MemWriter, Acceptor, Listener, TcpStream, IoResult, IoError, EndOfFile }; use std::io::net::tcp::{ TcpAcceptor, TcpListener }; +use std::thread::Thread; use packet; use packet::{ Packet, ReadingResult, PacketType }; use packet::PacketType::{ Command, Answer, Error }; +use utils::from_elem; // Default timeout when waiting data on a stream (for instance: 'TcpStream::read'). static DEFAULT_TIMEOUT: Option = Some(500); // [ms]. @@ -30,18 +32,19 @@ impl Server { acceptor: acceptor.clone() }; - spawn(proc() { + Thread::spawn(move || { loop { for stream in acceptor.incoming() { match stream { - Ok(stream) => spawn(proc() { - Server::handle_client(EndPoint::new(stream, variant)); - }), + Ok(stream) => + Thread::spawn(move || { + Server::handle_client(EndPoint::new(stream, variant)); + }).detach(), _ => return } } } - }); + }).detach(); Ok(server) } @@ -193,7 +196,7 @@ impl Client { // 5) |client: &mut Client| -> bool { println!("Sending a packet with too small data..."); - let command = Command(Packet::new_packet_data(0, Vec::from_elem(6, 0x00))); + let command = Command(Packet::new_packet_data(0, from_elem(6, 0x00))); match client.end_point.send_with_result(command) { Ok(Err(packet::ReadingError::IO( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { @@ -205,7 +208,7 @@ impl Client { // 6) |client: &mut Client| -> bool { println!("Sending a packet with too large data..."); - let command = Command(Packet::new_packet_data(0, Vec::from_elem(40, 0x00))); + let command = Command(Packet::new_packet_data(0, from_elem(40, 0x00))); match client.end_point.send_with_result(command) { Ok(Err(packet::ReadingError::IO( IoError { kind: io::TimedOut, .. }))) => true, // OK: the server should not send any packet. other => { @@ -236,7 +239,7 @@ impl Client { let mut nb_test_passed = 0; for (i, test) in range(1, tests.len()+1).zip(tests.iter_mut()) { - println!("===== Test case #{}:", i) + println!("===== Test case #{}:", i); if execute(test) { nb_test_passed += 1; println!("===== Test passed");