Skip to content

Commit d117f4f

Browse files
authored
refactoring: move exec_command into Zeroconf (#273)
* also fix a flaky test
1 parent 39acd80 commit d117f4f

File tree

2 files changed

+80
-75
lines changed

2 files changed

+80
-75
lines changed

src/service_daemon.rs

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,15 @@ impl ServiceDaemon {
563563
zc.status = DaemonStatus::Shutdown;
564564
return Some(command);
565565
}
566-
Self::exec_command(&mut zc, command, false);
566+
zc.exec_command(command, false);
567567
}
568568

569569
// check for repeated commands and run them if their time is up.
570570
let mut i = 0;
571571
while i < zc.retransmissions.len() {
572572
if now >= zc.retransmissions[i].next_time {
573573
let rerun = zc.retransmissions.remove(i);
574-
Self::exec_command(&mut zc, rerun.command, true);
574+
zc.exec_command(rerun.command, true);
575575
} else {
576576
i += 1;
577577
}
@@ -624,76 +624,6 @@ impl ServiceDaemon {
624624
}
625625
}
626626
}
627-
628-
/// The entry point that executes all commands received by the daemon.
629-
///
630-
/// `repeating`: whether this is a retransmission.
631-
fn exec_command(zc: &mut Zeroconf, command: Command, repeating: bool) {
632-
match command {
633-
Command::Browse(ty, next_delay, listener) => {
634-
zc.exec_command_browse(repeating, ty, next_delay, listener);
635-
}
636-
637-
Command::ResolveHostname(hostname, next_delay, listener, timeout) => {
638-
zc.exec_command_resolve_hostname(
639-
repeating, hostname, next_delay, listener, timeout,
640-
);
641-
}
642-
643-
Command::Register(service_info) => {
644-
zc.register_service(service_info);
645-
zc.increase_counter(Counter::Register, 1);
646-
}
647-
648-
Command::RegisterResend(fullname, intf) => {
649-
debug!("register-resend service: {fullname} on {:?}", &intf.addr);
650-
zc.exec_command_register_resend(fullname, intf);
651-
}
652-
653-
Command::Unregister(fullname, resp_s) => {
654-
debug!("unregister service {} repeat {}", &fullname, &repeating);
655-
zc.exec_command_unregister(repeating, fullname, resp_s);
656-
}
657-
658-
Command::UnregisterResend(packet, ip) => {
659-
zc.exec_command_unregister_resend(packet, ip);
660-
}
661-
662-
Command::StopBrowse(ty_domain) => zc.exec_command_stop_browse(ty_domain),
663-
664-
Command::StopResolveHostname(hostname) => {
665-
zc.exec_command_stop_resolve_hostname(hostname)
666-
}
667-
668-
Command::Resolve(instance, try_count) => zc.exec_command_resolve(instance, try_count),
669-
670-
Command::GetMetrics(resp_s) => match resp_s.send(zc.counters.clone()) {
671-
Ok(()) => debug!("Sent metrics to the client"),
672-
Err(e) => error!("Failed to send metrics: {}", e),
673-
},
674-
675-
Command::GetStatus(resp_s) => match resp_s.send(zc.status.clone()) {
676-
Ok(()) => debug!("Sent status to the client"),
677-
Err(e) => error!("Failed to send status: {}", e),
678-
},
679-
680-
Command::Monitor(resp_s) => {
681-
zc.monitors.push(resp_s);
682-
}
683-
684-
Command::SetOption(daemon_opt) => {
685-
zc.process_set_option(daemon_opt);
686-
}
687-
688-
Command::Verify(instance_fullname, timeout) => {
689-
zc.exec_command_verify(instance_fullname, timeout, repeating);
690-
}
691-
692-
_ => {
693-
error!("unexpected command: {:?}", &command);
694-
}
695-
}
696-
}
697627
}
698628

699629
/// Creates a new UDP socket that uses `intf` to send and recv multicast.
@@ -2402,6 +2332,76 @@ impl Zeroconf {
24022332
}
24032333
}
24042334

