Skip to content

Commit 9955162

Browse files
committed
refactor: Use InstanceHandle for Boblight server
1 parent 272471a commit 9955162

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

src/api/boblight.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::sync::Arc;
22

33
use thiserror::Error;
4-
use tokio::sync::mpsc::Sender;
54

65
use crate::{
76
global::{InputMessage, InputMessageData, InputSourceHandle, Message},
8-
models::{Color, InstanceConfig},
7+
instance::{InstanceHandle, InstanceHandleError},
8+
models::Color,
99
};
1010

1111
pub mod message;
@@ -17,27 +17,27 @@ pub enum BoblightApiError {
1717
Broadcast(#[from] tokio::sync::mpsc::error::SendError<InputMessage>),
1818
#[error("missing command data in protobuf frame")]
1919
MissingCommand,
20+
#[error("invalid instance")]
21+
InvalidInstance(#[from] InstanceHandleError),
2022
}
2123

2224
pub struct ClientConnection {
2325
handle: InputSourceHandle<InputMessage>,
2426
led_colors: Vec<Color>,
2527
priority: i32,
26-
tx: Sender<InputMessage>,
27-
instance: Arc<InstanceConfig>,
28+
instance: InstanceHandle,
2829
}
2930

3031
impl ClientConnection {
3132
pub fn new(
3233
handle: InputSourceHandle<InputMessage>,
33-
tx: Sender<InputMessage>,
34-
instance: Arc<InstanceConfig>,
34+
led_count: usize,
35+
instance: InstanceHandle,
3536
) -> Self {
3637
Self {
3738
handle,
38-
led_colors: vec![Color::default(); instance.leds.leds.len()],
39+
led_colors: vec![Color::default(); led_count],
3940
priority: 128,
40-
tx,
4141
instance,
4242
}
4343
}
@@ -51,8 +51,9 @@ impl ClientConnection {
5151
}
5252
}
5353

54-
async fn sync(&self) -> Result<(), tokio::sync::mpsc::error::SendError<InputMessage>> {
55-
self.tx
54+
async fn sync(&self) -> Result<(), BoblightApiError> {
55+
Ok(self
56+
.instance
5657
.send(InputMessage::new(
5758
self.handle.id(),
5859
crate::component::ComponentName::BoblightServer,
@@ -62,7 +63,7 @@ impl ClientConnection {
6263
led_colors: Arc::new(self.led_colors.clone()),
6364
},
6465
))
65-
.await
66+
.await?)
6667
}
6768

6869
pub async fn handle_request(
@@ -75,7 +76,7 @@ impl ClientConnection {
7576
BoblightRequest::Get(get) => match get {
7677
message::GetArg::Version => Ok(Some(BoblightResponse::Version)),
7778
message::GetArg::Lights => Ok(Some(BoblightResponse::Lights {
78-
leds: self.instance.leds.leds.clone(),
79+
leds: self.instance.config().await?.leds.leds.clone(),
7980
})),
8081
},
8182
BoblightRequest::Set(set) => {

src/instance.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,22 @@ impl Instance {
7070
let muxer = PriorityMuxer::new(global.clone()).await;
7171
let core = Core::new(&config).await;
7272

73+
let (tx, handle_rx) = mpsc::channel(1);
74+
let id = config.instance.id;
75+
let handle = InstanceHandle { id, tx, local_tx };
76+
7377
let config = Arc::new(config);
7478
let _boblight_server = if config.boblight_server.enable {
7579
let server_handle = servers::bind(
7680
"Boblight",
7781
config.boblight_server.clone(),
7882
global.clone(),
7983
{
80-
let instance = config.clone();
81-
let local_tx = local_tx.clone();
84+
let led_count = config.leds.leds.len();
85+
let handle = handle.clone();
8286

8387
move |tcp, global| {
84-
servers::boblight::handle_client(
85-
tcp,
86-
local_tx.clone(),
87-
instance.clone(),
88-
global,
89-
)
88+
servers::boblight::handle_client(tcp, led_count, handle.clone(), global)
9089
}
9190
},
9291
)
@@ -104,9 +103,6 @@ impl Instance {
104103
None
105104
};
106105

107-
let (tx, handle_rx) = mpsc::channel(1);
108-
let id = config.instance.id;
109-
110106
(
111107
Self {
112108
config,
@@ -118,7 +114,7 @@ impl Instance {
118114
core,
119115
_boblight_server,
120116
},
121-
InstanceHandle { id, tx, local_tx },
117+
handle,
122118
)
123119
}
124120

@@ -261,8 +257,8 @@ pub enum InstanceHandleError {
261257
Dropped,
262258
}
263259

264-
impl From<tokio::sync::mpsc::error::SendError<InstanceMessage>> for InstanceHandleError {
265-
fn from(_: tokio::sync::mpsc::error::SendError<InstanceMessage>) -> Self {
260+
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for InstanceHandleError {
261+
fn from(_: tokio::sync::mpsc::error::SendError<T>) -> Self {
266262
Self::Dropped
267263
}
268264
}
@@ -278,6 +274,10 @@ impl InstanceHandle {
278274
self.id
279275
}
280276

277+
pub async fn send(&self, input: InputMessage) -> Result<(), InstanceHandleError> {
278+
Ok(self.local_tx.send(input).await?)
279+
}
280+
281281
pub async fn current_priorities(&self) -> Result<Vec<PriorityInfo>, InstanceHandleError> {
282282
let (tx, rx) = oneshot::channel();
283283
self.tx.send(InstanceMessage::PriorityInfo(tx)).await?;

src/servers/boblight.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
//! Boblight protocol server implementation
22
33
use std::net::SocketAddr;
4-
use std::sync::Arc;
54

65
use futures::prelude::*;
76
use thiserror::Error;
8-
use tokio::{net::TcpStream, sync::mpsc::Sender};
7+
use tokio::net::TcpStream;
98
use tokio_util::codec::Framed;
109

1110
use crate::{
1211
api::boblight::{self, BoblightApiError},
13-
global::{Global, InputMessage, InputSourceName},
14-
models::InstanceConfig,
12+
global::{Global, InputSourceName},
13+
instance::InstanceHandle,
1514
};
1615

1716
/// Boblight protocol codec definition
@@ -30,8 +29,8 @@ pub enum BoblightServerError {
3029

3130
pub async fn handle_client(
3231
(socket, peer_addr): (TcpStream, SocketAddr),
33-
tx: Sender<InputMessage>,
34-
instance: Arc<InstanceConfig>,
32+
led_count: usize,
33+
instance: InstanceHandle,
3534
global: Global,
3635
) -> Result<(), BoblightServerError> {
3736
debug!("accepted new connection from {}", peer_addr,);
@@ -43,7 +42,8 @@ pub async fn handle_client(
4342
.register_input_source(InputSourceName::Boblight { peer_addr }, None)
4443
.await
4544
.unwrap();
46-
let mut connection = boblight::ClientConnection::new(source_handle, tx, instance);
45+
46+
let mut connection = boblight::ClientConnection::new(source_handle, led_count, instance);
4747

4848
while let Some(request) = reader.next().await {
4949
trace!("({}) processing request: {:?}", peer_addr, request);

0 commit comments

Comments
 (0)