implemeting client functionality and changed types
This commit is contained in:
parent
f4bd223d12
commit
7731e18d8b
19
Cargo.toml
19
Cargo.toml
|
|
@ -8,20 +8,19 @@ edition = "2018"
|
|||
|
||||
[dependencies]
|
||||
regex = "1"
|
||||
uuid = "0.8"
|
||||
crossbeam = "0.7"
|
||||
crossbeam-channel = "0.4"
|
||||
crossbeam-utils = "0.7"
|
||||
crossbeam-queue = "0.2"
|
||||
parking_lot = "0.10"
|
||||
dashmap = "3.11.4"
|
||||
crossbeam = "0.8.0"
|
||||
crossbeam-channel = "0.5.0"
|
||||
crossbeam-queue = "0.3.1"
|
||||
parking_lot = "0.11.1"
|
||||
dashmap = "4.0.2"
|
||||
rayon = "1.3.1"
|
||||
zeroize = "1.1.0"
|
||||
crossterm = "0.17.7"
|
||||
clap = "3.0.0-beta.1"
|
||||
crossterm = "0.19.0"
|
||||
clap = "2.33.3"
|
||||
log = "0.4"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
url = "2.2.0"
|
||||
uuid = {version = "0.8", features = ["serde", "v4"]}
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
|
||||
[profile.dev]
|
||||
|
|
|
|||
|
|
@ -9,3 +9,10 @@ pub trait IOwner<T> {
|
|||
fn get_ref(&self) -> Weak<Self>;
|
||||
}
|
||||
|
||||
pub trait IMessagable<M> {
|
||||
fn send_message(&self, msg: M);
|
||||
}
|
||||
|
||||
pub trait ICooperative {
|
||||
fn tick(&self);
|
||||
}
|
||||
|
|
@ -3,7 +3,6 @@
|
|||
pub mod traits;
|
||||
|
||||
use std::sync::Mutex;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::net::TcpStream;
|
||||
use std::sync::Weak;
|
||||
use std::sync::Arc;
|
||||
|
|
@ -11,10 +10,12 @@ use std::cmp::Ordering;
|
|||
use std::mem;
|
||||
|
||||
use uuid::Uuid;
|
||||
use serde::Serialize;
|
||||
use crossbeam_channel::{Sender, Receiver, unbounded};
|
||||
|
||||
use IOwned::lib::Foundation::IOwned;
|
||||
use crate::lib::Foundation::{IOwned, ICooperative, IMessagable};
|
||||
use super::ClientManager;
|
||||
use traits::TClient;
|
||||
use traits::IClient;
|
||||
|
||||
pub enum ClientMessage {
|
||||
a,
|
||||
|
|
@ -31,38 +32,49 @@ pub enum ClientMessage {
|
|||
///
|
||||
/// - stream: The socket for the connected client.
|
||||
/// - owner: An optional reference to the owning object.
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
#[derive(Serialize)]
|
||||
pub struct Client {
|
||||
pub uuid: String,
|
||||
pub uuid: Uuid,
|
||||
username: String,
|
||||
address: String,
|
||||
|
||||
#[serde(skip)]
|
||||
// non serializable
|
||||
#[serde(skip)]
|
||||
output_channel: Mutex<Receiver<ClientMessage>>,
|
||||
|
||||
#[serde(skip)]
|
||||
input_channel: Mutex<Sender<ClientMessage>>,
|
||||
|
||||
#[serde(skip)]
|
||||
stream: Mutex<Option<TcpStream>>,
|
||||
|
||||
#[serde(skip)]
|
||||
#[serde(skip)]
|
||||
owner: Mutex<Option<Weak<ClientManager>>>
|
||||
|
||||
}
|
||||
|
||||
impl TClient<ClientMessage> for Client {
|
||||
// client funciton implmentations
|
||||
impl IClient<ClientMessage> for Client {
|
||||
fn new(uuid: Uuid, name: String, addr: String) -> Arc<Client> {
|
||||
let (sender, reciever) = unbounded();
|
||||
|
||||
Arc::new(Client {
|
||||
username: name,
|
||||
uuid: uuid.to_string(),
|
||||
uuid: Uuid::new_v4(),
|
||||
address: addr,
|
||||
|
||||
output_channel: Mutex::new(reciever),
|
||||
input_channel: Mutex::new(sender),
|
||||
|
||||
stream: Mutex::new(None),
|
||||
owner: Mutex::new(None)
|
||||
})
|
||||
}
|
||||
|
||||
// MARK: - removeable
|
||||
fn send(&self, bytes: Vec<u8>) -> Result<(), &str> { todo!() }
|
||||
fn recv(&self) -> Option<Vec<u8>> { todo!() }
|
||||
|
||||
fn send_msg(&self, msg: ClientMessage) -> Result<(), &str> { todo!() }
|
||||
fn recv_msg(&self) -> Option<ClientMessage> { todo!() }
|
||||
|
||||
fn tick(&self) { }
|
||||
// Mark: end -
|
||||
}
|
||||
|
||||
impl IOwned<ClientManager> for Client {
|
||||
|
|
@ -72,7 +84,37 @@ impl IOwned<ClientManager> for Client {
|
|||
}
|
||||
}
|
||||
|
||||
impl IMessagable<ClientMessage> for Client{
|
||||
fn send_message(&self, msg: ClientMessage) {
|
||||
self.input_channel.lock().unwrap().send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// cooperative multitasking implementation
|
||||
impl ICooperative for Client {
|
||||
fn tick(&self) {
|
||||
}
|
||||
}
|
||||
|
||||
// default value implementation
|
||||
impl Default for Client {
|
||||
fn default() -> Self {
|
||||
let (sender, reciever) = unbounded();
|
||||
return Client {
|
||||
username: "generic_client".to_string(),
|
||||
uuid: Uuid::new_v4(),
|
||||
address: "127.0.0.1".to_string(),
|
||||
|
||||
output_channel: Mutex::new(reciever),
|
||||
input_channel: Mutex::new(sender),
|
||||
|
||||
stream: Mutex::new(None),
|
||||
owner: Mutex::new(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - used for sorting.
|
||||
impl PartialEq for Client {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.uuid == other.uuid
|
||||
|
|
@ -82,15 +124,14 @@ impl PartialEq for Client {
|
|||
impl Eq for Client {
|
||||
}
|
||||
|
||||
impl PartialOrd for Client {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Client {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
self.uuid.cmp(&other.uuid)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Client {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,14 +12,9 @@ use uuid::Uuid;
|
|||
/// - recv: if there is a message in the queue, returns the message
|
||||
/// - send_msg: sends a event message to the client
|
||||
/// - recv_msg: used by the client to receive and process event messages
|
||||
pub trait TClient<TClientMessage> {
|
||||
pub trait IClient<TClientMessage> {
|
||||
fn new(uuid: Uuid, name: String, addr: String) -> Arc<Self>;
|
||||
|
||||
fn send(&self, bytes: Vec<u8>) -> Result<(), &str>;
|
||||
fn recv(&self) -> Option<Vec<u8>>;
|
||||
|
||||
fn send_msg(&self, msg: TClientMessage) -> Result<(), &str>;
|
||||
fn recv_msg(&self) -> Option<TClientMessage>;
|
||||
|
||||
fn tick(&self);
|
||||
}
|
||||
|
|
@ -8,13 +8,12 @@ use std::sync::Weak;
|
|||
use crossbeam_channel::{unbounded, Receiver, Sender};
|
||||
use uuid::Uuid;
|
||||
|
||||
use IOwner::lib::Foundation::IOwner;
|
||||
use IOwned::lib::Foundation::IOwned;
|
||||
use crate::lib::Foundation::{IOwner, IOwned};
|
||||
use self::client::Client;
|
||||
use self::client::ClientMessage;
|
||||
// use client::client_v3::Client;
|
||||
use self::traits::TClientManager;
|
||||
use client::traits::TClient;
|
||||
use crate::lib::Foundation::IMessagable;
|
||||
use crate::lib::Foundation::ICooperative;
|
||||
|
||||
enum ClientManagerMessages {}
|
||||
|
||||
|
|
@ -54,9 +53,7 @@ impl ClientManager {
|
|||
manager_ref
|
||||
}
|
||||
|
||||
pub fn get_ref(&self) -> Weak<Self> {
|
||||
self.weak_self.lock().unwrap().clone().unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn set_ref(&self, reference: Arc<Self>) {
|
||||
let mut lock = self.weak_self.lock().unwrap();
|
||||
|
|
@ -70,21 +67,19 @@ impl TClientManager<Client, ClientMessage> for ClientManager {
|
|||
}
|
||||
|
||||
fn remove_client(&self, uuid: Uuid) {
|
||||
let uuid_str = uuid.to_string();
|
||||
let mut client_list = self.clients.lock().unwrap();
|
||||
client_list.sort();
|
||||
if let Ok(index) = client_list.binary_search_by(move |client| client.uuid.cmp(&uuid_str)) {
|
||||
if let Ok(index) = client_list.binary_search_by(move |client| client.uuid.cmp(&uuid)) {
|
||||
client_list.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
fn message_client(&self, id: Uuid, msg: ClientMessage) -> Result<(), &str> {
|
||||
let uuid_str = id.to_string();
|
||||
let mut client_list = self.clients.lock().unwrap();
|
||||
client_list.sort();
|
||||
if let Ok(index) = client_list.binary_search_by(move |client| client.uuid.cmp(&uuid_str)) {
|
||||
if let Ok(index) = client_list.binary_search_by(move |client| client.uuid.cmp(&id)) {
|
||||
if let Some(client) = client_list.get(index) {
|
||||
let _ = client.send_msg(msg);
|
||||
let _ = client.send_message(msg);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
@ -100,6 +95,10 @@ impl IOwner<Client> for ClientManager{
|
|||
fn add_child(&self, child: Arc<Client>) {
|
||||
child.set_owner(self.get_ref());
|
||||
self.clients.lock().unwrap().push(child);
|
||||
}
|
||||
|
||||
fn get_ref(&self) -> Weak<Self> {
|
||||
self.weak_self.lock().unwrap().clone().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -107,6 +106,7 @@ impl IOwner<Client> for ClientManager{
|
|||
mod test {
|
||||
use super::ClientManager;
|
||||
use std::sync::Arc;
|
||||
use crate::lib::Foundation::{IOwner};
|
||||
|
||||
#[test]
|
||||
fn test_get_ref() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue