fix single param commands on the client
This commit is contained in:
parent
c20410b6f2
commit
7e4883663b
@ -88,7 +88,8 @@ fn process_message(msg_bytes: &[u8], nick: &str) {
|
|||||||
},
|
},
|
||||||
|
|
||||||
codes::client::MESSAGE => {
|
codes::client::MESSAGE => {
|
||||||
let message = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
let message: String =
|
||||||
|
String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
||||||
println!("[server]:{}", message);
|
println!("[server]:{}", message);
|
||||||
}
|
}
|
||||||
codes::client::MESSAGE_ROOM => {
|
codes::client::MESSAGE_ROOM => {
|
||||||
@ -110,10 +111,7 @@ fn process_message(msg_bytes: &[u8], nick: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::RESPONSE_OK => {
|
codes::RESPONSE_OK => {}
|
||||||
// #[cfg(debug_assertions)]
|
|
||||||
// println!("RESPONSE_OK");
|
|
||||||
}
|
|
||||||
codes::RESPONSE => {
|
codes::RESPONSE => {
|
||||||
let message = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
let message = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
||||||
println!("{}", message);
|
println!("{}", message);
|
||||||
@ -135,7 +133,6 @@ fn disconnect(stream: &mut TcpStream) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn help() {
|
fn help() {
|
||||||
clear();
|
|
||||||
println!("Available commands:");
|
println!("Available commands:");
|
||||||
println!("/quit <- Disconnect and stop the client");
|
println!("/quit <- Disconnect and stop the client");
|
||||||
println!("/rooms <- List all of the rooms on the server");
|
println!("/rooms <- List all of the rooms on the server");
|
||||||
@ -143,7 +140,7 @@ fn help() {
|
|||||||
println!("/list [room-name] <- List all of the users in the given room");
|
println!("/list [room-name] <- List all of the users in the given room");
|
||||||
println!("/join [room-name] <- Join the given room. Create the room if it does not exist");
|
println!("/join [room-name] <- Join the given room. Create the room if it does not exist");
|
||||||
println!(
|
println!(
|
||||||
"/leave [room-name] <- Leave the given room. Error if the you are not already in the room"
|
"/leave [room-name] <- Leave the given room. Error if you are not already in the room"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,7 +171,7 @@ pub fn start() {
|
|||||||
let nick_clone: String = nick.clone();
|
let nick_clone: String = nick.clone();
|
||||||
|
|
||||||
//timestamp for detecting unresponsive server
|
//timestamp for detecting unresponsive server
|
||||||
let timestamp = Arc::new(Mutex::new(Instant::now()));
|
let timestamp: Arc<Mutex<Instant>> = Arc::new(Mutex::new(Instant::now()));
|
||||||
let mut timestamp_clone: Arc<Mutex<Instant>> = Arc::clone(×tamp);
|
let mut timestamp_clone: Arc<Mutex<Instant>> = Arc::clone(×tamp);
|
||||||
|
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
@ -184,8 +181,8 @@ pub fn start() {
|
|||||||
//watchdog to send keep_alive and stop client if server fails to respond
|
//watchdog to send keep_alive and stop client if server fails to respond
|
||||||
thread::spawn(move || loop {
|
thread::spawn(move || loop {
|
||||||
thread::sleep(Duration::from_secs(5));
|
thread::sleep(Duration::from_secs(5));
|
||||||
let lock = timestamp.lock().unwrap();
|
let lock: std::sync::MutexGuard<'_, Instant> = timestamp.lock().unwrap();
|
||||||
let now = Instant::now();
|
let now: Instant = Instant::now();
|
||||||
if now.duration_since(*lock) > Duration::from_secs(30) {
|
if now.duration_since(*lock) > Duration::from_secs(30) {
|
||||||
eprintln!("Server is unresponsive. Stopping client");
|
eprintln!("Server is unresponsive. Stopping client");
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
@ -198,27 +195,21 @@ pub fn start() {
|
|||||||
one_param_op(codes::client::REGISTER_NICK, &mut stream, &nick);
|
one_param_op(codes::client::REGISTER_NICK, &mut stream, &nick);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let inp = input!("");
|
let inp: String = input!("");
|
||||||
|
|
||||||
match inp.split_once(" ") {
|
match inp.split_once(" ") {
|
||||||
Some((cmd, param)) => match cmd {
|
Some((cmd, param)) => match cmd {
|
||||||
"/quit" => {
|
|
||||||
disconnect(&mut stream);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
"/rooms" => no_param_op(codes::client::LIST_ROOMS, &mut stream),
|
|
||||||
"/users" => no_param_op(codes::client::LIST_USERS, &mut stream),
|
|
||||||
"/list" => match param.split_once(" ") {
|
"/list" => match param.split_once(" ") {
|
||||||
Some((room, _)) => {
|
Some((_, _)) => {
|
||||||
one_param_op(codes::client::LIST_USERS_IN_ROOM, &mut stream, room);
|
eprintln!("Malformaed. Try /list [room-name]");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Malformaed. Try /list [room-name]");
|
one_param_op(codes::client::LIST_USERS_IN_ROOM, &mut stream, param);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/join" => match param.split_once(" ") {
|
"/join" => match param.split_once(" ") {
|
||||||
Some((_, _)) => {
|
Some((_, _)) => {
|
||||||
println!("Malformed. Try /join [room-name]");
|
eprintln!("Malformed. Try /join [room-name]");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
one_param_op(codes::client::JOIN_ROOM, &mut stream, param);
|
one_param_op(codes::client::JOIN_ROOM, &mut stream, param);
|
||||||
@ -227,7 +218,7 @@ pub fn start() {
|
|||||||
|
|
||||||
"/leave" => match param.split_once(" ") {
|
"/leave" => match param.split_once(" ") {
|
||||||
Some((_, _)) => {
|
Some((_, _)) => {
|
||||||
println!("Malformed. Try /leave [room-name]");
|
eprintln!("Malformed. Try /leave [room-name]");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
one_param_op(codes::client::LEAVE_ROOM, &mut stream, param);
|
one_param_op(codes::client::LEAVE_ROOM, &mut stream, param);
|
||||||
@ -238,22 +229,29 @@ pub fn start() {
|
|||||||
two_param_op(codes::client::MESSAGE_ROOM, &mut stream, room, msg);
|
two_param_op(codes::client::MESSAGE_ROOM, &mut stream, room, msg);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Usage: /msg [room] [message]");
|
eprintln!("Usage: /msg [room] [message]");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
_ => {
|
||||||
|
eprintln!("Malformed command. Try /help");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
_ => match inp.as_str() {
|
||||||
|
"/quit" => {
|
||||||
|
disconnect(&mut stream);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
"/rooms" => no_param_op(codes::client::LIST_ROOMS, &mut stream),
|
||||||
|
"/users" => no_param_op(codes::client::LIST_USERS, &mut stream),
|
||||||
"/help" => {
|
"/help" => {
|
||||||
help();
|
help();
|
||||||
}
|
}
|
||||||
"/" => {
|
"/" => {
|
||||||
println!("Invalid command");
|
println!("Invalid command");
|
||||||
}
|
}
|
||||||
_ => {
|
_ => one_param_op(codes::client::MESSAGE, &mut stream, &inp),
|
||||||
one_param_op(codes::client::MESSAGE, &mut stream, &inp);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
_ => {
|
|
||||||
one_param_op(codes::client::MESSAGE, &mut stream, &inp);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Reference in New Issue
Block a user