575df0509538729a748c50c4c3165b30046c877d
[crypto_lab1.git] / src / client.rs
1 use std::io::{ TcpStream, IoResult };
2 use command::{ Command, Packet, Error };
3
4 pub struct Client {
5 socket: TcpStream,
6 current_timestamp: u64
7 }
8
9 impl Client {
10 pub fn new(address: &str, port: u16) -> IoResult<Client> {
11 Ok(Client {
12 socket: try!(TcpStream::connect(address, port)),
13 current_timestamp: 0
14 })
15 }
16
17 pub fn close(&mut self) {
18 self.socket.close_read();
19 self.socket.close_write();
20 }
21
22 pub fn start_tests(&mut self) {
23
24 let command = Packet::new_random_command(self.current_timestamp);
25 self.send(command);
26
27 }
28
29 fn send(&mut self, p: Packet) {
30 p.write(&mut self.socket);
31 }
32 }