mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 15:24:48 +01:00
feat: CodempClient is also an inner Arc<>
This commit is contained in:
parent
e1ba683fd0
commit
775cde1d7f
1 changed files with 17 additions and 11 deletions
|
@ -2,6 +2,8 @@
|
||||||
//!
|
//!
|
||||||
//! codemp client manager, containing grpc services
|
//! codemp client manager, containing grpc services
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use dashmap::DashMap;
|
use dashmap::DashMap;
|
||||||
use tonic::transport::{Channel, Endpoint};
|
use tonic::transport::{Channel, Endpoint};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
@ -10,7 +12,7 @@ use codemp_proto::auth::auth_client::AuthClient;
|
||||||
use codemp_proto::auth::{Token, WorkspaceJoinRequest};
|
use codemp_proto::auth::{Token, WorkspaceJoinRequest};
|
||||||
use crate::workspace::Workspace;
|
use crate::workspace::Workspace;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Debug)]
|
||||||
pub struct AuthWrap {
|
pub struct AuthWrap {
|
||||||
username: String,
|
username: String,
|
||||||
password: String,
|
password: String,
|
||||||
|
@ -49,7 +51,11 @@ impl AuthWrap {
|
||||||
/// contains all required grpc services and the unique user id
|
/// contains all required grpc services and the unique user id
|
||||||
/// will disconnect when dropped
|
/// will disconnect when dropped
|
||||||
/// can be used to interact with server
|
/// can be used to interact with server
|
||||||
pub struct Client {
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Client(Arc<ClientInner>);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct ClientInner {
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
host: String,
|
host: String,
|
||||||
workspaces: DashMap<String, Workspace>,
|
workspaces: DashMap<String, Workspace>,
|
||||||
|
@ -72,42 +78,42 @@ impl Client {
|
||||||
let user_id = uuid::Uuid::new_v4();
|
let user_id = uuid::Uuid::new_v4();
|
||||||
let auth = AuthWrap::try_new(username.as_ref(), password.as_ref(), &host).await?;
|
let auth = AuthWrap::try_new(username.as_ref(), password.as_ref(), &host).await?;
|
||||||
|
|
||||||
Ok(Client {
|
Ok(Client(Arc::new(ClientInner {
|
||||||
user_id,
|
user_id,
|
||||||
host,
|
host,
|
||||||
workspaces: DashMap::default(),
|
workspaces: DashMap::default(),
|
||||||
auth,
|
auth,
|
||||||
})
|
})))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// join a workspace, returns an [tokio::sync::RwLock] to interact with it
|
/// join a workspace, returns an [tokio::sync::RwLock] to interact with it
|
||||||
pub async fn join_workspace(&self, workspace: impl AsRef<str>) -> crate::Result<Workspace> {
|
pub async fn join_workspace(&self, workspace: impl AsRef<str>) -> crate::Result<Workspace> {
|
||||||
let token = self.auth.login_workspace(workspace.as_ref()).await?;
|
let token = self.0.auth.login_workspace(workspace.as_ref()).await?;
|
||||||
|
|
||||||
let ws = Workspace::try_new(
|
let ws = Workspace::try_new(
|
||||||
workspace.as_ref().to_string(),
|
workspace.as_ref().to_string(),
|
||||||
self.user_id,
|
self.0.user_id,
|
||||||
&self.host,
|
&self.0.host,
|
||||||
token.clone()
|
token.clone()
|
||||||
).await?;
|
).await?;
|
||||||
|
|
||||||
self.workspaces.insert(workspace.as_ref().to_string(), ws.clone());
|
self.0.workspaces.insert(workspace.as_ref().to_string(), ws.clone());
|
||||||
|
|
||||||
Ok(ws)
|
Ok(ws)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// leaves a [Workspace] by name
|
/// leaves a [Workspace] by name
|
||||||
pub fn leave_workspace(&self, id: &str) -> bool {
|
pub fn leave_workspace(&self, id: &str) -> bool {
|
||||||
self.workspaces.remove(id).is_some()
|
self.0.workspaces.remove(id).is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// gets a [Workspace] by name
|
/// gets a [Workspace] by name
|
||||||
pub fn get_workspace(&self, id: &str) -> Option<Workspace> {
|
pub fn get_workspace(&self, id: &str) -> Option<Workspace> {
|
||||||
self.workspaces.get(id).map(|x| x.clone())
|
self.0.workspaces.get(id).map(|x| x.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// accessor for user id
|
/// accessor for user id
|
||||||
pub fn user_id(&self) -> Uuid {
|
pub fn user_id(&self) -> Uuid {
|
||||||
self.user_id
|
self.0.user_id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue