pub struct TetherAgentOptionsBuilder { /* private fields */ }

Implementations§

source§

impl TetherAgentOptionsBuilder

source

pub fn new(role: &str) -> Self

Initialise Tether Options struct with default options; call other methods to customise. Call build() to get the actual TetherAgent instance (and connect automatically, by default)

Examples found in repository?
examples/custom_options.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let tether_agent = TetherAgentOptionsBuilder::new("example")
        .id("customId")
        .host("localhost")
        .port(1883)
        .username("tether")
        .password("sp_ceB0ss!")
        .build()
        .expect("failed to create Tether Agent");

    let _output_plug = PlugOptionsBuilder::create_output("anOutput")
        .qos(2)
        .retain(true)
        .build(&tether_agent);
    let _input_plug = PlugOptionsBuilder::create_input("everything")
        .topic("#")
        .build(&tether_agent);

    // And then proceed as usual!
}
More examples
Hide additional examples
examples/publish.rs (line 22)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn main() {
    println!("Rust Tether Agent publish example");

    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .build()
        .expect("failed to connect Tether");
    let (role, id) = agent.description();
    info!("Created agent OK: {}, {}", role, id);

    let empty_message_output = PlugOptionsBuilder::create_output("nothing")
        .build(&agent)
        .expect("failed to create output");
    let boolean_message_output = PlugOptionsBuilder::create_output("one")
        .build(&agent)
        .expect("failed to create output");
    let custom_output = PlugOptionsBuilder::create_output("two")
        .build(&agent)
        .expect("failed to create input/subscribe");

    for i in 1..=10 {
        info!("#{i}: Sending empty message...");
        agent.publish(&empty_message_output, None).unwrap();

        let bool = i % 2 == 0;
        info!("#{i}: Sending boolean message...");
        agent
            .publish(&boolean_message_output, Some(&[bool.into()]))
            .unwrap();

        info!("#{i}: Sending custom struct message...");
        let custom_message = CustomStruct {
            id: i,
            name: "hello".into(),
        };
        agent
            .encode_and_publish(&custom_output, custom_message)
            .unwrap();

        thread::sleep(Duration::from_millis(1000))
    }
}
examples/username_password.rs (line 22)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
fn main() {
    println!("Rust Tether Agent: with username and password");

    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .host("10.112.10.10")
        .username("connected.space")
        .password("connected.space")
        .build()
        .expect("Failed to initialise and connect");
    let (role, id) = tether_agent.description();
    info!("Created agent OK: {}, {}", role, id);

    let empty_message_output = PlugOptionsBuilder::create_output("nothing")
        .build(&tether_agent)
        .expect("failed to create output");
    let boolean_message_output = PlugOptionsBuilder::create_output("one")
        .build(&tether_agent)
        .expect("failed to create output");
    let custom_output = PlugOptionsBuilder::create_output("two")
        .build(&tether_agent)
        .expect("failed to create output");

    for i in 1..=10 {
        info!("#{i}: Sending empty message...");
        tether_agent.publish(&empty_message_output, None).unwrap();

        let bool = i % 2 == 0;
        info!("#{i}: Sending boolean message...");
        tether_agent
            .publish(&boolean_message_output, Some(&[bool.into()]))
            .unwrap();

        let custom_message = CustomStruct {
            foo: "hello".into(),
            bar: 0.42,
        };
        tether_agent
            .encode_and_publish(&custom_output, custom_message)
            .unwrap();

        thread::sleep(Duration::from_millis(1000))
    }
}
examples/subscribe_threaded.rs (line 29)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
fn main() {
    println!("Rust Tether Agent subscribe example");

    let tether_agent = Arc::new(Mutex::new(
        TetherAgentOptionsBuilder::new("RustDemoAgent")
            .id("example")
            .build()
            .expect("failed to init/connect"),
    ));

    match tether_agent.lock() {
        Ok(a) => {
            let _input_plug = PlugOptionsBuilder::create_output("one").build(&a);
        }
        Err(e) => {
            panic!("Failed to acquire lock for Tether Agent setup: {}", e);
        }
    };

    let (tx, rx) = mpsc::channel();

    let receiver_agent = Arc::clone(&tether_agent);
    thread::spawn(move || {
        println!("Checking messages every 1s, 10x...");

        let mut message_count = 0;
        // let mut i = 0;

        loop {
            // i += 1;
            // println!("#{i}: Checking messages...");
            match receiver_agent.try_lock() {
                Ok(a) => {
                    if let Some((topic, _message)) = a.check_messages() {
                        message_count += 1;
                        println!("<<<<<<<< CHECKING LOOP: Received a message on topic {topic}",);
                        tx.send(format!("received message #{message_count}"))
                            .expect("failed to send message via channel");
                    }
                }
                Err(e) => {
                    println!("Failed to acquire lock: {}", e);
                }
            }
            thread::sleep(Duration::from_millis(1));
        }
    });

    let mut main_thread_received_count = 0;

    loop {
        println!("Main thread sleep...");
        for rx in rx.try_iter() {
            main_thread_received_count += 1;
            println!(
                "<<<<<<<< MAIN THREAD: received {} (count: {})",
                rx, main_thread_received_count
            );
        }
        if main_thread_received_count >= 10 {
            println!("We're done!");
            std::process::exit(0);
        }
        std::thread::sleep(Duration::from_secs(1));
    }
}
examples/error_handling.rs (line 18)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let bad_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .username("bla")
        .password("bla")
        .build();
    match bad_tether_agent {
        Ok(_agent) => {
            panic!("Connection: This shouldn't work!");
        }
        Err(e) => warn!("Got a connection error as expected: {e:?}"),
    }

    let disconnected = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .auto_connect(false)
        .build()
        .expect("this ought initialise but not conect");

    let output = PlugOptionsBuilder::create_output("values")
        .build(&disconnected)
        .expect("this output should be valid always");

    let an_array = &vec![0, 1, 2, 3];
    match disconnected.encode_and_publish(&output, an_array) {
        Ok(()) => panic!("Publish on disconnected agent: This shouldn't work!"),
        Err(e) => warn!("Got a not-connected error as expected: {e:?}"),
    }

    let input_on_disconnected = PlugOptionsBuilder::create_input("something");
    match input_on_disconnected.build(&disconnected) {
        Ok(_) => panic!("Input plug subscribe on disconnected client: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe failure error as expected: {e:?}"),
    }

    // Rust's type-checking kind of prevents this happening at all!
    // let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    // match working_tether_agent.encode_and_publish::<CustomStruct>(&output, bad_payload) {
    //     Ok(()) => panic!("Encoding: This shouldn't work!"),
    //     Err(e) => warn!("Got an encoding error as expected: {e:?}"),
    // }

    let working_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .build()
        .expect("this should connect to local server");

    let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    working_tether_agent
        .publish(&output, Some(bad_payload))
        .expect("This will produce an error when DECODING, but not checked by library");

    let bad_topic_input = PlugOptionsBuilder::create_input("something").topic("*/#/house+");
    match bad_topic_input.build(&working_tether_agent) {
        Ok(_) => panic!("Weird topic: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe error (bad topic) as expected: {e:?}"),
    }
}
examples/subscribe.rs (line 25)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
fn main() {
    println!("Rust Tether Agent subscribe example");

    let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .id("example")
        .build()
        .expect("failed to init Tether agent");

    let input_one = PlugOptionsBuilder::create_input("one")
        .build(&tether_agent)
        .expect("failed to create input");
    debug!("input one {} = {}", input_one.name(), input_one.topic());
    let input_two = PlugOptionsBuilder::create_input("two")
        .build(&tether_agent)
        .expect("failed to create input");
    debug!("input two {} = {}", input_two.name(), input_two.topic());
    let input_empty = PlugOptionsBuilder::create_input("nothing")
        .build(&tether_agent)
        .expect("failed to create input");

    info!("Checking messages every 1s, 10x...");

    for i in 1..10 {
        info!("#{i}: Checking for messages...");
        while let Some((plug_name, message)) = tether_agent.check_messages() {
            debug!(
                "Received a message topic {} => plug name {}",
                message.topic(),
                plug_name
            );
            if &plug_name == input_one.name() {
                info!(
                    "******** INPUT ONE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_one.name(),
                    message.topic(),
                    message.payload().len()
                );
            }
            if &plug_name == input_two.name() {
                info!(
                    "******** INPUT TWO:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_two.name(),
                    message.topic(),
                    message.payload().len()
                );
                // Notice how you must give the from_slice function a type so it knows what to expect
                let decoded = from_slice::<CustomMessage>(&message.payload());
                match decoded {
                    Ok(d) => {
                        info!("Yes, we decoded the MessagePack payload as: {:?}", d);
                        let CustomMessage { name, id } = d;
                        debug!("Name is {} and ID is {}", name, id);
                    }
                    Err(e) => {
                        warn!("Failed to decode the payload: {}", e)
                    }
                };
            }
            if &plug_name == input_empty.name() {
                info!(
                    "******** EMPTY MESSAGE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_empty.name(),
                    message.topic(),
                    message.payload().len()
                );
            }
        }
        thread::sleep(Duration::from_millis(1000))
    }
}
source

