1- use anyhow:: { anyhow, Context , Ok } ;
1+ use anyhow:: { anyhow, Context } ;
22use clap:: Args ;
3- use paho_mqtt:: Client ;
3+ use paho_mqtt:: AsyncClient ;
44use serde:: { Deserialize , Serialize } ;
55use spin_app:: MetadataKey ;
66use spin_core:: async_trait;
@@ -89,7 +89,7 @@ impl TriggerExecutor for MqttTrigger {
8989 let qos = config. qos . parse :: < i32 > ( ) ?;
9090 let topic = config. topic . clone ( ) ;
9191 acc. push ( ( component, qos, topic) ) ;
92- Ok ( acc)
92+ anyhow :: Ok ( acc)
9393 } ) ?;
9494
9595 Ok ( Self {
@@ -105,7 +105,8 @@ impl TriggerExecutor for MqttTrigger {
105105 async fn run ( self , config : Self :: RunConfig ) -> anyhow:: Result < ( ) > {
106106 if config. test {
107107 for component in & self . component_configs {
108- self . handle_mqtt_event ( & component. 0 , "test message" ) . await ?;
108+ self . handle_mqtt_event ( & component. 0 , b"test message" . to_vec ( ) )
109+ . await ?;
109110 }
110111
111112 Ok ( ( ) )
@@ -144,7 +145,7 @@ impl TriggerExecutor for MqttTrigger {
144145}
145146
146147impl MqttTrigger {
147- async fn handle_mqtt_event ( & self , component_id : & str , message : & str ) -> anyhow:: Result < ( ) > {
148+ async fn handle_mqtt_event ( & self , component_id : & str , message : Vec < u8 > ) -> anyhow:: Result < ( ) > {
148149 // Load the guest wasm component
149150 let ( instance, mut store) = self . engine . prepare_instance ( component_id) . await ?;
150151
@@ -156,7 +157,7 @@ impl MqttTrigger {
156157 let instance = SpinMqtt :: new ( & mut store, & instance) ?;
157158
158159 instance
159- . call_handle_message ( store, & message. as_bytes ( ) . to_vec ( ) )
160+ . call_handle_message ( store, & message)
160161 . await ?
161162 . map_err ( |err| anyhow ! ( "failed to execute guest: {err}" ) )
162163 }
@@ -168,7 +169,7 @@ impl MqttTrigger {
168169 topic : String ,
169170 ) -> anyhow:: Result < ( ) > {
170171 // Receive the messages here from the specific topic in mqtt broker.
171- let client = Client :: new ( self . address . clone ( ) ) ?;
172+ let mut client = AsyncClient :: new ( self . address . clone ( ) ) ?;
172173 let conn_opts = paho_mqtt:: ConnectOptionsBuilder :: new ( )
173174 . keep_alive_interval ( Duration :: from_secs ( self . keep_alive_interval ) )
174175 . user_name ( & self . username )
@@ -177,19 +178,32 @@ impl MqttTrigger {
177178
178179 client
179180 . connect ( conn_opts)
181+ . await
180182 . context ( format ! ( "failed to connect to {:?}" , self . address) ) ?;
181183 client
182184 . subscribe ( & topic, qos)
185+ . await
183186 . context ( format ! ( "failed to subscribe to {topic:?}" ) ) ?;
184187
185- for msg in client. start_consuming ( ) {
186- if let Some ( msg) = msg {
187- _ = self
188- . handle_mqtt_event ( & component_id, & msg. payload_str ( ) )
189- . await
190- . map_err ( |err| tracing:: error!( "{err}" ) ) ;
191- } else {
192- continue ;
188+ // Should the buffer be bounded/configurable?
189+ let rx = client. get_stream ( None ) ;
190+
191+ loop {
192+ match rx. recv ( ) . await {
193+ Ok ( Some ( msg) ) => {
194+ // Handle the received message
195+ _ = self
196+ . handle_mqtt_event ( & component_id, msg. payload ( ) . to_vec ( ) )
197+ . await
198+ . map_err ( |err| tracing:: error!( "{err}" ) ) ;
199+ }
200+ Ok ( None ) => {
201+ // Todo: Figure out what this case is
202+ }
203+ Err ( _) => {
204+ // Channel is empty and closed
205+ break ;
206+ }
193207 }
194208 }
195209
0 commit comments