@@ -15,7 +15,7 @@ use spin_factors::{
1515use spin_world:: v2:: mqtt:: { self as v2, Error , Qos } ;
1616use tokio:: sync:: Mutex ;
1717
18- pub use host:: { ClientCreator , MqttClient } ;
18+ pub use host:: MqttClient ;
1919
2020pub struct OutboundMqttFactor {
2121 create_client : Arc < dyn ClientCreator > ,
@@ -73,6 +73,19 @@ pub struct NetworkedMqttClient {
7373const MQTT_CHANNEL_CAP : usize = 1000 ;
7474
7575impl NetworkedMqttClient {
76+ /// Create a [`ClientCreator`] that creates a [`NetworkedMqttClient`].
77+ pub fn creator ( ) -> Arc < dyn ClientCreator > {
78+ Arc :: new ( |address, username, password, keep_alive_interval| {
79+ Ok ( Arc :: new ( NetworkedMqttClient :: create (
80+ address,
81+ username,
82+ password,
83+ keep_alive_interval,
84+ ) ?) as _ )
85+ } )
86+ }
87+
88+ /// Create a new [`NetworkedMqttClient`] with the given address, username, password, and keep alive interval.
7689 pub fn create (
7790 address : String ,
7891 username : String ,
@@ -127,3 +140,30 @@ impl MqttClient for NetworkedMqttClient {
127140 Ok ( ( ) )
128141 }
129142}
143+
144+ /// A trait for creating MQTT client.
145+ #[ async_trait]
146+ pub trait ClientCreator : Send + Sync {
147+ fn create (
148+ & self ,
149+ address : String ,
150+ username : String ,
151+ password : String ,
152+ keep_alive_interval : Duration ,
153+ ) -> Result < Arc < dyn MqttClient > , Error > ;
154+ }
155+
156+ impl < F > ClientCreator for F
157+ where
158+ F : Fn ( String , String , String , Duration ) -> Result < Arc < dyn MqttClient > , Error > + Send + Sync ,
159+ {
160+ fn create (
161+ & self ,
162+ address : String ,
163+ username : String ,
164+ password : String ,
165+ keep_alive_interval : Duration ,
166+ ) -> Result < Arc < dyn MqttClient > , Error > {
167+ self ( address, username, password, keep_alive_interval)
168+ }
169+ }
0 commit comments