pub fn id(self, id: &str) -> Self

Examples found in repository?
examples/custom_options.rs (line 5)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let tether_agent = TetherAgentOptionsBuilder::new("example")
        .id("customId")
        .host("localhost")
        .port(1883)
        .username("tether")
        .password("sp_ceB0ss!")
        .build()
        .expect("failed to create Tether Agent");

    let _output_plug = PlugOptionsBuilder::create_output("anOutput")
        .qos(2)
        .retain(true)
        .build(&tether_agent);
    let _input_plug = PlugOptionsBuilder::create_input("everything")
        .topic("#")
        .build(&tether_agent);

    // And then proceed as usual!
}
More examples
Hide additional examples
examples/subscribe_threaded.rs (line 30)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
fn main() {
    println!("Rust Tether Agent subscribe example");

    let tether_agent = Arc::new(Mutex::new(
        TetherAgentOptionsBuilder::new("RustDemoAgent")
            .id("example")
            .build()
            .expect("failed to init/connect"),
    ));

    match tether_agent.lock() {
        Ok(a) => {
            let _input_plug = PlugOptionsBuilder::create_output("one").build(&a);
        }
        Err(e) => {
            panic!("Failed to acquire lock for Tether Agent setup: {}", e);
        }
    };

    let (tx, rx) = mpsc::channel();

    let receiver_agent = Arc::clone(&tether_agent);
    thread::spawn(move || {
        println!("Checking messages every 1s, 10x...");

        let mut message_count = 0;
        // let mut i = 0;

        loop {
            // i += 1;
            // println!("#{i}: Checking messages...");
            match receiver_agent.try_lock() {
                Ok(a) => {
                    if let Some((topic, _message)) = a.check_messages() {
                        message_count += 1;
                        println!("<<<<<<<< CHECKING LOOP: Received a message on topic {topic}",);
                        tx.send(format!("received message #{message_count}"))
                            .expect("failed to send message via channel");
                    }
                }
                Err(e) => {
                    println!("Failed to acquire lock: {}", e);
                }
            }
            thread::sleep(Duration::from_millis(1));
        }
    });

    let mut main_thread_received_count = 0;

    loop {
        println!("Main thread sleep...");
        for rx in rx.try_iter() {
            main_thread_received_count += 1;
            println!(
                "<<<<<<<< MAIN THREAD: received {} (count: {})",
                rx, main_thread_received_count
            );
        }
        if main_thread_received_count >= 10 {
            println!("We're done!");
            std::process::exit(0);
        }
        std::thread::sleep(Duration::from_secs(1));
    }
}
examples/subscribe.rs (line 26)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
fn main() {
    println!("Rust Tether Agent subscribe example");

    let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .id("example")
        .build()
        .expect("failed to init Tether agent");

    let input_one = PlugOptionsBuilder::create_input("one")
        .build(&tether_agent)
        .expect("failed to create input");
    debug!("input one {} = {}", input_one.name(), input_one.topic());
    let input_two = PlugOptionsBuilder::create_input("two")
        .build(&tether_agent)
        .expect("failed to create input");
    debug!("input two {} = {}", input_two.name(), input_two.topic());
    let input_empty = PlugOptionsBuilder::create_input("nothing")
        .build(&tether_agent)
        .expect("failed to create input");

    info!("Checking messages every 1s, 10x...");

    for i in 1..10 {
        info!("#{i}: Checking for messages...");
        while let Some((plug_name, message)) = tether_agent.check_messages() {
            debug!(
                "Received a message topic {} => plug name {}",
                message.topic(),
                plug_name
            );
            if &plug_name == input_one.name() {
                info!(
                    "******** INPUT ONE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_one.name(),
                    message.topic(),
                    message.payload().len()
                );
            }
            if &plug_name == input_two.name() {
                info!(
                    "******** INPUT TWO:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_two.name(),
                    message.topic(),
                    message.payload().len()
                );
                // Notice how you must give the from_slice function a type so it knows what to expect
                let decoded = from_slice::<CustomMessage>(&message.payload());
                match decoded {
                    Ok(d) => {
                        info!("Yes, we decoded the MessagePack payload as: {:?}", d);
                        let CustomMessage { name, id } = d;
                        debug!("Name is {} and ID is {}", name, id);
                    }
                    Err(e) => {
                        warn!("Failed to decode the payload: {}", e)
                    }
                };
            }
            if &plug_name == input_empty.name() {
                info!(
                    "******** EMPTY MESSAGE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_empty.name(),
                    message.topic(),
                    message.payload().len()
                );
            }
        }
        thread::sleep(Duration::from_millis(1000))
    }
}
examples/subscribe_publish_threaded.rs (line 34)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
fn main() {
    let check_interval = 0.01;
    let publish_count_target = 100;
    let publish_interval = 0.1;

    println!("Rust Tether Agent threaded publish-while-consuming example");

    let tether_agent = Arc::new(Mutex::new(
        TetherAgentOptionsBuilder::new("RustDemoAgent")
            .id("example")
            .build()
            .expect("failed to init/connect"),
    ));

    println!("Set up tether agent OK");

    let mut output_plug = None;

    // Here we call .lock() because it is OK to block while "setting up", connecting
    if let Ok(a) = tether_agent.lock() {
        let _input_plug = PlugOptionsBuilder::create_input("one").build(&a);
        output_plug = Some(
            PlugOptionsBuilder::create_output("one")
                .build(&a)
                .expect("failed to create output plug"),
        );
    } else {
        panic!("Error setting up Tether Agent!");
    }

    let receiver_agent = Arc::clone(&tether_agent);
    thread::spawn(move || {
        println!("Checking messages every {check_interval}s...");

        let mut i = 0;
        let mut count_messages_received = 0;

        /*
         Infinite loop. But because we never join the threads, this thread will terminate
         as soon as the main thread does.
        */
        loop {
            i += 1;
            println!("CHECKING LOOP: Checking messages attempt #{i}...");

            /*
              Here we call try_lock() because we do not want to block
              if the Agent is currently locked by another thread.
              Just print a message, wait and try again later.
            */
            match receiver_agent.try_lock() {
                Ok(a) => {
                    if let Some((topic, _message)) = a.check_messages() {
                        count_messages_received += 1;
                        println!("<<<<<<<< CHECKING LOOP: Received a message on topic {topic}; Now has {count_messages_received} messages");
                    }
                }
                Err(e) => {
                    println!("CHECKING LOOP: Failed to acquire lock: {}", e);
                }
            }
            thread::sleep(Duration::from_secs_f32(check_interval));
        }
    });

    let sending_agent = Arc::clone(&tether_agent);
    println!(
        "Sending a message, every {}s, exactly {}x times...",
        publish_interval, publish_count_target
    );
    let mut count_messages_sent = 0;
    for i in 1..=publish_count_target {
        println!("MAIN THREAD LOOP: Send attempt #{i}");
        /*
          In this particular case, lock() is preferable to try_lock() because
          we are not doing anything else on this thread. Waiting (blocking)
          to acquire the lock
          is fine; the other thread will let it go soon.
        */
        match sending_agent.lock() {
            Ok(a) => {
                count_messages_sent += 1;
                if let Some(plug) = &output_plug {
                    a.publish(plug, Some(&[0])).expect("Failed to publish");
                    println!(">>>>>>>> MAIN THREAD LOOP: sent {count_messages_sent} messages");
                }
            }
            Err(e) => {
                panic!("MAIN THREAD LOOP: Failed to acquire lock: {}", e);
            }
        }
        thread::sleep(Duration::from_secs_f32(publish_interval));
    }
}
source

