Skip to content

Commit

Permalink
Bump up polling crate version and minor refactoring. (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
keepsimple1 authored Feb 20, 2023
1 parent 565bf07 commit e1026d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ default = ["async", "logging"]
flume = { version = "0.10", default-features = false } # channel between threads
if-addrs = "0.7" # get local IP addresses
log = { version = "0.4.14", optional = true } # logging
polling = "2.1.0" # select/poll sockets
polling = "2.4" # select/poll sockets
socket2 = { version = "0.4", features = ["all"] } # socket APIs

[dev-dependencies]
Expand Down
36 changes: 19 additions & 17 deletions src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,11 @@ impl ServiceDaemon {
debug!("announce service: {}", &fullname);
match zc.my_services.get(&fullname) {
Some(info) => {
let mut announced_addrs = Vec::new();
for (_, intf_sock) in zc.intf_socks.iter() {
if zc.broadcast_service_on_intf(info, intf_sock) {
announced_addrs.push(intf_sock.intf.ip);
}
}
if !announced_addrs.is_empty() {
let outgoing_addrs = zc.send_unsolicited_response(info);
if !outgoing_addrs.is_empty() {
zc.notify_monitors(DaemonEvent::Announce(
fullname,
format!("{:?}", &announced_addrs),
format!("{:?}", &outgoing_addrs),
));
}
zc.increase_counter(Counter::RegisterResend, 1);
Expand Down Expand Up @@ -727,16 +722,11 @@ impl Zeroconf {
///
/// Zeroconf will then respond to requests for information about this service.
fn register_service(&mut self, info: ServiceInfo) {
let mut announced_addrs = Vec::new();
for (_, intf_sock) in self.intf_socks.iter() {
if self.broadcast_service_on_intf(&info, intf_sock) {
announced_addrs.push(intf_sock.intf.ip);
}
}
if !announced_addrs.is_empty() {
let outgoing_addrs = self.send_unsolicited_response(&info);
if !outgoing_addrs.is_empty() {
self.notify_monitors(DaemonEvent::Announce(
info.get_fullname().to_string(),
format!("{:?}", &announced_addrs),
format!("{:?}", &outgoing_addrs),
));
}

Expand All @@ -755,7 +745,19 @@ impl Zeroconf {
self.my_services.insert(service_fullname, info);
}

/// Send an unsolicited response for owned service
/// Sends out annoucement of `info` on every valid interface.
/// Returns the list of interface IPs that sent out the annoucement.
fn send_unsolicited_response(&self, info: &ServiceInfo) -> Vec<Ipv4Addr> {
let mut outgoing_addrs = Vec::new();
for (_, intf_sock) in self.intf_socks.iter() {
if self.broadcast_service_on_intf(info, intf_sock) {
outgoing_addrs.push(intf_sock.intf.ip);
}
}
outgoing_addrs
}

/// Send an unsolicited response for owned service via `intf_sock`.
fn broadcast_service_on_intf(&self, info: &ServiceInfo, intf_sock: &IntfSock) -> bool {
let service_fullname = info.get_fullname();
debug!("broadcast service {}", service_fullname);
Expand Down

0 comments on commit e1026d0

Please sign in to comment.