pub enum PlugOptionsBuilder {
InputPlugOptions(InputPlugOptions),
OutputPlugOptions(OutputPlugOptions),
}Expand description
This is the definition of an Input or Output Plug.
You typically don’t use an instance of this directly; call .build() at the
end of the chain to get a usable PlugDefinition
Variants§
InputPlugOptions(InputPlugOptions)
OutputPlugOptions(OutputPlugOptions)
Implementations§
Source§impl PlugOptionsBuilder
impl PlugOptionsBuilder
Sourcepub fn create_input(name: &str) -> PlugOptionsBuilder
pub fn create_input(name: &str) -> PlugOptionsBuilder
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}More examples
17fn main() {
18 println!("Rust Tether Agent subscribe example");
19
20 let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
21 builder.filter_module("tether_agent", log::LevelFilter::Warn);
22 builder.filter_module("rumqttc", log::LevelFilter::Warn);
23 builder.init();
24
25 debug!("Debugging is enabled; could be verbose");
26
27 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
28 .id(Some("example"))
29 .build()
30 .expect("failed to init Tether agent");
31
32 let input_one = PlugOptionsBuilder::create_input("one")
33 .build(&mut tether_agent)
34 .expect("failed to create input");
35 info!("input one {} = {}", input_one.name(), input_one.topic());
36 let input_two = PlugOptionsBuilder::create_input("two")
37 .role(Some("specific"))
38 .build(&mut tether_agent)
39 .expect("failed to create input");
40 info!("input two {} = {}", input_two.name(), input_two.topic());
41 let input_empty = PlugOptionsBuilder::create_input("nothing")
42 .build(&mut tether_agent)
43 .expect("failed to create input");
44
45 let input_everything = PlugOptionsBuilder::create_input("everything")
46 .topic(Some("#"))
47 .build(&mut tether_agent)
48 .expect("failed to create input");
49
50 let input_specify_id = PlugOptionsBuilder::create_input("groupMessages")
51 .id(Some("someGroup"))
52 .name(None)
53 .build(&mut tether_agent)
54 .expect("failed to create input");
55
56 debug!(
57 "input everything {} = {}",
58 input_everything.name(),
59 input_everything.topic()
60 );
61
62 info!("Checking messages every 1s, 10x...");
63
64 loop {
65 debug!("Checking for messages...");
66 while let Some((topic, payload)) = tether_agent.check_messages() {
67 // debug!(
68 // "........ Received a message topic {:?} => topic parts {:?}",
69 // topic, topic
70 // );
71
72 if input_one.matches(&topic) {
73 info!(
74 "******** INPUT ONE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
75 input_one.name(),
76 topic,
77 payload.len()
78 );
79 // assert_eq!(parse_plug_name(topic.un), Some("one"));
80 }
81 if input_two.matches(&topic) {
82 info!(
83 "******** INPUT TWO:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
84 input_two.name(),
85 topic,
86 payload.len()
87 );
88 // assert_eq!(parse_plug_name(message.topic()), Some("two"));
89 // assert_ne!(parse_plug_name(message.topic()), Some("one"));
90
91 // Notice how you must give the from_slice function a type so it knows what to expect
92 let decoded = from_slice::<CustomMessage>(&payload);
93 match decoded {
94 Ok(d) => {
95 info!("Yes, we decoded the MessagePack payload as: {:?}", d);
96 let CustomMessage { name, id } = d;
97 debug!("Name is {} and ID is {}", name, id);
98 }
99 Err(e) => {
100 warn!("Failed to decode the payload: {}", e)
101 }
102 };
103 }
104 if input_empty.matches(&topic) {
105 info!(
106 "******** EMPTY MESSAGE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
107 input_empty.name(),
108 topic,
109 payload.len()
110 );
111 // assert_eq!(parse_plug_name(topic), Some("nothing"));
112 }
113 if input_everything.matches(&topic) {
114 info!(
115 "******** EVERYTHING MATCHES HERE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
116 input_everything.name(),
117 topic,
118 payload.len()
119 );
120 }
121 if input_specify_id.matches(&topic) {
122 info!("******** ID MATCH:\n Should match any role and plug name, but only messages with ID \"groupMessages\"");
123 info!(
124 "\n Received a message from plug named \"{}\" on topic {:?} with length {} bytes",
125 input_specify_id.name(),
126 topic,
127 payload.len()
128 );
129 // assert_eq!(parse_agent_id(message.topic()), Some("groupMessages"));
130 }
131 }
132
133 thread::sleep(Duration::from_millis(1000))
134 }
135}Sourcepub fn create_output(name: &str) -> PlugOptionsBuilder
pub fn create_output(name: &str) -> PlugOptionsBuilder
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}More examples
14fn main() {
15 println!("Rust Tether Agent publish example");
16
17 let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
18 builder.init();
19
20 debug!("Debugging is enabled; could be verbose");
21
22 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
23 .build()
24 .expect("failed to connect Tether");
25 let (role, id, _) = tether_agent.description();
26 info!("Created agent OK: {}, {}", role, id);
27
28 let empty_message_output = PlugOptionsBuilder::create_output("nothing")
29 .build(&mut tether_agent)
30 .expect("failed to create output");
31 let boolean_message_output = PlugOptionsBuilder::create_output("one")
32 .build(&mut tether_agent)
33 .expect("failed to create output");
34 let custom_output = PlugOptionsBuilder::create_output("two")
35 .topic(Some("custom/custom/two"))
36 .build(&mut tether_agent)
37 .expect("failed to create output");
38 let grouped_output_1 = PlugOptionsBuilder::create_output("one")
39 .id(Some("groupMessages"))
40 .build(&mut tether_agent)
41 .expect("failed to create output");
42 let grouped_output_2 = PlugOptionsBuilder::create_output("two")
43 .id(Some("groupMessages"))
44 .build(&mut tether_agent)
45 .expect("failed to create output");
46
47 for i in 1..=10 {
48 info!("#{i}: Sending empty message...");
49 tether_agent.publish(&empty_message_output, None).unwrap();
50
51 let bool = i % 2 == 0;
52 info!("#{i}: Sending boolean message...");
53 tether_agent
54 .publish(&boolean_message_output, Some(&[bool.into()]))
55 .unwrap();
56
57 info!("#{i}: Sending custom struct message...");
58 let custom_message = CustomStruct {
59 id: i,
60 name: "hello".into(),
61 };
62 tether_agent
63 .encode_and_publish(&custom_output, custom_message)
64 .unwrap();
65
66 info!("#{i}: Sending grouped messages...");
67 tether_agent.publish(&grouped_output_1, None).unwrap();
68 tether_agent.publish(&grouped_output_2, None).unwrap();
69
70 thread::sleep(Duration::from_millis(1000))
71 }
72}Sourcepub fn qos(self, qos: Option<i32>) -> Self
pub fn qos(self, qos: Option<i32>) -> Self
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}Sourcepub fn role(self, role: Option<&str>) -> Self
pub fn role(self, role: Option<&str>) -> Self
Override the “role” part of the topic that gets generated for this Plug.
- For Input Plugs, this means you want to be specific about the Role part
of the topic, instead of using the default wildcard
+at this location - For Output Plugs, this means you want to override the Role part instead of using your Agent’s “own” Role with which you created the Tether Agent
If you override the entire topic using .topic this will be ignored.
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}More examples
17fn main() {
18 println!("Rust Tether Agent subscribe example");
19
20 let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
21 builder.filter_module("tether_agent", log::LevelFilter::Warn);
22 builder.filter_module("rumqttc", log::LevelFilter::Warn);
23 builder.init();
24
25 debug!("Debugging is enabled; could be verbose");
26
27 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
28 .id(Some("example"))
29 .build()
30 .expect("failed to init Tether agent");
31
32 let input_one = PlugOptionsBuilder::create_input("one")
33 .build(&mut tether_agent)
34 .expect("failed to create input");
35 info!("input one {} = {}", input_one.name(), input_one.topic());
36 let input_two = PlugOptionsBuilder::create_input("two")
37 .role(Some("specific"))
38 .build(&mut tether_agent)
39 .expect("failed to create input");
40 info!("input two {} = {}", input_two.name(), input_two.topic());
41 let input_empty = PlugOptionsBuilder::create_input("nothing")
42 .build(&mut tether_agent)
43 .expect("failed to create input");
44
45 let input_everything = PlugOptionsBuilder::create_input("everything")
46 .topic(Some("#"))
47 .build(&mut tether_agent)
48 .expect("failed to create input");
49
50 let input_specify_id = PlugOptionsBuilder::create_input("groupMessages")
51 .id(Some("someGroup"))
52 .name(None)
53 .build(&mut tether_agent)
54 .expect("failed to create input");
55
56 debug!(
57 "input everything {} = {}",
58 input_everything.name(),
59 input_everything.topic()
60 );
61
62 info!("Checking messages every 1s, 10x...");
63
64 loop {
65 debug!("Checking for messages...");
66 while let Some((topic, payload)) = tether_agent.check_messages() {
67 // debug!(
68 // "........ Received a message topic {:?} => topic parts {:?}",
69 // topic, topic
70 // );
71
72 if input_one.matches(&topic) {
73 info!(
74 "******** INPUT ONE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
75 input_one.name(),
76 topic,
77 payload.len()
78 );
79 // assert_eq!(parse_plug_name(topic.un), Some("one"));
80 }
81 if input_two.matches(&topic) {
82 info!(
83 "******** INPUT TWO:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
84 input_two.name(),
85 topic,
86 payload.len()
87 );
88 // assert_eq!(parse_plug_name(message.topic()), Some("two"));
89 // assert_ne!(parse_plug_name(message.topic()), Some("one"));
90
91 // Notice how you must give the from_slice function a type so it knows what to expect
92 let decoded = from_slice::<CustomMessage>(&payload);
93 match decoded {
94 Ok(d) => {
95 info!("Yes, we decoded the MessagePack payload as: {:?}", d);
96 let CustomMessage { name, id } = d;
97 debug!("Name is {} and ID is {}", name, id);
98 }
99 Err(e) => {
100 warn!("Failed to decode the payload: {}", e)
101 }
102 };
103 }
104 if input_empty.matches(&topic) {
105 info!(
106 "******** EMPTY MESSAGE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
107 input_empty.name(),
108 topic,
109 payload.len()
110 );
111 // assert_eq!(parse_plug_name(topic), Some("nothing"));
112 }
113 if input_everything.matches(&topic) {
114 info!(
115 "******** EVERYTHING MATCHES HERE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
116 input_everything.name(),
117 topic,
118 payload.len()
119 );
120 }
121 if input_specify_id.matches(&topic) {
122 info!("******** ID MATCH:\n Should match any role and plug name, but only messages with ID \"groupMessages\"");
123 info!(
124 "\n Received a message from plug named \"{}\" on topic {:?} with length {} bytes",
125 input_specify_id.name(),
126 topic,
127 payload.len()
128 );
129 // assert_eq!(parse_agent_id(message.topic()), Some("groupMessages"));
130 }
131 }
132
133 thread::sleep(Duration::from_millis(1000))
134 }
135}Sourcepub fn id(self, id: Option<&str>) -> Self
pub fn id(self, id: Option<&str>) -> Self
Override the “id” part of the topic that gets generated for this Plug.
- For Input Plugs, this means you want to be specific about the ID part
of the topic, instead of using the default wildcard
+at this location - For Output Plugs, this means you want to override the ID part instead of using your Agent’s “own” ID which you specified (or left blank, i.e. “any”) when creating the Tether Agent
If you override the entire topic using .topic this will be ignored.
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}More examples
14fn main() {
15 println!("Rust Tether Agent publish example");
16
17 let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
18 builder.init();
19
20 debug!("Debugging is enabled; could be verbose");
21
22 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
23 .build()
24 .expect("failed to connect Tether");
25 let (role, id, _) = tether_agent.description();
26 info!("Created agent OK: {}, {}", role, id);
27
28 let empty_message_output = PlugOptionsBuilder::create_output("nothing")
29 .build(&mut tether_agent)
30 .expect("failed to create output");
31 let boolean_message_output = PlugOptionsBuilder::create_output("one")
32 .build(&mut tether_agent)
33 .expect("failed to create output");
34 let custom_output = PlugOptionsBuilder::create_output("two")
35 .topic(Some("custom/custom/two"))
36 .build(&mut tether_agent)
37 .expect("failed to create output");
38 let grouped_output_1 = PlugOptionsBuilder::create_output("one")
39 .id(Some("groupMessages"))
40 .build(&mut tether_agent)
41 .expect("failed to create output");
42 let grouped_output_2 = PlugOptionsBuilder::create_output("two")
43 .id(Some("groupMessages"))
44 .build(&mut tether_agent)
45 .expect("failed to create output");
46
47 for i in 1..=10 {
48 info!("#{i}: Sending empty message...");
49 tether_agent.publish(&empty_message_output, None).unwrap();
50
51 let bool = i % 2 == 0;
52 info!("#{i}: Sending boolean message...");
53 tether_agent
54 .publish(&boolean_message_output, Some(&[bool.into()]))
55 .unwrap();
56
57 info!("#{i}: Sending custom struct message...");
58 let custom_message = CustomStruct {
59 id: i,
60 name: "hello".into(),
61 };
62 tether_agent
63 .encode_and_publish(&custom_output, custom_message)
64 .unwrap();
65
66 info!("#{i}: Sending grouped messages...");
67 tether_agent.publish(&grouped_output_1, None).unwrap();
68 tether_agent.publish(&grouped_output_2, None).unwrap();
69
70 thread::sleep(Duration::from_millis(1000))
71 }
72}17fn main() {
18 println!("Rust Tether Agent subscribe example");
19
20 let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
21 builder.filter_module("tether_agent", log::LevelFilter::Warn);
22 builder.filter_module("rumqttc", log::LevelFilter::Warn);
23 builder.init();
24
25 debug!("Debugging is enabled; could be verbose");
26
27 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
28 .id(Some("example"))
29 .build()
30 .expect("failed to init Tether agent");
31
32 let input_one = PlugOptionsBuilder::create_input("one")
33 .build(&mut tether_agent)
34 .expect("failed to create input");
35 info!("input one {} = {}", input_one.name(), input_one.topic());
36 let input_two = PlugOptionsBuilder::create_input("two")
37 .role(Some("specific"))
38 .build(&mut tether_agent)
39 .expect("failed to create input");
40 info!("input two {} = {}", input_two.name(), input_two.topic());
41 let input_empty = PlugOptionsBuilder::create_input("nothing")
42 .build(&mut tether_agent)
43 .expect("failed to create input");
44
45 let input_everything = PlugOptionsBuilder::create_input("everything")
46 .topic(Some("#"))
47 .build(&mut tether_agent)
48 .expect("failed to create input");
49
50 let input_specify_id = PlugOptionsBuilder::create_input("groupMessages")
51 .id(Some("someGroup"))
52 .name(None)
53 .build(&mut tether_agent)
54 .expect("failed to create input");
55
56 debug!(
57 "input everything {} = {}",
58 input_everything.name(),
59 input_everything.topic()
60 );
61
62 info!("Checking messages every 1s, 10x...");
63
64 loop {
65 debug!("Checking for messages...");
66 while let Some((topic, payload)) = tether_agent.check_messages() {
67 // debug!(
68 // "........ Received a message topic {:?} => topic parts {:?}",
69 // topic, topic
70 // );
71
72 if input_one.matches(&topic) {
73 info!(
74 "******** INPUT ONE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
75 input_one.name(),
76 topic,
77 payload.len()
78 );
79 // assert_eq!(parse_plug_name(topic.un), Some("one"));
80 }
81 if input_two.matches(&topic) {
82 info!(
83 "******** INPUT TWO:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
84 input_two.name(),
85 topic,
86 payload.len()
87 );
88 // assert_eq!(parse_plug_name(message.topic()), Some("two"));
89 // assert_ne!(parse_plug_name(message.topic()), Some("one"));
90
91 // Notice how you must give the from_slice function a type so it knows what to expect
92 let decoded = from_slice::<CustomMessage>(&payload);
93 match decoded {
94 Ok(d) => {
95 info!("Yes, we decoded the MessagePack payload as: {:?}", d);
96 let CustomMessage { name, id } = d;
97 debug!("Name is {} and ID is {}", name, id);
98 }
99 Err(e) => {
100 warn!("Failed to decode the payload: {}", e)
101 }
102 };
103 }
104 if input_empty.matches(&topic) {
105 info!(
106 "******** EMPTY MESSAGE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
107 input_empty.name(),
108 topic,
109 payload.len()
110 );
111 // assert_eq!(parse_plug_name(topic), Some("nothing"));
112 }
113 if input_everything.matches(&topic) {
114 info!(
115 "******** EVERYTHING MATCHES HERE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
116 input_everything.name(),
117 topic,
118 payload.len()
119 );
120 }
121 if input_specify_id.matches(&topic) {
122 info!("******** ID MATCH:\n Should match any role and plug name, but only messages with ID \"groupMessages\"");
123 info!(
124 "\n Received a message from plug named \"{}\" on topic {:?} with length {} bytes",
125 input_specify_id.name(),
126 topic,
127 payload.len()
128 );
129 // assert_eq!(parse_agent_id(message.topic()), Some("groupMessages"));
130 }
131 }
132
133 thread::sleep(Duration::from_millis(1000))
134 }
135}Sourcepub fn name(self, override_plug_name: Option<&str>) -> Self
pub fn name(self, override_plug_name: Option<&str>) -> Self
Override the “name” part of the topic that gets generated for this Plug.
This is mainly to facilitate wildcard subscriptions such as
someRole/someID/+ instead of someRole/someID/originalPlugName.
In the case of Input Topics, a wildcard + can be used to substitute
the last part of the topic as in role/id/+ but will NOT affect the stored “name”
of the Plug Definition itself. Anything else will be ignored with an error.
Output Plugs will ignore (with an error) any attempt to change the name after instantiation.
Examples found in repository?
17fn main() {
18 println!("Rust Tether Agent subscribe example");
19
20 let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
21 builder.filter_module("tether_agent", log::LevelFilter::Warn);
22 builder.filter_module("rumqttc", log::LevelFilter::Warn);
23 builder.init();
24
25 debug!("Debugging is enabled; could be verbose");
26
27 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
28 .id(Some("example"))
29 .build()
30 .expect("failed to init Tether agent");
31
32 let input_one = PlugOptionsBuilder::create_input("one")
33 .build(&mut tether_agent)
34 .expect("failed to create input");
35 info!("input one {} = {}", input_one.name(), input_one.topic());
36 let input_two = PlugOptionsBuilder::create_input("two")
37 .role(Some("specific"))
38 .build(&mut tether_agent)
39 .expect("failed to create input");
40 info!("input two {} = {}", input_two.name(), input_two.topic());
41 let input_empty = PlugOptionsBuilder::create_input("nothing")
42 .build(&mut tether_agent)
43 .expect("failed to create input");
44
45 let input_everything = PlugOptionsBuilder::create_input("everything")
46 .topic(Some("#"))
47 .build(&mut tether_agent)
48 .expect("failed to create input");
49
50 let input_specify_id = PlugOptionsBuilder::create_input("groupMessages")
51 .id(Some("someGroup"))
52 .name(None)
53 .build(&mut tether_agent)
54 .expect("failed to create input");
55
56 debug!(
57 "input everything {} = {}",
58 input_everything.name(),
59 input_everything.topic()
60 );
61
62 info!("Checking messages every 1s, 10x...");
63
64 loop {
65 debug!("Checking for messages...");
66 while let Some((topic, payload)) = tether_agent.check_messages() {
67 // debug!(
68 // "........ Received a message topic {:?} => topic parts {:?}",
69 // topic, topic
70 // );
71
72 if input_one.matches(&topic) {
73 info!(
74 "******** INPUT ONE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
75 input_one.name(),
76 topic,
77 payload.len()
78 );
79 // assert_eq!(parse_plug_name(topic.un), Some("one"));
80 }
81 if input_two.matches(&topic) {
82 info!(
83 "******** INPUT TWO:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
84 input_two.name(),
85 topic,
86 payload.len()
87 );
88 // assert_eq!(parse_plug_name(message.topic()), Some("two"));
89 // assert_ne!(parse_plug_name(message.topic()), Some("one"));
90
91 // Notice how you must give the from_slice function a type so it knows what to expect
92 let decoded = from_slice::<CustomMessage>(&payload);
93 match decoded {
94 Ok(d) => {
95 info!("Yes, we decoded the MessagePack payload as: {:?}", d);
96 let CustomMessage { name, id } = d;
97 debug!("Name is {} and ID is {}", name, id);
98 }
99 Err(e) => {
100 warn!("Failed to decode the payload: {}", e)
101 }
102 };
103 }
104 if input_empty.matches(&topic) {
105 info!(
106 "******** EMPTY MESSAGE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
107 input_empty.name(),
108 topic,
109 payload.len()
110 );
111 // assert_eq!(parse_plug_name(topic), Some("nothing"));
112 }
113 if input_everything.matches(&topic) {
114 info!(
115 "******** EVERYTHING MATCHES HERE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
116 input_everything.name(),
117 topic,
118 payload.len()
119 );
120 }
121 if input_specify_id.matches(&topic) {
122 info!("******** ID MATCH:\n Should match any role and plug name, but only messages with ID \"groupMessages\"");
123 info!(
124 "\n Received a message from plug named \"{}\" on topic {:?} with length {} bytes",
125 input_specify_id.name(),
126 topic,
127 payload.len()
128 );
129 // assert_eq!(parse_agent_id(message.topic()), Some("groupMessages"));
130 }
131 }
132
133 thread::sleep(Duration::from_millis(1000))
134 }
135}Sourcepub fn any_plug(self) -> Self
pub fn any_plug(self) -> Self
Call this if you would like your Input plug to match any plug.
This is equivalent to .name(Some("+")) but is provided for convenience
since it does not require you to remember the wildcard string.
This also does not prevent you from further restricting the topic
subscription match by Role and/or ID. So, for example, if you are
interested in all messages from an Agent with the role "brain",
it is valid to create a plug with .role("brain").any_plug() and this
will subscribe to "brain/+/+" as expected.
Sourcepub fn topic(self, override_topic: Option<&str>) -> Self
pub fn topic(self, override_topic: Option<&str>) -> Self
Override the final topic to use for publishing or subscribing. The provided topic will be checked against the Tether Three Part Topic convention, but the function will not reject topic strings - just produce a warning. It’s therefore valid to use a wildcard such as “#”, for Input (subscribing).
Any customisations specified using .role(...) or .id(...) will be ignored if this function is called.
By default, the override_topic is None, but you can specify None explicitly using this function.
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}More examples
14fn main() {
15 println!("Rust Tether Agent publish example");
16
17 let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
18 builder.init();
19
20 debug!("Debugging is enabled; could be verbose");
21
22 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
23 .build()
24 .expect("failed to connect Tether");
25 let (role, id, _) = tether_agent.description();
26 info!("Created agent OK: {}, {}", role, id);
27
28 let empty_message_output = PlugOptionsBuilder::create_output("nothing")
29 .build(&mut tether_agent)
30 .expect("failed to create output");
31 let boolean_message_output = PlugOptionsBuilder::create_output("one")
32 .build(&mut tether_agent)
33 .expect("failed to create output");
34 let custom_output = PlugOptionsBuilder::create_output("two")
35 .topic(Some("custom/custom/two"))
36 .build(&mut tether_agent)
37 .expect("failed to create output");
38 let grouped_output_1 = PlugOptionsBuilder::create_output("one")
39 .id(Some("groupMessages"))
40 .build(&mut tether_agent)
41 .expect("failed to create output");
42 let grouped_output_2 = PlugOptionsBuilder::create_output("two")
43 .id(Some("groupMessages"))
44 .build(&mut tether_agent)
45 .expect("failed to create output");
46
47 for i in 1..=10 {
48 info!("#{i}: Sending empty message...");
49 tether_agent.publish(&empty_message_output, None).unwrap();
50
51 let bool = i % 2 == 0;
52 info!("#{i}: Sending boolean message...");
53 tether_agent
54 .publish(&boolean_message_output, Some(&[bool.into()]))
55 .unwrap();
56
57 info!("#{i}: Sending custom struct message...");
58 let custom_message = CustomStruct {
59 id: i,
60 name: "hello".into(),
61 };
62 tether_agent
63 .encode_and_publish(&custom_output, custom_message)
64 .unwrap();
65
66 info!("#{i}: Sending grouped messages...");
67 tether_agent.publish(&grouped_output_1, None).unwrap();
68 tether_agent.publish(&grouped_output_2, None).unwrap();
69
70 thread::sleep(Duration::from_millis(1000))
71 }
72}17fn main() {
18 println!("Rust Tether Agent subscribe example");
19
20 let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
21 builder.filter_module("tether_agent", log::LevelFilter::Warn);
22 builder.filter_module("rumqttc", log::LevelFilter::Warn);
23 builder.init();
24
25 debug!("Debugging is enabled; could be verbose");
26
27 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
28 .id(Some("example"))
29 .build()
30 .expect("failed to init Tether agent");
31
32 let input_one = PlugOptionsBuilder::create_input("one")
33 .build(&mut tether_agent)
34 .expect("failed to create input");
35 info!("input one {} = {}", input_one.name(), input_one.topic());
36 let input_two = PlugOptionsBuilder::create_input("two")
37 .role(Some("specific"))
38 .build(&mut tether_agent)
39 .expect("failed to create input");
40 info!("input two {} = {}", input_two.name(), input_two.topic());
41 let input_empty = PlugOptionsBuilder::create_input("nothing")
42 .build(&mut tether_agent)
43 .expect("failed to create input");
44
45 let input_everything = PlugOptionsBuilder::create_input("everything")
46 .topic(Some("#"))
47 .build(&mut tether_agent)
48 .expect("failed to create input");
49
50 let input_specify_id = PlugOptionsBuilder::create_input("groupMessages")
51 .id(Some("someGroup"))
52 .name(None)
53 .build(&mut tether_agent)
54 .expect("failed to create input");
55
56 debug!(
57 "input everything {} = {}",
58 input_everything.name(),
59 input_everything.topic()
60 );
61
62 info!("Checking messages every 1s, 10x...");
63
64 loop {
65 debug!("Checking for messages...");
66 while let Some((topic, payload)) = tether_agent.check_messages() {
67 // debug!(
68 // "........ Received a message topic {:?} => topic parts {:?}",
69 // topic, topic
70 // );
71
72 if input_one.matches(&topic) {
73 info!(
74 "******** INPUT ONE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
75 input_one.name(),
76 topic,
77 payload.len()
78 );
79 // assert_eq!(parse_plug_name(topic.un), Some("one"));
80 }
81 if input_two.matches(&topic) {
82 info!(
83 "******** INPUT TWO:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
84 input_two.name(),
85 topic,
86 payload.len()
87 );
88 // assert_eq!(parse_plug_name(message.topic()), Some("two"));
89 // assert_ne!(parse_plug_name(message.topic()), Some("one"));
90
91 // Notice how you must give the from_slice function a type so it knows what to expect
92 let decoded = from_slice::<CustomMessage>(&payload);
93 match decoded {
94 Ok(d) => {
95 info!("Yes, we decoded the MessagePack payload as: {:?}", d);
96 let CustomMessage { name, id } = d;
97 debug!("Name is {} and ID is {}", name, id);
98 }
99 Err(e) => {
100 warn!("Failed to decode the payload: {}", e)
101 }
102 };
103 }
104 if input_empty.matches(&topic) {
105 info!(
106 "******** EMPTY MESSAGE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
107 input_empty.name(),
108 topic,
109 payload.len()
110 );
111 // assert_eq!(parse_plug_name(topic), Some("nothing"));
112 }
113 if input_everything.matches(&topic) {
114 info!(
115 "******** EVERYTHING MATCHES HERE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
116 input_everything.name(),
117 topic,
118 payload.len()
119 );
120 }
121 if input_specify_id.matches(&topic) {
122 info!("******** ID MATCH:\n Should match any role and plug name, but only messages with ID \"groupMessages\"");
123 info!(
124 "\n Received a message from plug named \"{}\" on topic {:?} with length {} bytes",
125 input_specify_id.name(),
126 topic,
127 payload.len()
128 );
129 // assert_eq!(parse_agent_id(message.topic()), Some("groupMessages"));
130 }
131 }
132
133 thread::sleep(Duration::from_millis(1000))
134 }
135}Sourcepub fn retain(self, should_retain: Option<bool>) -> Self
pub fn retain(self, should_retain: Option<bool>) -> Self
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}Sourcepub fn build(self, tether_agent: &mut TetherAgent) -> Result<PlugDefinition>
pub fn build(self, tether_agent: &mut TetherAgent) -> Result<PlugDefinition>
Finalise the options (substituting suitable defaults if no custom values have been provided) and return a valid PlugDefinition that you can actually use.
Examples found in repository?
7fn main() {
8 let mut tether_agent = TetherAgentOptionsBuilder::new("example")
9 .id(None)
10 .host(Some("localhost"))
11 .port(Some(1883))
12 .username(Some("tether"))
13 .password(Some("sp_ceB0ss!"))
14 .build()
15 .expect("failed to create Tether Agent");
16
17 let output_plug = PlugOptionsBuilder::create_output("anOutput")
18 .role(Some("pretendingToBeSomethingElse"))
19 .qos(Some(2))
20 .retain(Some(true))
21 .build(&mut tether_agent)
22 .expect("failed to create output plug");
23 let input_wildcard_plug = PlugOptionsBuilder::create_input("everything")
24 .topic(Some("#"))
25 .build(&mut tether_agent);
26
27 let input_customid_plug = PlugOptionsBuilder::create_input("someData")
28 .role(None) // i.e., just use default
29 .id(Some("specificIDonly"))
30 .build(&mut tether_agent);
31
32 println!("Agent looks like this: {:?}", tether_agent.description());
33 let (role, id, _) = tether_agent.description();
34 assert_eq!(role, "example");
35 assert_eq!(id, "any"); // because we set None
36
37 if let PlugDefinition::OutputPlug(p) = &output_plug {
38 println!("output plug: {:?}", p);
39 assert_eq!(p.topic_str(), "pretendingToBeSomethingElse/any/anOutput");
40 }
41
42 println!("wildcard input plug: {:?}", input_wildcard_plug);
43 println!("speific ID input plug: {:?}", input_customid_plug);
44
45 let payload =
46 rmp_serde::to_vec::<String>(&String::from("boo")).expect("failed to serialise payload");
47 tether_agent
48 .publish(&output_plug, Some(&payload))
49 .expect("failed to publish");
50
51 std::thread::sleep(Duration::from_millis(4000));
52}More examples
14fn main() {
15 println!("Rust Tether Agent publish example");
16
17 let mut builder = Builder::from_env(Env::default().default_filter_or("info"));
18 builder.init();
19
20 debug!("Debugging is enabled; could be verbose");
21
22 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
23 .build()
24 .expect("failed to connect Tether");
25 let (role, id, _) = tether_agent.description();
26 info!("Created agent OK: {}, {}", role, id);
27
28 let empty_message_output = PlugOptionsBuilder::create_output("nothing")
29 .build(&mut tether_agent)
30 .expect("failed to create output");
31 let boolean_message_output = PlugOptionsBuilder::create_output("one")
32 .build(&mut tether_agent)
33 .expect("failed to create output");
34 let custom_output = PlugOptionsBuilder::create_output("two")
35 .topic(Some("custom/custom/two"))
36 .build(&mut tether_agent)
37 .expect("failed to create output");
38 let grouped_output_1 = PlugOptionsBuilder::create_output("one")
39 .id(Some("groupMessages"))
40 .build(&mut tether_agent)
41 .expect("failed to create output");
42 let grouped_output_2 = PlugOptionsBuilder::create_output("two")
43 .id(Some("groupMessages"))
44 .build(&mut tether_agent)
45 .expect("failed to create output");
46
47 for i in 1..=10 {
48 info!("#{i}: Sending empty message...");
49 tether_agent.publish(&empty_message_output, None).unwrap();
50
51 let bool = i % 2 == 0;
52 info!("#{i}: Sending boolean message...");
53 tether_agent
54 .publish(&boolean_message_output, Some(&[bool.into()]))
55 .unwrap();
56
57 info!("#{i}: Sending custom struct message...");
58 let custom_message = CustomStruct {
59 id: i,
60 name: "hello".into(),
61 };
62 tether_agent
63 .encode_and_publish(&custom_output, custom_message)
64 .unwrap();
65
66 info!("#{i}: Sending grouped messages...");
67 tether_agent.publish(&grouped_output_1, None).unwrap();
68 tether_agent.publish(&grouped_output_2, None).unwrap();
69
70 thread::sleep(Duration::from_millis(1000))
71 }
72}17fn main() {
18 println!("Rust Tether Agent subscribe example");
19
20 let mut builder = Builder::from_env(Env::default().default_filter_or("debug"));
21 builder.filter_module("tether_agent", log::LevelFilter::Warn);
22 builder.filter_module("rumqttc", log::LevelFilter::Warn);
23 builder.init();
24
25 debug!("Debugging is enabled; could be verbose");
26
27 let mut tether_agent = TetherAgentOptionsBuilder::new("RustDemo")
28 .id(Some("example"))
29 .build()
30 .expect("failed to init Tether agent");
31
32 let input_one = PlugOptionsBuilder::create_input("one")
33 .build(&mut tether_agent)
34 .expect("failed to create input");
35 info!("input one {} = {}", input_one.name(), input_one.topic());
36 let input_two = PlugOptionsBuilder::create_input("two")
37 .role(Some("specific"))
38 .build(&mut tether_agent)
39 .expect("failed to create input");
40 info!("input two {} = {}", input_two.name(), input_two.topic());
41 let input_empty = PlugOptionsBuilder::create_input("nothing")
42 .build(&mut tether_agent)
43 .expect("failed to create input");
44
45 let input_everything = PlugOptionsBuilder::create_input("everything")
46 .topic(Some("#"))
47 .build(&mut tether_agent)
48 .expect("failed to create input");
49
50 let input_specify_id = PlugOptionsBuilder::create_input("groupMessages")
51 .id(Some("someGroup"))
52 .name(None)
53 .build(&mut tether_agent)
54 .expect("failed to create input");
55
56 debug!(
57 "input everything {} = {}",
58 input_everything.name(),
59 input_everything.topic()
60 );
61
62 info!("Checking messages every 1s, 10x...");
63
64 loop {
65 debug!("Checking for messages...");
66 while let Some((topic, payload)) = tether_agent.check_messages() {
67 // debug!(
68 // "........ Received a message topic {:?} => topic parts {:?}",
69 // topic, topic
70 // );
71
72 if input_one.matches(&topic) {
73 info!(
74 "******** INPUT ONE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
75 input_one.name(),
76 topic,
77 payload.len()
78 );
79 // assert_eq!(parse_plug_name(topic.un), Some("one"));
80 }
81 if input_two.matches(&topic) {
82 info!(
83 "******** INPUT TWO:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
84 input_two.name(),
85 topic,
86 payload.len()
87 );
88 // assert_eq!(parse_plug_name(message.topic()), Some("two"));
89 // assert_ne!(parse_plug_name(message.topic()), Some("one"));
90
91 // Notice how you must give the from_slice function a type so it knows what to expect
92 let decoded = from_slice::<CustomMessage>(&payload);
93 match decoded {
94 Ok(d) => {
95 info!("Yes, we decoded the MessagePack payload as: {:?}", d);
96 let CustomMessage { name, id } = d;
97 debug!("Name is {} and ID is {}", name, id);
98 }
99 Err(e) => {
100 warn!("Failed to decode the payload: {}", e)
101 }
102 };
103 }
104 if input_empty.matches(&topic) {
105 info!(
106 "******** EMPTY MESSAGE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
107 input_empty.name(),
108 topic,
109 payload.len()
110 );
111 // assert_eq!(parse_plug_name(topic), Some("nothing"));
112 }
113 if input_everything.matches(&topic) {
114 info!(
115 "******** EVERYTHING MATCHES HERE:\n Received a message for plug named \"{}\" on topic {:?} with length {} bytes",
116 input_everything.name(),
117 topic,
118 payload.len()
119 );
120 }
121 if input_specify_id.matches(&topic) {
122 info!("******** ID MATCH:\n Should match any role and plug name, but only messages with ID \"groupMessages\"");
123 info!(
124 "\n Received a message from plug named \"{}\" on topic {:?} with length {} bytes",
125 input_specify_id.name(),
126 topic,
127 payload.len()
128 );
129 // assert_eq!(parse_agent_id(message.topic()), Some("groupMessages"));
130 }
131 }
132
133 thread::sleep(Duration::from_millis(1000))
134 }
135}