Change the structure of a packet.
[crypto_lab1.git] / src / client.rs
index 8392460..14df2d4 100644 (file)
@@ -1,10 +1,8 @@
 use std::io::{ TcpStream, IoResult };
-use command::{ Command, Packet, Error };
-use crypto::Crypto;
+use packet::{ Command, Packet };
 
 pub struct Client {
    socket: TcpStream,
-   crypto: Crypto,
    current_timestamp: u64
 }
 
@@ -12,24 +10,27 @@ impl Client {
    pub fn new(address: &str, port: u16) -> IoResult<Client> {
       Ok(Client { 
          socket: try!(TcpStream::connect(address, port)),
-         current_timestamp: 0,
-         crypto: Crypto::new()
+         current_timestamp: 0
       })
    }
    
-   pub fn close(&mut self) {
-      self.socket.close_read();
-      self.socket.close_write();
+   pub fn close(&mut self) -> IoResult<()> {
+      try!(self.socket.close_read());
+      try!(self.socket.close_write());
+      Ok(())
    }
    
    pub fn start_tests(&mut self) {
-      
-      let command = Packet::newRandomCommand(self.current_timestamp);      
-      self.send(command);
-      
+      self.current_timestamp += 1;
+      let timestamp = self.current_timestamp;
+      self.send(Packet { timestamp: timestamp, t: Command(Packet::random_packet_data()) });
    }
    
-   fn send(&mut self, p: Packet) {      
-      p.write(&mut self.socket);
+   fn send(&mut self, p: Packet) {
+      println!("Client sends packet: {}", p);
+      match p.write(&mut self.socket) {
+         Err(e) => println!("Error on sending: {}", e),
+         _ => ()
+      }
    }
 }
\ No newline at end of file