Skip to main content

IpHostSubscriber

Struct IpHostSubscriber 

Source
pub struct IpHostSubscriber { /* private fields */ }
Available on crate feature broadcast only.
Expand description

Subscriber to IP address changes.

Implementations§

Source§

impl IpHostSubscriber

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more