diff options
Diffstat (limited to 'crates/common/mqtt_channel/src/config.rs')
-rw-r--r-- | crates/common/mqtt_channel/src/config.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/crates/common/mqtt_channel/src/config.rs b/crates/common/mqtt_channel/src/config.rs index 17875753..3c825a18 100644 --- a/crates/common/mqtt_channel/src/config.rs +++ b/crates/common/mqtt_channel/src/config.rs @@ -15,7 +15,8 @@ pub struct Config { /// The session name to be use on connect /// - /// If no session name is provided, a random one will be created on connect. + /// If no session name is provided, a random one will be created on connect, + /// and the session will be clean on connect. /// /// Default: None pub session_name: Option<String>, @@ -118,4 +119,27 @@ impl Config { ..self } } + + /// Wrap this config into an internal set of options for `rumqttc`. + pub(crate) fn mqtt_options(&self) -> rumqttc::MqttOptions { + let id = match &self.session_name { + None => std::iter::repeat_with(fastrand::lowercase) + .take(10) + .collect(), + Some(name) => name.clone(), + }; + + let mut mqtt_options = rumqttc::MqttOptions::new(id, &self.host, self.port); + + if self.session_name.is_none() { + // There is no point to have a session with a random name that will not be reused. + mqtt_options.set_clean_session(true); + } else { + mqtt_options.set_clean_session(self.clean_session); + } + + mqtt_options.set_max_packet_size(self.max_packet_size, self.max_packet_size); + + mqtt_options + } } |