added currently online users func

uses a hash map where the key is the users unique uuid and the value is their username
This commit is contained in:
Mitchell 2020-04-11 20:10:13 +00:00
parent 80a93d01ca
commit e2a0b9bc1f
1 changed files with 38 additions and 7 deletions

View File

@ -1,30 +1,61 @@
use rust_chat_server::ThreadPool;
use std::net::TcpListener;
use std::net::TcpStream;
use std::sync::Mutex;
use std::sync::Arc;
use std::collections::HashMap;
use std::io::prelude::*;
fn main(){
let listener = TcpListener::bind("127.0.0.1:6001").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming() {
let stream = stream.unwrap();
//let mut connected_clients: Vec<String> = Vec::new();
//let connected_clients = Arc::new(Mutex::new(Vec::new()));
let connected_clients = Arc::new(Mutex::new(HashMap::new()));
pool.execute(|| {
handle_connection(stream);
for stream in listener.incoming().take(2) {
let stream = stream.unwrap();
let clients_ref = Arc::clone(&connected_clients);
pool.execute(move || {
handle_connection(stream, clients_ref);
});
}
println!("Shutting down.");
}
fn handle_connection(mut stream: TcpStream){
fn handle_connection(mut stream: TcpStream, clients_ref: Arc<Mutex<HashMap<String,String>>>){
let mut buffer = [0; 512];
stream.read(&mut buffer).unwrap();
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
let incoming_message = String::from_utf8_lossy(&buffer[..]);
println!("Request: {}", incoming_message);
let connection_status = b"STATUS:online";
let mut uuid = String::new();
let mut username = String::new();
if buffer.starts_with(connection_status){
for data in incoming_message.split_whitespace(){
if data.contains("UUID"){
for id in data.split(":"){
if !id.contains("UUID"){
uuid.push_str(id);
}
}
}else if data.contains("UNAME"){
for uname in data.split(":"){
if !uname.contains("UNAME"){
username.push_str(uname);
}
}
}
}
let mut clients_hashmap = clients_ref.lock().unwrap();
clients_hashmap.insert(uuid,username);
}
//stream.write(response.as_bytes()).unwrap();
//stream.flush().unwrap();