feat: skip send colors for disconnected board.

This commit is contained in:
Ivan Li 2023-04-30 22:48:25 +08:00
parent 82d4adfe0f
commit ca9a2ba34d
3 changed files with 28 additions and 24 deletions

View File

@ -425,7 +425,6 @@ async fn main() {
error!("boards change receiver changed error: {}", err);
return;
}
log::info!("boards changed");
let boards = receiver.borrow().clone();

View File

@ -14,6 +14,7 @@ pub enum BoardConnectStatus {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct BoardInfo {
pub fullname: String,
pub host: String,
pub address: Ipv4Addr,
pub port: u16,
@ -23,8 +24,9 @@ pub struct BoardInfo {
}
impl BoardInfo {
pub fn new(host: String, address: Ipv4Addr, port: u16) -> Self {
pub fn new(fullname: String, host: String, address: Ipv4Addr, port: u16) -> Self {
Self {
fullname,
host,
address,
port,

View File

@ -1,9 +1,4 @@
use std::{
collections::{HashMap, HashSet},
net::Ipv4Addr,
sync::Arc,
time::Duration,
};
use std::{collections::HashMap, net::Ipv4Addr, sync::Arc, time::Duration};
use futures::future::join_all;
use mdns_sd::{ServiceDaemon, ServiceEvent};
@ -13,12 +8,12 @@ use tokio::{
sync::{watch, OnceCell, RwLock},
};
use super::{BoardConnectStatus, BoardInfo};
use super::{BoardInfo, BoardConnectStatus};
#[derive(Debug, Clone)]
pub struct UdpRpc {
socket: Arc<UdpSocket>,
boards: Arc<RwLock<HashMap<Ipv4Addr, BoardInfo>>>,
boards: Arc<RwLock<HashMap<String, BoardInfo>>>,
boards_change_sender: Arc<watch::Sender<Vec<BoardInfo>>>,
}
@ -71,18 +66,6 @@ impl UdpRpc {
tokio::spawn(async move {
shared_self_for_check.check_boards().await;
});
// let shared_self_for_watch = shared_self.clone();
// tokio::spawn(async move {
// let mut rx = shared_self_for_watch.clone_boards_change_receiver().await;
// // let mut rx = sub_tx.subscribe();
// // drop(sub_tx);
// while rx.changed().await.is_ok() {
// let boards = rx.borrow().clone();
// info!("boards changed: {:?}", boards);
// }
// });
}
async fn search_boards(&self) -> anyhow::Result<()> {
@ -109,12 +92,13 @@ impl UdpRpc {
let mut boards = self.boards.write().await;
let board = BoardInfo::new(
info.get_fullname().to_string(),
info.get_fullname().to_string(),
info.get_addresses().iter().next().unwrap().clone(),
info.get_port(),
);
if boards.insert(board.address, board.clone()).is_some() {
if boards.insert(board.fullname.clone(), board.clone()).is_some() {
info!("added board {:?}", board);
}
@ -124,8 +108,21 @@ impl UdpRpc {
sender.send(tx_boards)?;
tokio::task::yield_now().await;
}
ServiceEvent::ServiceRemoved(_, fullname) => {
info!("removed board {:?}", fullname);
let mut boards = self.boards.write().await;
if boards.remove(&fullname).is_some() {
info!("removed board {:?} successful", fullname);
}
let tx_boards = boards.values().cloned().collect();
drop(boards);
sender.send(tx_boards)?;
tokio::task::yield_now().await;
}
other_event => {
warn!("{:?}", &other_event);
// log::info!("{:?}", &other_event);
}
}
}
@ -147,6 +144,12 @@ impl UdpRpc {
let socket = self.socket.clone();
let handlers = boards.into_iter().map(|board| {
if board.connect_status == BoardConnectStatus::Disconnected {
return tokio::spawn(async move {
log::debug!("board {} is disconnected, skip.", board.host);
});
}
let socket = socket.clone();
let buff = buff.clone();
tokio::spawn(async move {