From 25c2f0baa32147fb60f81e20e2ec5608cddd294e Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Fri, 12 Aug 2022 22:52:47 +0200 Subject: [PATCH] fix(id): fix add one to next id - Fixed add one to next ID - Added command /id - Added `id` field to Client struct --- plugin_test/src/lib.rs | 4 +--- renovate.json | 25 +++++++++++++++++++++++++ src/commands/disconnect.rs | 5 +---- src/commands/help.rs | 4 +--- src/commands/id.rs | 26 ++++++++++++++++++++++++++ src/commands/mod.rs | 5 +++-- src/tcp/client.rs | 19 +++++++++++++++---- src/tcp/server.rs | 23 +++++++++++++++-------- 8 files changed, 87 insertions(+), 24 deletions(-) create mode 100644 renovate.json create mode 100644 src/commands/id.rs diff --git a/plugin_test/src/lib.rs b/plugin_test/src/lib.rs index 01d78f7..8622dcf 100644 --- a/plugin_test/src/lib.rs +++ b/plugin_test/src/lib.rs @@ -32,9 +32,7 @@ impl Command for PluginTest { } /// Command function. async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> { - client.send("successful executed command from dylib")?; - - Ok(()) + client.send("successful executed command from dylib") } } diff --git a/renovate.json b/renovate.json new file mode 100644 index 0000000..d5d1e0b --- /dev/null +++ b/renovate.json @@ -0,0 +1,25 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": [ + "config:base", + "schedule:weekly", + "group:allNonMajor", + ":semanticCommits" + ], + "labels": [ + "dependencies" + ], + "automergeType": "pr", + "prCreation": "immediate", + "packageRules": [ + { + "matchUpdateTypes": [ + "minor", + "patch", + "pin", + "digest" + ], + "automerge": true + } + ] +} diff --git a/src/commands/disconnect.rs b/src/commands/disconnect.rs index 1ce1f04..a448c00 100644 --- a/src/commands/disconnect.rs +++ b/src/commands/disconnect.rs @@ -21,9 +21,6 @@ impl Command for Disconnect { } async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> { - // close the connection - client.close()?; - - Ok(()) + client.close() } } diff --git a/src/commands/help.rs b/src/commands/help.rs index 8eaa858..982b30e 100644 --- a/src/commands/help.rs +++ b/src/commands/help.rs @@ -40,8 +40,6 @@ impl Command for Help { )) } - client.send(msg.join("\n"))?; - - Ok(()) + client.send(msg.join("\n")) } } diff --git a/src/commands/id.rs b/src/commands/id.rs new file mode 100644 index 0000000..d1e743e --- /dev/null +++ b/src/commands/id.rs @@ -0,0 +1,26 @@ +use crate::plugins::prelude::*; + +pub struct ID; + +#[async_trait] +impl Command for ID { + fn name(&self) -> &'static str { + "/id" + } + + fn aliases(&self) -> Vec<&'static str> { + Vec::new() + } + + fn help(&self) -> &'static str { + "Get id of the client" + } + + fn usage(&self) -> &'static str { + "/id" + } + + async fn execute(&self, client: &Client, _args: Vec<&str>) -> anyhow::Result<()> { + client.send(client.id) + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 27b6d18..8c77122 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,9 +1,10 @@ mod disconnect; mod help; +mod id; -use self::{disconnect::Disconnect, help::Help}; +use self::{disconnect::Disconnect, help::Help, id::ID}; use crate::plugins::prelude::*; pub fn register_commands() -> Vec> { - vec![Box::new(Help), Box::new(Disconnect)] + vec![Box::new(Help), Box::new(Disconnect), Box::new(ID)] } diff --git a/src/tcp/client.rs b/src/tcp/client.rs index 8afc426..b0efbda 100644 --- a/src/tcp/client.rs +++ b/src/tcp/client.rs @@ -16,6 +16,7 @@ pub const MAX_PACKET_LEN: usize = 65536; #[derive(Debug, Clone)] pub struct Client { + pub id: usize, pub stream: ClientStream, pub map: HashMap, pub plugins_manager: PluginsManagerType, @@ -47,6 +48,7 @@ pub enum ClientStream { impl From for Client { fn from(stream: TcpStream) -> Self { Self { + id: 0, stream: ClientStream::TCP(Arc::new(stream)), map: HashMap::new(), plugins_manager: PLUGINS_MANAGER.clone(), @@ -57,6 +59,7 @@ impl From for Client { impl From> for Client { fn from(stream: WebSocket) -> Self { Self { + id: 0, stream: ClientStream::WebSocket(Arc::new(Mutex::new(stream))), map: HashMap::new(), plugins_manager: PLUGINS_MANAGER.clone(), @@ -66,15 +69,23 @@ impl From> for Client { impl Client { /// Create a new TCP Client instance - pub fn new_tcp(stream: TcpStream) -> Self { - Self::from(stream) + pub fn new_tcp(stream: TcpStream, id: usize) -> Self { + let mut client = Self::from(stream); + + client.id = id; + + client } /// Create a new WebSocket Client instance - pub fn new_websocket(stream: TcpStream) -> anyhow::Result { + pub fn new_websocket(stream: TcpStream, id: usize) -> anyhow::Result { let websocket = accept(stream)?; - Ok(Self::from(websocket)) + let mut client = Self::from(websocket); + + client.id = id; + + Ok(client) } /// Recieve a message from the client diff --git a/src/tcp/server.rs b/src/tcp/server.rs index 48ade3f..0c61570 100644 --- a/src/tcp/server.rs +++ b/src/tcp/server.rs @@ -88,11 +88,15 @@ async fn start_tcp(host: String) -> anyhow::Result<()> { for stream in incoming { let stream = stream?; - task::spawn(async { - let client = Client::new_tcp(stream); + // get id for the client + let id = *CLIENT_NEXT.lock().unwrap(); + // add one to next id + *CLIENT_NEXT.lock().unwrap() += 1; + + task::spawn(async move { // get id for the client and add one to next id - let id = (*CLIENT_NEXT.lock().unwrap() + 1).clone(); + let client = Client::new_tcp(stream, id); // insert the cloned client to CLIENTS CLIENTS.lock().unwrap().insert(id, client.clone()); @@ -117,17 +121,20 @@ async fn start_websocket(host: String) -> anyhow::Result<()> { for stream in incoming { let stream = stream?; - task::spawn(async { - let client = Client::new_websocket(stream).unwrap(); + // get id for the client + let id = *CLIENT_NEXT.lock().unwrap(); - // get id for the client and add one to next id - let id = (*CLIENT_NEXT.lock().unwrap() + 1).clone(); + // add one to next id + *CLIENT_NEXT.lock().unwrap() += 1; + + task::spawn(async move { + let client = Client::new_websocket(stream, id).unwrap(); // insert the cloned client to CLIENTS CLIENTS.lock().unwrap().insert(id, client.clone()); if let Err(err) = process(client).await { - error!("TCP client error: {}", err); + error!("WebSocket client error: {}", err); } // delete the client from CLIENTS map