fix(id): fix add one to next id

- Fixed add one to next ID
- Added command /id
- Added `id` field to Client struct
This commit is contained in:
MedzikUser 2022-08-12 22:52:47 +02:00
parent d0120a0703
commit 25c2f0baa3
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
8 changed files with 87 additions and 24 deletions

View File

@ -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")
}
}

25
renovate.json Normal file
View File

@ -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
}
]
}

View File

@ -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()
}
}

View File

@ -40,8 +40,6 @@ impl Command for Help {
))
}
client.send(msg.join("\n"))?;
Ok(())
client.send(msg.join("\n"))
}
}

26
src/commands/id.rs Normal file
View File

@ -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)
}
}

View File

@ -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<Box<dyn Command>> {
vec![Box::new(Help), Box::new(Disconnect)]
vec![Box::new(Help), Box::new(Disconnect), Box::new(ID)]
}

View File

@ -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<String, ClientMapValue>,
pub plugins_manager: PluginsManagerType,
@ -47,6 +48,7 @@ pub enum ClientStream {
impl From<TcpStream> 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<TcpStream> for Client {
impl From<WebSocket<TcpStream>> for Client {
fn from(stream: WebSocket<TcpStream>) -> 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<WebSocket<TcpStream>> 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<Self> {
pub fn new_websocket(stream: TcpStream, id: usize) -> anyhow::Result<Self> {
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

View File

@ -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