2023-02-04 01:01:49 +00:00
|
|
|
//! Domain of rooms — chats with multiple participants.
|
2023-02-03 22:43:59 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
hash::Hash,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
|
|
|
|
2023-02-12 22:23:52 +00:00
|
|
|
use prometheus::{IntGauge, Registry as MetricRegistry};
|
2023-02-14 00:12:27 +00:00
|
|
|
use tokio::sync::RwLock as AsyncRwLock;
|
2023-02-03 22:43:59 +00:00
|
|
|
|
|
|
|
use crate::{
|
2023-02-14 22:22:04 +00:00
|
|
|
core::player::{PlayerHandle, PlayerId, Updates},
|
2023-02-03 22:43:59 +00:00
|
|
|
prelude::*,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Opaque room id
|
2023-02-12 23:31:16 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2023-02-14 19:07:07 +00:00
|
|
|
pub struct RoomId(ByteVec);
|
|
|
|
impl RoomId {
|
|
|
|
pub fn from_bytes(bytes: ByteVec) -> Result<RoomId> {
|
|
|
|
if bytes.len() > 32 {
|
2023-02-14 19:42:52 +00:00
|
|
|
return Err(anyhow::Error::msg(
|
|
|
|
"Room name cannot be longer than 32 symbols",
|
|
|
|
));
|
2023-02-14 19:07:07 +00:00
|
|
|
}
|
|
|
|
if bytes.contains(&b' ') {
|
|
|
|
return Err(anyhow::Error::msg("Room name cannot contain spaces"));
|
|
|
|
}
|
|
|
|
Ok(RoomId(bytes))
|
|
|
|
}
|
2023-02-14 19:42:52 +00:00
|
|
|
pub fn as_bytes(&self) -> &ByteVec {
|
|
|
|
&self.0
|
|
|
|
}
|
2023-02-14 19:07:07 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
|
|
|
|
/// Shared datastructure for storing metadata about rooms.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RoomRegistry(Arc<RwLock<RoomRegistryInner>>);
|
|
|
|
impl RoomRegistry {
|
2023-02-12 22:23:52 +00:00
|
|
|
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()))?;
|
2023-02-03 22:43:59 +00:00
|
|
|
let inner = RoomRegistryInner {
|
|
|
|
rooms: HashMap::new(),
|
2023-02-12 22:23:52 +00:00
|
|
|
metric_active_rooms,
|
2023-02-03 22:43:59 +00:00
|
|
|
};
|
2023-02-12 22:23:52 +00:00
|
|
|
Ok(RoomRegistry(Arc::new(RwLock::new(inner))))
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 23:31:16 +00:00
|
|
|
pub fn get_or_create_room(&mut self, room_id: RoomId) -> RoomHandle {
|
2023-02-03 22:43:59 +00:00
|
|
|
let mut inner = self.0.write().unwrap();
|
2023-02-14 00:12:27 +00:00
|
|
|
if let Some(room_handle) = inner.rooms.get(&room_id) {
|
2023-02-13 18:32:52 +00:00
|
|
|
room_handle.clone()
|
|
|
|
} else {
|
2023-02-14 00:12:27 +00:00
|
|
|
let room = Room {
|
|
|
|
room_id: room_id.clone(),
|
|
|
|
subscriptions: HashMap::new(),
|
2023-02-14 18:46:42 +00:00
|
|
|
topic: b"New room".to_vec(),
|
2023-02-14 00:12:27 +00:00
|
|
|
};
|
|
|
|
let room_handle = RoomHandle(Arc::new(AsyncRwLock::new(room)));
|
|
|
|
inner.rooms.insert(room_id, room_handle.clone());
|
2023-02-13 18:32:52 +00:00
|
|
|
inner.metric_active_rooms.inc();
|
|
|
|
room_handle
|
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:53:43 +00:00
|
|
|
pub fn get_room(&self, room_id: &RoomId) -> Option<RoomHandle> {
|
2023-02-03 22:43:59 +00:00
|
|
|
let inner = self.0.read().unwrap();
|
2023-02-14 17:53:43 +00:00
|
|
|
let res = inner.rooms.get(room_id);
|
2023-02-14 00:12:27 +00:00
|
|
|
res.map(|r| r.clone())
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RoomRegistryInner {
|
2023-02-14 00:12:27 +00:00
|
|
|
rooms: HashMap<RoomId, RoomHandle>,
|
2023-02-12 22:23:52 +00:00
|
|
|
metric_active_rooms: IntGauge,
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2023-02-14 00:12:27 +00:00
|
|
|
pub struct RoomHandle(Arc<AsyncRwLock<Room>>);
|
2023-02-03 22:43:59 +00:00
|
|
|
impl RoomHandle {
|
2023-02-14 22:22:04 +00:00
|
|
|
pub async fn subscribe(&self, player_id: PlayerId, player_handle: PlayerHandle) {
|
2023-02-14 00:12:27 +00:00
|
|
|
let mut lock = self.0.write().await;
|
2023-02-14 22:22:04 +00:00
|
|
|
lock.add_subscriber(player_id, player_handle).await;
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:22:04 +00:00
|
|
|
pub async fn send_message(&self, player_id: PlayerId, body: String) {
|
2023-02-14 00:12:27 +00:00
|
|
|
let lock = self.0.read().await;
|
2023-02-14 22:22:04 +00:00
|
|
|
lock.send_message(player_id, body).await;
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
2023-02-14 00:44:03 +00:00
|
|
|
|
2023-02-14 17:53:43 +00:00
|
|
|
pub async fn get_room_info(&self) -> RoomInfo {
|
|
|
|
let lock = self.0.read().await;
|
|
|
|
RoomInfo {
|
|
|
|
id: lock.room_id.clone(),
|
|
|
|
members: lock
|
|
|
|
.subscriptions
|
|
|
|
.keys()
|
|
|
|
.map(|x| x.clone())
|
|
|
|
.collect::<Vec<_>>(),
|
2023-02-14 18:46:42 +00:00
|
|
|
topic: lock.topic.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 22:22:04 +00:00
|
|
|
pub async fn set_topic(&mut self, changer_id: PlayerId, new_topic: ByteVec) {
|
2023-02-14 18:46:42 +00:00
|
|
|
let mut lock = self.0.write().await;
|
|
|
|
lock.topic = new_topic.clone();
|
2023-02-14 22:22:04 +00:00
|
|
|
let update = Updates::RoomTopicChanged {
|
|
|
|
room_id: lock.room_id.clone(),
|
|
|
|
new_topic: new_topic.clone(),
|
|
|
|
};
|
|
|
|
lock.broadcast_update(update, &changer_id).await;
|
2023-02-14 00:44:03 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Room {
|
2023-02-14 00:12:27 +00:00
|
|
|
room_id: RoomId,
|
2023-02-03 22:43:59 +00:00
|
|
|
subscriptions: HashMap<PlayerId, PlayerHandle>,
|
2023-02-14 18:46:42 +00:00
|
|
|
topic: ByteVec,
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
impl Room {
|
2023-02-14 22:22:04 +00:00
|
|
|
async fn add_subscriber(&mut self, player_id: PlayerId, player_handle: PlayerHandle) {
|
2023-02-14 00:12:27 +00:00
|
|
|
tracing::info!("Adding a subscriber to room");
|
2023-02-14 17:53:43 +00:00
|
|
|
self.subscriptions.insert(player_id.clone(), player_handle);
|
2023-02-14 19:42:52 +00:00
|
|
|
let update = Updates::RoomJoined {
|
|
|
|
room_id: self.room_id.clone(),
|
|
|
|
new_member_id: player_id.clone(),
|
|
|
|
};
|
2023-02-14 22:22:04 +00:00
|
|
|
self.broadcast_update(update, &player_id).await;
|
2023-02-14 00:12:27 +00:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:22:04 +00:00
|
|
|
async fn send_message(&self, author_id: PlayerId, body: String) {
|
2023-02-14 00:12:27 +00:00
|
|
|
tracing::info!("Adding a message to room");
|
2023-02-14 19:42:52 +00:00
|
|
|
let update = Updates::NewMessage {
|
|
|
|
room_id: self.room_id.clone(),
|
2023-02-14 22:22:04 +00:00
|
|
|
author_id: author_id.clone(),
|
2023-02-14 19:42:52 +00:00
|
|
|
body,
|
|
|
|
};
|
2023-02-14 22:22:04 +00:00
|
|
|
self.broadcast_update(update, &author_id).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn broadcast_update(&self, update: Updates, except: &PlayerId) {
|
|
|
|
for (player_id, sub) in &self.subscriptions {
|
|
|
|
if player_id == except {
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-14 00:12:27 +00:00
|
|
|
log::info!("Sending a message from room to player");
|
2023-02-14 19:42:52 +00:00
|
|
|
sub.update(update.clone()).await;
|
2023-02-14 00:12:27 +00:00
|
|
|
}
|
2023-02-03 22:43:59 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-14 00:44:03 +00:00
|
|
|
|
|
|
|
pub struct RoomInfo {
|
|
|
|
pub id: RoomId,
|
|
|
|
pub members: Vec<PlayerId>,
|
|
|
|
pub topic: ByteVec,
|
|
|
|
}
|