forked from lavina/lavina
1
0
Fork 0
lavina/src/core/room.rs

119 lines
3.5 KiB
Rust

//! Domain of rooms — chats with multiple participants.
use std::{
collections::HashMap,
hash::Hash,
sync::{Arc, RwLock},
};
use prometheus::{IntGauge, Registry as MetricRegistry};
use tokio::sync::RwLock as AsyncRwLock;
use crate::{
core::player::{PlayerHandle, PlayerId},
prelude::*,
};
use super::player::ConnectionId;
/// Opaque room id
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RoomId(pub ByteVec);
/// Shared datastructure for storing metadata about rooms.
#[derive(Clone)]
pub struct RoomRegistry(Arc<RwLock<RoomRegistryInner>>);
impl RoomRegistry {
pub fn empty(metrics: &mut MetricRegistry) -> Result<RoomRegistry> {
let metric_active_rooms =
IntGauge::new("chat_rooms_active", "Number of alive room actors")?;
metrics.register(Box::new(metric_active_rooms.clone()))?;
let inner = RoomRegistryInner {
rooms: HashMap::new(),
metric_active_rooms,
};
Ok(RoomRegistry(Arc::new(RwLock::new(inner))))
}
pub fn get_or_create_room(&mut self, room_id: RoomId) -> RoomHandle {
let mut inner = self.0.write().unwrap();
if let Some(room_handle) = inner.rooms.get(&room_id) {
room_handle.clone()
} else {
let room = Room {
room_id: room_id.clone(),
subscriptions: HashMap::new(),
};
let room_handle = RoomHandle(Arc::new(AsyncRwLock::new(room)));
inner.rooms.insert(room_id, room_handle.clone());
inner.metric_active_rooms.inc();
room_handle
}
}
pub fn get_room(&self, room_id: RoomId) -> Option<RoomHandle> {
let inner = self.0.read().unwrap();
let res = inner.rooms.get(&room_id);
res.map(|r| r.clone())
}
}
struct RoomRegistryInner {
rooms: HashMap<RoomId, RoomHandle>,
metric_active_rooms: IntGauge,
}
#[derive(Clone)]
pub struct RoomHandle(Arc<AsyncRwLock<Room>>);
impl RoomHandle {
pub async fn subscribe(&mut self, player_id: PlayerId, player_handle: PlayerHandle) {
let mut lock = self.0.write().await;
lock.add_subscriber(player_id, player_handle);
}
pub async fn send_message(
&self,
player_id: PlayerId,
connection_id: ConnectionId,
body: String,
) {
let lock = self.0.read().await;
lock.send_message(player_id, connection_id, body).await;
}
pub async fn get_members(&self) -> Vec<PlayerId> {
let lock = self.0.read().await;
lock.subscriptions.keys().map(|x| x.clone()).collect::<Vec<_>>()
}
}
struct Room {
room_id: RoomId,
subscriptions: HashMap<PlayerId, PlayerHandle>,
}
impl Room {
fn add_subscriber(&mut self, player_id: PlayerId, player_handle: PlayerHandle) {
tracing::info!("Adding a subscriber to room");
self.subscriptions.insert(player_id, player_handle);
}
async fn send_message(&self, player_id: PlayerId, connection_id: ConnectionId, body: String) {
tracing::info!("Adding a message to room");
for (_, sub) in &self.subscriptions {
log::info!("Sending a message from room to player");
sub.receive_message(
self.room_id.clone(),
player_id.clone(),
connection_id.clone(),
body.clone(),
)
.await;
}
}
}
pub struct RoomInfo {
pub id: RoomId,
pub members: Vec<PlayerId>,
pub topic: ByteVec,
}