pub fn host(self, host: &str) -> Self

Examples found in repository?
examples/custom_options.rs (line 6)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let tether_agent = TetherAgentOptionsBuilder::new("example")
        .id("customId")
        .host("localhost")
        .port(1883)
        .username("tether")
        .password("sp_ceB0ss!")
        .build()
        .expect("failed to create Tether Agent");

    let _output_plug = PlugOptionsBuilder::create_output("anOutput")
        .qos(2)
        .retain(true)
        .build(&tether_agent);
    let _input_plug = PlugOptionsBuilder::create_input("everything")
        .topic("#")
        .build(&tether_agent);

    // And then proceed as usual!
}
More examples
Hide additional examples
examples/username_password.rs (line 23)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
fn main() {
    println!("Rust Tether Agent: with username and password");

    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .host("10.112.10.10")
        .username("connected.space")
        .password("connected.space")
        .build()
        .expect("Failed to initialise and connect");
    let (role, id) = tether_agent.description();
    info!("Created agent OK: {}, {}", role, id);

    let empty_message_output = PlugOptionsBuilder::create_output("nothing")
        .build(&tether_agent)
        .expect("failed to create output");
    let boolean_message_output = PlugOptionsBuilder::create_output("one")
        .build(&tether_agent)
        .expect("failed to create output");
    let custom_output = PlugOptionsBuilder::create_output("two")
        .build(&tether_agent)
        .expect("failed to create output");

    for i in 1..=10 {
        info!("#{i}: Sending empty message...");
        tether_agent.publish(&empty_message_output, None).unwrap();

        let bool = i % 2 == 0;
        info!("#{i}: Sending boolean message...");
        tether_agent
            .publish(&boolean_message_output, Some(&[bool.into()]))
            .unwrap();

        let custom_message = CustomStruct {
            foo: "hello".into(),
            bar: 0.42,
        };
        tether_agent
            .encode_and_publish(&custom_output, custom_message)
            .unwrap();

        thread::sleep(Duration::from_millis(1000))
    }
}
examples/error_handling.rs (line 19)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let bad_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .username("bla")
        .password("bla")
        .build();
    match bad_tether_agent {
        Ok(_agent) => {
            panic!("Connection: This shouldn't work!");
        }
        Err(e) => warn!("Got a connection error as expected: {e:?}"),
    }

    let disconnected = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .auto_connect(false)
        .build()
        .expect("this ought initialise but not conect");

    let output = PlugOptionsBuilder::create_output("values")
        .build(&disconnected)
        .expect("this output should be valid always");

    let an_array = &vec![0, 1, 2, 3];
    match disconnected.encode_and_publish(&output, an_array) {
        Ok(()) => panic!("Publish on disconnected agent: This shouldn't work!"),
        Err(e) => warn!("Got a not-connected error as expected: {e:?}"),
    }

    let input_on_disconnected = PlugOptionsBuilder::create_input("something");
    match input_on_disconnected.build(&disconnected) {
        Ok(_) => panic!("Input plug subscribe on disconnected client: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe failure error as expected: {e:?}"),
    }

    // Rust's type-checking kind of prevents this happening at all!
    // let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    // match working_tether_agent.encode_and_publish::<CustomStruct>(&output, bad_payload) {
    //     Ok(()) => panic!("Encoding: This shouldn't work!"),
    //     Err(e) => warn!("Got an encoding error as expected: {e:?}"),
    // }

    let working_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .build()
        .expect("this should connect to local server");

    let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    working_tether_agent
        .publish(&output, Some(bad_payload))
        .expect("This will produce an error when DECODING, but not checked by library");

    let bad_topic_input = PlugOptionsBuilder::create_input("something").topic("*/#/house+");
    match bad_topic_input.build(&working_tether_agent) {
        Ok(_) => panic!("Weird topic: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe error (bad topic) as expected: {e:?}"),
    }
}
source

