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