sylvia_iot_broker/libs/mq/
mod.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
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
//! To management queues for applications and networks.
//!
//! For applications, the [`application::ApplicationMgr`] manages the following kind of queues:
//! - uldata: uplink data from the broker to the application.
//! - dldata: downlink data from the application to the broker.
//! - dldata-resp: the response of downlink data.
//! - dldata-result: the data process result from the network.
//!
//! For networks, the [`network::NetworkMgr`] manages the following kind of queues:
//! - uldata: device uplink data from the network to the broker.
//! - dldata: downlink data from the broker to the network.
//! - dldata-result: the data process result from the network.

use std::{
    collections::HashMap,
    error::Error as StdError,
    sync::{Arc, Mutex},
};

use serde::{Deserialize, Serialize};
use url::Url;

use general_mq::{
    connection::GmqConnection, queue::Status, AmqpConnection, AmqpConnectionOptions,
    AmqpQueueOptions, MqttConnection, MqttConnectionOptions, MqttQueueOptions, Queue, QueueOptions,
};

pub mod application;
pub mod control;
pub mod data;
pub mod network;

/// The general connection type with reference counter for upper layer maintenance.
#[derive(Clone)]
pub enum Connection {
    Amqp(AmqpConnection, Arc<Mutex<isize>>),
    Mqtt(MqttConnection, Arc<Mutex<isize>>),
}

/// Manager status.
#[derive(PartialEq)]
pub enum MgrStatus {
    /// One or more queues are not connected.
    NotReady,
    /// All queues are connected.
    Ready,
}

/// Detail queue connection status.
pub struct MgrMqStatus {
    /// For `uldata`.
    pub uldata: Status,
    /// For `dldata`.
    pub dldata: Status,
    /// For `dldata-resp`.
    pub dldata_resp: Status,
    /// For `dldata-result`.
    pub dldata_result: Status,
    /// For `ctrl`.
    pub ctrl: Status,
}

/// The options of the application/network manager.
#[derive(Default, Deserialize, Serialize)]
pub struct Options {
    /// The associated unit ID of the application/network. Empty for public network.
    #[serde(rename = "unitId")]
    pub unit_id: String,
    /// The associated unit code of the application/network. Empty for public network.
    #[serde(rename = "unitCode")]
    pub unit_code: String,
    /// The associated application/network ID.
    pub id: String,
    /// The associated application/network code.
    pub name: String,
    /// AMQP prefetch option.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prefetch: Option<u16>,
    pub persistent: bool,
    /// MQTT shared queue prefix option.
    #[serde(rename = "sharedPrefix", skip_serializing_if = "Option::is_none")]
    pub shared_prefix: Option<String>,
}

/// Support application/network host schemes.
pub const SUPPORT_SCHEMES: &'static [&'static str] = &["amqp", "amqps", "mqtt", "mqtts"];

/// The default prefetch value for AMQP.
const DEF_PREFETCH: u16 = 100;

impl Copy for MgrStatus {}

impl Clone for MgrStatus {
    fn clone(&self) -> MgrStatus {
        *self
    }
}

/// Utility function to get the message queue connection instance. A new connection will be created
/// if the host does not exist.
fn get_connection(
    conn_pool: &Arc<Mutex<HashMap<String, Connection>>>,
    host_uri: &Url,
) -> Result<Connection, String> {
    let uri = host_uri.to_string();
    let mut mutex = conn_pool.lock().unwrap();
    if let Some(conn) = mutex.get(&uri) {
        return Ok(conn.clone());
    }

    match host_uri.scheme() {
        "amqp" | "amqps" => {
            let opts = AmqpConnectionOptions {
                uri: host_uri.to_string(),
                ..Default::default()
            };
            let mut conn = AmqpConnection::new(opts)?;
            let _ = conn.connect();
            let conn = Connection::Amqp(conn, Arc::new(Mutex::new(0)));
            mutex.insert(uri, conn.clone());
            Ok(conn)
        }
        "mqtt" | "mqtts" => {
            let opts = MqttConnectionOptions {
                uri: host_uri.to_string(),
                ..Default::default()
            };
            let mut conn = MqttConnection::new(opts)?;
            let _ = conn.connect();
            let conn = Connection::Mqtt(conn, Arc::new(Mutex::new(0)));
            mutex.insert(uri, conn.clone());
            Ok(conn)
        }
        s => Err(format!("unsupport scheme {}", s)),
    }
}