pub fn port(self, port: u16) -> Self

Examples found in repository?
examples/custom_options.rs (line 7)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let tether_agent = TetherAgentOptionsBuilder::new("example")
        .id("customId")
        .host("localhost")
        .port(1883)
        .username("tether")
        .password("sp_ceB0ss!")
        .build()
        .expect("failed to create Tether Agent");

    let _output_plug = PlugOptionsBuilder::create_output("anOutput")
        .qos(2)
        .retain(true)
        .build(&tether_agent);
    let _input_plug = PlugOptionsBuilder::create_input("everything")
        .topic("#")
        .build(&tether_agent);

    // And then proceed as usual!
}
source

pub fn username(self, username: &str) -> Self

Examples found in repository?
examples/custom_options.rs (line 8)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let tether_agent = TetherAgentOptionsBuilder::new("example")
        .id("customId")
        .host("localhost")
        .port(1883)
        .username("tether")
        .password("sp_ceB0ss!")
        .build()
        .expect("failed to create Tether Agent");

    let _output_plug = PlugOptionsBuilder::create_output("anOutput")
        .qos(2)
        .retain(true)
        .build(&tether_agent);
    let _input_plug = PlugOptionsBuilder::create_input("everything")
        .topic("#")
        .build(&tether_agent);

    // And then proceed as usual!
}
More examples
Hide additional examples
examples/username_password.rs (line 24)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
fn main() {
    println!("Rust Tether Agent: with username and password");

    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .host("10.112.10.10")
        .username("connected.space")
        .password("connected.space")
        .build()
        .expect("Failed to initialise and connect");
    let (role, id) = tether_agent.description();
    info!("Created agent OK: {}, {}", role, id);

    let empty_message_output = PlugOptionsBuilder::create_output("nothing")
        .build(&tether_agent)
        .expect("failed to create output");
    let boolean_message_output = PlugOptionsBuilder::create_output("one")
        .build(&tether_agent)
        .expect("failed to create output");
    let custom_output = PlugOptionsBuilder::create_output("two")
        .build(&tether_agent)
        .expect("failed to create output");

    for i in 1..=10 {
        info!("#{i}: Sending empty message...");
        tether_agent.publish(&empty_message_output, None).unwrap();

        let bool = i % 2 == 0;
        info!("#{i}: Sending boolean message...");
        tether_agent
            .publish(&boolean_message_output, Some(&[bool.into()]))
            .unwrap();

        let custom_message = CustomStruct {
            foo: "hello".into(),
            bar: 0.42,
        };
        tether_agent
            .encode_and_publish(&custom_output, custom_message)
            .unwrap();

        thread::sleep(Duration::from_millis(1000))
    }
}
examples/error_handling.rs (line 20)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let bad_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .username("bla")
        .password("bla")
        .build();
    match bad_tether_agent {
        Ok(_agent) => {
            panic!("Connection: This shouldn't work!");
        }
        Err(e) => warn!("Got a connection error as expected: {e:?}"),
    }

    let disconnected = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .auto_connect(false)
        .build()
        .expect("this ought initialise but not conect");

    let output = PlugOptionsBuilder::create_output("values")
        .build(&disconnected)
        .expect("this output should be valid always");

    let an_array = &vec![0, 1, 2, 3];
    match disconnected.encode_and_publish(&output, an_array) {
        Ok(()) => panic!("Publish on disconnected agent: This shouldn't work!"),
        Err(e) => warn!("Got a not-connected error as expected: {e:?}"),
    }

    let input_on_disconnected = PlugOptionsBuilder::create_input("something");
    match input_on_disconnected.build(&disconnected) {
        Ok(_) => panic!("Input plug subscribe on disconnected client: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe failure error as expected: {e:?}"),
    }

    // Rust's type-checking kind of prevents this happening at all!
    // let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    // match working_tether_agent.encode_and_publish::<CustomStruct>(&output, bad_payload) {
    //     Ok(()) => panic!("Encoding: This shouldn't work!"),
    //     Err(e) => warn!("Got an encoding error as expected: {e:?}"),
    // }

    let working_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .build()
        .expect("this should connect to local server");

    let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    working_tether_agent
        .publish(&output, Some(bad_payload))
        .expect("This will produce an error when DECODING, but not checked by library");

    let bad_topic_input = PlugOptionsBuilder::create_input("something").topic("*/#/house+");
    match bad_topic_input.build(&working_tether_agent) {
        Ok(_) => panic!("Weird topic: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe error (bad topic) as expected: {e:?}"),
    }
}
source

