further refactor
This commit is contained in:
parent
2697653b76
commit
0904161a1f
@ -81,6 +81,23 @@ fn process_message(msg_bytes: &[u8]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
codes::client::MESSAGE => {
|
||||||
|
let message = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
||||||
|
println!("{}", message);
|
||||||
|
}
|
||||||
|
codes::client::MESSAGE_ROOM => {
|
||||||
|
let params = String::from_utf8(msg_bytes[1..msg_bytes.len()].to_vec()).unwrap();
|
||||||
|
match params.split_once(" ") {
|
||||||
|
Some((room, msg)) => {
|
||||||
|
println!("{}: {}", room, msg);
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
println!("Malformed message recieved");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
codes::RESPONSE_OK => {
|
codes::RESPONSE_OK => {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!("RESPONSE_OK");
|
println!("RESPONSE_OK");
|
||||||
@ -185,12 +202,7 @@ pub fn start() {
|
|||||||
},
|
},
|
||||||
"/msg" => match inp.split_once(" ") {
|
"/msg" => match inp.split_once(" ") {
|
||||||
Some((room, msg)) => {
|
Some((room, msg)) => {
|
||||||
two_param_op(
|
two_param_op(codes::client::MESSAGE_ROOM, &mut stream, room, msg);
|
||||||
codes::client::SEND_MESSAGE_TO_ROOM,
|
|
||||||
&mut stream,
|
|
||||||
room,
|
|
||||||
msg,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
println!("Usage: /msg [room] [message]");
|
println!("Usage: /msg [room] [message]");
|
||||||
@ -208,7 +220,7 @@ pub fn start() {
|
|||||||
} else {
|
} else {
|
||||||
let message: String = inp;
|
let message: String = inp;
|
||||||
two_param_op(
|
two_param_op(
|
||||||
codes::client::SEND_MESSAGE_TO_ROOM,
|
codes::client::MESSAGE_ROOM,
|
||||||
&mut stream,
|
&mut stream,
|
||||||
&active_room,
|
&active_room,
|
||||||
&message,
|
&message,
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
pub mod codes {
|
pub mod codes {
|
||||||
pub const END: u8 = 0x00;
|
pub const TBD: u8 = 0x00;
|
||||||
pub mod client {
|
pub mod client {
|
||||||
pub const JOIN_ROOM: u8 = 0x01;
|
pub const JOIN_ROOM: u8 = 0x01;
|
||||||
pub const JOIN_SERVER: u8 = 0x02;
|
pub const JOIN_SERVER: u8 = 0x02;
|
||||||
pub const LEAVE_ROOM: u8 = 0x03;
|
pub const LEAVE_ROOM: u8 = 0x03;
|
||||||
pub const LIST_ROOMS: u8 = 0x04;
|
pub const LIST_ROOMS: u8 = 0x04;
|
||||||
pub const SEND_MESSAGE: u8 = 0x05;
|
pub const MESSAGE: u8 = 0x05;
|
||||||
pub const REGISTER_NICK: u8 = 0x06;
|
pub const REGISTER_NICK: u8 = 0x06;
|
||||||
pub const LIST_USERS: u8 = 0x07;
|
pub const LIST_USERS: u8 = 0x07;
|
||||||
pub const LIST_USERS_IN_ROOM: u8 = 0x08;
|
pub const LIST_USERS_IN_ROOM: u8 = 0x08;
|
||||||
pub const SEND_MESSAGE_TO_ROOM: u8 = 0x09;
|
pub const MESSAGE_ROOM: u8 = 0x09;
|
||||||
}
|
}
|
||||||
pub const QUIT: u8 = 0x0B;
|
pub const QUIT: u8 = 0x0B;
|
||||||
pub const KEEP_ALIVE: u8 = 0x0C;
|
pub const KEEP_ALIVE: u8 = 0x0C;
|
||||||
|
@ -13,12 +13,12 @@ const SERVER_ADDRESS: &str = "0.0.0.0:6667";
|
|||||||
const MAX_USERS: usize = 20;
|
const MAX_USERS: usize = 20;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Server {
|
struct Server<'a> {
|
||||||
users: HashMap<String, TcpStream>,
|
users: HashMap<String, &'a mut TcpStream>,
|
||||||
rooms: HashMap<String, Vec<String>>,
|
rooms: HashMap<String, Vec<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Server {
|
impl<'a> Server<'a> {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Server {
|
Server {
|
||||||
users: HashMap::new(),
|
users: HashMap::new(),
|
||||||
@ -27,19 +27,28 @@ impl Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn message(room: &str, message: &str) {}
|
fn message_room(room: &str, message: &str) {}
|
||||||
|
|
||||||
fn send_all(op: u8, listener: TcpListener) {
|
fn message_user(user: &str, message: &str) {}
|
||||||
for tcpstream in listener.incoming() {
|
|
||||||
match tcpstream {
|
fn broadcast(op: u8, server: &Arc<Mutex<Server>>, message: &str) {
|
||||||
Ok(mut stream) => {
|
let size = message.len() + 1;
|
||||||
stream.write_all(&[op]).unwrap();
|
let mut out_buf: Vec<u8> = vec![0; size];
|
||||||
}
|
out_buf[0] = op;
|
||||||
Err(_) => {}
|
|
||||||
|
for i in 1..size {
|
||||||
|
out_buf[i] = *message.as_bytes().get(i - 1).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
|
||||||
|
let streams: std::collections::hash_map::ValuesMut<'_, String, &mut TcpStream> =
|
||||||
|
unlocked_server.users.values_mut();
|
||||||
|
for stream in streams {
|
||||||
|
stream.write_all(&out_buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn handle_client(
|
fn handle_client(
|
||||||
server: &Arc<Mutex<Server>>,
|
server: &Arc<Mutex<Server>>,
|
||||||
stream: &mut TcpStream,
|
stream: &mut TcpStream,
|
||||||
@ -111,17 +120,17 @@ fn handle_client(
|
|||||||
leave_room(server, &nickname, room, stream);
|
leave_room(server, &nickname, room, stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::client::SEND_MESSAGE => {
|
codes::client::MESSAGE => {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!("SEND_MESSAGE");
|
println!("MESSAGE");
|
||||||
}
|
}
|
||||||
|
|
||||||
codes::client::SEND_MESSAGE_TO_ROOM => {
|
codes::client::MESSAGE_ROOM => {
|
||||||
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
let p: String = String::from_utf8_lossy(param_bytes).to_string();
|
||||||
let params: Option<(&str, &str)> = p.split_once(" ");
|
let params: Option<(&str, &str)> = p.split_once(" ");
|
||||||
match params {
|
match params {
|
||||||
Some((room, msg)) => {
|
Some((room, msg)) => {
|
||||||
message(room, msg);
|
// message(room, msg);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
stream
|
stream
|
||||||
@ -130,7 +139,7 @@ fn handle_client(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!("SEND_MESSAGE_TO_ROOM, {} ", p);
|
println!("MESSAGE_ROOM, {} ", p);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
@ -151,9 +160,9 @@ fn register_nick(server: &Arc<Mutex<Server>>, nickname: &str, stream: &mut TcpSt
|
|||||||
.write_all(&[codes::ERROR, codes::error::NICKNAME_COLLISION])
|
.write_all(&[codes::ERROR, codes::error::NICKNAME_COLLISION])
|
||||||
.unwrap();
|
.unwrap();
|
||||||
} else {
|
} else {
|
||||||
let stream_clone = stream.try_clone().expect("failed to clone");
|
|
||||||
// Add the user to the user list
|
// Add the user to the user list
|
||||||
unlocked_server.users.insert(nickname.to_string(), stream_clone);
|
let cl: TcpStream = stream.try_clone().expect("fail to clone");
|
||||||
|
// unlocked_server.users.insert(nickname.to_string(), cloned_stream);
|
||||||
|
|
||||||
// Send response ok
|
// Send response ok
|
||||||
stream.write_all(&[codes::RESPONSE_OK]).unwrap();
|
stream.write_all(&[codes::RESPONSE_OK]).unwrap();
|
||||||
@ -166,8 +175,10 @@ fn join_room(server: &Arc<Mutex<Server>>, user: &str, room: &str, stream: &mut T
|
|||||||
match unlocked_server.rooms.get_mut(room) {
|
match unlocked_server.rooms.get_mut(room) {
|
||||||
Some(l) => {
|
Some(l) => {
|
||||||
for ele in l.into_iter() {
|
for ele in l.into_iter() {
|
||||||
if ele == user{
|
if ele == user {
|
||||||
stream.write_all(&[codes::ERROR, codes::error::ALREADY_IN_ROOM]).unwrap();
|
stream
|
||||||
|
.write_all(&[codes::ERROR, codes::error::ALREADY_IN_ROOM])
|
||||||
|
.unwrap();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -244,7 +255,7 @@ pub fn start() {
|
|||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("Error parsing client");
|
eprintln!("Error parsing client");
|
||||||
stream.write(&[codes::END]).unwrap();
|
stream.write(&[codes::QUIT]).unwrap();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -260,7 +271,7 @@ pub fn start() {
|
|||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
eprintln!("Error parsing client");
|
eprintln!("Error parsing client");
|
||||||
stream.write(&[codes::END]).unwrap();
|
stream.write(&[codes::QUIT]).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -276,6 +287,7 @@ pub fn start() {
|
|||||||
println!("0: Quit Server");
|
println!("0: Quit Server");
|
||||||
println!("1: list connected users");
|
println!("1: list connected users");
|
||||||
println!("2: list rooms");
|
println!("2: list rooms");
|
||||||
|
println!("3: Broadcast message to all");
|
||||||
let inp: String = input!(":");
|
let inp: String = input!(":");
|
||||||
match inp.parse::<u8>() {
|
match inp.parse::<u8>() {
|
||||||
Ok(num) => match num {
|
Ok(num) => match num {
|
||||||
@ -284,6 +296,10 @@ pub fn start() {
|
|||||||
}
|
}
|
||||||
1 => println!("Users: {:?}", server.lock().unwrap().users),
|
1 => println!("Users: {:?}", server.lock().unwrap().users),
|
||||||
2 => println!("Rooms: {:?}", server.lock().unwrap().rooms),
|
2 => println!("Rooms: {:?}", server.lock().unwrap().rooms),
|
||||||
|
3 => {
|
||||||
|
let inp = input!("Enter message: ");
|
||||||
|
broadcast(codes::client::MESSAGE, &server, &inp)
|
||||||
|
}
|
||||||
_ => println!("Invalid Input"),
|
_ => println!("Invalid Input"),
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
Reference in New Issue
Block a user