Skip to content

Commit

Permalink
refactoring: move exec_command into Zeroconf (#273)
Browse files Browse the repository at this point in the history
* also fix a flaky test
  • Loading branch information
keepsimple1 authored Nov 23, 2024
1 parent 39acd80 commit d117f4f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 75 deletions.
144 changes: 72 additions & 72 deletions src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,15 @@ impl ServiceDaemon {
zc.status = DaemonStatus::Shutdown;
return Some(command);
}
Self::exec_command(&mut zc, command, false);
zc.exec_command(command, false);
}

// check for repeated commands and run them if their time is up.
let mut i = 0;
while i < zc.retransmissions.len() {
if now >= zc.retransmissions[i].next_time {
let rerun = zc.retransmissions.remove(i);
Self::exec_command(&mut zc, rerun.command, true);
zc.exec_command(rerun.command, true);
} else {
i += 1;
}
Expand Down Expand Up @@ -624,76 +624,6 @@ impl ServiceDaemon {
}
}
}

/// The entry point that executes all commands received by the daemon.
///
/// `repeating`: whether this is a retransmission.
fn exec_command(zc: &mut Zeroconf, command: Command, repeating: bool) {
match command {
Command::Browse(ty, next_delay, listener) => {
zc.exec_command_browse(repeating, ty, next_delay, listener);
}

Command::ResolveHostname(hostname, next_delay, listener, timeout) => {
zc.exec_command_resolve_hostname(
repeating, hostname, next_delay, listener, timeout,
);
}

Command::Register(service_info) => {
zc.register_service(service_info);
zc.increase_counter(Counter::Register, 1);
}

Command::RegisterResend(fullname, intf) => {
debug!("register-resend service: {fullname} on {:?}", &intf.addr);
zc.exec_command_register_resend(fullname, intf);
}

Command::Unregister(fullname, resp_s) => {
debug!("unregister service {} repeat {}", &fullname, &repeating);
zc.exec_command_unregister(repeating, fullname, resp_s);
}

Command::UnregisterResend(packet, ip) => {
zc.exec_command_unregister_resend(packet, ip);
}

Command::StopBrowse(ty_domain) => zc.exec_command_stop_browse(ty_domain),

Command::StopResolveHostname(hostname) => {
zc.exec_command_stop_resolve_hostname(hostname)
}

Command::Resolve(instance, try_count) => zc.exec_command_resolve(instance, try_count),

Command::GetMetrics(resp_s) => match resp_s.send(zc.counters.clone()) {
Ok(()) => debug!("Sent metrics to the client"),
Err(e) => error!("Failed to send metrics: {}", e),
},

Command::GetStatus(resp_s) => match resp_s.send(zc.status.clone()) {
Ok(()) => debug!("Sent status to the client"),
Err(e) => error!("Failed to send status: {}", e),
},

Command::Monitor(resp_s) => {
zc.monitors.push(resp_s);
}

Command::SetOption(daemon_opt) => {
zc.process_set_option(daemon_opt);
}

Command::Verify(instance_fullname, timeout) => {
zc.exec_command_verify(instance_fullname, timeout, repeating);
}

_ => {
error!("unexpected command: {:?}", &command);
}
}
}
}

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

/// The entry point that executes all commands received by the daemon.
///
/// `repeating`: whether this is a retransmission.
fn exec_command(&mut self, command: Command, repeating: bool) {
match command {
Command::Browse(ty, next_delay, listener) => {
self.exec_command_browse(repeating, ty, next_delay, listener);
}

Command::ResolveHostname(hostname, next_delay, listener, timeout) => {
self.exec_command_resolve_hostname(
repeating, hostname, next_delay, listener, timeout,
);
}

Command::Register(service_info) => {
self.register_service(service_info);
self.increase_counter(Counter::Register, 1);
}

Command::RegisterResend(fullname, intf) => {
debug!("register-resend service: {fullname} on {:?}", &intf.addr);
self.exec_command_register_resend(fullname, intf);
}

Command::Unregister(fullname, resp_s) => {
debug!("unregister service {} repeat {}", &fullname, &repeating);
self.exec_command_unregister(repeating, fullname, resp_s);
}

Command::UnregisterResend(packet, ip) => {
self.exec_command_unregister_resend(packet, ip);
}

Command::StopBrowse(ty_domain) => self.exec_command_stop_browse(ty_domain),

Command::StopResolveHostname(hostname) => {
self.exec_command_stop_resolve_hostname(hostname)
}

Command::Resolve(instance, try_count) => self.exec_command_resolve(instance, try_count),

Command::GetMetrics(resp_s) => match resp_s.send(self.counters.clone()) {
Ok(()) => debug!("Sent metrics to the client"),
Err(e) => error!("Failed to send metrics: {}", e),
},

Command::GetStatus(resp_s) => match resp_s.send(self.status.clone()) {
Ok(()) => debug!("Sent status to the client"),
Err(e) => error!("Failed to send status: {}", e),
},

Command::Monitor(resp_s) => {
self.monitors.push(resp_s);
}

Command::SetOption(daemon_opt) => {
self.process_set_option(daemon_opt);
}

Command::Verify(instance_fullname, timeout) => {
self.exec_command_verify(instance_fullname, timeout, repeating);
}

_ => {
error!("unexpected command: {:?}", &command);
}
}
}

fn exec_command_browse(
&mut self,
repeating: bool,
Expand Down
11 changes: 8 additions & 3 deletions tests/mdns_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,9 +1654,14 @@ fn test_name_tiebreaking() {
// Verify that we have resolve two services instead of one.
assert_eq!(resolved_services.len(), 2);

// Verify that server2 was resolved first, i.e. won the tiebreaking.
let first_addrs = resolved_services[0].get_addresses();
assert_eq!(first_addrs.iter().next().unwrap(), &ip_addr2);
// Verify that server2 (its ip_addr2) won the tiebreaking for the hostname.
for resolved_service in resolved_services {
if resolved_service.get_hostname() == host_name {
let service_addr = resolved_service.get_addresses().iter().next().unwrap();
assert_eq!(service_addr, &ip_addr2);
println!("server2 won the tiebreaking");
}
}
}

#[test]
Expand Down

0 comments on commit d117f4f

Please sign in to comment.