Development into Master #10

Merged
michael-bailey merged 102 commits from Development into master 2021-08-03 20:53:15 +00:00
4 changed files with 50 additions and 8 deletions
Showing only changes of commit f4bd223d12 - Show all commits

11
src/lib/Foundation/mod.rs Normal file
View File

@ -0,0 +1,11 @@
use std::sync::{Weak,Arc};
pub trait IOwned<T> {
fn set_owner(&self, owner: Weak<T>);
}
pub trait IOwner<T> {
fn add_child(&self, child: Arc<T>);
fn get_ref(&self) -> Weak<Self>;
}

View File

@ -1,6 +1,7 @@
// pub mod commands;
pub mod prelude;
pub mod server;
pub mod Foundation;
use std::sync::Arc;
use std::sync::Mutex;

View File

@ -2,13 +2,17 @@
// pub mod client_v3;
pub mod traits;
use std::sync::Mutex;
use serde::{Serialize, Deserialize};
use std::net::TcpStream;
use std::sync::Weak;
use std::sync::Arc;
use uuid::Uuid;
use std::cmp::Ordering;
use std::mem;
use uuid::Uuid;
use IOwned::lib::Foundation::IOwned;
use super::ClientManager;
use traits::TClient;
@ -27,21 +31,30 @@ pub enum ClientMessage {
///
/// - stream: The socket for the connected client.
/// - owner: An optional reference to the owning object.
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
pub struct Client {
pub uuid: String,
username: String,
address: String,
#[serde(skip)]
stream: Option<TcpStream>,
stream: Mutex<Option<TcpStream>>,
#[serde(skip)]
owner: Option<Weak<ClientManager>>
owner: Mutex<Option<Weak<ClientManager>>>
}
impl TClient<ClientMessage> for Client {
fn new(uuid: Uuid, name: String, addr: String) -> Arc<Client> { todo!() }
fn new(uuid: Uuid, name: String, addr: String) -> Arc<Client> {
Arc::new(Client {
username: name,
uuid: uuid.to_string(),
address: addr,
stream: Mutex::new(None),
owner: Mutex::new(None)
})
}
fn send(&self, bytes: Vec<u8>) -> Result<(), &str> { todo!() }
fn recv(&self) -> Option<Vec<u8>> { todo!() }
@ -52,6 +65,14 @@ impl TClient<ClientMessage> for Client {
fn tick(&self) { }
}
impl IOwned<ClientManager> for Client {
fn set_owner(&self, owner: Weak<ClientManager>) {
let mut owner_mut = self.owner.lock().unwrap();
let _ = mem::replace(&mut *owner_mut, Some(owner));
}
}
impl PartialEq for Client {
fn eq(&self, other: &Self) -> bool {
self.uuid == other.uuid
@ -71,4 +92,5 @@ impl PartialOrd for Client {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
}

View File

@ -6,9 +6,10 @@ use std::sync::Mutex;
use std::sync::Weak;
use crossbeam_channel::{unbounded, Receiver, Sender};
use uuid::Uuid;
use IOwner::lib::Foundation::IOwner;
use IOwned::lib::Foundation::IOwned;
use self::client::Client;
use self::client::ClientMessage;
// use client::client_v3::Client;
@ -65,7 +66,7 @@ impl ClientManager {
impl TClientManager<Client, ClientMessage> for ClientManager {
fn add_client(&self, client: std::sync::Arc<Client>) {
self.clients.lock().unwrap().push(client);
self.add_child(client);
}
fn remove_client(&self, uuid: Uuid) {
@ -95,6 +96,13 @@ impl TClientManager<Client, ClientMessage> for ClientManager {
}
}
impl IOwner<Client> for ClientManager{
fn add_child(&self, child: Arc<Client>) {
child.set_owner(self.get_ref());
self.clients.lock().unwrap().push(child);
}
}
#[cfg(test)]
mod test {
use super::ClientManager;