2335+
/// The entry point that executes all commands received by the daemon.
2336+
///
2337+
/// `repeating`: whether this is a retransmission.
2338+
fn exec_command(&mut self, command: Command, repeating: bool) {
2339+
match command {
2340+
Command::Browse(ty, next_delay, listener) => {
2341+
self.exec_command_browse(repeating, ty, next_delay, listener);
2342+
}
2343+
2344+
Command::ResolveHostname(hostname, next_delay, listener, timeout) => {
2345+
self.exec_command_resolve_hostname(
2346+
repeating, hostname, next_delay, listener, timeout,
2347+
);
2348+
}
2349+
2350+
Command::Register(service_info) => {
2351+
self.register_service(service_info);
2352+
self.increase_counter(Counter::Register, 1);
2353+
}
2354+
2355+
Command::RegisterResend(fullname, intf) => {
2356+
debug!("register-resend service: {fullname} on {:?}", &intf.addr);
2357+
self.exec_command_register_resend(fullname, intf);
2358+
}
2359+
2360+
Command::Unregister(fullname, resp_s) => {
2361+
debug!("unregister service {} repeat {}", &fullname, &repeating);
2362+
self.exec_command_unregister(repeating, fullname, resp_s);
2363+
}
2364+
2365+
Command::UnregisterResend(packet, ip) => {
2366+
self.exec_command_unregister_resend(packet, ip);
2367+
}
2368+
2369+
Command::StopBrowse(ty_domain) => self.exec_command_stop_browse(ty_domain),
2370+
2371+
Command::StopResolveHostname(hostname) => {
2372+
self.exec_command_stop_resolve_hostname(hostname)
2373+
}
2374+
2375+
Command::Resolve(instance, try_count) => self.exec_command_resolve(instance, try_count),
2376+
2377+
Command::GetMetrics(resp_s) => match resp_s.send(self.counters.clone()) {
2378+
Ok(()) => debug!("Sent metrics to the client"),
2379+
Err(e) => error!("Failed to send metrics: {}", e),
2380+
},
2381+
2382+
Command::GetStatus(resp_s) => match resp_s.send(self.status.clone()) {
2383+
Ok(()) => debug!("Sent status to the client"),
2384+
Err(e) => error!("Failed to send status: {}", e),
2385+
},
2386+
2387+
Command::Monitor(resp_s) => {
2388+
self.monitors.push(resp_s);
2389+
}
2390+
2391+
Command::SetOption(daemon_opt) => {
2392+
self.process_set_option(daemon_opt);
2393+
}
2394+
2395+
Command::Verify(instance_fullname, timeout) => {
2396+
self.exec_command_verify(instance_fullname, timeout, repeating);
2397+
}
2398+
2399+
_ => {
2400+
error!("unexpected command: {:?}", &command);
2401+
}
2402+
}
2403+
}
2404+
24052405
fn exec_command_browse(
24062406
&mut self,
24072407
repeating: bool,

tests/mdns_test.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,9 +1654,14 @@ fn test_name_tiebreaking() {
16541654
// Verify that we have resolve two services instead of one.
16551655
assert_eq!(resolved_services.len(), 2);
16561656

1657-
// Verify that server2 was resolved first, i.e. won the tiebreaking.
1658-
let first_addrs = resolved_services[0].get_addresses();
1659-
assert_eq!(first_addrs.iter().next().unwrap(), &ip_addr2);
1657+
// Verify that server2 (its ip_addr2) won the tiebreaking for the hostname.
1658+
for resolved_service in resolved_services {
1659+
if resolved_service.get_hostname() == host_name {
1660+
let service_addr = resolved_service.get_addresses().iter().next().unwrap();
1661+
assert_eq!(service_addr, &ip_addr2);
1662+
println!("server2 won the tiebreaking");
1663+
}
1664+
}
16601665
}
16611666

16621667
#[test]

0 commit comments

Comments
 (0)