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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use std::slice;
use std::ffi::{CString, IntoStringError};
use std::string::{FromUtf8Error};
use std::os::raw::{c_void};
use std::convert::From;
use ffi;
#[derive(Debug)]
pub struct Message {
pub cmsg: ffi::MQTTAsync_message,
pub topic: CString,
payload: Vec<u8>
}
impl Message {
pub fn new<V>(topic: &str, payload: V, qos: i32) -> Message
where V: Into<Vec<u8>>
{
let mut msg = Message {
cmsg: ffi::MQTTAsync_message::default(),
topic: CString::new(topic).unwrap(),
payload: payload.into(),
};
msg.cmsg.qos = qos;
Message::fixup(msg)
}
pub fn new_retained<V>(topic: &str, payload: V, qos: i32) -> Message
where V: Into<Vec<u8>>
{
let mut msg = Message {
cmsg: ffi::MQTTAsync_message::default(),
topic: CString::new(topic).unwrap(),
payload: payload.into(),
};
msg.cmsg.qos = qos;
msg.cmsg.retained = 1;
Message::fixup(msg)
}
pub unsafe fn from_c_parts(topic: CString, cmsg: &ffi::MQTTAsync_message) -> Message {
let len = cmsg.payloadlen as usize;
let payload = slice::from_raw_parts(cmsg.payload as *mut u8, len);
let msg = Message {
cmsg: cmsg.clone(),
topic,
payload: payload.to_vec(),
};
Message::fixup(msg)
}
fn fixup(mut msg: Message) -> Message {
msg.cmsg.payload = msg.payload.as_mut_ptr() as *mut c_void;
msg.cmsg.payloadlen = msg.payload.len() as i32;
msg
}
pub fn get_topic(&self) -> Result<String, IntoStringError> {
self.topic.clone().into_string()
}
pub fn get_payload(&self) -> &Vec<u8> {
&self.payload
}
pub fn get_payload_str(&self) -> Result<String, FromUtf8Error> {
String::from_utf8(self.payload.clone())
}
pub fn get_qos(&self) -> i32 {
self.cmsg.qos
}
pub fn get_retained(&self) -> bool {
self.cmsg.retained != 0
}
}
impl Default for Message {
fn default() -> Message {
let msg = Message {
cmsg: ffi::MQTTAsync_message::default(),
topic: CString::new("").unwrap(),
payload: Vec::new(),
};
Message::fixup(msg)
}
}
impl Clone for Message {
fn clone(&self) -> Message {
let msg = Message {
cmsg: self.cmsg.clone(),
topic: self.topic.clone(),
payload: self.payload.clone(),
};
Message::fixup(msg)
}
}
impl<'a, 'b> From<(&'a str, &'b [u8])> for Message {
fn from((topic, payload): (&'a str, &'b [u8])) -> Self {
let msg = Message {
cmsg: ffi::MQTTAsync_message::default(),
topic: CString::new(topic).unwrap(),
payload: payload.to_vec(),
};
Message::fixup(msg)
}
}
impl<'a, 'b> From<(&'a str, &'b [u8], i32, bool)> for Message {
fn from((topic, payload, qos, retained): (&'a str, &'b [u8], i32, bool)) -> Self {
let mut msg = Message {
cmsg: ffi::MQTTAsync_message::default(),
topic: CString::new(topic).unwrap(),
payload: payload.to_vec(),
};
msg.cmsg.qos = qos;
msg.cmsg.retained = if retained { 1 } else { 0 };
Message::fixup(msg)
}
}
#[derive(Debug)]
pub struct MessageBuilder {
topic: String,
payload: Vec<u8>,
qos: i32,
retained: bool,
}
impl MessageBuilder
{
pub fn new() -> MessageBuilder {
MessageBuilder {
topic: String::new(),
payload: Vec::new(),
qos: 0,
retained: false,
}
}
pub fn topic(mut self, topic: &str) -> MessageBuilder {
self.topic = topic.to_string();
self
}
pub fn payload<V>(mut self, payload: V) -> MessageBuilder
where V: Into<Vec<u8>>
{
self.payload = payload.into();
self
}
pub fn qos(mut self, qos: i32) -> MessageBuilder {
self.qos = qos;
self
}
pub fn retained(mut self, retained: bool) -> MessageBuilder {
self.retained = retained;
self
}
pub fn finalize(self) -> Message {
let mut msg = Message {
cmsg: ffi::MQTTAsync_message::default(),
topic: CString::new(self.topic).unwrap(),
payload: self.payload,
};
msg.cmsg.qos = self.qos;
msg.cmsg.retained = if self.retained { 1 } else { 0 };
Message::fixup(msg)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::os::raw::{c_char};
#[test]
fn test_new() {
const TOPIC: &'static str = "test";
const PAYLOAD: &'static [u8] = b"Hello world";
const QOS: i32 = 2;
let msg = Message::new(TOPIC, PAYLOAD, QOS);
assert_eq!([ 'M' as c_char, 'Q' as i8, 'T' as i8, 'M' as i8 ], msg.cmsg.struct_id);
assert_eq!(0, msg.cmsg.struct_version);
assert_eq!(TOPIC, msg.topic.to_str().unwrap());
assert_eq!(PAYLOAD, msg.payload.as_slice());
assert_eq!(msg.payload.len() as i32, msg.cmsg.payloadlen);
assert_eq!(msg.payload.as_ptr() as *mut c_void, msg.cmsg.payload);
assert_eq!(QOS, msg.cmsg.qos);
assert!(msg.cmsg.retained == 0);
}
#[test]
fn test_from_tuple() {
const TOPIC: &'static str = "test";
const PAYLOAD: &'static [u8] = b"Hello world";
const QOS: i32 = 2;
const RETAINED: bool = true;
let msg = Message::from((TOPIC, PAYLOAD, QOS, RETAINED));
assert_eq!(TOPIC, msg.topic.to_str().unwrap());
assert_eq!(PAYLOAD, msg.payload.as_slice());
assert_eq!(msg.payload.len() as i32, msg.cmsg.payloadlen);
assert_eq!(msg.payload.as_ptr() as *mut c_void, msg.cmsg.payload);
assert_eq!(QOS, msg.cmsg.qos);
assert!(msg.cmsg.retained != 0);
}
#[test]
fn test_builder_default() {
let msg = MessageBuilder::new().finalize();
let cmsg = ffi::MQTTAsync_message::default();
assert_eq!([ 'M' as c_char, 'Q' as i8, 'T' as i8, 'M' as i8 ], cmsg.struct_id);
assert_eq!(0, cmsg.struct_version);
assert_eq!(cmsg.struct_id, msg.cmsg.struct_id);
assert_eq!(cmsg.struct_version, msg.cmsg.struct_version);
assert_eq!(0, msg.topic.as_bytes().len());
assert_eq!(&[] as &[u8], msg.topic.as_bytes());
assert_eq!(vec![] as Vec<u8>, msg.payload);
}
#[test]
fn test_builder_topic() {
const TOPIC: &'static str = "test";
let msg = MessageBuilder::new()
.topic(TOPIC).finalize();
assert_eq!(TOPIC, msg.topic.to_str().unwrap());
}
#[test]
fn test_builder_payload() {
const PAYLOAD: &'static [u8] = b"Hello world";
let msg = MessageBuilder::new()
.payload(PAYLOAD).finalize();
assert_eq!(PAYLOAD, msg.payload.as_slice());
assert_eq!(msg.payload.len() as i32, msg.cmsg.payloadlen);
assert_eq!(msg.payload.as_ptr() as *mut c_void, msg.cmsg.payload);
}
#[test]
fn test_builder_qos() {
const QOS: i32 = 2;
let msg = MessageBuilder::new()
.qos(QOS).finalize();
assert_eq!(QOS, msg.cmsg.qos);
}
#[test]
fn test_builder_retained() {
let msg = MessageBuilder::new()
.retained(false).finalize();
assert!(msg.cmsg.retained == 0);
let msg = MessageBuilder::new()
.retained(true).finalize();
assert!(msg.cmsg.retained != 0);
}
#[test]
fn test_copy() {
const TOPIC: &'static str = "test";
const PAYLOAD: &'static [u8] = b"Hello world";
const QOS: i32 = 2;
const RETAINED: bool = true;
let org_msg = MessageBuilder::new()
.topic(TOPIC)
.payload(PAYLOAD)
.qos(QOS)
.retained(RETAINED)
.finalize();
let msg = org_msg;
assert_eq!(TOPIC, msg.topic.to_str().unwrap());
assert_eq!(PAYLOAD, msg.payload.as_slice());
assert_eq!(msg.payload.len() as i32, msg.cmsg.payloadlen);
assert_eq!(msg.payload.as_ptr() as *mut c_void, msg.cmsg.payload);
assert_eq!(QOS, msg.cmsg.qos);
assert!(msg.cmsg.retained != 0);
}
#[test]
fn test_clone() {
const TOPIC: &'static str = "test";
const PAYLOAD: &'static [u8] = b"Hello world";
const QOS: i32 = 2;
const RETAINED: bool = true;
let msg = {
let org_msg = MessageBuilder::new()
.topic(TOPIC)
.payload(PAYLOAD)
.qos(QOS)
.retained(RETAINED)
.finalize();
org_msg.clone()
};
assert_eq!(TOPIC, msg.topic.to_str().unwrap());
assert_eq!(PAYLOAD, msg.payload.as_slice());
assert_eq!(msg.payload.len() as i32, msg.cmsg.payloadlen);
assert_eq!(msg.payload.as_ptr() as *mut c_void, msg.cmsg.payload);
assert_eq!(QOS, msg.cmsg.qos);
assert!(msg.cmsg.retained != 0);
}
}