pub fn password(self, password: &str) -> Self

Examples found in repository?
examples/custom_options.rs (line 9)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let tether_agent = TetherAgentOptionsBuilder::new("example")
        .id("customId")
        .host("localhost")
        .port(1883)
        .username("tether")
        .password("sp_ceB0ss!")
        .build()
        .expect("failed to create Tether Agent");

    let _output_plug = PlugOptionsBuilder::create_output("anOutput")
        .qos(2)
        .retain(true)
        .build(&tether_agent);
    let _input_plug = PlugOptionsBuilder::create_input("everything")
        .topic("#")
        .build(&tether_agent);

    // And then proceed as usual!
}
More examples
Hide additional examples
examples/username_password.rs (line 25)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
fn main() {
    println!("Rust Tether Agent: with username and password");

    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .host("10.112.10.10")
        .username("connected.space")
        .password("connected.space")
        .build()
        .expect("Failed to initialise and connect");
    let (role, id) = tether_agent.description();
    info!("Created agent OK: {}, {}", role, id);

    let empty_message_output = PlugOptionsBuilder::create_output("nothing")
        .build(&tether_agent)
        .expect("failed to create output");
    let boolean_message_output = PlugOptionsBuilder::create_output("one")
        .build(&tether_agent)
        .expect("failed to create output");
    let custom_output = PlugOptionsBuilder::create_output("two")
        .build(&tether_agent)
        .expect("failed to create output");

    for i in 1..=10 {
        info!("#{i}: Sending empty message...");
        tether_agent.publish(&empty_message_output, None).unwrap();

        let bool = i % 2 == 0;
        info!("#{i}: Sending boolean message...");
        tether_agent
            .publish(&boolean_message_output, Some(&[bool.into()]))
            .unwrap();

        let custom_message = CustomStruct {
            foo: "hello".into(),
            bar: 0.42,
        };
        tether_agent
            .encode_and_publish(&custom_output, custom_message)
            .unwrap();

        thread::sleep(Duration::from_millis(1000))
    }
}
examples/error_handling.rs (line 21)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let bad_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .username("bla")
        .password("bla")
        .build();
    match bad_tether_agent {
        Ok(_agent) => {
            panic!("Connection: This shouldn't work!");
        }
        Err(e) => warn!("Got a connection error as expected: {e:?}"),
    }

    let disconnected = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .auto_connect(false)
        .build()
        .expect("this ought initialise but not conect");

    let output = PlugOptionsBuilder::create_output("values")
        .build(&disconnected)
        .expect("this output should be valid always");

    let an_array = &vec![0, 1, 2, 3];
    match disconnected.encode_and_publish(&output, an_array) {
        Ok(()) => panic!("Publish on disconnected agent: This shouldn't work!"),
        Err(e) => warn!("Got a not-connected error as expected: {e:?}"),
    }

    let input_on_disconnected = PlugOptionsBuilder::create_input("something");
    match input_on_disconnected.build(&disconnected) {
        Ok(_) => panic!("Input plug subscribe on disconnected client: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe failure error as expected: {e:?}"),
    }

    // Rust's type-checking kind of prevents this happening at all!
    // let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    // match working_tether_agent.encode_and_publish::<CustomStruct>(&output, bad_payload) {
    //     Ok(()) => panic!("Encoding: This shouldn't work!"),
    //     Err(e) => warn!("Got an encoding error as expected: {e:?}"),
    // }

    let working_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .build()
        .expect("this should connect to local server");

    let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    working_tether_agent
        .publish(&output, Some(bad_payload))
        .expect("This will produce an error when DECODING, but not checked by library");

    let bad_topic_input = PlugOptionsBuilder::create_input("something").topic("*/#/house+");
    match bad_topic_input.build(&working_tether_agent) {
        Ok(_) => panic!("Weird topic: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe error (bad topic) as expected: {e:?}"),
    }
}
source

