1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::{
client::{
Client,
ClientOptions,
KeepAlive,
},
Result,
util::{
TokioRuntime,
}
};
#[cfg(feature = "tls")]
use rustls;
#[cfg(feature = "tls")]
use std::sync::Arc;
use tokio::time::Duration;
#[derive(Default)]
pub struct ClientBuilder {
host: Option<String>,
port: Option<u16>,
username: Option<String>,
password: Option<Vec<u8>>,
keep_alive: Option<KeepAlive>,
runtime: TokioRuntime,
client_id: Option<String>,
packet_buffer_len: Option<usize>,
max_packet_len: Option<usize>,
operation_timeout: Option<Duration>,
#[cfg(feature = "tls")]
tls_client_config: Option<Arc<rustls::ClientConfig>>,
automatic_connect: Option<bool>,
connect_retry_delay: Option<Duration>,
}
impl ClientBuilder {
pub fn build(&mut self) -> Result<Client> {
Client::new(
ClientOptions {
host: match self.host {
Some(ref h) => h.clone(),
None => return Err("You must set a host to build a Client".into())
},
port: self.port.unwrap_or(1883),
username: self.username.clone(),
password: self.password.clone(),
keep_alive: self.keep_alive.unwrap_or(KeepAlive::from_secs(30)),
runtime: self.runtime.clone(),
client_id: self.client_id.clone(),
packet_buffer_len: self.packet_buffer_len.unwrap_or(100),
max_packet_len: self.max_packet_len.unwrap_or(64 * 1024),
operation_timeout: self.operation_timeout.unwrap_or(Duration::from_secs(20)),
#[cfg(feature = "tls")]
tls_client_config: match self.tls_client_config {
Some(ref c) => Some(c.clone()),
None => None,
},
automatic_connect: self.automatic_connect.unwrap_or(true),
connect_retry_delay: self.connect_retry_delay.unwrap_or(Duration::from_secs(30)),
})
}
pub fn set_host(&mut self, host: String) -> &mut Self {
self.host = Some(host);
self
}
pub fn set_port(&mut self, port: u16) -> &mut Self {
self.port = Some(port);
self
}
pub fn set_username(&mut self, username: Option<String>) -> &mut Self {
self.username = username;
self
}
pub fn set_password(&mut self, password: Option<Vec<u8>>) -> &mut Self {
self.password = password;
self
}
pub fn set_keep_alive(&mut self, keep_alive: KeepAlive) -> &mut Self {
self.keep_alive = Some(keep_alive);
self
}
pub fn set_tokio_runtime(&mut self, rt: TokioRuntime) -> &mut Self {
self.runtime = rt;
self
}
pub fn set_client_id(&mut self, client_id: Option<String>) -> &mut Self {
self.client_id = client_id;
self
}
pub fn set_packet_buffer_len(&mut self, packet_buffer_len: usize) -> &mut Self {
self.packet_buffer_len = Some(packet_buffer_len);
self
}
pub fn set_max_packet_len(&mut self, max_packet_len: usize) -> &mut Self {
self.max_packet_len = Some(max_packet_len);
self
}
pub fn set_operation_timeout(&mut self, operation_timeout: Duration) -> &mut Self {
self.operation_timeout = Some(operation_timeout);
self
}
#[cfg(feature = "tls")]
pub fn set_tls_client_config(&mut self, tls_client_config: rustls::ClientConfig) -> &mut Self {
self.tls_client_config = Some(Arc::new(tls_client_config));
self
}
pub fn set_automatic_connect(&mut self, automatic_connect: bool) -> &mut Self {
self.automatic_connect = Some(automatic_connect);
self
}
pub fn set_connect_retry_delay(&mut self, connect_retry_delay: Duration) -> &mut Self {
self.connect_retry_delay = Some(connect_retry_delay);
self
}
}