redesigned client for the new exec structure

This commit is contained in:
michael-bailey 2021-03-17 10:41:47 +00:00
parent db72977d2f
commit cd81b1e250
2 changed files with 35 additions and 32 deletions

View File

@ -2,25 +2,21 @@
// pub mod client_v3; // pub mod client_v3;
pub mod traits; pub mod traits;
use std::sync::Mutex; use std::collections::HashMap;
use std::net::TcpStream;
use std::sync::Weak;
use std::sync::Arc;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::mem; use std::net::TcpStream;
use std::sync::Mutex;
use std::sync::Arc;
use uuid::Uuid; use uuid::Uuid;
use serde::Serialize; use serde::Serialize;
use crossbeam_channel::{Sender, Receiver, unbounded}; use crossbeam_channel::{Sender, Receiver, unbounded};
use crate::lib::Foundation::{IOwned, ICooperative, IMessagable};
use super::ClientManager;
use traits::IClient; use traits::IClient;
use crate::lib::Foundation::{ICooperative, IMessagable};
use crate::lib::server::ServerMessages;
pub enum ClientMessage { pub enum ClientMessage {}
a,
b,
}
/// # Client /// # Client
/// This struct represents a connected user. /// This struct represents a connected user.
@ -40,43 +36,46 @@ pub struct Client {
// non serializable // non serializable
#[serde(skip)] #[serde(skip)]
server_channel: Sender<ServerMessages>, server_channel: Option<Sender<ServerMessages>>,
#[serde(skip)]
input: Sender<ClientMessage>,
#[serde(skip)]
output: Receiver<ClientMessage>,
#[serde(skip)] #[serde(skip)]
stream: Mutex<Option<TcpStream>>, stream: Mutex<Option<TcpStream>>,
#[serde(skip)]
owner: Mutex<Option<Weak<ClientManager>>>
} }
// client funciton implmentations // client funciton implmentations
impl IClient<ClientMessage> for Client { impl IClient<ClientMessage> for Client {
fn new(uuid: Uuid, name: String, addr: String) -> Arc<Client> { fn new(map: HashMap<String, String>, server_channel: Sender<ServerMessages> ) -> Arc<Client> {
let (sender, reciever) = unbounded(); let (sender, receiver) = unbounded();
Arc::new(Client { Arc::new(Client {
username: name, username: map.get(&"name".to_string()).unwrap().clone(),
uuid: Uuid::new_v4(), uuid: Uuid::parse_str(map.get(&"uuid".to_string()).unwrap().as_str()).expect("invalid id"),
address: addr, address: map.get(&"host".to_string()).unwrap().clone(),
output_channel: Mutex::new(reciever), server_channel: Some(server_channel),
input_channel: Mutex::new(sender),
input: sender,
output: receiver,
stream: Mutex::new(None), stream: Mutex::new(None),
owner: Mutex::new(None)
}) })
} }
// MARK: - removeable // MARK: - removeable
fn send(&self, bytes: Vec<u8>) -> Result<(), &str> { todo!() } fn send(&self, _bytes: Vec<u8>) -> Result<(), &str> { todo!() }
fn recv(&self) -> Option<Vec<u8>> { todo!() } fn recv(&self) -> Option<Vec<u8>> { todo!() }
// Mark: end - // Mark: end -
} }
impl IMessagable<ClientMessage> for Client{ impl IMessagable<ClientMessage> for Client{
fn send_message(&self, msg: ClientMessage) { fn send_message(&self, msg: ClientMessage) {
self.input_channel.lock().unwrap().send(msg); self.input.send(msg).expect("failed to send message to client.");
} }
} }
@ -90,16 +89,17 @@ impl ICooperative for Client {
impl Default for Client { impl Default for Client {
fn default() -> Self { fn default() -> Self {
let (sender, reciever) = unbounded(); let (sender, reciever) = unbounded();
return Client { Client {
username: "generic_client".to_string(), username: "generic_client".to_string(),
uuid: Uuid::new_v4(), uuid: Uuid::new_v4(),
address: "127.0.0.1".to_string(), address: "127.0.0.1".to_string(),
output_channel: Mutex::new(reciever), output: reciever,
input_channel: Mutex::new(sender), input: sender,
server_channel: None,
stream: Mutex::new(None), stream: Mutex::new(None),
owner: Mutex::new(None)
} }
} }
} }

View File

@ -1,6 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use std::collections::HashMap;
use uuid::Uuid; use crossbeam_channel::Sender;
use crate::lib::server::ServerMessages;
/// # TClient /// # TClient
/// This trait represents the methods that a client must implement /// This trait represents the methods that a client must implement
@ -13,7 +16,7 @@ use uuid::Uuid;
/// - send_msg: sends a event message to the client /// - send_msg: sends a event message to the client
/// - recv_msg: used by the client to receive and process event messages /// - recv_msg: used by the client to receive and process event messages
pub trait IClient<TClientMessage> { pub trait IClient<TClientMessage> {
fn new(uuid: Uuid, name: String, addr: String) -> Arc<Self>; fn new(map: HashMap<String, String>, server_channel: Sender<ServerMessages> ) -> Arc<Self>;
fn send(&self, bytes: Vec<u8>) -> Result<(), &str>; fn send(&self, bytes: Vec<u8>) -> Result<(), &str>;
fn recv(&self) -> Option<Vec<u8>>; fn recv(&self) -> Option<Vec<u8>>;