1
+ use aws_iot_device_sdk_rust:: settings:: MQTTOptionsOverrides ;
1
2
use aws_iot_device_sdk_rust:: {
2
3
async_event_loop_listener, AWSIoTAsyncClient , AWSIoTSettings , Packet , QoS ,
3
4
} ;
@@ -39,6 +40,10 @@ struct Args {
39
40
#[ arg( long, env = "AWS_IOT_ENDPOINT" ) ]
40
41
endpoint : String ,
41
42
43
+ /// AWS IoT endpoint URL
44
+ #[ arg( long, env = "AWS_IOT_PORT" , default_value = "8883" ) ]
45
+ port : u16 ,
46
+
42
47
/// Client ID for MQTT connection
43
48
#[ arg( long, env = "AWS_IOT_CLIENT_ID" ) ]
44
49
client_id : String ,
@@ -119,20 +124,33 @@ async fn main() -> Result<(), Box<dyn Error>> {
119
124
120
125
debug ! ( "Parsed CLI arguments: {:?}" , args) ;
121
126
127
+ let mqtt_option_overrides = MQTTOptionsOverrides {
128
+ port : Some ( args. port ) ,
129
+ clean_session : Some ( true ) ,
130
+ keep_alive : None ,
131
+ max_packet_size : None ,
132
+ request_channel_capacity : None ,
133
+ pending_throttle : None ,
134
+ inflight : None ,
135
+ last_will : None ,
136
+ conn_timeout : None ,
137
+ transport : None ,
138
+ } ;
122
139
let aws_settings = AWSIoTSettings :: new (
123
140
args. client_id . clone ( ) ,
124
141
args. root_ca . to_str ( ) . unwrap ( ) . to_string ( ) ,
125
142
args. device_cert . to_str ( ) . unwrap ( ) . to_string ( ) ,
126
143
args. private_key . to_str ( ) . unwrap ( ) . to_string ( ) ,
127
144
args. endpoint . clone ( ) ,
128
- None ,
145
+ Some ( mqtt_option_overrides ) ,
129
146
) ;
130
147
131
148
debug ! ( "Connecting with client_id: {}" , args. client_id. blue( ) ) ;
132
149
debug ! ( "Using endpoint: {}" , args. endpoint) ;
133
150
134
- let ( iot_core_client, event_loop) = AWSIoTAsyncClient :: new ( aws_settings) . await ?;
135
- let iot_core_client = Arc :: new ( Mutex :: new ( iot_core_client) ) ;
151
+ let ( iot_core_client, ( event_loop, sender) ) = AWSIoTAsyncClient :: new ( aws_settings) . await ?;
152
+ let raw_client = iot_core_client. get_client ( ) . await ;
153
+ let client = Arc :: new ( Mutex :: new ( raw_client) ) ;
136
154
137
155
match args. command {
138
156
Some ( CliCommand :: Sub {
@@ -157,7 +175,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
157
175
let exclude_regex = exclude. map ( |s| Regex :: new ( & s) . unwrap ( ) ) ;
158
176
159
177
for topic in topic_list {
160
- iot_core_client
178
+ client
161
179
. lock ( )
162
180
. await
163
181
. subscribe ( topic. to_string ( ) , QoS :: AtMostOnce )
@@ -166,7 +184,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
166
184
}
167
185
168
186
// For subscriptions, keep listening to messages
169
- let receiver = iot_core_client . lock ( ) . await . get_receiver ( ) . await ;
187
+ let receiver = sender . subscribe ( ) ;
170
188
let receiver = Arc :: new ( Mutex :: new ( receiver) ) ;
171
189
172
190
let recv_thread = task:: spawn ( async move {
@@ -196,7 +214,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
196
214
} ) ;
197
215
198
216
let listen_thread = task:: spawn ( async move {
199
- async_event_loop_listener ( event_loop) . await . unwrap ( ) ;
217
+ async_event_loop_listener ( ( event_loop, sender) )
218
+ . await
219
+ . unwrap ( ) ;
200
220
} ) ;
201
221
202
222
let ( recv_result, listen_result) = tokio:: join!( recv_thread, listen_thread) ;
@@ -210,7 +230,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
210
230
let topic_list: Vec < & str > = topics. split ( ',' ) . collect ( ) ;
211
231
212
232
// Create a receiver to drain incoming events
213
- let receiver = iot_core_client . lock ( ) . await . get_receiver ( ) . await ;
233
+ let receiver = sender . subscribe ( ) ;
214
234
let receiver = Arc :: new ( Mutex :: new ( receiver) ) ;
215
235
let drain_task = task:: spawn ( async move {
216
236
while ( receiver. lock ( ) . await . recv ( ) . await ) . is_ok ( ) {
@@ -222,10 +242,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
222
242
} ) ;
223
243
224
244
for topic in topic_list {
225
- iot_core_client
245
+ client
226
246
. lock ( )
227
247
. await
228
- . publish ( topic. to_string ( ) , QoS :: AtMostOnce , message. to_string ( ) )
248
+ . publish ( topic, QoS :: AtMostOnce , false , message. clone ( ) )
229
249
. await ?;
230
250
println ! ( "{}" , format!( "Published to topic: {}" , topic) . blue( ) ) ;
231
251
match serde_json:: from_str :: < Value > ( & message) {
@@ -236,7 +256,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
236
256
237
257
// Run the event loop briefly to ensure the message is sent
238
258
let event_loop_task = task:: spawn ( async move {
239
- async_event_loop_listener ( event_loop) . await . unwrap ( ) ;
259
+ async_event_loop_listener ( ( event_loop, sender) )
260
+ . await
261
+ . unwrap ( ) ;
240
262
} ) ;
241
263
242
264
// Allow time for the event loop to process the message
0 commit comments