pub struct EndPoint {
socket: TcpStream,
- current_timestamp: u64
+ current_timestamp: u64,
+ variant: packet::Variant,
}
impl Server {
- pub fn new(interface: &str, port: u16) -> IoResult<Server> {
+ pub fn new(interface: &str, port: u16, variant: packet::Variant) -> IoResult<Server> {
let mut acceptor = try!(TcpListener::bind(interface, port).listen());
let server = Server {
for stream in acceptor.incoming() {
match stream {
Ok(stream) => spawn(proc() {
- Server::handle_client(EndPoint::new(stream));
+ Server::handle_client(EndPoint::new(stream, variant));
}),
_ => return
}
loop {
match end_point.read() {
Ok(packet@Packet { t: Command(..), .. }) => {
- println!("[Server] Valid command received: {}", packet);
+ end_point.print("Server", format!("Valid command received: {}", packet));
let answer = Answer(Packet::random_packet_data([]));
match end_point.send(answer.clone()) {
Ok(_) =>
- println!("[Server] Answer sent: {}", answer),
+ end_point.print("Server", format!("Answer sent: {}", answer)),
Err(e) =>
- println!("[Server] Can't send the answer. Error: {}", e)
+ end_point.print("Server", format!("Can't send the answer. Error: {}", e))
}
},
// Socket has been closed.
Err(packet::IOReadError(IoError { kind: EndOfFile, .. })) => {
- println!("[Server] Connection closed: EOF");
+ end_point.print("Server", format!("Connection closed: EOF"));
return
},
other =>
- println!("[Server] Error or invalid packet: {}", other)
+ end_point.print("Server", format!("Error or invalid packet: {}", other))
}
}
}
}
impl Client {
- pub fn new(address: &str, port: u16) -> IoResult<Client> {
+ pub fn new(address: &str, port: u16, variant: packet::Variant) -> IoResult<Client> {
Ok(Client { end_point: EndPoint {
socket: try!(TcpStream::connect(address, port)),
- current_timestamp: 0
+ current_timestamp: 0,
+ variant: variant,
}})
}
self.end_point.close()
}
- pub fn send(&mut self, packet: PacketType) {
+ pub fn send(&mut self, packet: PacketType) -> bool {
match packet {
Command(_) => {
- println!("[Client] Sending: {}", packet);
+ self.end_point.print("Client", format!("Sending: {}", packet));
match self.end_point.send_with_result(packet) {
- Ok(Ok(packet@Packet { t: Answer(..), .. })) =>
- println!("[Client] Command transmitted correctly, answer: {}", packet),
- Ok(Ok(packet)) =>
- println!("[Client] Command transmitted correctly, wrong answer: {}", packet),
- Ok(Err(e)) =>
- println!("[Client] Answer error: {}", e),
- Err(e) =>
- println!("[Client] Can't send the packet. Error: {}", e)
+ Ok(Ok(packet@Packet { t: Answer(..), .. })) => {
+ self.end_point.print("Client", format!("Command transmitted correctly, answer: {}", packet));
+ true
+ },
+ Ok(Ok(packet)) => {
+ self.end_point.print("Client", format!("Command transmitted correctly, wrong answer: {}", packet));
+ false
+ }
+ Ok(Err(e)) => {
+ self.end_point.print("Client", format!("Answer error: {}", e));
+ false
+ }
+ Err(e) => {
+ self.end_point.print("Client", format!("Can't send the packet. Error: {}", e));
+ false
+ }
}
},
- other =>
- println!("[Client] Cannot send this type of packet: {}", other)
+ other => {
+ self.end_point.print("Client", format!("Cannot send this type of packet: {}", other));
+ false
+ }
}
}
/// Send some valid and not-valid packets to the server and check the result.
/// For each test a new client is created.
- pub fn start_tests(address: &str, port: u16) {
+ pub fn start_tests(address: &str, port: u16, variant: packet::Variant) {
let execute = |f: &mut |&mut Client| -> bool| -> bool {
- match Client::new(address, port) {
+ match Client::new(address, port, variant) {
Ok(ref mut client) => (*f)(client),
Err(e) => {
println!("Unable to create a client. Error: {}", e);
fn raw_packet(timestamp: u64) -> Vec<u8> {
let mut m = MemWriter::new();
- match (Packet { t: Command(Packet::random_packet_data([42])), timestamp: timestamp }).write(&mut m) {
+ match (Packet { t: Command(Packet::random_packet_data([42])), timestamp: timestamp }).write(&mut m, packet::Weak) {
Err(_) => vec!(),
_ => m.unwrap()
}
// 1)
|client: &mut Client| -> bool {
println!("Sending a valid packet...");
- match client.end_point.send_with_result(Command(Packet::random_packet_data([42]))) {
- Ok(Ok(Packet { t: Answer(..), .. })) => true,
- other => {
- println!("Error: {}", other);
- false
- }
- }
+ client.send(Command(Packet::random_packet_data([42])))
},
// 2)
|client: &mut Client| -> bool {
println!("Sending a packet with wrong padding (all 0)...");
client.end_point.current_timestamp += 1;
let mut m = MemWriter::new();
- let raw_packet = match (Packet { t: Command(Packet::random_packet_data([42])), timestamp: client.end_point.current_timestamp }).write_with_padding_fun(&mut m, |_, _| -> u8 { 0 }) {
+ let raw_packet = match (Packet { t: Command(Packet::random_packet_data([42])), timestamp: client.end_point.current_timestamp }).write_with_padding_fun(&mut m, packet::Weak, |_, _| -> u8 { 0 }) {
Err(_) => vec!(),
_ => m.unwrap()
};
}
impl EndPoint {
- pub fn new(socket: TcpStream) -> EndPoint {
- EndPoint { socket: socket, current_timestamp: 0 }
+ pub fn new(mut socket: TcpStream, variant: packet::Variant) -> EndPoint {
+ let _ = socket.set_nodelay(true);
+ EndPoint { socket: socket, current_timestamp: 0 , variant: variant}
}
fn close(&mut self) -> IoResult<()> {
Ok(())
}
+ fn print(&self, prefix: &str, s: String) {
+ println!("[{}] time: {}. {}", prefix, self.current_timestamp, s);
+ }
+
/// Send a packet and wait for an answer synchronously.
fn send_with_result(&mut self, p: PacketType) -> IoResult<ReadingResult> {
match self.send(p) {
/// Send arbitrary data and wait for an answer synchronously.
/// Do not increment the current timestamp.
- fn send_raw_with_result(&mut self, p: &[u8]) -> IoResult<ReadingResult> {
+ pub fn send_raw_with_result(&mut self, p: &[u8]) -> IoResult<ReadingResult> {
self.socket.set_timeout(DEFAULT_TIMEOUT);
match self.socket.write(p) {
Err(e) => Err(e),
fn send(&mut self, p: PacketType) -> IoResult<()> {
self.socket.set_timeout(DEFAULT_TIMEOUT);
self.current_timestamp += 1;
- match (Packet { t: p, timestamp: self.current_timestamp }).write(&mut self.socket) {
+ match (Packet { t: p, timestamp: self.current_timestamp }).write(&mut self.socket, self.variant) {
Err(packet::WriteIOError(e)) => Err(e),
_ => Ok(())
}
};
self.socket.set_timeout(DEFAULT_TIMEOUT);
- match Packet::read(&mut self.socket) {
+ match Packet::read(&mut self.socket, self.variant) {
Ok(packet) => {
if packet.timestamp <= self.current_timestamp {
println!("Error, timestamp mismatch, current timestamp: {}, packet received: {}", self.current_timestamp, packet);