2022-01-14 12:52:03 +00:00
|
|
|
use crate::ws::connection::{FlowyRawWebSocket, FlowyWebSocket};
|
2022-01-13 02:53:30 +00:00
|
|
|
use dashmap::DashMap;
|
|
|
|
use flowy_error::FlowyError;
|
2022-01-24 08:27:40 +00:00
|
|
|
use futures_util::future::BoxFuture;
|
2022-01-13 02:53:30 +00:00
|
|
|
use lib_infra::future::FutureResult;
|
2022-01-22 10:48:43 +00:00
|
|
|
use lib_ws::{WSChannel, WSConnectState, WSMessageReceiver, WebSocketRawMessage};
|
2022-01-13 02:53:30 +00:00
|
|
|
use parking_lot::RwLock;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::{broadcast, broadcast::Receiver, mpsc::UnboundedReceiver};
|
|
|
|
|
|
|
|
pub struct LocalWebSocket {
|
|
|
|
user_id: Arc<RwLock<Option<String>>>,
|
2022-01-22 10:48:43 +00:00
|
|
|
receivers: Arc<DashMap<WSChannel, Arc<dyn WSMessageReceiver>>>,
|
2022-01-13 02:53:30 +00:00
|
|
|
state_sender: broadcast::Sender<WSConnectState>,
|
|
|
|
server_ws_receiver: RwLock<Option<UnboundedReceiver<WebSocketRawMessage>>>,
|
|
|
|
server_ws_sender: broadcast::Sender<WebSocketRawMessage>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LocalWebSocket {
|
|
|
|
pub fn new(
|
|
|
|
server_ws_receiver: UnboundedReceiver<WebSocketRawMessage>,
|
|
|
|
server_ws_sender: broadcast::Sender<WebSocketRawMessage>,
|
|
|
|
) -> Self {
|
|
|
|
let user_id = Arc::new(RwLock::new(None));
|
|
|
|
let receivers = Arc::new(DashMap::new());
|
|
|
|
let server_ws_receiver = RwLock::new(Some(server_ws_receiver));
|
|
|
|
let (state_sender, _) = broadcast::channel(16);
|
|
|
|
LocalWebSocket {
|
|
|
|
user_id,
|
|
|
|
receivers,
|
|
|
|
state_sender,
|
|
|
|
server_ws_receiver,
|
|
|
|
server_ws_sender,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FlowyRawWebSocket for LocalWebSocket {
|
|
|
|
fn initialize(&self) -> FutureResult<(), FlowyError> {
|
|
|
|
let mut server_ws_receiver = self.server_ws_receiver.write().take().expect("Only take once");
|
|
|
|
let receivers = self.receivers.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
while let Some(message) = server_ws_receiver.recv().await {
|
2022-01-22 10:48:43 +00:00
|
|
|
match receivers.get(&message.channel) {
|
2022-01-13 02:53:30 +00:00
|
|
|
None => tracing::error!("Can't find any handler for message: {:?}", message),
|
|
|
|
Some(receiver) => receiver.receive_message(message.clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
FutureResult::new(async { Ok(()) })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_connect(&self, _addr: String, user_id: String) -> FutureResult<(), FlowyError> {
|
|
|
|
*self.user_id.write() = Some(user_id);
|
|
|
|
FutureResult::new(async { Ok(()) })
|
|
|
|
}
|
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
fn stop_connect(&self) -> FutureResult<(), FlowyError> {
|
|
|
|
FutureResult::new(async { Ok(()) })
|
|
|
|
}
|
2022-01-13 02:53:30 +00:00
|
|
|
|
2022-01-24 08:27:40 +00:00
|
|
|
fn subscribe_connect_state(&self) -> BoxFuture<Receiver<WSConnectState>> {
|
|
|
|
let subscribe = self.state_sender.subscribe();
|
|
|
|
Box::pin(async move { subscribe })
|
|
|
|
}
|
2022-01-13 02:53:30 +00:00
|
|
|
|
2022-01-24 09:35:58 +00:00
|
|
|
fn reconnect(&self, _count: usize) -> FutureResult<(), FlowyError> {
|
|
|
|
FutureResult::new(async { Ok(()) })
|
|
|
|
}
|
2022-01-13 02:53:30 +00:00
|
|
|
|
2022-01-23 14:33:47 +00:00
|
|
|
fn add_msg_receiver(&self, receiver: Arc<dyn WSMessageReceiver>) -> Result<(), FlowyError> {
|
2022-01-22 10:48:43 +00:00
|
|
|
tracing::trace!("Local web socket add ws receiver: {:?}", receiver.source());
|
2022-01-13 02:53:30 +00:00
|
|
|
self.receivers.insert(receiver.source(), receiver);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-01-24 08:27:40 +00:00
|
|
|
fn ws_msg_sender(&self) -> FutureResult<Option<Arc<dyn FlowyWebSocket>>, FlowyError> {
|
|
|
|
let ws: Arc<dyn FlowyWebSocket> = Arc::new(LocalWebSocketAdaptor(self.server_ws_sender.clone()));
|
|
|
|
FutureResult::new(async move { Ok(Some(ws)) })
|
2022-01-13 02:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct LocalWebSocketAdaptor(broadcast::Sender<WebSocketRawMessage>);
|
|
|
|
|
2022-01-14 12:52:03 +00:00
|
|
|
impl FlowyWebSocket for LocalWebSocketAdaptor {
|
2022-01-13 02:53:30 +00:00
|
|
|
fn send(&self, msg: WebSocketRawMessage) -> Result<(), FlowyError> {
|
|
|
|
let _ = self.0.send(msg);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|