2018-03-18 21:03:50 +00:00
|
|
|
use std;
|
2018-03-18 20:07:20 +00:00
|
|
|
use std::io::*;
|
2018-02-26 10:52:41 +00:00
|
|
|
use std::net::TcpStream;
|
2018-03-18 20:07:20 +00:00
|
|
|
use std::thread;
|
|
|
|
|
2018-03-21 11:12:35 +00:00
|
|
|
// static leave_msg: &str = "BYE";
|
2018-03-18 20:07:20 +00:00
|
|
|
|
2018-02-26 10:52:41 +00:00
|
|
|
fn get_entry() -> String {
|
|
|
|
let mut buf = String::new();
|
|
|
|
|
|
|
|
stdin().read_line(&mut buf).unwrap();
|
|
|
|
buf.replace("\n", "").replace("\r", "")
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
fn write_to_server(stream: TcpStream) {
|
|
|
|
let mut writer = BufWriter::new(&stream);
|
|
|
|
|
2018-03-21 14:51:34 +00:00
|
|
|
// entrée du nom d'utilisateur
|
2018-03-05 16:39:11 +00:00
|
|
|
loop {
|
2018-03-18 20:07:20 +00:00
|
|
|
match &*get_entry() {
|
2018-03-21 14:51:34 +00:00
|
|
|
"" => {
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-18 20:07:20 +00:00
|
|
|
"/quit" => {
|
|
|
|
println!("Disconnecting...");
|
2018-03-21 11:12:35 +00:00
|
|
|
writeln!(writer, "BYE").unwrap();
|
|
|
|
writer.flush().unwrap();
|
2018-03-18 20:07:20 +00:00
|
|
|
println!("Disconnected!");
|
|
|
|
return ();
|
2018-03-05 16:39:11 +00:00
|
|
|
}
|
2018-03-18 20:07:20 +00:00
|
|
|
line => {
|
2018-03-21 14:51:34 +00:00
|
|
|
let line_str : String = String::from(line);
|
|
|
|
// let spliced: Vec<&str> = line_str.split(" ").collect();
|
|
|
|
let spliced: Vec<&str> = line_str.split_whitespace().collect();
|
|
|
|
if spliced.len() > 1 {
|
|
|
|
println!("Cannot use whitespace in username.");
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-21 11:12:35 +00:00
|
|
|
writeln!(writer, "{}", line).unwrap();
|
|
|
|
writer.flush().unwrap();
|
2018-03-05 16:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 14:51:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match &*get_entry() {
|
|
|
|
"" => {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
"/quit" => {
|
|
|
|
println!("Disconnecting...");
|
|
|
|
writeln!(writer, "BYE").unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
println!("Disconnected!");
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
"/clients" => {
|
|
|
|
writeln!(writer, "REQ CLIENTS").unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
}
|
|
|
|
line => {
|
|
|
|
writeln!(writer, "MSG {}", line).unwrap();
|
|
|
|
writer.flush().unwrap();
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 16:39:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:07:20 +00:00
|
|
|
fn exchange_with_server(stream: TcpStream) {
|
|
|
|
let server = stream.peer_addr().unwrap();
|
|
|
|
println!("Connected to {}", server);
|
2018-03-21 14:51:34 +00:00
|
|
|
|
2018-03-21 11:12:35 +00:00
|
|
|
let stream_cpy = stream.try_clone().unwrap();
|
|
|
|
let mut reader = BufReader::new(&stream_cpy);
|
2018-03-18 20:07:20 +00:00
|
|
|
|
|
|
|
println!("Enter `/quit` when you want to leave");
|
|
|
|
|
|
|
|
macro_rules! receive {
|
|
|
|
() => ({
|
|
|
|
let mut line = String::new();
|
|
|
|
match reader.read_line(&mut line) {
|
|
|
|
Ok(len) => {
|
|
|
|
if len == 0 {
|
2018-03-18 21:03:50 +00:00
|
|
|
// Reader is at EOF. Could use ErrorKind::UnexpectedEOF, but still unstable.
|
|
|
|
let ret = std::io::Error::new(std::io::ErrorKind::Other, "test");
|
|
|
|
return Err(ret): std::result::Result<&str, std::io::Error>;
|
2018-03-18 20:07:20 +00:00
|
|
|
}
|
|
|
|
line.pop();
|
|
|
|
}
|
2018-03-18 21:03:50 +00:00
|
|
|
Err(e) => { return Err(e); }
|
2018-03-18 20:07:20 +00:00
|
|
|
};
|
|
|
|
line
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
write_to_server(stream.try_clone().unwrap());
|
2018-03-05 16:39:11 +00:00
|
|
|
});
|
|
|
|
|
2018-03-18 21:03:50 +00:00
|
|
|
match (|| loop {
|
2018-03-21 14:51:34 +00:00
|
|
|
let input: String = String::from(receive!());
|
|
|
|
let spliced_input: Vec<&str> = input.split(" ").collect();
|
|
|
|
// if spliced_input[0] == "FROM" {
|
|
|
|
// println!("<{}>: {}", spliced_input[1], spliced_input[3]);
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
match spliced_input[0] {
|
|
|
|
"FROM" => {
|
|
|
|
println!("<{}>: {}", spliced_input[1], spliced_input[3]);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
println!("{}", input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// println!("{}", input);
|
2018-03-18 21:03:50 +00:00
|
|
|
})()
|
|
|
|
{
|
2018-03-18 20:07:20 +00:00
|
|
|
Ok(_) => {
|
|
|
|
println!("Left?");
|
|
|
|
}
|
2018-03-21 11:12:35 +00:00
|
|
|
Err(_) => {
|
|
|
|
println!(">>> Successfully left the room <<<");
|
2018-02-26 10:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn client(server_address: String) {
|
|
|
|
println!("Tentative de connexion a serveur...");
|
|
|
|
match TcpStream::connect(server_address) {
|
|
|
|
Ok(stream) => {
|
|
|
|
println!("Connexion au serveur réussie !");
|
|
|
|
exchange_with_server(stream);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("La connection au serveur a échoué : {}", e);
|
2018-03-05 16:39:11 +00:00
|
|
|
return;
|
2018-02-26 10:52:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|