aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/watch.rs
blob: 4e41371497691d9def65684b0a2cd982f5588af6 (plain) (blame)
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
use futures::future::Future as _;
use futures::stream::Stream as _;
use snafu::ResultExt as _;
use std::io::Write as _;

#[derive(Debug, snafu::Snafu)]
pub enum Error {
    #[snafu(display("failed to connect: {}", source))]
    Connect { source: std::io::Error },

    #[snafu(display("failed to read message: {}", source))]
    Read { source: crate::protocol::Error },

    #[snafu(display("failed to write message: {}", source))]
    Write { source: crate::protocol::Error },

    #[snafu(display("failed to write to terminal: {}", source))]
    WriteTerminal { source: std::io::Error },

    #[snafu(display("communication with server failed: {}", source))]
    Client { source: crate::client::Error },

    #[snafu(display(
        "failed to read message: unexpected message received: {:?}",
        message
    ))]
    UnexpectedMessage { message: crate::protocol::Message },
}

pub type Result<T> = std::result::Result<T, Error>;

pub fn cmd<'a, 'b>(app: clap::App<'a, 'b>) -> clap::App<'a, 'b> {
    app.about("Watch shellshare streams")
        .arg(
            clap::Arg::with_name("address")
                .long("address")
                .takes_value(true),
        )
        .arg(clap::Arg::with_name("id"))
}

pub fn run<'a>(matches: &clap::ArgMatches<'a>) -> super::Result<()> {
    run_impl(
        matches.value_of("address").unwrap_or("127.0.0.1:4144"),
        matches.value_of("id"),
    )
    .context(super::Watch)
}

fn run_impl(address: &str, id: Option<&str>) -> Result<()> {
    if let Some(id) = id {
        watch(address, id)
    } else {
        list(address)
    }
}

fn list(address: &str) -> Result<()> {
    let sock = std::net::TcpStream::connect(address).context(Connect)?;
    let term = std::env::var("TERM").unwrap_or_else(|_| "".to_string());
    let size = crossterm::terminal().terminal_size();
    let msg = crate::protocol::Message::login(
        "doy",
        &term,
        (u32::from(size.0), u32::from(size.1)),
    );
    msg.write(&sock).context(Write)?;

    let msg = crate::protocol::Message::list_sessions();
    msg.write(&sock).context(Write)?;

    let res = crate::protocol::Message::read(&sock).context(Read)?;
    match res {
        crate::protocol::Message::Sessions { sessions } => {
            println!("available sessions:");
            for session in sessions {
                println!(
                    "{}: {}, {}x{}, TERM={}",
                    session.id,
                    session.username,
                    session.size.0,
                    session.size.1,
                    session.term_type
                );
            }
        }
        crate::protocol::Message::Error { msg } => {
            eprintln!("server error: {}", msg);
        }
        _ => {
            return Err(Error::UnexpectedMessage { message: res });
        }
    }

    Ok(())
}

fn watch(address: &str, id: &str) -> Result<()> {
    tokio::run(
        WatchSession::new(
            id,
            address,
            "doy",
            std::time::Duration::from_secs(5),
        )?
        .map_err(|e| {
            eprintln!("{}", e);
        }),
    );

    Ok(())
}

struct WatchSession {
    client: crate::client::Client,
}

impl WatchSession {
    fn new(
        id: &str,
        address: &str,
        username: &str,
        heartbeat_duration: std::time::Duration,
    ) -> Result<Self> {
        let client = crate::client::Client::watch(
            address,
            username,
            heartbeat_duration,
            id,
        );
        Ok(Self { client })
    }
}

impl WatchSession {
    const POLL_FNS: &'static [&'static dyn for<'a> Fn(
        &'a mut Self,
    ) -> Result<
        crate::component_future::Poll<()>,
    >] = &[&Self::poll_read_client];

    fn poll_read_client(
        &mut self,
    ) -> Result<crate::component_future::Poll<()>> {
        match self.client.poll().context(Client)? {
            futures::Async::Ready(Some(e)) => match e {
                crate::client::Event::Reconnect => {
                    Ok(crate::component_future::Poll::DidWork)
                }
                crate::client::Event::ServerMessage(msg) => match msg {
                    crate::protocol::Message::TerminalOutput { data } => {
                        // TODO async
                        let stderr = std::io::stderr();
                        let mut stderr = stderr.lock();
                        stderr.write(&data).context(WriteTerminal)?;
                        Ok(crate::component_future::Poll::DidWork)
                    }
                    crate::protocol::Message::Disconnected => {
                        Ok(crate::component_future::Poll::Event(()))
                    }
                    crate::protocol::Message::Error { msg } => {
                        eprintln!("server error: {}", msg);
                        Ok(crate::component_future::Poll::Event(()))
                    }
                    msg => Err(Error::UnexpectedMessage { message: msg }),
                },
            },
            futures::Async::Ready(None) => {
                // the client should never exit on its own
                unreachable!()
            }
            futures::Async::NotReady => {
                Ok(crate::component_future::Poll::NotReady)
            }
        }
    }
}

#[must_use = "futures do nothing unless polled"]
impl futures::future::Future for WatchSession {
    type Item = ();
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Self::Item, Self::Error> {
        crate::component_future::poll_future(self, Self::POLL_FNS)
    }
}