/// Utility function to remove connection from the pool if the reference count meet zero.
async fn remove_connection(
    conn_pool: &Arc<Mutex<HashMap<String, Connection>>>,
    host_uri: &String,
    count: isize,
) -> Result<(), Box<dyn StdError + Send + Sync>> {
    let conn = {
        let mut mutex = conn_pool.lock().unwrap();
        match mutex.get(host_uri) {
            None => return Ok(()),
            Some(conn) => match conn {
                Connection::Amqp(_, counter) => {
                    let mut mutex = counter.lock().unwrap();
                    *mutex -= count;
                    if *mutex > 0 {
                        return Ok(());
                    }
                }
                Connection::Mqtt(_, counter) => {
                    let mut mutex = counter.lock().unwrap();
                    *mutex -= count;
                    if *mutex > 0 {
                        return Ok(());
                    }
                }
            },
        }
        mutex.remove(host_uri)
    };
    if let Some(conn) = conn {
        match conn {
            Connection::Amqp(mut conn, _) => {
                conn.close().await?;
            }
            Connection::Mqtt(mut conn, _) => {
                conn.close().await?;
            }
        }
    }
    Ok(())
}

/// The utility function for creating application/network control queue with the following name:
/// - `[prefix].[unit].[code].ctrl`
fn new_ctrl_queues(
    conn: &Connection,
    opts: &Options,
    prefix: &str,
) -> Result<Arc<Mutex<Queue>>, String> {
    let ctrl: Arc<Mutex<Queue>>;

    if opts.unit_id.len() == 0 {
        if opts.unit_code.len() != 0 {
            return Err("unit_id and unit_code must both empty or non-empty".to_string());
        }
    } else {
        if opts.unit_code.len() == 0 {
            return Err("unit_id and unit_code must both empty or non-empty".to_string());
        }
    }
    if opts.id.len() == 0 {
        return Err("`id` cannot be empty".to_string());
    }
    if opts.name.len() == 0 {
        return Err("`name` cannot be empty".to_string());
    }

    let unit = match opts.unit_code.len() {
        0 => "_",
        _ => opts.unit_code.as_str(),
    };

    match conn {
        Connection::Amqp(conn, _) => {
            let prefetch = match opts.prefetch {
                None => DEF_PREFETCH,
                Some(prefetch) => match prefetch {
                    0 => DEF_PREFETCH,
                    _ => prefetch,
                },
            };

            let ctrl_opts = QueueOptions::Amqp(
                AmqpQueueOptions {
                    name: format!("{}.{}.{}.ctrl", prefix, unit, opts.name.as_str()),
                    is_recv: false,
                    reliable: true,
                    broadcast: false,
                    prefetch,
                    ..Default::default()
                },
                conn,
            );
            ctrl = Arc::new(Mutex::new(Queue::new(ctrl_opts)?));
        }
        Connection::Mqtt(conn, _) => {
            let ctrl_opts = QueueOptions::Mqtt(
                MqttQueueOptions {
                    name: format!("{}.{}.{}.ctrl", prefix, unit, opts.name.as_str()),
                    is_recv: false,
                    reliable: true,
                    broadcast: false,
                    shared_prefix: opts.shared_prefix.clone(),
                    ..Default::default()
                },
                conn,
            );
            ctrl = Arc::new(Mutex::new(Queue::new(ctrl_opts)?));
        }
    }

    Ok(ctrl)
}

/// The utility function for creating application/network data queues. The return tuple contains:
/// - `[prefix].[unit].[code].uldata`
/// - `[prefix].[unit].[code].dldata`
/// - `[prefix].[unit].[code].dldata-resp`: `Some` for applications and `None` for networks.
/// - `[prefix].[unit].[code].dldata-result`
fn new_data_queues(
    conn: &Connection,
    opts: &Options,
    prefix: &str,
    is_network: bool,
) -> Result<
    (
        Arc<Mutex<Queue>>,
        Arc<Mutex<Queue>>,
        Option<Arc<Mutex<Queue>>>,
        Arc<Mutex<Queue>>,
    ),
    String,
