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
use std::net::TcpStream;
use std::os::unix::io::AsRawFd;
use std::io::Write;
use std::sync::mpsc::Sender;
use std::thread;
use std::net::ToSocketAddrs;

use model::SonicMessage;
use error::Result;
use io::*;

// TODO rewrite using mio and futures
fn send_cmd(stream: &mut TcpStream, cmd: SonicMessage) -> Result<()> {

    debug!("framing command {:?}", &cmd);

    // frame command
    let fbytes = try!(frame(cmd));

    debug!("framed command into {} bytes", fbytes.len());

    // send query
    try!(stream.write(&fbytes.as_slice()));

    Ok(())
}

pub fn stream<A>(addr: A, cmd: SonicMessage, tx: Sender<Result<SonicMessage>>) -> Result<()>
    where A: ToSocketAddrs
{

    let mut stream = try!(TcpStream::connect(addr));

    // set timeout 10s
    try!(stream.set_read_timeout(Some(::std::time::Duration::new(10, 0))));

    // FIXME when librux I/O layer is ready
    thread::spawn(move || {

        let tx = tx.clone();

        match send_cmd(&mut stream, cmd) {
            Ok(_) => {

                let fd = stream.as_raw_fd();

                loop {
                    match read_message(fd) {
                        o @ Ok(_) => tx.send(o).unwrap(),
                        e @ Err(_) => {
                            tx.send(e).unwrap();
                            break;
                        }
                    }
                }
            }
            Err(e) => {
                tx.send(Err(e)).unwrap();
            }
        }
    });

    Ok(())
}