Struct Connection

Source
pub struct Connection<C: Connectioner> { /* private fields */ }

Implementations§

Source§

impl<C: Connectioner> Connection<C>

Source

pub fn new(connection: C) -> Self

Examples found in repository?
examples/dump-events.rs (line 20)
14fn main() -> std::io::Result<()> {
15    let args: Vec<String> = env::args().collect();
16    let host = &args[1];
17    let event = &args[2];
18    let mut stream = TcpStream::connect(host)?;
19
20    let conn = Connection::new(stream);
21    let mut client = Client::new(conn);
22    let mut stats = Stats{
23        event_per_tick: 0,
24        events_counts_per_sec: HashMap::new()
25    };
26
27    client.auth("cloudpbx").expect("fails to authenticate");
28    client.event(event).expect("fails enabling events");
29
30    let mut now = std::time::Instant::now();
31    let notify_on_secs = std::time::Duration::from_secs(1);
32
33    loop {
34        let event: Event = client.pull_event().unwrap();
35        let event_name = event.get("Event-Name").unwrap().to_string();
36
37        stats.event_per_tick += 1;
38        if event_name == "CUSTOM" {
39            let event_subclass = event.get("Event-Subclass").unwrap().to_string();
40            let mut event_name = format!("{}:{}", event_name, event_subclass);
41            if let Some(action) = event.get("CC-Action") {
42                event_name = format!("{}:{}", event_name, action.to_string())
43            }
44            if let Some(counter) = stats.events_counts_per_sec.get_mut(&event_name) {
45                *counter += 1;
46            } else {
47                stats.events_counts_per_sec.insert(event_name, 1);
48            }
49        } else {
50            if let Some(counter) = stats.events_counts_per_sec.get_mut(&event_name) {
51                *counter += 1;
52            } else {
53                stats.events_counts_per_sec.insert(event_name, 1);
54            }
55        }
56
57        if now.elapsed() >= notify_on_secs {
58            let stamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
59            println!("{} {:?} {:?}", stamp.as_secs(), now.elapsed(), stats);
60
61            now = std::time::Instant::now();
62            stats.event_per_tick = 0;
63            stats.events_counts_per_sec.clear();
64        }
65    }
66    
67    Ok(())
68}
More examples
Hide additional examples
examples/stats-events.rs (line 20)
14fn main() -> std::io::Result<()> {
15    let args: Vec<String> = env::args().collect();
16    let host = &args[1];
17    let event = &args[2];
18    let mut stream = TcpStream::connect(host)?;
19
20    let conn = Connection::new(stream);
21    let mut client = Client::new(conn);
22    let mut stats = Stats{
23        event_per_tick: 0,
24        events_counts_per_tick: HashMap::new()
25    };
26
27    client.auth("cloudpbx").expect("fails to authenticate");
28    client.event(event).expect("fails enabling events");
29
30    let mut now = std::time::Instant::now();
31    let notify_on_secs = std::time::Duration::from_secs(1);
32
33    loop {
34        let event: Event = client.pull_event().unwrap();
35        let event_name = event.get("Event-Name").unwrap().to_string();
36
37        stats.event_per_tick += 1;
38        if event_name == "CUSTOM" {
39            let event_subclass = event.get("Event-Subclass").unwrap().to_string();
40            let mut event_name = format!("{}:{}", event_name, event_subclass);
41            if let Some(action) = event.get("CC-Action") {
42                event_name = format!("{}:{}", event_name, action.to_string())
43            }
44            if let Some(counter) = stats.events_counts_per_tick.get_mut(&event_name) {
45                *counter += 1;
46            } else {
47                stats.events_counts_per_tick.insert(event_name, 1);
48            }
49        } else {
50            if let Some(counter) = stats.events_counts_per_tick.get_mut(&event_name) {
51                *counter += 1;
52            } else {
53                stats.events_counts_per_tick.insert(event_name, 1);
54            }
55        }
56
57        if now.elapsed() >= notify_on_secs {
58            let stamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
59            println!("{} {:?} {:?}", stamp.as_secs(), now.elapsed(), stats);
60
61            now = std::time::Instant::now();
62            stats.event_per_tick = 0;
63            stats.events_counts_per_tick.clear();
64        }
65    }
66    
67    Ok(())
68}

Auto Trait Implementations§

§

impl<C> Freeze for Connection<C>
where C: Freeze,

§

impl<C> RefUnwindSafe for Connection<C>
where C: RefUnwindSafe,

§

impl<C> Send for Connection<C>
where C: Send,

§

impl<C> Sync for Connection<C>
where C: Sync,

§

impl<C> Unpin for Connection<C>
where C: Unpin,

§

impl<C> UnwindSafe for Connection<C>
where C: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.