pub fn auto_connect(self, should_auto_connect: bool) -> Self

Examples found in repository?
examples/error_handling.rs (line 32)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let bad_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .username("bla")
        .password("bla")
        .build();
    match bad_tether_agent {
        Ok(_agent) => {
            panic!("Connection: This shouldn't work!");
        }
        Err(e) => warn!("Got a connection error as expected: {e:?}"),
    }

    let disconnected = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .auto_connect(false)
        .build()
        .expect("this ought initialise but not conect");

    let output = PlugOptionsBuilder::create_output("values")
        .build(&disconnected)
        .expect("this output should be valid always");

    let an_array = &vec![0, 1, 2, 3];
    match disconnected.encode_and_publish(&output, an_array) {
        Ok(()) => panic!("Publish on disconnected agent: This shouldn't work!"),
        Err(e) => warn!("Got a not-connected error as expected: {e:?}"),
    }

    let input_on_disconnected = PlugOptionsBuilder::create_input("something");
    match input_on_disconnected.build(&disconnected) {
        Ok(_) => panic!("Input plug subscribe on disconnected client: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe failure error as expected: {e:?}"),
    }

    // Rust's type-checking kind of prevents this happening at all!
    // let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    // match working_tether_agent.encode_and_publish::<CustomStruct>(&output, bad_payload) {
    //     Ok(()) => panic!("Encoding: This shouldn't work!"),
    //     Err(e) => warn!("Got an encoding error as expected: {e:?}"),
    // }

    let working_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .build()
        .expect("this should connect to local server");

    let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    working_tether_agent
        .publish(&output, Some(bad_payload))
        .expect("This will produce an error when DECODING, but not checked by library");

    let bad_topic_input = PlugOptionsBuilder::create_input("something").topic("*/#/house+");
    match bad_topic_input.build(&working_tether_agent) {
        Ok(_) => panic!("Weird topic: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe error (bad topic) as expected: {e:?}"),
    }
}
source

