improvements to join room

This commit is contained in:
David Westgate 2023-11-25 16:36:31 -08:00
parent f78acbdb10
commit da336e41be
2 changed files with 55 additions and 11 deletions

View File

@ -73,7 +73,27 @@ fn users(stream: &mut TcpStream) {
fn msg(stream: &mut TcpStream) {}
fn join(stream: &mut TcpStream) {}
fn join(nick: &str, room: &str, stream: &mut TcpStream) {
#[cfg(debug_assertions)]
println!("nick {} joining room s{} ", nick, room);
let size = room.to_string().capacity() + nick.to_string().capacity() + 2;
let mut out_buf: Vec<u8> = vec![0; size];
let mut byte:usize = 0;
out_buf[byte] = codes::client::JOIN_ROOM;
byte+=1;
for i in 0..nick.len() {
out_buf[byte] = *nick.as_bytes().get(i).unwrap();
byte+=1;
}
out_buf[byte] = 0x20;
byte += 1;
for i in 0.. room.len() {
out_buf[byte] = *room.as_bytes().get(i).unwrap();
byte+=1;
}
stream.write(&out_buf);
}
fn show(stream: &mut TcpStream) {}
fn leave(stream: &mut TcpStream) {}
@ -102,12 +122,16 @@ pub fn start() {
stream.write(&buf);
loop {
let cmd: String = input!(":");
match cmd.trim() {
let inp: String = input!(":");
let cmds: Vec<_> = inp.split(" ").collect();
match *cmds.get(0).unwrap() {
"/quit" => disconnect(),
"/rooms" => rooms(&mut stream),
"/users" => users(&mut stream),
"/join" => join(&mut stream),
"/join" => {
let room = *cmds.get(1).unwrap();
join(&nick,room ,&mut stream)
},
"/show" => show(&mut stream),
"/leave" => leave(&mut stream),
"/msg" => msg(&mut stream),

View File

@ -19,7 +19,7 @@ struct Server {
}
impl Server {
fn new() -> Server {
fn new() -> Self {
Server {
users: HashSet::new(),
rooms: HashMap::new(),
@ -74,7 +74,14 @@ fn handle_client(
}
codes::client::JOIN_ROOM => {
#[cfg(debug_assertions)]
println!("JOIN_ROOM");
println!("JOIN_ROOM ");
let p: String = String::from_utf8_lossy(param_bytes).to_string();
let params: Vec<&str> = p.split(' ').collect();
let user = params.get(0).unwrap();
let room = params.get(1).unwrap();
join_room(server, *user, room, stream)
}
codes::client::LEAVE_ROOM => {
#[cfg(debug_assertions)]
@ -86,7 +93,7 @@ fn handle_client(
}
_ => {
#[cfg(debug_assertions)]
println!("Unspecified client Op");
println!("Unspecified client Op, {:x?}", cmd_bytes);
}
}
@ -95,7 +102,7 @@ fn handle_client(
fn register_nick(server: &Arc<Mutex<Server>>, nickname: String, stream: &mut TcpStream) {
// Check for nickname collision
let mut unlocked_server = server.lock().unwrap();
let mut unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
if unlocked_server.users.contains(&nickname) {
#[cfg(debug_assertions)]
println!("nickname collision, {}", nickname);
@ -109,6 +116,20 @@ fn register_nick(server: &Arc<Mutex<Server>>, nickname: String, stream: &mut Tcp
}
}
fn join_room(server: &Arc<Mutex<Server>>, user: &str, room: &str, stream: &mut TcpStream) {
let mut unlocked_server: std::sync::MutexGuard<'_, Server> = server.lock().unwrap();
match unlocked_server.rooms.get_mut(room) {
Some(l) => {
l.push(user.to_string());
}
None => {
let list: Vec<String> = vec![user.to_string()];
unlocked_server.rooms.insert(room.to_string(), list);
}
}
}
pub fn start() {
let listener: TcpListener = TcpListener::bind(SERVER_ADDRESS).expect("Failed to bind to port");
let server: Arc<Mutex<Server>> = Arc::new(Mutex::new(Server::new()));
@ -120,16 +141,15 @@ pub fn start() {
match tcpstream {
Ok(mut stream) => {
let mut buf_in: [u8; 1024] = [0; 1024];
let server_innter = Arc::clone(&server_outer);
let server_inner = Arc::clone(&server_outer);
thread::spawn(move || loop {
match stream.read(&mut buf_in) {
Ok(size) => {
let cmd_bytes: &[u8] = &buf_in[0..1];
let param_bytes: &[u8] = &buf_in[1..size];
handle_client(&server_innter, &mut stream, cmd_bytes, param_bytes);
handle_client(&server_inner, &mut stream, cmd_bytes, param_bytes);
}
Err(_) => {
println!("Error parsing client");