feat: mdns search.

This commit is contained in:
Ivan Li 2023-04-29 12:40:34 +08:00
parent 3deb14823d
commit e5527ce3c3
7 changed files with 295 additions and 72 deletions

240
src-tauri/Cargo.lock generated
View File

@ -17,6 +17,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "aho-corasick"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04"
dependencies = [
"memchr",
]
[[package]]
name = "alloc-no-stdlib"
version = "2.0.4"
@ -70,7 +79,7 @@ dependencies = [
"glib-sys",
"gobject-sys",
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -182,7 +191,7 @@ checksum = "3c55d429bef56ac9172d25fecb85dc8068307d17acd74b377866b7a1ef25d3c8"
dependencies = [
"glib-sys",
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -228,11 +237,12 @@ dependencies = [
[[package]]
name = "cfg-expr"
version = "0.14.0"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a35b255461940a32985c627ce82900867c61db1659764d3675ea81963f72a4c6"
checksum = "c8790cf1286da485c72cf5fc7aeba308438800036ec67d89425924c4807268c9"
dependencies = [
"smallvec",
"target-lexicon",
]
[[package]]
@ -361,9 +371,9 @@ dependencies = [
[[package]]
name = "cpufeatures"
version = "0.2.6"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "280a9f2d8b3a38871a3c8a46fb80db65e5e5ed97da80c4d08bf27fb63e35e181"
checksum = "3e4c1eaa2012c47becbbad2ab175484c2a84d1185b566fb2cc5b8707343dfe58"
dependencies = [
"libc",
]
@ -559,9 +569,9 @@ dependencies = [
[[package]]
name = "dunce"
version = "1.0.3"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bd4b30a6560bbd9b4620f4de34c3f14f60848e58a9b7216801afcb4c7b31c3c"
checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b"
[[package]]
name = "either"
@ -674,6 +684,18 @@ dependencies = [
"miniz_oxide 0.6.2",
]
[[package]]
name = "flume"
version = "0.10.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1657b4441c3403d9f7b3409e47575237dac27b1b5726df654a6ecbf92f0f7577"
dependencies = [
"futures-core",
"futures-sink",
"pin-project",
"spin",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -857,7 +879,7 @@ dependencies = [
"glib-sys",
"gobject-sys",
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -874,7 +896,7 @@ dependencies = [
"libc",
"pango-sys",
"pkg-config",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -886,21 +908,21 @@ dependencies = [
"gdk-sys",
"glib-sys",
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
"x11",
]
[[package]]
name = "generator"
version = "0.7.3"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33a20a288a94683f5f4da0adecdbe095c94a77c295e514cc6484e9394dd8376e"
checksum = "f3e123d9ae7c02966b4d892e550bdc32164f05853cd40ab570650ad600596a8a"
dependencies = [
"cc",
"libc",
"log",
"rustversion",
"windows 0.44.0",
"windows 0.48.0",
]
[[package]]
@ -961,7 +983,7 @@ dependencies = [
"glib-sys",
"gobject-sys",
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
"winapi",
]
@ -1007,7 +1029,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef4b192f8e65e9cf76cbf4ea71fa8e3be4a0e18ffe3d68b8da6836974cc5bad4"
dependencies = [
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -1022,7 +1044,7 @@ version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "029d74589adefde59de1a0c4f4732695c32805624aec7b68d91503d4dba79afc"
dependencies = [
"aho-corasick",
"aho-corasick 0.7.20",
"bstr",
"fnv",
"log",
@ -1037,7 +1059,7 @@ checksum = "0d57ce44246becd17153bd035ab4d32cfee096a657fc01f2231c9278378d1e0a"
dependencies = [
"glib-sys",
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -1078,7 +1100,7 @@ dependencies = [
"gobject-sys",
"libc",
"pango-sys",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -1200,6 +1222,16 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "if-addrs"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbc0fa01ffc752e9dbc72818cdb072cd028b86be5e09dd04c5a643704fe101a9"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "ignore"
version = "0.4.18"
@ -1377,9 +1409,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.141"
version = "0.2.142"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3304a64d199bb964be99741b7a14d26972741915b3649639149b2479bb46f4b5"
checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317"
[[package]]
name = "line-wrap"
@ -1392,9 +1424,9 @@ dependencies = [
[[package]]
name = "linux-raw-sys"
version = "0.3.1"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f"
checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c"
[[package]]
name = "lock_api"
@ -1474,6 +1506,19 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
[[package]]
name = "mdns-sd"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed0b1898d42fb934073c893ab88e8ae72076a5d862417dcf214c028011019753"
dependencies = [
"flume",
"if-addrs",
"log",
"polling",
"socket2",
]
[[package]]
name = "memchr"
version = "2.5.0"
@ -1677,9 +1722,9 @@ dependencies = [
[[package]]
name = "openssl-sys"
version = "0.9.85"
version = "0.9.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d3d193fb1488ad46ffe3aaabc912cc931d02ee8518fe2959aea8ef52718b0c0"
checksum = "8e17f59264b2809d77ae94f0e1ebabc434773f370d6ca667bd223ea10e06cc7e"
dependencies = [
"cc",
"libc",
@ -1741,7 +1786,7 @@ dependencies = [
"glib-sys",
"gobject-sys",
"libc",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -1889,6 +1934,26 @@ dependencies = [
"siphasher",
]
[[package]]
name = "pin-project"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad29a609b6bcd67fee905812e544992d216af9d755757c05ed2d0e15a74c6ecc"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "pin-project-lite"
version = "0.2.9"
@ -1916,7 +1981,7 @@ dependencies = [
"base64 0.21.0",
"indexmap",
"line-wrap",
"quick-xml 0.28.2",
"quick-xml",
"serde",
"time",
]
@ -1934,6 +1999,22 @@ dependencies = [
"miniz_oxide 0.7.1",
]
[[package]]
name = "polling"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce"
dependencies = [
"autocfg",
"bitflags",
"cfg-if",
"concurrent-queue",
"libc",
"log",
"pin-project-lite",
"windows-sys 0.48.0",
]
[[package]]
name = "ppv-lite86"
version = "0.2.17"
@ -1995,15 +2076,6 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "quick-xml"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8533f14c8382aaad0d592c812ac3b826162128b65662331e1127b45c3d18536b"
dependencies = [
"memchr",
]
[[package]]
name = "quick-xml"
version = "0.28.2"
@ -2140,13 +2212,13 @@ dependencies = [
[[package]]
name = "regex"
version = "1.7.3"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d"
checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370"
dependencies = [
"aho-corasick",
"aho-corasick 1.0.1",
"memchr",
"regex-syntax",
"regex-syntax 0.7.1",
]
[[package]]
@ -2155,7 +2227,7 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
"regex-syntax 0.6.29",
]
[[package]]
@ -2164,6 +2236,12 @@ version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "regex-syntax"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c"
[[package]]
name = "rustc_version"
version = "0.4.0"
@ -2175,9 +2253,9 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.37.11"
version = "0.37.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85597d61f83914ddeba6a47b3b8ffe7365107221c2e557ed94426489fefb5f77"
checksum = "a0661814f891c57c930a610266415528da53c4933e6dea5fb350cbfe048a9ece"
dependencies = [
"bitflags",
"errno",
@ -2454,6 +2532,15 @@ dependencies = [
"system-deps 5.0.0",
]
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
@ -2538,11 +2625,11 @@ dependencies = [
[[package]]
name = "system-deps"
version = "6.0.4"
version = "6.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "555fc8147af6256f3931a36bb83ad0023240ce9cf2b319dec8236fd1f220b05f"
checksum = "d0fe581ad25d11420b873cf9aedaca0419c2b411487b134d4d21065f3d092055"
dependencies = [
"cfg-expr 0.14.0",
"cfg-expr 0.15.1",
"heck 0.4.1",
"pkg-config",
"toml 0.7.3",
@ -2606,6 +2693,12 @@ dependencies = [
"xattr",
]
[[package]]
name = "target-lexicon"
version = "0.12.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd1ba337640d60c3e96bc6f0638a939b9c9a7f2c316a1598c279828b3d1dc8c5"
[[package]]
name = "tauri"
version = "1.2.4"
@ -2823,6 +2916,7 @@ dependencies = [
"hex",
"itertools",
"log",
"mdns-sd",
"paho-mqtt",
"paris",
"percent-encoding",
@ -2832,6 +2926,7 @@ dependencies = [
"tauri-build",
"time",
"tokio",
"tokio-stream",
"toml 0.7.3",
"url-build-parse",
]
@ -2916,9 +3011,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.27.0"
version = "1.28.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0de47a4eecbe11f498978a9b29d792f0d2692d1dd003650c24c76510e3bc001"
checksum = "c3c786bf8134e5a3a166db9b29ab8f48134739014a3eca7bc6bfa95d673b136f"
dependencies = [
"autocfg",
"bytes",
@ -2930,20 +3025,31 @@ dependencies = [
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.45.0",
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-macros"
version = "2.0.0"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a573bdc87985e9d6ddeed1b3d864e8a302c847e40d647746df2f1de209d1ce"
checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.15",
]
[[package]]
name = "tokio-stream"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]]
name = "toml"
version = "0.5.11"
@ -2989,11 +3095,10 @@ dependencies = [
[[package]]
name = "tracing"
version = "0.1.37"
version = "0.1.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8"
checksum = "cf9cf6a813d3f40c88b0b6b6f29a5c95c6cdbf97c1f9cc53fb820200f5ad814d"
dependencies = [
"cfg-if",
"pin-project-lite",
"tracing-attributes",
"tracing-core",
@ -3001,13 +3106,13 @@ dependencies = [
[[package]]
name = "tracing-attributes"
version = "0.1.23"
version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a"
checksum = "0f57e3ca2a01450b1a921183a9c9cbfda207fd822cef4ccb00a65402cbba7a74"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
"syn 2.0.15",
]
[[package]]
@ -3033,9 +3138,9 @@ dependencies = [
[[package]]
name = "tracing-subscriber"
version = "0.3.16"
version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70"
checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77"
dependencies = [
"matchers",
"nu-ansi-term",
@ -3235,7 +3340,7 @@ dependencies = [
"pango-sys",
"pkg-config",
"soup2-sys",
"system-deps 6.0.4",
"system-deps 6.0.5",
]
[[package]]
@ -3336,6 +3441,15 @@ dependencies = [
"windows-targets 0.42.2",
]
[[package]]
name = "windows"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f"
dependencies = [
"windows-targets 0.48.0",
]
[[package]]
name = "windows-bindgen"
version = "0.39.0"
@ -3633,11 +3747,11 @@ dependencies = [
[[package]]
name = "xcb"
version = "1.2.0"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0faeb4d7e2d54fff4a0584f61297e86b106914af2029778de7b427f72564d6c5"
checksum = "4b90c622d513012e7419594a2138953603c63848cb189041e7b5dc04d3895da5"
dependencies = [
"bitflags",
"libc",
"quick-xml 0.22.0",
"quick-xml",
]

View File

@ -32,6 +32,8 @@ paho-mqtt = "0.12.1"
time = {version="0.3.20", features= ["formatting"] }
itertools = "0.10.5"
core-foundation = "0.9.3"
tokio-stream = "0.1.14"
mdns-sd = "0.7.2"
[features]
# this feature is used for production builds or when `devPath` points to the filesystem

View File

@ -110,7 +110,7 @@ impl LedColorsPublisher {
tokio::spawn(async move {
match Self::send_colors_by_display(colors, mappers).await {
Ok(_) => {
log::info!("sent colors: #{: >15}", display_id);
// log::info!("sent colors: #{: >15}", display_id);
}
Err(err) => {
warn!("Failed to send colors: #{: >15}\t{}", display_id, err);
@ -328,12 +328,6 @@ impl LedColorsPublisher {
tx_buffer.push((offset & 0xff) as u8);
tx_buffer.append(&mut buffer);
socket.send_to(&tx_buffer, "192.168.31.206:23042").await?;
match Self::send_colors((group.start.min(group.end)) as u16, buffer).await {
Ok(_) => {}
Err(err) => {
warn!("Failed to send colors: {}", err);
}
};
}
Ok(())

View File

@ -11,7 +11,7 @@ mod screenshot_manager;
use ambient_light::{Border, ColorCalibration, LedStripConfig, LedStripConfigGroup};
use display_info::DisplayInfo;
use paris::{error, info, warn};
use rpc::MqttRpc;
use rpc::{MqttRpc, UdpRpc};
use screenshot::Screenshot;
use screenshot_manager::ScreenshotManager;
use serde::{Deserialize, Serialize};
@ -200,6 +200,8 @@ async fn main() {
let _mqtt = MqttRpc::global().await;
let _udp = UdpRpc::global().await;
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
greet,

View File

@ -0,0 +1,8 @@
use std::net::{Ipv4Addr};
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub struct BoardInfo {
pub name: String,
pub address: Ipv4Addr,
pub port: u16,
}

View File

@ -1,3 +1,7 @@
mod board_info;
mod mqtt;
mod udp;
pub use mqtt::*;
pub use board_info::*;
pub use mqtt::*;
pub use udp::*;

99
src-tauri/src/rpc/udp.rs Normal file
View File

@ -0,0 +1,99 @@
use std::{collections::HashSet, sync::Arc, time::Duration};
use mdns_sd::{ServiceDaemon, ServiceEvent};
use paris::{error, info, warn};
use tokio::{
net::UdpSocket,
sync::{Mutex, OnceCell},
};
use super::BoardInfo;
#[derive(Debug, Clone)]
pub struct UdpRpc {
socket: Arc<Mutex<UdpSocket>>,
boards: Arc<Mutex<HashSet<BoardInfo>>>,
}
impl UdpRpc {
pub async fn global() -> &'static anyhow::Result<Self> {
static UDP_RPC: OnceCell<anyhow::Result<UdpRpc>> = OnceCell::const_new();
UDP_RPC
.get_or_init(|| async {
let udp_rpc = UdpRpc::new().await?;
udp_rpc.initialize().await;
Ok(udp_rpc)
})
.await
}
async fn new() -> anyhow::Result<Self> {
let socket = UdpSocket::bind("0.0.0.0:0").await?;
let socket = Arc::new(Mutex::new(socket));
let boards = Arc::new(Mutex::new(HashSet::new()));
Ok(Self { socket, boards })
}
async fn initialize(&self) {
let shared_self = Arc::new(self.clone());
tokio::spawn(async move {
loop {
match shared_self.search_boards().await {
Ok(_) => {
info!("search_boards finished");
}
Err(err) => {
error!("search_boards failed: {:?}", err);
tokio::time::sleep(Duration::from_secs(5)).await;
}
}
}
});
}
pub async fn search_boards(&self) -> anyhow::Result<()> {
let mdns = ServiceDaemon::new()?;
let shared_self = Arc::new(Mutex::new(self.clone()));
let service_type = "_ambient_light._udp.local.";
let receiver = mdns.browse(&service_type).map_err(|e| {
warn!("Failed to browse for {:?}: {:?}", service_type, e);
e
})?;
while let Ok(event) = receiver.recv() {
match event {
ServiceEvent::ServiceResolved(info) => {
info!(
"Resolved a new service: {} host: {} port: {} IP: {:?} TXT properties: {:?}",
info.get_fullname(),
info.get_hostname(),
info.get_port(),
info.get_addresses(),
info.get_properties(),
);
let shared_self = shared_self.lock().await;
let mut boards = shared_self.boards.clone().lock_owned().await;
let board = BoardInfo {
name: info.get_fullname().to_string(),
address: info.get_addresses().iter().next().unwrap().clone(),
port: info.get_port(),
};
if boards.insert(board.clone()) {
info!("added board {:?}", board);
}
}
other_event => {
warn!("{:?}", &other_event);
}
}
}
Ok(())
}
}