Callbacks

Struct Callbacks 

Source
pub struct Callbacks<'a, T> {
    pub data: T,
    /* private fields */
}
Expand description

Handling mosquitto callbacks. This will pass a mutable reference to the contained data to the callbacks.

Fields§

§data: T

Implementations§

Source§

impl<'a, T> Callbacks<'a, T>

Source

pub fn new(mosq: &Mosquitto, data: T) -> Callbacks<'_, T>

create a new callback handler with data. Initialize with an existing Mosquitto reference.

Source

pub fn mosq(&self) -> &Mosquitto

a reference to the Mosquitto instance

Source

pub fn on_message<C: Fn(&mut T, MosqMessage) + 'a>(&mut self, callback: C)

provide a closure which will be called when messages arrive. You are passed a mutable reference to data and the message

Examples found in repository?
examples/subscribe-and-listen.rs (lines 15-25)
4fn main() {
5    let m = Mosquitto::new("test");
6
7    m.will_set("test/will",b"finished!",0,false).expect("can't set will");
8
9    m.connect("localhost",1883).expect("can't connect");
10    let bonzo = m.subscribe("bonzo/#",0).expect("can't subscribe to bonzo");
11    let frodo = m.subscribe("frodo/#",0).expect("can't subscribe to frodo");
12
13    // not interested in any retained messages!
14    let mut mc = m.callbacks(());
15    mc.on_message(|_,msg| {
16        if ! msg.retained() {
17            if bonzo.matches(&msg) {
18                println!("bonzo {:?}",msg);
19            } else
20            if frodo.matches(&msg) {
21                println!("frodo {:?}",msg);
22                m.disconnect().unwrap();
23            }
24        }
25    });
26
27    m.loop_forever(200).expect("broken loop");
28}
More examples
Hide additional examples
examples/self-publish.rs (lines 22-25)
5fn main() {
6    let m = Mosquitto::new("test");
7    
8    m.connect("localhost",1883).expect("can't connect");
9    m.subscribe("bilbo/#",1).expect("can't subscribe to bonzo");
10    
11    let mt = m.clone();
12    thread::spawn(move || {
13        let timeout = time::Duration::from_millis(500);
14        for _ in 0..5 {
15            mt.publish("bilbo/baggins","hello dolly".as_bytes(), 1, false).unwrap();
16            thread::sleep(timeout);
17        }
18        mt.disconnect().unwrap();
19    });
20    
21    let mut mc = m.callbacks(0);
22    mc.on_message(|data,msg| {
23        println!("bilbo {:?}",msg);
24        *data += 1;
25    });
26    mc.on_disconnect(|_,rc| println!("disconnect {}",rc));
27    
28    
29    m.loop_until_disconnect(200).expect("broken loop");
30    println!("received {} messages",mc.data);
31}
Source

pub fn on_connect<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)

provide a closure which is called when connection happens. You are passed a mutable reference to data and the status.

Source

pub fn on_publish<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)

provide a closure which is called after publishing a message. You are passed a mutable reference to data and the message id.

Examples found in repository?
examples/verify-publish.rs (lines 16-20)
6fn go() -> Result<(),Box<Error>> {
7    let m = Mosquitto::new("test");
8
9    m.connect("localhost",1883)?;
10
11    // publish and get a message id
12    let our_mid = m.publish("bonzo/dog","hello dolly".as_bytes(), 2, false)?;
13
14    // and wait for confirmation for that message id
15    let mut mc = m.callbacks(());
16    mc.on_publish(|_,mid| {
17        if mid == our_mid {
18            m.disconnect().unwrap();
19        }
20    });
21
22    // wait forever until explicit disconnect
23    m.loop_until_disconnect(-1)?;
24    Ok(())
25}
Source

pub fn on_subscribe<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)

provide a closure which is called after subscribing. You are passed a mutable reference to data and the subscription id.

Source

pub fn on_unsubscribe<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)

provide a closure which is called after unsubscribing from a topic You are passed a mutable reference to data and the subscription id.

Source

pub fn on_disconnect<C: Fn(&mut T, i32) + 'a>(&mut self, callback: C)

provide a closure which is called when client disconnects from broker. You are passed a mutable reference to data and ….

Examples found in repository?
examples/self-publish.rs (line 26)
5fn main() {
6    let m = Mosquitto::new("test");
7    
8    m.connect("localhost",1883).expect("can't connect");
9    m.subscribe("bilbo/#",1).expect("can't subscribe to bonzo");
10    
11    let mt = m.clone();
12    thread::spawn(move || {
13        let timeout = time::Duration::from_millis(500);
14        for _ in 0..5 {
15            mt.publish("bilbo/baggins","hello dolly".as_bytes(), 1, false).unwrap();
16            thread::sleep(timeout);
17        }
18        mt.disconnect().unwrap();
19    });
20    
21    let mut mc = m.callbacks(0);
22    mc.on_message(|data,msg| {
23        println!("bilbo {:?}",msg);
24        *data += 1;
25    });
26    mc.on_disconnect(|_,rc| println!("disconnect {}",rc));
27    
28    
29    m.loop_until_disconnect(200).expect("broken loop");
30    println!("received {} messages",mc.data);
31}
Source

pub fn on_log<C: Fn(&mut T, u32, &str) + 'a>(&mut self, callback: C)

provide a closure which is called for each log message You are passed a mutable reference to data, a logging level, and the text of the log message

Trait Implementations§

Source§

impl<'a, T> Drop for Callbacks<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Callbacks<'a, T>
where T: Freeze,

§

impl<'a, T> !RefUnwindSafe for Callbacks<'a, T>

§

impl<'a, T> !Send for Callbacks<'a, T>

§

impl<'a, T> !Sync for Callbacks<'a, T>

§

impl<'a, T> Unpin for Callbacks<'a, T>
where T: Unpin,

§

impl<'a, T> !UnwindSafe for Callbacks<'a, T>

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, 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.