elbus/
common.rs

1#[cfg(feature = "rpc")]
2use crate::Error;
3#[cfg(feature = "rpc")]
4use serde::{Deserialize, Serialize};
5#[cfg(feature = "rpc")]
6use serde_value::Value;
7#[cfg(feature = "rpc")]
8use std::collections::HashMap;
9
10#[cfg_attr(feature = "rpc", derive(Serialize, Deserialize))]
11#[derive(Eq, PartialEq, Clone)]
12pub struct ClientInfo<'a> {
13    pub name: &'a str,
14    pub kind: &'a str,
15    pub source: Option<&'a str>,
16    pub port: Option<&'a str>,
17    pub r_frames: u64,
18    pub r_bytes: u64,
19    pub w_frames: u64,
20    pub w_bytes: u64,
21    pub queue: usize,
22    pub instances: usize,
23}
24impl<'a> Ord for ClientInfo<'a> {
25    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
26        self.name.cmp(other.name)
27    }
28}
29impl<'a> PartialOrd for ClientInfo<'a> {
30    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
31        Some(self.cmp(other))
32    }
33}
34#[cfg_attr(feature = "rpc", derive(Serialize, Deserialize))]
35#[derive(Clone)]
36pub struct ClientList<'a> {
37    #[cfg_attr(feature = "rpc", serde(borrow))]
38    pub clients: Vec<ClientInfo<'a>>,
39}
40
41#[cfg_attr(feature = "rpc", derive(Serialize, Deserialize))]
42#[derive(Clone)]
43pub struct BrokerStats {
44    pub uptime: u64,
45    pub r_frames: u64,
46    pub r_bytes: u64,
47    pub w_frames: u64,
48    pub w_bytes: u64,
49}
50
51#[cfg_attr(feature = "rpc", derive(Serialize, Deserialize))]
52#[derive(Clone)]
53pub struct BrokerInfo<'a> {
54    pub author: &'a str,
55    pub version: &'a str,
56}
57
58#[allow(clippy::ptr_arg)]
59#[cfg(feature = "rpc")]
60pub fn str_to_params_map<'a>(s: &'a [&'a str]) -> Result<HashMap<&'a str, Value>, Error> {
61    let mut params: HashMap<&str, Value> = HashMap::new();
62    for pair in s {
63        if !pair.is_empty() {
64            let mut psp = pair.split('=');
65            let var = psp
66                .next()
67                .ok_or_else(|| Error::data("var name not specified"))?;
68            let v = psp
69                .next()
70                .ok_or_else(|| Error::data("var value not specified"))?;
71            let value = if v == "false" {
72                Value::Bool(false)
73            } else if v == "true" {
74                Value::Bool(true)
75            } else if let Ok(i) = v.parse::<i64>() {
76                Value::I64(i)
77            } else if let Ok(f) = v.parse::<f64>() {
78                Value::F64(f)
79            } else {
80                Value::String(v.to_owned())
81            };
82            params.insert(var, value);
83        }
84    }
85    Ok(params)
86}
87
88#[cfg(feature = "broker")]
89#[allow(clippy::cast_sign_loss)]
90/// # Panics
91///
92/// Will panic if system clock is not available
93pub fn now_ns() -> u64 {
94    let t = nix::time::clock_gettime(nix::time::ClockId::CLOCK_REALTIME).unwrap();
95    t.tv_sec() as u64 * 1_000_000_000 + t.tv_nsec() as u64
96}