forked from lavina/lavina
broadcasting
This commit is contained in:
parent
486bc17ec5
commit
23a59bc303
|
@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
mod broadcast;
|
pub mod broadcast;
|
||||||
|
|
||||||
type Addresses = Vec<SocketAddr>;
|
type Addresses = Vec<SocketAddr>;
|
||||||
|
|
||||||
|
@ -41,6 +41,14 @@ pub struct SendMessageReq<'a> {
|
||||||
pub created_at: &'a str,
|
pub created_at: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
pub struct BroadcastMessageReq<'a> {
|
||||||
|
pub room_id: &'a str,
|
||||||
|
pub author_id: &'a str,
|
||||||
|
pub message: &'a str,
|
||||||
|
pub created_at: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct SetRoomTopicReq<'a> {
|
pub struct SetRoomTopicReq<'a> {
|
||||||
pub room_id: &'a str,
|
pub room_id: &'a str,
|
||||||
|
@ -48,6 +56,12 @@ pub struct SetRoomTopicReq<'a> {
|
||||||
pub topic: &'a str,
|
pub topic: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod paths {
|
||||||
|
pub const ADD_MESSAGE: &'static str = "/cluster/rooms/add_message";
|
||||||
|
pub const BROADCAST_MESSAGE: &'static str = "/cluster/rooms/broadcast_message";
|
||||||
|
pub const SET_TOPIC: &'static str = "/cluster/rooms/set_topic";
|
||||||
|
}
|
||||||
|
|
||||||
impl LavinaClient {
|
impl LavinaClient {
|
||||||
pub fn new(addresses: Addresses) -> Self {
|
pub fn new(addresses: Addresses) -> Self {
|
||||||
let client = ClientBuilder::new(Client::new()).with(TracingMiddleware::<DefaultSpanBackend>::new()).build();
|
let client = ClientBuilder::new(Client::new()).with(TracingMiddleware::<DefaultSpanBackend>::new()).build();
|
||||||
|
@ -64,7 +78,7 @@ impl LavinaClient {
|
||||||
tracing::error!("Failed");
|
tracing::error!("Failed");
|
||||||
return Err(anyhow!("Unknown node"));
|
return Err(anyhow!("Unknown node"));
|
||||||
};
|
};
|
||||||
match self.client.post(format!("http://{}/cluster/rooms/add_message", address)).json(&req).send().await {
|
match self.client.post(format!("http://{}{}", address, paths::BROADCAST_MESSAGE)).json(&req).send().await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
tracing::info!("Message sent");
|
tracing::info!("Message sent");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -76,13 +90,32 @@ impl LavinaClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip(self, req), name = "LavinaClient::broadcast_room_message")]
|
||||||
|
pub async fn broadcast_room_message(&self, node_id: u32, req: BroadcastMessageReq<'_>) -> Result<()> {
|
||||||
|
tracing::info!("Broadcasting a message to a room on a remote node");
|
||||||
|
let Some(address) = self.addresses.get(node_id as usize) else {
|
||||||
|
tracing::error!("Failed");
|
||||||
|
return Err(anyhow!("Unknown node"));
|
||||||
|
};
|
||||||
|
match self.client.post(format!("http://{}{}", address, paths::BROADCAST_MESSAGE)).json(&req).send().await {
|
||||||
|
Ok(_) => {
|
||||||
|
tracing::info!("Message broadcasted");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
tracing::error!("Failed to broadcast message: {e:?}");
|
||||||
|
Err(e.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn set_room_topic(&self, node_id: u32, req: SetRoomTopicReq<'_>) -> Result<()> {
|
pub async fn set_room_topic(&self, node_id: u32, req: SetRoomTopicReq<'_>) -> Result<()> {
|
||||||
tracing::info!("Setting the topic of a room on a remote node");
|
tracing::info!("Setting the topic of a room on a remote node");
|
||||||
let Some(address) = self.addresses.get(node_id as usize) else {
|
let Some(address) = self.addresses.get(node_id as usize) else {
|
||||||
tracing::error!("Failed");
|
tracing::error!("Failed");
|
||||||
return Err(anyhow!("Unknown node"));
|
return Err(anyhow!("Unknown node"));
|
||||||
};
|
};
|
||||||
match self.client.post(format!("http://{}/cluster/rooms/set_topic", address)).json(&req).send().await {
|
match self.client.post(format!("http://{}{}", address, paths::SET_TOPIC)).json(&req).send().await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
tracing::info!("Room topic set");
|
tracing::info!("Room topic set");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,29 +1,58 @@
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use crate::player::PlayerId;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
|
use crate::player::{PlayerId, PlayerRegistry, Updates};
|
||||||
use crate::prelude::Str;
|
use crate::prelude::Str;
|
||||||
use crate::room::RoomId;
|
use crate::room::RoomId;
|
||||||
|
|
||||||
/// Receives updates from other nodes and broadcasts them to local player actors.
|
/// Receives updates from other nodes and broadcasts them to local player actors.
|
||||||
struct Broadcasting {
|
struct BroadcastingInner {
|
||||||
subscriptions: HashMap<RoomId, HashSet<PlayerId>>,
|
subscriptions: HashMap<RoomId, HashSet<PlayerId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Broadcasting {}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Broadcasting(Arc<Mutex<BroadcastingInner>>);
|
||||||
impl Broadcasting {
|
impl Broadcasting {
|
||||||
/// Creates a new broadcasting instance.
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
let inner = BroadcastingInner {
|
||||||
subscriptions: HashMap::new(),
|
subscriptions: HashMap::new(),
|
||||||
|
};
|
||||||
|
Self(Arc::new(Mutex::new(inner)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Broadcasts the given update to subscribed player actors on local node.
|
||||||
|
pub async fn broadcast(
|
||||||
|
&self,
|
||||||
|
players: &PlayerRegistry,
|
||||||
|
room_id: RoomId,
|
||||||
|
author_id: PlayerId,
|
||||||
|
message: Str,
|
||||||
|
created_at: DateTime<Utc>,
|
||||||
|
) {
|
||||||
|
let inner = self.0.lock().await;
|
||||||
|
let Some(subscribers) = inner.subscriptions.get(&room_id) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let update = Updates::NewMessage {
|
||||||
|
room_id: room_id.clone(),
|
||||||
|
author_id: author_id.clone(),
|
||||||
|
body: message.clone(),
|
||||||
|
created_at: created_at.clone(),
|
||||||
|
};
|
||||||
|
for i in subscribers {
|
||||||
|
let Some(player) = players.get_player(i).await else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
player.update(update.clone()).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Broadcasts the given update to player actors.
|
pub async fn subscribe(&self, subscriber: PlayerId, room_id: RoomId) {
|
||||||
pub fn broadcast(&self, room_id: RoomId, author_id: PlayerId, message: Str) {
|
self.0.lock().await.subscriptions.entry(room_id).or_insert_with(HashSet::new).insert(subscriber);
|
||||||
self.subscriptions.get(&room_id).map(|players| {
|
|
||||||
players.iter().for_each(|player_id| {
|
|
||||||
// Send the message to the player actor.
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,8 +3,10 @@ use crate::clustering::{ClusterConfig, LavinaClient};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use prometheus::Registry as MetricsRegistry;
|
use prometheus::Registry as MetricsRegistry;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use crate::auth::Authenticator;
|
use crate::auth::Authenticator;
|
||||||
|
use crate::clustering::broadcast::Broadcasting;
|
||||||
use crate::dialog::DialogRegistry;
|
use crate::dialog::DialogRegistry;
|
||||||
use crate::player::PlayerRegistry;
|
use crate::player::PlayerRegistry;
|
||||||
use crate::repo::Storage;
|
use crate::repo::Storage;
|
||||||
|
@ -26,6 +28,7 @@ pub struct LavinaCore {
|
||||||
pub players: PlayerRegistry,
|
pub players: PlayerRegistry,
|
||||||
pub rooms: RoomRegistry,
|
pub rooms: RoomRegistry,
|
||||||
pub dialogs: DialogRegistry,
|
pub dialogs: DialogRegistry,
|
||||||
|
pub broadcasting: Broadcasting,
|
||||||
pub authenticator: Authenticator,
|
pub authenticator: Authenticator,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,6 +39,7 @@ impl LavinaCore {
|
||||||
storage: Storage,
|
storage: Storage,
|
||||||
) -> Result<LavinaCore> {
|
) -> Result<LavinaCore> {
|
||||||
// TODO shutdown all services in reverse order on error
|
// TODO shutdown all services in reverse order on error
|
||||||
|
let broadcasting = Broadcasting::new();
|
||||||
let client = LavinaClient::new(cluster_config.addresses.clone());
|
let client = LavinaClient::new(cluster_config.addresses.clone());
|
||||||
let rooms = RoomRegistry::new(&mut metrics, storage.clone())?;
|
let rooms = RoomRegistry::new(&mut metrics, storage.clone())?;
|
||||||
let dialogs = DialogRegistry::new(storage.clone());
|
let dialogs = DialogRegistry::new(storage.clone());
|
||||||
|
@ -46,6 +50,7 @@ impl LavinaCore {
|
||||||
&mut metrics,
|
&mut metrics,
|
||||||
Arc::new(cluster_config.metadata),
|
Arc::new(cluster_config.metadata),
|
||||||
client,
|
client,
|
||||||
|
broadcasting.clone(),
|
||||||
)?;
|
)?;
|
||||||
dialogs.set_players(players.clone()).await;
|
dialogs.set_players(players.clone()).await;
|
||||||
let authenticator = Authenticator::new(storage.clone());
|
let authenticator = Authenticator::new(storage.clone());
|
||||||
|
@ -53,6 +58,7 @@ impl LavinaCore {
|
||||||
players,
|
players,
|
||||||
rooms,
|
rooms,
|
||||||
dialogs,
|
dialogs,
|
||||||
|
broadcasting,
|
||||||
authenticator,
|
authenticator,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use tracing::{Instrument, Span};
|
use tracing::{Instrument, Span};
|
||||||
|
|
||||||
|
use crate::clustering::broadcast::Broadcasting;
|
||||||
use crate::clustering::{ClusterMetadata, LavinaClient, SendMessageReq, SetRoomTopicReq};
|
use crate::clustering::{ClusterMetadata, LavinaClient, SendMessageReq, SetRoomTopicReq};
|
||||||
use crate::dialog::DialogRegistry;
|
use crate::dialog::DialogRegistry;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
@ -275,6 +276,7 @@ impl PlayerRegistry {
|
||||||
metrics: &mut MetricsRegistry,
|
metrics: &mut MetricsRegistry,
|
||||||
cluster_metadata: Arc<ClusterMetadata>,
|
cluster_metadata: Arc<ClusterMetadata>,
|
||||||
cluster_client: LavinaClient,
|
cluster_client: LavinaClient,
|
||||||
|
broadcasting: Broadcasting,
|
||||||
) -> Result<PlayerRegistry> {
|
) -> Result<PlayerRegistry> {
|
||||||
let metric_active_players = IntGauge::new("chat_players_active", "Number of alive player actors")?;
|
let metric_active_players = IntGauge::new("chat_players_active", "Number of alive player actors")?;
|
||||||
metrics.register(Box::new(metric_active_players.clone()))?;
|
metrics.register(Box::new(metric_active_players.clone()))?;
|
||||||
|
@ -284,6 +286,7 @@ impl PlayerRegistry {
|
||||||
storage,
|
storage,
|
||||||
cluster_metadata,
|
cluster_metadata,
|
||||||
cluster_client,
|
cluster_client,
|
||||||
|
broadcasting,
|
||||||
players: HashMap::new(),
|
players: HashMap::new(),
|
||||||
metric_active_players,
|
metric_active_players,
|
||||||
};
|
};
|
||||||
|
@ -337,6 +340,7 @@ impl PlayerRegistry {
|
||||||
inner.dialogs.clone(),
|
inner.dialogs.clone(),
|
||||||
inner.cluster_metadata.clone(),
|
inner.cluster_metadata.clone(),
|
||||||
inner.cluster_client.clone(),
|
inner.cluster_client.clone(),
|
||||||
|
inner.broadcasting.clone(),
|
||||||
inner.storage.clone(),
|
inner.storage.clone(),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
@ -373,6 +377,7 @@ struct PlayerRegistryInner {
|
||||||
storage: Storage,
|
storage: Storage,
|
||||||
cluster_metadata: Arc<ClusterMetadata>,
|
cluster_metadata: Arc<ClusterMetadata>,
|
||||||
cluster_client: LavinaClient,
|
cluster_client: LavinaClient,
|
||||||
|
broadcasting: Broadcasting,
|
||||||
/// Active player actors.
|
/// Active player actors.
|
||||||
players: HashMap<PlayerId, (PlayerHandle, JoinHandle<Player>)>,
|
players: HashMap<PlayerId, (PlayerHandle, JoinHandle<Player>)>,
|
||||||
metric_active_players: IntGauge,
|
metric_active_players: IntGauge,
|
||||||
|
@ -397,6 +402,7 @@ struct Player {
|
||||||
storage: Storage,
|
storage: Storage,
|
||||||
cluster_metadata: Arc<ClusterMetadata>,
|
cluster_metadata: Arc<ClusterMetadata>,
|
||||||
cluster_client: LavinaClient,
|
cluster_client: LavinaClient,
|
||||||
|
broadcasting: Broadcasting,
|
||||||
}
|
}
|
||||||
impl Player {
|
impl Player {
|
||||||
async fn launch(
|
async fn launch(
|
||||||
|
@ -405,6 +411,7 @@ impl Player {
|
||||||
dialogs: DialogRegistry,
|
dialogs: DialogRegistry,
|
||||||
cluster_metadata: Arc<ClusterMetadata>,
|
cluster_metadata: Arc<ClusterMetadata>,
|
||||||
cluster_client: LavinaClient,
|
cluster_client: LavinaClient,
|
||||||
|
broadcasting: Broadcasting,
|
||||||
storage: Storage,
|
storage: Storage,
|
||||||
) -> (PlayerHandle, JoinHandle<Player>) {
|
) -> (PlayerHandle, JoinHandle<Player>) {
|
||||||
let (tx, rx) = channel(32);
|
let (tx, rx) = channel(32);
|
||||||
|
@ -427,6 +434,7 @@ impl Player {
|
||||||
storage,
|
storage,
|
||||||
cluster_metadata,
|
cluster_metadata,
|
||||||
cluster_client,
|
cluster_client,
|
||||||
|
broadcasting,
|
||||||
};
|
};
|
||||||
let fiber = tokio::task::spawn(player.main_loop());
|
let fiber = tokio::task::spawn(player.main_loop());
|
||||||
(handle_clone, fiber)
|
(handle_clone, fiber)
|
||||||
|
@ -449,7 +457,8 @@ impl Player {
|
||||||
let rooms = self.storage.get_rooms_of_a_user(self.storage_id).await.unwrap();
|
let rooms = self.storage.get_rooms_of_a_user(self.storage_id).await.unwrap();
|
||||||
for room_id in rooms {
|
for room_id in rooms {
|
||||||
if let Some(remote_node) = self.room_location(&room_id) {
|
if let Some(remote_node) = self.room_location(&room_id) {
|
||||||
self.my_rooms.insert(room_id, RoomRef::Remote { node_id: remote_node });
|
self.my_rooms.insert(room_id.clone(), RoomRef::Remote { node_id: remote_node });
|
||||||
|
self.broadcasting.subscribe(self.player_id.clone(), room_id).await;
|
||||||
} else {
|
} else {
|
||||||
let room = self.rooms.get_room(&room_id).await;
|
let room = self.rooms.get_room(&room_id).await;
|
||||||
if let Some(room) = room {
|
if let Some(room) = room {
|
||||||
|
|
|
@ -9,6 +9,7 @@ use tokio::net::tcp::{ReadHalf, WriteHalf};
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
use lavina_core::auth::Authenticator;
|
use lavina_core::auth::Authenticator;
|
||||||
|
use lavina_core::clustering::{ClusterConfig, ClusterMetadata};
|
||||||
use lavina_core::player::{JoinResult, PlayerId, SendMessageResult};
|
use lavina_core::player::{JoinResult, PlayerId, SendMessageResult};
|
||||||
use lavina_core::repo::{Storage, StorageConfig};
|
use lavina_core::repo::{Storage, StorageConfig};
|
||||||
use lavina_core::room::RoomId;
|
use lavina_core::room::RoomId;
|
||||||
|
@ -118,7 +119,16 @@ impl TestServer {
|
||||||
db_path: ":memory:".into(),
|
db_path: ":memory:".into(),
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
let core = LavinaCore::new(metrics.clone(), storage.clone()).await?;
|
let cluster_config = ClusterConfig {
|
||||||
|
addresses: vec![],
|
||||||
|
metadata: ClusterMetadata {
|
||||||
|
node_id: 0,
|
||||||
|
main_owner: 0,
|
||||||
|
test_owner: 0,
|
||||||
|
test2_owner: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let core = LavinaCore::new(metrics.clone(), cluster_config, storage.clone()).await?;
|
||||||
let server = launch(config, core.clone(), metrics.clone()).await.unwrap();
|
let server = launch(config, core.clone(), metrics.clone()).await.unwrap();
|
||||||
Ok(TestServer {
|
Ok(TestServer {
|
||||||
metrics,
|
metrics,
|
||||||
|
@ -133,6 +143,15 @@ impl TestServer {
|
||||||
listen_on: "127.0.0.1:0".parse().unwrap(),
|
listen_on: "127.0.0.1:0".parse().unwrap(),
|
||||||
server_name: "testserver".into(),
|
server_name: "testserver".into(),
|
||||||
};
|
};
|
||||||
|
let cluster_config = ClusterConfig {
|
||||||
|
addresses: vec![],
|
||||||
|
metadata: ClusterMetadata {
|
||||||
|
node_id: 0,
|
||||||
|
main_owner: 0,
|
||||||
|
test_owner: 0,
|
||||||
|
test2_owner: 0,
|
||||||
|
},
|
||||||
|
};
|
||||||
let TestServer {
|
let TestServer {
|
||||||
metrics: _,
|
metrics: _,
|
||||||
storage,
|
storage,
|
||||||
|
@ -142,7 +161,7 @@ impl TestServer {
|
||||||
server.terminate().await?;
|
server.terminate().await?;
|
||||||
core.shutdown().await?;
|
core.shutdown().await?;
|
||||||
let metrics = MetricsRegistry::new();
|
let metrics = MetricsRegistry::new();
|
||||||
let core = LavinaCore::new(metrics.clone(), storage.clone()).await?;
|
let core = LavinaCore::new(metrics.clone(), cluster_config, storage.clone()).await?;
|
||||||
let server = launch(config, core.clone(), metrics.clone()).await.unwrap();
|
let server = launch(config, core.clone(), metrics.clone()).await.unwrap();
|
||||||
Ok(TestServer {
|
Ok(TestServer {
|
||||||
metrics,
|
metrics,
|
||||||
|
|
41
src/http.rs
41
src/http.rs
|
@ -13,13 +13,13 @@ use serde::{Deserialize, Serialize};
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
use lavina_core::auth::UpdatePasswordResult;
|
use lavina_core::auth::UpdatePasswordResult;
|
||||||
use lavina_core::clustering::SendMessageReq;
|
use lavina_core::clustering::{BroadcastMessageReq, SendMessageReq};
|
||||||
use lavina_core::player::{PlayerId, PlayerRegistry, SendMessageResult};
|
use lavina_core::player::{PlayerId, PlayerRegistry, SendMessageResult};
|
||||||
use lavina_core::prelude::*;
|
use lavina_core::prelude::*;
|
||||||
use lavina_core::repo::Storage;
|
use lavina_core::repo::Storage;
|
||||||
use lavina_core::room::{RoomId, RoomRegistry};
|
use lavina_core::room::{RoomId, RoomRegistry};
|
||||||
use lavina_core::terminator::Terminator;
|
use lavina_core::terminator::Terminator;
|
||||||
use lavina_core::LavinaCore;
|
use lavina_core::{clustering, LavinaCore};
|
||||||
|
|
||||||
use mgmt_api::*;
|
use mgmt_api::*;
|
||||||
|
|
||||||
|
@ -91,7 +91,10 @@ async fn route(
|
||||||
(&Method::POST, paths::SET_PASSWORD) => endpoint_set_password(request, core).await.or5xx(),
|
(&Method::POST, paths::SET_PASSWORD) => endpoint_set_password(request, core).await.or5xx(),
|
||||||
(&Method::POST, rooms::paths::SEND_MESSAGE) => endpoint_send_room_message(request, core).await.or5xx(),
|
(&Method::POST, rooms::paths::SEND_MESSAGE) => endpoint_send_room_message(request, core).await.or5xx(),
|
||||||
(&Method::POST, rooms::paths::SET_TOPIC) => endpoint_set_room_topic(request, core).await.or5xx(),
|
(&Method::POST, rooms::paths::SET_TOPIC) => endpoint_set_room_topic(request, core).await.or5xx(),
|
||||||
(&Method::POST, "/cluster/rooms/add_message") => endpoint_cluster_add_message(request, core).await.or5xx(),
|
(&Method::POST, clustering::paths::ADD_MESSAGE) => endpoint_cluster_add_message(request, core).await.or5xx(),
|
||||||
|
(&Method::POST, clustering::paths::BROADCAST_MESSAGE) => {
|
||||||
|
endpoint_cluster_broadcast_message(request, core).await.or5xx()
|
||||||
|
}
|
||||||
_ => endpoint_not_found(),
|
_ => endpoint_not_found(),
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
|
@ -238,6 +241,38 @@ async fn endpoint_cluster_add_message(
|
||||||
Ok(empty_204_request())
|
Ok(empty_204_request())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tracing::instrument(skip_all, name = "endpoint_cluster_broadcast_message")]
|
||||||
|
async fn endpoint_cluster_broadcast_message(
|
||||||
|
request: Request<hyper::body::Incoming>,
|
||||||
|
core: &LavinaCore,
|
||||||
|
) -> Result<Response<Full<Bytes>>> {
|
||||||
|
let str = request.collect().await?.to_bytes();
|
||||||
|
let Ok(req) = serde_json::from_slice::<BroadcastMessageReq>(&str[..]) else {
|
||||||
|
return Ok(malformed_request());
|
||||||
|
};
|
||||||
|
let Ok(created_at) = chrono::DateTime::parse_from_rfc3339(req.created_at) else {
|
||||||
|
return Ok(malformed_request());
|
||||||
|
};
|
||||||
|
let Ok(room_id) = RoomId::from(req.room_id) else {
|
||||||
|
return Ok(room_not_found());
|
||||||
|
};
|
||||||
|
let Ok(author_id) = PlayerId::from(req.author_id) else {
|
||||||
|
return Ok(player_not_found());
|
||||||
|
};
|
||||||
|
let broadcasting = core.broadcasting.0.lock().await;
|
||||||
|
broadcasting
|
||||||
|
.broadcast(
|
||||||
|
&core.players,
|
||||||
|
room_id,
|
||||||
|
author_id,
|
||||||
|
req.message.into(),
|
||||||
|
created_at.to_utc(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
drop(broadcasting);
|
||||||
|
Ok(empty_204_request())
|
||||||
|
}
|
||||||
|
|
||||||
fn endpoint_not_found() -> Response<Full<Bytes>> {
|
fn endpoint_not_found() -> Response<Full<Bytes>> {
|
||||||
let payload = ErrorResponse {
|
let payload = ErrorResponse {
|
||||||
code: errors::INVALID_PATH,
|
code: errors::INVALID_PATH,
|
||||||
|
|
Loading…
Reference in New Issue