client object changes
This commit is contained in:
parent
a9578e4473
commit
da5df01a95
|
|
@ -0,0 +1 @@
|
|||
pub mod client_profile;
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
pub struct Client{
|
||||
uuid: String,
|
||||
username: String,
|
||||
address: String,
|
||||
}
|
||||
|
||||
impl Client{
|
||||
pub fn get_uuid(&self) -> String{
|
||||
self.uuid
|
||||
}
|
||||
|
||||
pub fn get_username(&self) -> String{
|
||||
self.username
|
||||
}
|
||||
|
||||
pub fn get_addres(&self) -> String{
|
||||
self.address
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,12 @@
|
|||
mod client_management;
|
||||
|
||||
use crate::client_management::client_profile::Client;
|
||||
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::collections::HashMap;
|
||||
use std::io::prelude::*;
|
||||
|
||||
fn main(){
|
||||
|
|
@ -12,22 +15,30 @@ fn main(){
|
|||
|
||||
//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()));
|
||||
//let connected_clients = Arc::new(Mutex::new(HashMap::new()));
|
||||
|
||||
//let mut connected_clients = vec![];
|
||||
//let mut connected_clients: Vec<String> = vec![];
|
||||
let connected_clients = Arc::new(Mutex::new(vec![]));
|
||||
|
||||
for stream in listener.incoming().take(2) {
|
||||
let stream = stream.unwrap();
|
||||
let clients_ref = Arc::clone(&connected_clients);
|
||||
loop{
|
||||
//for stream in listener.incoming(){
|
||||
if let Ok((stream, address)) = listener.accept(){
|
||||
println!("Connected: {}", address);
|
||||
//let stream = stream.unwrap();
|
||||
let clients_ref = Arc::clone(&connected_clients);
|
||||
|
||||
pool.execute(move || {
|
||||
handle_connection(stream, clients_ref);
|
||||
});
|
||||
pool.execute(move || {
|
||||
handle_connection(stream, clients_ref, address.to_string());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
println!("Shutting down.");
|
||||
//println!("Shutting down.");
|
||||
}
|
||||
|
||||
fn handle_connection(mut stream: TcpStream, clients_ref: Arc<Mutex<HashMap<String,String>>>){
|
||||
let mut buffer = [0; 512];
|
||||
fn handle_connection(mut stream: TcpStream, connect_clients: Arc<Mutex<Vec<String>>>, new_address: String){//vec needs to be of type clients
|
||||
let mut buffer = [0; 1024];
|
||||
stream.read(&mut buffer).unwrap();
|
||||
|
||||
let incoming_message = String::from_utf8_lossy(&buffer[..]);
|
||||
|
|
@ -36,29 +47,38 @@ fn handle_connection(mut stream: TcpStream, clients_ref: Arc<Mutex<HashMap<Strin
|
|||
let connection_status = b"STATUS:online";
|
||||
let clients_request = b"CLIENT:request_clients";
|
||||
let ip_request = b"CLIENT:request_ip";
|
||||
let mut uuid = String::new();
|
||||
let mut username = String::new();
|
||||
let ip = format!("{}", stream.local_addr().unwrap());
|
||||
|
||||
if buffer.starts_with(connection_status){
|
||||
let mut new_uuid = String::new();
|
||||
let mut new_username = String::new();
|
||||
//let ip = format!("{}", stream.local_addr().unwrap());
|
||||
|
||||
for data in incoming_message.split_whitespace(){
|
||||
if data.contains("UUID"){
|
||||
for id in data.split(":"){
|
||||
if !id.contains("UUID"){
|
||||
uuid.push_str(id);
|
||||
new_uuid.push_str(id);
|
||||
}
|
||||
}
|
||||
}else if data.contains("UNAME"){
|
||||
for uname in data.split(":"){
|
||||
if !uname.contains("UNAME"){
|
||||
username.push_str(uname);
|
||||
new_username.push_str(uname);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut clients_hashmap = clients_ref.lock().unwrap();
|
||||
clients_hashmap.insert(uuid,username);
|
||||
}else if buffer.starts_with(clients_request){
|
||||
//let mut clients_hashmap = clients_ref.lock().unwrap();
|
||||
//let user_details = [username, ip_request];
|
||||
//clients_hashmap.insert(uuid,user_details);
|
||||
let client = Client{
|
||||
uuid: new_uuid,
|
||||
username: new_username,
|
||||
address: new_address,
|
||||
};
|
||||
}
|
||||
}
|
||||
/*else if buffer.starts_with(clients_request){
|
||||
handle_clients_request(stream, &incoming_message, clients_ref);
|
||||
}else if buffer.starts_with(ip_request){
|
||||
handle_ip_request(stream, &incoming_message, clients_ref);
|
||||
|
|
@ -118,3 +138,4 @@ fn identify_requested_ip(incoming_message: &str) -> String{
|
|||
}
|
||||
|
||||
fn get_requested_user(client_ip: &str, clients_ref: Arc<Mutex<HashMap<String,String>>>) -> HashMap<String,String>{}
|
||||
*/
|
||||
Loading…
Reference in New Issue