forked from eclipse-paho/paho.mqtt.rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_response.rs
289 lines (261 loc) · 10.1 KB
/
server_response.rs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// paho-mqtt/src/server_response.rs
//
// This file is part of the Eclipse Paho MQTT Rust Client library.
//
/*******************************************************************************
* Copyright (c) 2018-2020 Frank Pagliughi <fpagliughi@mindspring.com>
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Frank Pagliughi - initial implementation and documentation
*******************************************************************************/
//! The Token module for the Paho MQTT Rust client library.
//!
//! Asynchronous operations return a `Token` that is a type of future. It
//! can be used to determine if an operation has completed, block and wait
//! for the operation to complete, and obtain the final result.
//! For example, you can start a connection, do something else, and then
//! wait for the connection to complete.
//!
//! The Token object implements the Future trait, and thus can be used and
//! combined with any other Rust futures.
//!
use std::ffi::CStr;
use crate::{ffi, from_c_bool, properties::Properties, reason_code::ReasonCode};
/////////////////////////////////////////////////////////////////////////////
// ServerRequest
/// The server requests that expect a response.
/// This is required because the `alt` union of the MQTTAsync_successData
/// struct from C library doesn't indicate which field is valid.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ServerRequest {
/// No response expected from the server
None,
/// Connecting to the server
Connect,
/// A subscription request for a single topic
Subscribe,
/// A subscription request for multiple topics
SubscribeMany(usize),
/// An unsubscribe request for a single topic
Unsubscribe,
/// An unsubscribe request for multiple topics
UnsubscribeMany(usize),
}
impl Default for ServerRequest {
fn default() -> Self {
ServerRequest::None
}
}
/////////////////////////////////////////////////////////////////////////////
// RequestResponse
/// The possible responses that may come back from the server, depending on
/// the type of request.
#[derive(Clone, Debug)]
pub enum RequestResponse {
/// No response from the server
None,
/// The server URI, MQTT version, and whether the session is present
Connect(ConnectResponse),
/// The granted QoS of the subscription
Subscribe(i32),
/// The granted QoS of all the subscriptions
SubscribeMany(Vec<i32>),
/// The granted QoS of the subscription
Unsubscribe(i32),
/// The granted QoS of all the subscriptions
UnsubscribeMany(Vec<i32>),
}
impl Default for RequestResponse {
fn default() -> Self {
RequestResponse::None
}
}
/// The response from the server on a connect request.
#[derive(Clone, Default, Debug)]
pub struct ConnectResponse {
/// The URI of the server.
pub server_uri: String,
/// The version of MQTT granted by the server.
pub mqtt_version: i32,
/// Whether the client session is already present on the server.
pub session_present: bool,
}
/////////////////////////////////////////////////////////////////////////////
// ServerResponse
/// Responses coming back from the server from client requests.
#[derive(Clone, Default, Debug)]
pub struct ServerResponse {
/// The request-specific response
rsp: RequestResponse,
/// MQTT v5 Properties
props: Properties,
/// MQTT v5 Reason Code
reason_code: ReasonCode,
}
impl ServerResponse {
/// Creates a new, empty, server response.
pub fn new() -> Self {
ServerResponse::default()
}
/// Creates the response object from the v3 "success" data structure
/// sent by the C lib on completion of the operation.
pub unsafe fn from_success(req: ServerRequest, rsp: &ffi::MQTTAsync_successData) -> Self {
let rsp = match req {
ServerRequest::Connect => {
let conn_rsp = ConnectResponse {
server_uri: CStr::from_ptr(rsp.alt.connect.serverURI)
.to_string_lossy()
.to_string(),
mqtt_version: rsp.alt.connect.MQTTVersion,
session_present: from_c_bool(rsp.alt.connect.sessionPresent),
};
RequestResponse::Connect(conn_rsp)
}
ServerRequest::Subscribe => RequestResponse::Subscribe(rsp.alt.qos),
ServerRequest::SubscribeMany(n) => {
let mut qosv = Vec::new();
if n == 1 {
qosv.push(rsp.alt.qos);
}
else if !rsp.alt.qosList.is_null() {
for i in 0..n {
qosv.push(*rsp.alt.qosList.add(i));
}
}
debug!("Subscribed to {} topics w/ QoS: {:?}", qosv.len(), qosv);
RequestResponse::SubscribeMany(qosv)
}
_ => RequestResponse::None,
};
ServerResponse {
rsp,
props: Properties::new(),
reason_code: ReasonCode::default(),
}
}
/// Creates the response object from the v5 "success" data structure
/// sent by the C lib on completion of the operation.
pub unsafe fn from_success5(req: ServerRequest, rsp: &ffi::MQTTAsync_successData5) -> Self {
let props = Properties::from_c_struct(&rsp.properties);
//debug!("Properties: {:?}", props);
let reason_code = ReasonCode::from(rsp.reasonCode);
let rsp = match req {
ServerRequest::Connect => {
let conn_rsp = ConnectResponse {
server_uri: CStr::from_ptr(rsp.alt.connect.serverURI)
.to_string_lossy()
.to_string(),
mqtt_version: rsp.alt.connect.MQTTVersion,
session_present: from_c_bool(rsp.alt.connect.sessionPresent),
};
RequestResponse::Connect(conn_rsp)
}
ServerRequest::Subscribe => RequestResponse::Subscribe(rsp.reasonCode as i32),
ServerRequest::SubscribeMany(n) => {
let ncode = rsp.alt.sub.reasonCodeCount as usize;
debug_assert!(n == ncode);
let n = std::cmp::min(n, ncode);
let mut qosv = Vec::new();
if n == 1 {
qosv.push(rsp.reasonCode as i32);
}
else if !rsp.alt.sub.reasonCodes.is_null() {
for i in 0..n {
qosv.push(rsp.alt.sub.reasonCodes.add(i) as i32);
}
}
debug!("Subscribed to {} topics w/ QoS: {:?}", qosv.len(), qosv);
RequestResponse::SubscribeMany(qosv)
}
ServerRequest::Unsubscribe => RequestResponse::Unsubscribe(rsp.reasonCode as i32),
ServerRequest::UnsubscribeMany(n) => {
let ncode = rsp.alt.unsub.reasonCodeCount as usize;
debug!("Server returned {} unsubscribe codes", ncode);
debug_assert!(n == ncode);
let n = std::cmp::min(n, ncode);
let mut qosv = Vec::new();
if n == 1 {
qosv.push(rsp.reasonCode as i32);
}
else if !rsp.alt.sub.reasonCodes.is_null() {
for i in 0..n {
qosv.push(rsp.alt.unsub.reasonCodes.add(i) as i32);
}
}
debug!("Subscribed to {} topics w/ Qos: {:?}", qosv.len(), qosv);
RequestResponse::SubscribeMany(qosv)
}
_ => RequestResponse::None,
};
ServerResponse {
rsp,
props,
reason_code,
}
}
/// Creates the response object from the v5 "failure" data structure
/// sent by the C lib on completion of the operation.
pub unsafe fn from_failure5(rsp: &ffi::MQTTAsync_failureData5) -> Self {
ServerResponse {
rsp: RequestResponse::default(),
props: Properties::from_c_struct(&rsp.properties),
reason_code: rsp.reasonCode.into(),
}
}
/// Gets the response for the specific type of request.
pub fn request_response(&self) -> &RequestResponse {
&self.rsp
}
/// Gets the response for a connection request
pub fn connect_response(&self) -> Option<ConnectResponse> {
match &self.rsp {
RequestResponse::Connect(conn_rsp) => Some(conn_rsp.clone()),
_ => None,
}
}
/// Gets the response for a subscription request.
pub fn subscribe_response(&self) -> Option<i32> {
match &self.rsp {
RequestResponse::Subscribe(qos) => Some(*qos),
_ => None,
}
}
/// Gets the response for a multi-topic subscription request.
pub fn subscribe_many_response(&self) -> Option<Vec<i32>> {
match &self.rsp {
RequestResponse::SubscribeMany(qosv) => Some(qosv.clone()),
_ => None,
}
}
/// Gets the response for an unsubscribe request.
pub fn unsubscribe_response(&self) -> Option<i32> {
match &self.rsp {
RequestResponse::Unsubscribe(qos) => Some(*qos),
_ => None,
}
}
/// Gets the response for a multi-topic unsubscribe request.
pub fn unsubscribe_many_response(&self) -> Option<Vec<i32>> {
match &self.rsp {
RequestResponse::UnsubscribeMany(qosv) => Some(qosv.clone()),
_ => None,
}
}
/// Gets the properties returned from the server.
pub fn properties(&self) -> &Properties {
&self.props
}
/// Gets the reason code returned from the server.
pub fn reason_code(&self) -> ReasonCode {
self.reason_code
}
}