From 48e5c2825dab798926c33f7a3d4adafc12be6511 Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 22 Mar 2018 16:10:16 +0100 Subject: [PATCH] Added timestamp to server --- Rust/Cargo.toml | 2 +- Rust/src/server.rs | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Rust/Cargo.toml b/Rust/Cargo.toml index b2bfffe..c82a584 100644 --- a/Rust/Cargo.toml +++ b/Rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "chat-reseau" -version = "0.2.0" +version = "0.3.0" authors = ["Lucien Cartier-Tilet "] [dependencies] diff --git a/Rust/src/server.rs b/Rust/src/server.rs index 3bcc982..1cdcb5e 100644 --- a/Rust/src/server.rs +++ b/Rust/src/server.rs @@ -1,8 +1,10 @@ +extern crate chrono; use std::io::*; use std::net::{SocketAddr, TcpListener, TcpStream}; use std::thread; use std::sync::{Arc, Mutex, MutexGuard}; use std::collections::HashMap; +use self::chrono::Local; // TODO: implement requests 1.x from protocol // TODO: forbid usernames already in use @@ -45,6 +47,11 @@ type UserMap = HashMap; // CODE // /////////////////////////////////////////////////////////////////////////////// +pub fn get_time() -> String { + let date = Local::now(); + date.format("[%H:%M:%S]").to_string() +} + fn distribute_message( msg: &str, not_to: &SocketAddr, @@ -80,8 +87,11 @@ fn distribute_message( Ok(_) => {} Err(e) => { println!( - "Client {} <{}> disappeared during message distribution: {}", - other_client, other_name, e + "{} Client {} <{}> disappeared during message distribution: {}", + get_time(), + other_client, + other_name, + e ); } } @@ -117,7 +127,7 @@ fn disconnect_user(name: &str, client: &SocketAddr, lock: &mut MutexGuard>) { // Get client IP and port let client = stream.peer_addr().unwrap(); - println!("New connection from {}", client); + println!("{} New connection from {}", get_time(), client); // Buffered reading and writing let mut reader = BufReader::new(&stream); @@ -158,13 +168,18 @@ fn handle_client(stream: TcpStream, clients: Arc>) { send!("Welcome!"); send!("Please enter your name:"); let name = receive!(); - println!("Client {} identified as {}", client, name); + println!("{} Client {} identified as {}", get_time(), client, name); Ok(name) })() { Ok(name) => name, Err(e) => { - println!("Client {} disappeared during initialization: {}", client, e); + println!( + "{} Client {} disappeared during initialization: {}", + get_time(), + client, + e + ); return (); } }; @@ -194,7 +209,7 @@ fn handle_client(stream: TcpStream, clients: Arc>) { send_clients_name(&client, &mut lock); } input => { - println!("{} <{}>: {}", client, name, input); + println!("{} {} <{}>: {}", get_time(), client, name, input); { let mut lock = clients.lock().unwrap(); distribute_message(&format!("{}", input), &client, &mut lock, true); @@ -204,12 +219,15 @@ fn handle_client(stream: TcpStream, clients: Arc>) { })() { Ok(_) => { - println!("Client {} <{}> left", client, name); + println!("{} Client {} <{}> left", get_time(), client, name); } Err(e) => { println!( - "Client {} <{}> disappeared during chat: {}", - client, name, e + "{} Client {} <{}> disappeared during chat: {}", + get_time(), + client, + name, + e ); } } @@ -232,7 +250,11 @@ pub fn serveur(addr: String) { Err(e) => panic!("Could not read start TCP listener: {}", e), }; - println!("Successfully started the server on {}", serv_addr); + println!( + "{} Successfully started the server on {}", + get_time(), + serv_addr + ); for stream in listener.incoming() { match stream {