summaryrefslogtreecommitdiffstats
path: root/src/client/mod.rs
blob: bab833ae29ad7371a26c564de0eaa4429503edf8 (plain)
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
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 2.0. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

pub mod connect;
pub mod publish;
mod receive;
mod state;

use std::sync::Arc;

use futures::lock::Mutex;

use self::publish::Acknowledge;
use self::publish::CallbackState;
use self::publish::ClientHandlers;
use self::publish::Id;
use self::state::ConnectState;
use self::state::SessionState;

struct InnerClient {
    connection_state: Option<ConnectState>,
    session_state: Option<SessionState>,
    default_handlers: ClientHandlers,
    outstanding_completions: std::collections::HashMap<Id, CallbackState>,
}

pub struct MqttClient {
    inner: Arc<Mutex<InnerClient>>,
}

impl MqttClient {
    pub fn new_with_default_handlers() -> MqttClient {
        MqttClient {
            inner: Arc::new(Mutex::new(InnerClient {
                connection_state: None,
                session_state: None,
                default_handlers: ClientHandlers {
                    on_packet_recv: Box::new(|_| ()),
                    handle_acknowledge: Box::new(|_| Acknowledge::Yes),
                },
                outstanding_completions: std::collections::HashMap::new(),
            })),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::client::ClientHandlers;
    use crate::client::MqttClient;

    static_assertions::assert_impl_all!(MqttClient: Send, Sync);
    static_assertions::assert_impl_all!(ClientHandlers: Send);
}