Skip to content

Commit 2a17b51

Browse files
committed
fix: Allow multiple dispatch instances to work
Add 16-bit random suffix to Avahi service name. Add rand crate as a dependency of dispatch in a way that doesn't add new deps. Resolves: #4
1 parent a0d5e19 commit 2a17b51

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ zbus = { version = "5.11", default-features = false, features = ["tokio"] }
2323
http-body-util = { version = "0.1", default-features = false }
2424
serde_json = { version = "1.0", default-features = false }
2525
anyhow = { version = "1.0", default-features = false }
26+
rand = { version = "0.9", default-features = false, features = ["std", "std_rng", "thread_rng"] }
2627

2728
[build-dependencies]
2829
uefi-reset = { version = "1.0", artifact = "bin", target = "x86_64-unknown-uefi" }

src/avahi.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ impl AvahiService {
7171
pub async fn register(&self, name: &str, port: u16, txt: &[(&str, &str)]) -> Result<()> {
7272
let service_type = format!("_{name}._tcp");
7373

74+
// Generate a random suffix to avoid service name collisions on the network.
75+
let random_suffix: u16 = rand::random();
76+
let service_name = format!("{name}-{random_suffix:04x}");
77+
7478
// Convert TXT records to the format expected by Avahi
7579
let txt: Vec<Vec<u8>> = txt
7680
.iter()
@@ -84,7 +88,7 @@ impl AvahiService {
8488
// domain: "" (use default)
8589
// host: "" (use local hostname)
8690
self.entry_group
87-
.add_service(-1, 0, 0, name, &service_type, "", "", port, txt)
91+
.add_service(-1, 0, 0, &service_name, &service_type, "", "", port, txt)
8892
.await
8993
.context("Failed to add service")?;
9094

0 commit comments

Comments
 (0)