feat(client): HashMap add Mutex and functions

Changed map type in Client struct to Arc<Mutex<HashMap<String, ClientMapValue>>>.
Implemented functions insert_key, get_value and delete_key to the Client type.
Re-export servers::server::ClientMapValue in servers::plugins::prelude.
This commit is contained in:
MedzikUser 2022-08-17 15:40:11 +02:00
parent bf1c3c4092
commit 7da5daf522
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
2 changed files with 28 additions and 9 deletions

View File

@ -12,5 +12,5 @@ pub mod prelude {
pub use async_trait::async_trait; pub use async_trait::async_trait;
pub use self::types::*; pub use self::types::*;
pub use crate::server::Client; pub use crate::server::{Client, ClientMapValue};
} }

View File

@ -22,7 +22,7 @@ pub struct Client {
/// Connection stream of the client /// Connection stream of the client
pub stream: ClientStream, pub stream: ClientStream,
/// Custom Client Map /// Custom Client Map
pub map: HashMap<String, ClientMapValue>, pub map: Arc<Mutex<HashMap<String, ClientMapValue>>>,
/// Plugins Manager /// Plugins Manager
pub plugins_manager: PluginsManagerType, pub plugins_manager: PluginsManagerType,
} }
@ -38,15 +38,10 @@ pub struct Client {
/// Value type of the client map entry /// Value type of the client map entry
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum ClientMapValue { pub enum ClientMapValue {
/// String type
String(String), String(String),
/// Vector with String type
Array(Vec<String>), Array(Vec<String>),
/// bool type
Bool(bool), Bool(bool),
/// isize type
Int(isize), Int(isize),
/// usize type
UInt(usize), UInt(usize),
} }
@ -64,7 +59,7 @@ impl From<TcpStream> for Client {
Self { Self {
id: 0, id: 0,
stream: ClientStream::TCP(Arc::new(stream)), stream: ClientStream::TCP(Arc::new(stream)),
map: HashMap::new(), map: Arc::new(Mutex::new(HashMap::new())),
plugins_manager: PLUGINS_MANAGER.clone(), plugins_manager: PLUGINS_MANAGER.clone(),
} }
} }
@ -75,7 +70,7 @@ impl From<WebSocket<TcpStream>> for Client {
Self { Self {
id: 0, id: 0,
stream: ClientStream::WebSocket(Arc::new(Mutex::new(stream))), stream: ClientStream::WebSocket(Arc::new(Mutex::new(stream))),
map: HashMap::new(), map: Arc::new(Mutex::new(HashMap::new())),
plugins_manager: PLUGINS_MANAGER.clone(), plugins_manager: PLUGINS_MANAGER.clone(),
} }
} }
@ -189,6 +184,30 @@ impl Client {
Ok(()) Ok(())
} }
/// Inserts a key-value pair into the map.
pub fn insert_key<S>(&self, key: S, value: ClientMapValue) -> Option<ClientMapValue>
where
S: ToString,
{
self.map.lock().unwrap().insert(key.to_string(), value)
}
/// Returns the value from the key.
pub fn get_value<S>(&self, key: S) -> Option<ClientMapValue>
where
S: ToString,
{
self.map.lock().unwrap().get(&key.to_string()).cloned()
}
/// Delete key from the map.
pub fn delete_key<S>(&self, key: S) -> Option<ClientMapValue>
where
S: ToString,
{
self.map.lock().unwrap().remove(&key.to_string())
}
pub async fn run_events(&self, event_type: EventType) -> anyhow::Result<()> { pub async fn run_events(&self, event_type: EventType) -> anyhow::Result<()> {
for event in self.plugins_manager.events.iter() { for event in self.plugins_manager.events.iter() {
if event.event() == event_type { if event.event() == event_type {