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
use std::io::Write;
use std::fmt::Display;

use time::get_time;
use num::Num;
use rotor_stream::ActiveStream;

use {Sender};

impl<'a, C: 'a, S: ActiveStream> Sender<'a, C, S> {
    /// Send a generic number with current timestamp
    pub fn add_value<N, V>(&mut self, name: N, value: V)
        where N: Display, V: Num + Display
    {
        // Unfortunately we skip everything if connection is not established
        // we may consider to buffer things in future
        self.0.transport().map(|mut trans| {
            let ts = get_time().sec;
            let mut buf = trans.output();
            let offset = buf.len();
            writeln!(buf, "{} {} {}", name, value, ts).unwrap();
            debug_assert!(!buf[offset..buf.len()-1].contains(&b'\n'));
        });
    }
    /// Send a generic number with specific timestamp
    pub fn add_value_at<N, V>(&mut self, name: N, value: V, ts: u64)
        where N: Display, V: Num + Display
    {
        // Unfortunately we skip everything if connection is not established
        // we may consider to buffer things in future
        self.0.transport().map(|mut trans| {
            let mut buf = trans.output();
            let offset = buf.len();
            writeln!(buf, "{} {} {}", name, value, ts).unwrap();
            debug_assert!(!buf[offset..buf.len()-1].contains(&b'\n'));
        });
    }
}

impl<'a, C, S: ActiveStream> Drop for Sender<'a, C, S> {
    fn drop(&mut self) {
        self.1.as_mut().map(|x| x.wakeup().unwrap());
    }
}