> {
    let uldata: Arc<Mutex<Queue>>;
    let dldata: Arc<Mutex<Queue>>;
    let dldata_resp: Option<Arc<Mutex<Queue>>>;
    let dldata_result: Arc<Mutex<Queue>>;

    if opts.unit_id.len() == 0 {
        if opts.unit_code.len() != 0 {
            return Err("unit_id and unit_code must both empty or non-empty".to_string());
        }
    } else {
        if opts.unit_code.len() == 0 {
            return Err("unit_id and unit_code must both empty or non-empty".to_string());
        }
    }
    if opts.id.len() == 0 {
        return Err("`id` cannot be empty".to_string());
    }
    if opts.name.len() == 0 {
        return Err("`name` cannot be empty".to_string());
    }

    let unit = match opts.unit_code.len() {
        0 => "_",
        _ => opts.unit_code.as_str(),
    };

    match conn {
        Connection::Amqp(conn, _) => {
            let prefetch = match opts.prefetch {
                None => DEF_PREFETCH,
                Some(prefetch) => match prefetch {
                    0 => DEF_PREFETCH,
                    _ => prefetch,
                },
            };

            let uldata_opts = QueueOptions::Amqp(
                AmqpQueueOptions {
                    name: format!("{}.{}.{}.uldata", prefix, unit, opts.name.as_str()),
                    is_recv: is_network,
                    reliable: true,
                    persistent: opts.persistent,
                    broadcast: false,
                    prefetch,
                    ..Default::default()
                },
                conn,
            );
            let dldata_opts = QueueOptions::Amqp(
                AmqpQueueOptions {
                    name: format!("{}.{}.{}.dldata", prefix, unit, opts.name.as_str()),
                    is_recv: !is_network,
                    reliable: true,
                    persistent: opts.persistent,
                    broadcast: false,
                    prefetch,
                    ..Default::default()
                },
                conn,
            );
            let dldata_resp_opts = QueueOptions::Amqp(
                AmqpQueueOptions {
                    name: format!("{}.{}.{}.dldata-resp", prefix, unit, opts.name.as_str()),
                    is_recv: is_network,
                    reliable: true,
                    persistent: opts.persistent,
                    broadcast: false,
                    prefetch,
                    ..Default::default()
                },
                conn,
            );
            let dldata_result_opts = QueueOptions::Amqp(
                AmqpQueueOptions {
                    name: format!("{}.{}.{}.dldata-result", prefix, unit, opts.name.as_str()),
                    is_recv: is_network,
                    reliable: true,
                    persistent: opts.persistent,
                    broadcast: false,
                    prefetch,
                    ..Default::default()
                },
                conn,
            );
            uldata = Arc::new(Mutex::new(Queue::new(uldata_opts)?));
            dldata = Arc::new(Mutex::new(Queue::new(dldata_opts)?));
            dldata_resp = match is_network {
                false => Some(Arc::new(Mutex::new(Queue::new(dldata_resp_opts)?))),
                true => None,
            };
            dldata_result = Arc::new(Mutex::new(Queue::new(dldata_result_opts)?));
        }
        Connection::Mqtt(conn, _) => {
            let uldata_opts = QueueOptions::Mqtt(
                MqttQueueOptions {
                    name: format!("{}.{}.{}.uldata", prefix, unit, opts.name.as_str()),
                    is_recv: is_network,
                    reliable: true,
                    broadcast: false,
                    shared_prefix: opts.shared_prefix.clone(),
                    ..Default::default()
                },
                conn,
            );
            let dldata_opts = QueueOptions::Mqtt(
                MqttQueueOptions {
                    name: format!("{}.{}.{}.dldata", prefix, unit, opts.name.as_str()),
                    is_recv: !is_network,
                    reliable: true,
                    broadcast: false,
                    shared_prefix: opts.shared_prefix.clone(),
                    ..Default::default()
                },
                conn,
            );
            let dldata_resp_opts = QueueOptions::Mqtt(
                MqttQueueOptions {
                    name: format!("{}.{}.{}.dldata-resp", prefix, unit, opts.name.as_str()),
                    is_recv: is_network,
                    reliable: true,
                    broadcast: false,
                    shared_prefix: opts.shared_prefix.clone(),
                    ..Default::default()
                },
                conn,
            );
            let dldata_result_opts = QueueOptions::Mqtt(
                MqttQueueOptions {
                    name: format!("{}.{}.{}.dldata-result", prefix, unit, opts.name.as_str()),
                    is_recv: is_network,
                    reliable: true,
                    broadcast: false,
                    shared_prefix: opts.shared_prefix.clone(),
                    ..Default::default()
                },
                conn,
            );
            uldata = Arc::new(Mutex::new(Queue::new(uldata_opts)?));
            dldata = Arc::new(Mutex::new(Queue::new(dldata_opts)?));
            dldata_resp = match is_network {
                false => Some(Arc::new(Mutex::new(Queue::new(dldata_resp_opts)?))),
                true => None,
            };
            dldata_result = Arc::new(Mutex::new(Queue::new(dldata_result_opts)?));
        }
    }

    Ok((uldata, dldata, dldata_resp, dldata_result))
}