Skip to content

Commit

Permalink
use from_state when creating client with clean_session false (Azure#4510
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ancaantochi authored Feb 27, 2021
1 parent 8c562b9 commit 12c8725
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions mqtt/mqtt-bridge/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,27 @@ where
Credentials::Anonymous(client_id) => (client_id.into(), None),
};

let client_id = if clean_session { None } else { Some(client_id) };

Self::validate(client_id.as_ref(), username.as_ref(), &keep_alive)?;

let client = Client::new(
client_id,
username,
None,
io_source,
DEFAULT_MAX_RECONNECT,
keep_alive,
);
let client = if clean_session {
Client::new(
Some(client_id),
username,
None,
io_source,
DEFAULT_MAX_RECONNECT,
keep_alive,
)
} else {
Client::from_state(
client_id,
username,
None,
io_source,
DEFAULT_MAX_RECONNECT,
keep_alive,
)
};

Ok(Self {
client,
Expand All @@ -132,13 +141,11 @@ where
}

fn validate(
client_id: Option<&String>,
client_id: &str,
username: Option<&String>,
keep_alive: &Duration,
) -> Result<(), ClientError> {
if let Some(id) = client_id {
validate_length(id)?;
};
validate_length(client_id)?;

if let Some(name) = username {
validate_length(name)?;
Expand Down

0 comments on commit 12c8725

Please sign in to comment.