Skip to content

Commit 234f9a6

Browse files
committed
fix: Allow multiple dispatch instances to work
Avahi names can't be the same. Add random suffix to the service name by default (-<4 hex digits>). Add two new command line options: --no-avahi-random (no suffix) --avahi-suffix <suffix> The two options are mutually exclusive. Resolves: #4
1 parent a0d5e19 commit 234f9a6

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

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.8", default-features = false, features = ["std", "std_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
@@ -69,7 +69,11 @@ impl AvahiService {
6969

7070
/// Register an HTTP service
7171
pub async fn register(&self, name: &str, port: u16, txt: &[(&str, &str)]) -> Result<()> {
72-
let service_type = format!("_{name}._tcp");
72+
// Service type should be the fixed dispatch service type (based on the
73+
// package name). The `name` parameter is the instance/display name
74+
// and may not match CARGO_PKG_NAME anymore;
75+
// beacon browses for the fixed service type.
76+
let service_type = format!("_{}._tcp", std::env!("CARGO_PKG_NAME"));
7377

7478
// Convert TXT records to the format expected by Avahi
7579
let txt: Vec<Vec<u8>> = txt

src/main.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ struct Args {
4444
/// Path to offer services on
4545
#[arg(short = 'p', long, default_value = concat!("/", std::env!("CARGO_PKG_NAME")))]
4646
path: String,
47+
48+
/// Disable random suffix of the Avahi service name, ie, disable default behavior
49+
#[arg(long, conflicts_with = "avahi_suffix")]
50+
no_avahi_random: bool,
51+
52+
/// Add a custom suffix to the Avahi service name (e.g., dispatch-mytest)
53+
#[arg(long, conflicts_with = "no_avahi_random")]
54+
avahi_suffix: Option<String>,
4755
}
4856

4957
#[tokio::main]
@@ -72,8 +80,18 @@ async fn main() -> Result<()> {
7280
// Create the HTTP server
7381
let server = Server::new(listener, status.clone(), github, path.clone())?;
7482

75-
// Create TXT records
76-
let name = std::env!("CARGO_PKG_NAME");
83+
// Build Avahi service name based on command line options
84+
// Clients browse by service type (_dispatch._tcp), not instance name
85+
let base = std::env!("CARGO_PKG_NAME");
86+
let name = if let Some(ref suffix) = args.avahi_suffix {
87+
format!("{base}-{suffix}")
88+
} else if !args.no_avahi_random {
89+
let random_suffix: u16 = rand::random();
90+
format!("{base}-{random_suffix:04x}")
91+
} else {
92+
base.to_string()
93+
};
94+
7795
let txt = [
7896
("description", std::env!("CARGO_PKG_DESCRIPTION")),
7997
("version", std::env!("CARGO_PKG_VERSION")),
@@ -82,7 +100,7 @@ async fn main() -> Result<()> {
82100

83101
// Start the Avahi service discovery.
84102
let avahi = AvahiService::new().await?;
85-
avahi.register(name, addr.port(), &txt).await?;
103+
avahi.register(name.as_str(), addr.port(), &txt).await?;
86104

87105
// Create event stream for terminal events
88106
let mut events = EventStream::new();

0 commit comments

Comments
 (0)