23
23
*/
24
24
25
25
use ffiasync;
26
-
27
26
use libc:: { c_char, c_int, c_void} ;
28
27
use std:: ffi:: { CStr , CString } ;
29
28
use std:: mem;
30
- use std:: ptr;
31
29
use std:: slice;
32
30
use std:: sync:: { Barrier , Mutex , Arc } ;
33
- use std:: error:: Error ;
34
- use std:: fmt;
35
-
36
- pub enum PersistenceType {
37
- Default = 0 ,
38
- Nothing = 1 ,
39
- User = 2 ,
40
- }
41
-
42
- #[ derive( Debug ) ]
43
- pub enum Qos {
44
- FireAndForget = 0 ,
45
- AtLeastOnce = 1 ,
46
- OnceAndOneOnly = 2 ,
47
- }
48
- impl Qos {
49
- fn from_int ( i : i32 ) -> Self {
50
- match i {
51
- 0 => Qos :: FireAndForget ,
52
- 1 => Qos :: AtLeastOnce ,
53
- 2 => Qos :: OnceAndOneOnly ,
54
- _ => unreachable ! ( ) ,
55
- }
56
- }
57
- }
58
-
59
- #[ derive( Debug ) ]
60
- pub struct Message {
61
- pub topic : String ,
62
- pub payload : Option < Vec < u8 > > ,
63
- pub qos : Qos ,
64
- pub retained : bool ,
65
- pub duplicate : bool ,
66
- }
67
-
68
- #[ derive( Debug , Clone ) ]
69
- pub enum MqttError {
70
- Create ( i32 ) ,
71
- Connect ( ConnectError ) ,
72
- Subscribe ( CommandError ) ,
73
- Send ( CommandError ) ,
74
- }
75
- impl fmt:: Display for MqttError {
76
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> Result < ( ) , fmt:: Error > {
77
- match * self {
78
- MqttError :: Create ( ref x) => fmt:: Display :: fmt ( & format ! ( "MqttError::Create({:?})" , x) , f) ,
79
- MqttError :: Connect ( ref x) => fmt:: Display :: fmt ( & format ! ( "MqttError::Connect({:?})" , x) , f) ,
80
- MqttError :: Subscribe ( ref x) => fmt:: Display :: fmt ( & format ! ( "MqttError::Subscribe({:?})" , x) , f) ,
81
- MqttError :: Send ( ref x) => fmt:: Display :: fmt ( & format ! ( "MqttError::Send({:?})" , x) , f) ,
82
- }
83
- }
84
- }
85
- impl Error for MqttError {
86
- fn description ( & self ) -> & str {
87
- match * self {
88
- MqttError :: Create ( _) => "Mqtt creation failed" ,
89
- MqttError :: Connect ( _) => "Mqtt connect failed" ,
90
- MqttError :: Subscribe ( _) => "Mqtt subscribe failed" ,
91
- MqttError :: Send ( _) => "Mqtt send failed" ,
92
- }
93
- }
94
- }
95
-
96
- #[ derive( Debug , Clone ) ]
97
- pub enum CommandError {
98
- ReturnCode ( i32 ) ,
99
- CallbackResponse ( i32 ) ,
100
- CallbackNullPtr
101
- }
102
31
103
- #[ derive( Debug , Clone ) ]
104
- pub enum ConnectError {
105
- ReturnCode ( ConnectErrReturnCode ) ,
106
- CallbackResponse ( i32 ) ,
107
- CallbackNullPtr
108
- }
32
+ use super :: Message ;
33
+ use super :: options:: { PersistenceType , Qos , AsyncConnectOptions } ;
34
+ use super :: error:: { MqttError , CommandError , ConnectError , ConnectErrReturnCode , CallbackError } ;
35
+ use super :: iterator:: AsyncClientIntoIterator ;
109
36
110
- #[ derive( Debug , Clone ) ]
111
- pub enum ConnectErrReturnCode {
112
- UnacceptableProtocol = 1 ,
113
- IdentifierRejected = 2 ,
114
- ServerUnavailable = 3 ,
115
- BadUsernameOrPassword = 4 ,
116
- NotAuthorized = 5 ,
117
- Reserved = 6 ,
118
- }
119
- impl ConnectErrReturnCode {
120
- fn from_int ( i : i32 ) -> Self {
121
- match i {
122
- 1 => ConnectErrReturnCode :: UnacceptableProtocol ,
123
- 2 => ConnectErrReturnCode :: IdentifierRejected ,
124
- 3 => ConnectErrReturnCode :: ServerUnavailable ,
125
- 4 => ConnectErrReturnCode :: BadUsernameOrPassword ,
126
- 5 => ConnectErrReturnCode :: NotAuthorized ,
127
- 6 => ConnectErrReturnCode :: Reserved ,
128
- _ => unreachable ! ( )
129
- }
130
- }
131
- }
132
-
133
- enum CallbackError {
134
- Response ( i32 ) ,
135
- NullPtr
136
- }
137
37
138
38
pub struct AsyncClient {
139
39
handle : ffiasync:: MQTTAsync ,
@@ -192,21 +92,23 @@ impl AsyncClient {
192
92
None ) ;
193
93
}
194
94
95
+ let mut async_opts = ffiasync:: MQTTAsync_connectOptions :: new ( ) ;
96
+
195
97
// fill in FFI private struct
196
- options . options . keepAliveInterval = options. keep_alive_interval ;
197
- options . options . cleansession = options. cleansession ;
198
- options . options . maxInflight = options. max_in_flight ;
199
- options . options . connectTimeout = options. connect_timeout ;
200
- options . options . retryInterval = options. retry_interval ;
98
+ async_opts . keepAliveInterval = options. keep_alive_interval ;
99
+ async_opts . cleansession = options. cleansession ;
100
+ async_opts . maxInflight = options. max_in_flight ;
101
+ async_opts . connectTimeout = options. connect_timeout ;
102
+ async_opts . retryInterval = options. retry_interval ;
201
103
202
104
// register callbacks
203
- options . options . context = self . context ( ) ;
204
- options . options . onSuccess = Some ( Self :: action_succeeded) ;
205
- options . options . onFailure = Some ( Self :: action_failed) ;
105
+ async_opts . context = self . context ( ) ;
106
+ async_opts . onSuccess = Some ( Self :: action_succeeded) ;
107
+ async_opts . onFailure = Some ( Self :: action_failed) ;
206
108
207
109
self . action_result = None ;
208
110
let error = unsafe {
209
- ffiasync:: MQTTAsync_connect ( self . handle , & options . options )
111
+ ffiasync:: MQTTAsync_connect ( self . handle , & async_opts )
210
112
} ;
211
113
if error == 0 {
212
114
self . barrier . wait ( ) ;
@@ -396,26 +298,7 @@ impl AsyncClient {
396
298
}
397
299
398
300
pub fn messages ( & mut self ) -> AsyncClientIntoIterator {
399
- AsyncClientIntoIterator {
400
- messages : self . messages . clone ( ) ,
401
- }
402
- }
403
- }
404
-
405
- pub struct AsyncClientIntoIterator {
406
- messages : Arc < Mutex < Vec < Message > > > ,
407
- }
408
-
409
- impl Iterator for AsyncClientIntoIterator {
410
- type Item = Message ;
411
- fn next ( & mut self ) -> Option < Message > {
412
- let mut messages = self . messages . lock ( ) . unwrap ( ) ;
413
- if messages. len ( ) > 0 {
414
- Some ( messages. remove ( 0 ) )
415
- }
416
- else {
417
- None
418
- }
301
+ AsyncClientIntoIterator :: new ( self . messages . clone ( ) )
419
302
}
420
303
}
421
304
@@ -424,70 +307,3 @@ impl Drop for AsyncClient {
424
307
unsafe { ffiasync:: MQTTAsync_destroy ( & mut self . handle ) } ;
425
308
}
426
309
}
427
-
428
- pub struct AsyncConnectOptions {
429
- options : ffiasync:: MQTTAsync_connectOptions ,
430
-
431
- pub keep_alive_interval : i32 ,
432
- pub cleansession : i32 ,
433
- pub max_in_flight : i32 ,
434
- pub connect_timeout : i32 ,
435
- pub retry_interval : i32 ,
436
- }
437
- impl AsyncConnectOptions {
438
- pub fn new ( ) -> AsyncConnectOptions {
439
- let ffioptions = ffiasync:: MQTTAsync_connectOptions {
440
- struct_id : [ 'M' as i8 , 'Q' as i8 , 'T' as i8 , 'C' as i8 ] ,
441
- struct_version : 3 ,
442
- keepAliveInterval : 60 ,
443
- cleansession : 1 ,
444
- maxInflight : 10 ,
445
- will : ptr:: null_mut ( ) ,
446
- username : ptr:: null_mut ( ) ,
447
- password : ptr:: null_mut ( ) ,
448
- connectTimeout : 30 ,
449
- retryInterval : 0 ,
450
- ssl : ptr:: null_mut ( ) ,
451
- onSuccess : None ,
452
- onFailure : None ,
453
- context : ptr:: null_mut ( ) ,
454
- serverURIcount : 0 ,
455
- serverURIs : ptr:: null_mut ( ) ,
456
- MQTTVersion : 0 ,
457
- } ;
458
-
459
- let options = AsyncConnectOptions {
460
- options : ffioptions,
461
-
462
- keep_alive_interval : 20 ,
463
- cleansession : 1 ,
464
- max_in_flight : 10 ,
465
- connect_timeout : 30 ,
466
- retry_interval : 0 ,
467
- } ;
468
-
469
- options
470
- }
471
- }
472
-
473
- // it is going to be used in the future
474
- // just silence warning for now
475
- #[ allow( dead_code) ]
476
- pub struct AsyncDisconnectOptions {
477
- options : ffiasync:: MQTTAsync_disconnectOptions ,
478
- }
479
- impl AsyncDisconnectOptions {
480
-
481
- pub fn new ( ) -> ffiasync:: MQTTAsync_disconnectOptions {
482
- let options = ffiasync:: MQTTAsync_disconnectOptions {
483
- struct_id : [ 'M' as i8 , 'Q' as i8 , 'T' as i8 , 'D' as i8 ] ,
484
- struct_version : 0 ,
485
- timeout : 0 ,
486
- onSuccess : ptr:: null_mut ( ) ,
487
- onFailure : ptr:: null_mut ( ) ,
488
- context : ptr:: null_mut ( ) ,
489
- } ;
490
-
491
- options
492
- }
493
- }
0 commit comments