-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmqtt_client_builder_example.rs
More file actions
195 lines (169 loc) · 6.61 KB
/
Copy pathmqtt_client_builder_example.rs
File metadata and controls
195 lines (169 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-License-Identifier: MPL-2.0
use flowsdk::mqtt_client::client::ConnectionResult;
use flowsdk::mqtt_client::tokio_async_client::{
TokioAsyncClientConfig, TokioAsyncMqttClient, TokioMqttEventHandler,
};
use flowsdk::mqtt_client::{MqttClientError, MqttClientOptions};
use flowsdk::mqtt_serde::mqttv5::publishv5::MqttPublish;
use std::io;
use std::sync::Arc;
/// Simple event handler for the example
#[derive(Clone)]
struct BuilderExampleHandler;
#[async_trait::async_trait]
impl TokioMqttEventHandler for BuilderExampleHandler {
async fn on_connected(&mut self, result: &ConnectionResult) {
println!(
"✅ Connected to broker - reason_code: {}, session_present: {}",
result.reason_code, result.session_present
);
}
async fn on_message_received(&mut self, publish: &MqttPublish) {
println!(
"📨 Message received on '{}': {}",
publish.topic_name,
String::from_utf8_lossy(&publish.payload)
);
}
async fn on_error(&mut self, error: &MqttClientError) {
eprintln!("⚠️ Error: {}", error.user_message());
}
}
#[allow(clippy::field_reassign_with_default)]
async fn run_example() -> io::Result<()> {
println!("🚀 MQTT Client Options Builder Pattern Examples\n");
println!("{}", "=".repeat(60));
// Example 1: Minimal configuration with defaults
println!("\n📋 Example 1: Minimal Configuration");
println!("{}", "-".repeat(60));
let options_minimal = MqttClientOptions::builder()
.peer("localhost:1883")
.client_id("builder_minimal_client")
.build();
println!("Peer: {}", options_minimal.peer);
println!("Client ID: {}", options_minimal.client_id);
println!("Keep Alive: {} seconds", options_minimal.keep_alive);
println!("Clean Start: {}", options_minimal.clean_start);
println!("Auto ACK: {}", options_minimal.auto_ack);
// Example 2: Configuration with authentication
println!("\n📋 Example 2: With Authentication");
println!("{}", "-".repeat(60));
let options_auth = MqttClientOptions::builder()
.peer("localhost:1883")
.client_id("builder_auth_client")
.username("mqtt_user")
.password(b"secret_password".to_vec())
.build();
println!("Peer: {}", options_auth.peer);
println!("Client ID: {}", options_auth.client_id);
println!("Username: {:?}", options_auth.username);
println!("Password: [REDACTED]");
// Example 3: Configuration with session expiry
println!("\n📋 Example 3: With Session Expiry (1 hour)");
println!("{}", "-".repeat(60));
let options_session = MqttClientOptions::builder()
.peer("localhost:1883")
.client_id("builder_session_client")
.clean_start(false)
.session_expiry_interval(3600) // 1 hour
.build();
println!("Peer: {}", options_session.peer);
println!("Client ID: {}", options_session.client_id);
println!("Clean Start: {}", options_session.clean_start);
println!(
"Session Expiry: {:?} seconds",
options_session.session_expiry_interval
);
// Example 4: Full featured configuration
println!("\n📋 Example 4: Full Featured Configuration");
println!("{}", "-".repeat(60));
let options_full = MqttClientOptions::builder()
.peer("localhost:1883")
.client_id("builder_full_client")
.username("admin")
.password(b"admin123".to_vec())
.clean_start(false)
.keep_alive(120)
.reconnect(true)
.auto_ack(true)
.sessionless(false)
.session_expiry_interval(7200) // 2 hours
.build();
println!("Peer: {}", options_full.peer);
println!("Client ID: {}", options_full.client_id);
println!("Username: {:?}", options_full.username);
println!("Keep Alive: {} seconds", options_full.keep_alive);
println!("Clean Start: {}", options_full.clean_start);
println!("Reconnect: {}", options_full.reconnect);
println!("Auto ACK: {}", options_full.auto_ack);
println!("Sessionless: {}", options_full.sessionless);
println!(
"Session Expiry: {:?} seconds",
options_full.session_expiry_interval
);
// Example 5: Using default and then modifying
println!("\n📋 Example 5: Using Default + Modifications");
println!("{}", "-".repeat(60));
let mut options_default = MqttClientOptions::default();
options_default.peer = "localhost:8883".to_string();
options_default.client_id = "modified_default_client".to_string();
println!("Peer: {}", options_default.peer);
println!("Client ID: {}", options_default.client_id);
println!("Keep Alive: {} seconds", options_default.keep_alive);
// Example 6: Actually create and connect a client
println!("\n📋 Example 6: Real Client Connection (if broker available)");
println!("{}", "-".repeat(60));
let options = MqttClientOptions::builder()
.peer("broker.emqx.io:1883")
.client_id("builder_example_working_client")
.clean_start(true)
.keep_alive(60)
.reconnect(false)
.auto_ack(true)
.build();
let handler = BuilderExampleHandler;
let config = TokioAsyncClientConfig::default();
match TokioAsyncMqttClient::new(options, Box::new(handler), config).await {
Ok(client) => {
let client = Arc::new(client);
println!("✅ Client created successfully");
// Try to connect (will only work if broker is running)
match client.connect_sync().await {
Ok(result) => {
println!("✅ Connected successfully!");
println!(" Reason Code: {}", result.reason_code);
println!(" Session Present: {}", result.session_present);
// Disconnect
if let Err(e) = client.disconnect().await {
eprintln!("⚠️ Error disconnecting: {}", e);
}
}
Err(e) => {
println!(
"ℹ️ Could not connect to broker (expected if not running): {}",
e
);
}
}
}
Err(e) => {
println!("ℹ️ Could not create client: {}", e);
}
}
println!("\n{}", "=".repeat(60));
println!("✅ Builder pattern examples completed!");
Ok(())
}
#[tokio::main]
async fn main() -> io::Result<()> {
run_example().await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_example() {
// Call run_example to get coverage
run_example().await.unwrap();
}
}