pub struct IpHostSubscriber { /* private fields */ }Available on crate feature
broadcast only.Expand description
Subscriber to IP address changes.
Implementations§
Source§impl IpHostSubscriber
impl IpHostSubscriber
Sourcepub fn wait_for_change_blocking(&self) -> Option<IpChanges>
pub fn wait_for_change_blocking(&self) -> Option<IpChanges>
Wait for next IP address changes, blocking the current thread.
Returns None if the resolver is dropped for some reason.
This function is safe to be called from an async context, like tokio::task::spawn_blocking().
Examples found in repository?
examples/monitor-iphost.rs (line 77)
19fn main() {
20 let args = std::env::args().collect::<Vec<_>>();
21 if args.len() < 2 {
22 println!("Usage: {} <hostname>", &args[0]);
23 std::process::exit(0);
24 }
25
26 let ip_host = match args[1].parse::<IpHost>() {
27 Ok(h) => h,
28 _ => {
29 eprintln!("{}: Invalid hostname", &args[0]);
30 std::process::exit(1);
31 }
32 };
33
34 let ip_host_clone = ip_host.clone();
35
36 print_event(format_args!(
37 "Monitoring address changes for hostname: {}",
38 &ip_host
39 ));
40 let resolver = AutoIpHostResolver::new(ip_host, Duration::from_secs(1));
41
42 #[cfg(not(feature = "broadcast"))]
43 let mut ipv4 = None;
44
45 #[cfg(not(feature = "broadcast"))]
46 let mut ipv6 = None;
47
48 #[cfg(not(feature = "broadcast"))]
49 loop {
50 let new_ipv4 = resolver.ipv4_addr();
51 if new_ipv4 != ipv4 {
52 if let Some(v4) = new_ipv4 {
53 print_event(format_args!("<{}> new_v4 = {}", &ip_host_clone, v4));
54 } else {
55 print_event(format_args!("<{}> new_v4 = (none)", &ip_host_clone));
56 }
57 ipv4 = new_ipv4;
58 }
59
60 let new_ipv6 = resolver.ipv6_addr();
61 if new_ipv6 != ipv6 {
62 if let Some(v6) = new_ipv6 {
63 print_event(format_args!("<{}> new_v6 = {}", &ip_host_clone, v6));
64 } else {
65 print_event(format_args!("<{}> new_v6 = (none)", &ip_host_clone));
66 }
67 ipv6 = new_ipv6;
68 }
69
70 std::thread::sleep(Duration::from_millis(100));
71 }
72
73 #[cfg(feature = "broadcast")]
74 {
75 let subscriber = resolver.subscribe_changes();
76 loop {
77 if let Some(changes) = subscriber.wait_for_change_blocking() {
78 if let Some(v4_change) = changes.v4_change() {
79 if let Some(v4_addr) = v4_change {
80 print_event(format_args!("<{}> new_v4 = {}", &ip_host_clone, v4_addr));
81 } else {
82 print_event(format_args!("<{}> new_v4 = (none)", &ip_host_clone));
83 }
84 }
85 if let Some(v6_change) = changes.v6_change() {
86 if let Some(v6_addr) = v6_change {
87 print_event(format_args!("<{}> new_v6 = {}", &ip_host_clone, v6_addr));
88 } else {
89 print_event(format_args!("<{}> new_v6 = (none)", &ip_host_clone));
90 }
91 }
92 } else {
93 print_event(format_args!("Error: Resolver exited"));
94 std::process::exit(1);
95 }
96 }
97 }
98}Auto Trait Implementations§
impl Freeze for IpHostSubscriber
impl RefUnwindSafe for IpHostSubscriber
impl Send for IpHostSubscriber
impl Sync for IpHostSubscriber
impl Unpin for IpHostSubscriber
impl UnsafeUnpin for IpHostSubscriber
impl UnwindSafe for IpHostSubscriber
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more