74 lines
2.0 KiB
Rust
74 lines
2.0 KiB
Rust
use crate::error::TcpServerError;
|
|
use domain::{BroadcastPort, DisplayHint, Layout, WidgetId, WidgetState};
|
|
use protocol::{ServerMessage, WidgetDescriptor, WireLayoutNode, encode};
|
|
use tokio::sync::broadcast;
|
|
|
|
pub struct TcpBroadcaster {
|
|
tx: broadcast::Sender<Vec<u8>>,
|
|
}
|
|
|
|
impl TcpBroadcaster {
|
|
pub fn new(capacity: usize) -> Self {
|
|
let (tx, _) = broadcast::channel(capacity);
|
|
Self { tx }
|
|
}
|
|
|
|
pub fn subscribe(&self) -> broadcast::Receiver<Vec<u8>> {
|
|
self.tx.subscribe()
|
|
}
|
|
|
|
fn send_frame(&self, frame: Vec<u8>) -> Result<(), TcpServerError> {
|
|
let _ = self.tx.send(frame);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl BroadcastPort for TcpBroadcaster {
|
|
type Error = TcpServerError;
|
|
|
|
async fn push_screen_update(
|
|
&self,
|
|
layout: &Layout,
|
|
widgets: &[(WidgetId, DisplayHint, WidgetState)],
|
|
) -> Result<(), Self::Error> {
|
|
let wire_layout: WireLayoutNode = (&layout.root).into();
|
|
let wire_widgets: Vec<WidgetDescriptor> = widgets
|
|
.iter()
|
|
.map(|(id, hint, state)| WidgetDescriptor {
|
|
id: *id,
|
|
display_hint: hint.into(),
|
|
state: state.into(),
|
|
})
|
|
.collect();
|
|
|
|
let msg = ServerMessage::ScreenUpdate {
|
|
layout: wire_layout,
|
|
widgets: wire_widgets,
|
|
};
|
|
|
|
let frame = encode(&msg).map_err(TcpServerError::Encode)?;
|
|
self.send_frame(frame)
|
|
}
|
|
|
|
async fn push_data_update(
|
|
&self,
|
|
updates: &[(WidgetId, DisplayHint, WidgetState)],
|
|
) -> Result<(), Self::Error> {
|
|
let wire_widgets: Vec<WidgetDescriptor> = updates
|
|
.iter()
|
|
.map(|(id, hint, state)| WidgetDescriptor {
|
|
id: *id,
|
|
display_hint: hint.into(),
|
|
state: state.into(),
|
|
})
|
|
.collect();
|
|
|
|
let msg = ServerMessage::DataUpdate {
|
|
widgets: wire_widgets,
|
|
};
|
|
|
|
let frame = encode(&msg).map_err(TcpServerError::Encode)?;
|
|
self.send_frame(frame)
|
|
}
|
|
}
|