pub fn build(self) -> Result<TetherAgent>

Examples found in repository?
examples/custom_options.rs (line 10)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    let tether_agent = TetherAgentOptionsBuilder::new("example")
        .id("customId")
        .host("localhost")
        .port(1883)
        .username("tether")
        .password("sp_ceB0ss!")
        .build()
        .expect("failed to create Tether Agent");

    let _output_plug = PlugOptionsBuilder::create_output("anOutput")
        .qos(2)
        .retain(true)
        .build(&tether_agent);
    let _input_plug = PlugOptionsBuilder::create_input("everything")
        .topic("#")
        .build(&tether_agent);

    // And then proceed as usual!
}
More examples
Hide additional examples
examples/publish.rs (line 23)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
fn main() {
    println!("Rust Tether Agent publish example");

    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .build()
        .expect("failed to connect Tether");
    let (role, id) = agent.description();
    info!("Created agent OK: {}, {}", role, id);

    let empty_message_output = PlugOptionsBuilder::create_output("nothing")
        .build(&agent)
        .expect("failed to create output");
    let boolean_message_output = PlugOptionsBuilder::create_output("one")
        .build(&agent)
        .expect("failed to create output");
    let custom_output = PlugOptionsBuilder::create_output("two")
        .build(&agent)
        .expect("failed to create input/subscribe");

    for i in 1..=10 {
        info!("#{i}: Sending empty message...");
        agent.publish(&empty_message_output, None).unwrap();

        let bool = i % 2 == 0;
        info!("#{i}: Sending boolean message...");
        agent
            .publish(&boolean_message_output, Some(&[bool.into()]))
            .unwrap();

        info!("#{i}: Sending custom struct message...");
        let custom_message = CustomStruct {
            id: i,
            name: "hello".into(),
        };
        agent
            .encode_and_publish(&custom_output, custom_message)
            .unwrap();

        thread::sleep(Duration::from_millis(1000))
    }
}
examples/username_password.rs (line 26)
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
fn main() {
    println!("Rust Tether Agent: with username and password");

    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .host("10.112.10.10")
        .username("connected.space")
        .password("connected.space")
        .build()
        .expect("Failed to initialise and connect");
    let (role, id) = tether_agent.description();
    info!("Created agent OK: {}, {}", role, id);

    let empty_message_output = PlugOptionsBuilder::create_output("nothing")
        .build(&tether_agent)
        .expect("failed to create output");
    let boolean_message_output = PlugOptionsBuilder::create_output("one")
        .build(&tether_agent)
        .expect("failed to create output");
    let custom_output = PlugOptionsBuilder::create_output("two")
        .build(&tether_agent)
        .expect("failed to create output");

    for i in 1..=10 {
        info!("#{i}: Sending empty message...");
        tether_agent.publish(&empty_message_output, None).unwrap();

        let bool = i % 2 == 0;
        info!("#{i}: Sending boolean message...");
        tether_agent
            .publish(&boolean_message_output, Some(&[bool.into()]))
            .unwrap();

        let custom_message = CustomStruct {
            foo: "hello".into(),
            bar: 0.42,
        };
        tether_agent
            .encode_and_publish(&custom_output, custom_message)
            .unwrap();

        thread::sleep(Duration::from_millis(1000))
    }
}
examples/subscribe_threaded.rs (line 31)
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
fn main() {
    println!("Rust Tether Agent subscribe example");

    let tether_agent = Arc::new(Mutex::new(
        TetherAgentOptionsBuilder::new("RustDemoAgent")
            .id("example")
            .build()
            .expect("failed to init/connect"),
    ));

    match tether_agent.lock() {
        Ok(a) => {
            let _input_plug = PlugOptionsBuilder::create_output("one").build(&a);
        }
        Err(e) => {
            panic!("Failed to acquire lock for Tether Agent setup: {}", e);
        }
    };

    let (tx, rx) = mpsc::channel();

    let receiver_agent = Arc::clone(&tether_agent);
    thread::spawn(move || {
        println!("Checking messages every 1s, 10x...");

        let mut message_count = 0;
        // let mut i = 0;

        loop {
            // i += 1;
            // println!("#{i}: Checking messages...");
            match receiver_agent.try_lock() {
                Ok(a) => {
                    if let Some((topic, _message)) = a.check_messages() {
                        message_count += 1;
                        println!("<<<<<<<< CHECKING LOOP: Received a message on topic {topic}",);
                        tx.send(format!("received message #{message_count}"))
                            .expect("failed to send message via channel");
                    }
                }
                Err(e) => {
                    println!("Failed to acquire lock: {}", e);
                }
            }
            thread::sleep(Duration::from_millis(1));
        }
    });

    let mut main_thread_received_count = 0;

    loop {
        println!("Main thread sleep...");
        for rx in rx.try_iter() {
            main_thread_received_count += 1;
            println!(
                "<<<<<<<< MAIN THREAD: received {} (count: {})",
                rx, main_thread_received_count
            );
        }
        if main_thread_received_count >= 10 {
            println!("We're done!");
            std::process::exit(0);
        }
        std::thread::sleep(Duration::from_secs(1));
    }
}
examples/error_handling.rs (line 22)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
    let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let bad_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .username("bla")
        .password("bla")
        .build();
    match bad_tether_agent {
        Ok(_agent) => {
            panic!("Connection: This shouldn't work!");
        }
        Err(e) => warn!("Got a connection error as expected: {e:?}"),
    }

    let disconnected = TetherAgentOptionsBuilder::new("tester")
        .host("tether-io.dev")
        .auto_connect(false)
        .build()
        .expect("this ought initialise but not conect");

    let output = PlugOptionsBuilder::create_output("values")
        .build(&disconnected)
        .expect("this output should be valid always");

    let an_array = &vec![0, 1, 2, 3];
    match disconnected.encode_and_publish(&output, an_array) {
        Ok(()) => panic!("Publish on disconnected agent: This shouldn't work!"),
        Err(e) => warn!("Got a not-connected error as expected: {e:?}"),
    }

    let input_on_disconnected = PlugOptionsBuilder::create_input("something");
    match input_on_disconnected.build(&disconnected) {
        Ok(_) => panic!("Input plug subscribe on disconnected client: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe failure error as expected: {e:?}"),
    }

    // Rust's type-checking kind of prevents this happening at all!
    // let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    // match working_tether_agent.encode_and_publish::<CustomStruct>(&output, bad_payload) {
    //     Ok(()) => panic!("Encoding: This shouldn't work!"),
    //     Err(e) => warn!("Got an encoding error as expected: {e:?}"),
    // }

    let working_tether_agent = TetherAgentOptionsBuilder::new("tester")
        .build()
        .expect("this should connect to local server");

    let bad_payload: &[u8; 9] = &[0x87, 0xA3, 0x69, 0x6E, 0x74, 0x01, 0xA5, 0x66, 0x6C];
    working_tether_agent
        .publish(&output, Some(bad_payload))
        .expect("This will produce an error when DECODING, but not checked by library");

    let bad_topic_input = PlugOptionsBuilder::create_input("something").topic("*/#/house+");
    match bad_topic_input.build(&working_tether_agent) {
        Ok(_) => panic!("Weird topic: This shouldn't work!"),
        Err(e) => warn!("Got a subscribe error (bad topic) as expected: {e:?}"),
    }
}
examples/subscribe.rs (line 27)
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
fn main() {
    println!("Rust Tether Agent subscribe example");

    let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
    builder.init();

    debug!("Debugging is enabled; could be verbose");

    let tether_agent = TetherAgentOptionsBuilder::new("RustDemoAgent")
        .id("example")
        .build()
        .expect("failed to init Tether agent");

    let input_one = PlugOptionsBuilder::create_input("one")
        .build(&tether_agent)
        .expect("failed to create input");
    debug!("input one {} = {}", input_one.name(), input_one.topic());
    let input_two = PlugOptionsBuilder::create_input("two")
        .build(&tether_agent)
        .expect("failed to create input");
    debug!("input two {} = {}", input_two.name(), input_two.topic());
    let input_empty = PlugOptionsBuilder::create_input("nothing")
        .build(&tether_agent)
        .expect("failed to create input");

    info!("Checking messages every 1s, 10x...");

    for i in 1..10 {
        info!("#{i}: Checking for messages...");
        while let Some((plug_name, message)) = tether_agent.check_messages() {
            debug!(
                "Received a message topic {} => plug name {}",
                message.topic(),
                plug_name
            );
            if &plug_name == input_one.name() {
                info!(
                    "******** INPUT ONE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_one.name(),
                    message.topic(),
                    message.payload().len()
                );
            }
            if &plug_name == input_two.name() {
                info!(
                    "******** INPUT TWO:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_two.name(),
                    message.topic(),
                    message.payload().len()
                );
                // Notice how you must give the from_slice function a type so it knows what to expect
                let decoded = from_slice::<CustomMessage>(&message.payload());
                match decoded {
                    Ok(d) => {
                        info!("Yes, we decoded the MessagePack payload as: {:?}", d);
                        let CustomMessage { name, id } = d;
                        debug!("Name is {} and ID is {}", name, id);
                    }
                    Err(e) => {
                        warn!("Failed to decode the payload: {}", e)
                    }
                };
            }
            if &plug_name == input_empty.name() {
                info!(
                    "******** EMPTY MESSAGE:\n Received a message from plug named \"{}\" on topic {} with length {} bytes",
                    input_empty.name(),
                    message.topic(),
                    message.payload().len()
                );
            }
        }
        thread::sleep(Duration::from_millis(1000))
    }
}

Trait Implementations§

source§

impl Clone for TetherAgentOptionsBuilder

source§

fn clone(&self) -> TetherAgentOptionsBuilder

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.