Skip to content

Commit 5f666f7

Browse files
markg-githubnpmccallum
authored andcommitted
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 d6c803a commit 5f666f7

3 files changed

Lines changed: 6 additions & 1 deletion

File tree

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 = ["thread_rng"] }
2627

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

src/avahi.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ 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 service_name = format!("{name}-{:04x}", rand::random::<u16>());
76+
7477
// Convert TXT records to the format expected by Avahi
7578
let txt: Vec<Vec<u8>> = txt
7679
.iter()
@@ -84,7 +87,7 @@ impl AvahiService {
8487
// domain: "" (use default)
8588
// host: "" (use local hostname)
8689
self.entry_group
87-
.add_service(-1, 0, 0, name, &service_type, "", "", port, txt)
90+
.add_service(-1, 0, 0, &service_name, &service_type, "", "", port, txt)
8891
.await
8992
.context("Failed to add service")?;
9093

0 commit comments

Comments
 (0)