add dashboard

This commit is contained in:
nora 2022-02-19 21:53:02 +01:00
parent 077b6fd633
commit dc8efd4e4e
13 changed files with 777 additions and 33 deletions

10
amqp_core/Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "amqp_core"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
parking_lot = "0.12.0"
uuid = "0.8.2"

59
amqp_core/src/lib.rs Normal file
View file

@ -0,0 +1,59 @@
use parking_lot::Mutex;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct GlobalData {
inner: Arc<Mutex<GlobalDataInner>>,
}
impl Default for GlobalData {
fn default() -> Self {
Self {
inner: Arc::new(Mutex::new(GlobalDataInner {
connections: HashMap::new(),
})),
}
}
}
impl GlobalData {
pub fn lock(&self) -> parking_lot::MutexGuard<'_, GlobalDataInner> {
self.inner.lock()
}
}
#[derive(Debug)]
pub struct GlobalDataInner {
pub connections: HashMap<Uuid, ConnectionHandle>,
}
pub type ConnectionHandle = Arc<Mutex<Connection>>;
#[derive(Debug)]
pub struct Connection {
pub id: Uuid,
pub peer_addr: SocketAddr,
pub global_data: GlobalData,
}
impl Connection {
pub fn new_handle(
id: Uuid,
peer_addr: SocketAddr,
global_data: GlobalData,
) -> ConnectionHandle {
Arc::new(Mutex::new(Self {
id,
peer_addr,
global_data,
}))
}
pub fn close(&self) {
let mut global_data = self.global_data.lock();
global_data.connections.remove(&self.id);
}
}