aboutsummaryrefslogtreecommitdiffstats
path: root/teleterm-web/src/model.rs
blob: e4919e4233703e32ffb74236014a0b79d7051c21 (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
use crate::prelude::*;

struct WatchConn {
    ws: WebSocket,
    term: vt100::Parser,
    received_data: bool,
}

impl Drop for WatchConn {
    fn drop(&mut self) {
        self.ws.close().unwrap();
    }
}

pub(crate) struct Model {
    config: crate::config::Config,
    sessions: Vec<crate::protocol::Session>,
    watch_conn: Option<WatchConn>,
}

impl Model {
    pub(crate) fn new(config: crate::config::Config) -> Self {
        Self {
            config,
            sessions: vec![],
            watch_conn: None,
        }
    }

    pub(crate) fn update(
        &mut self,
        msg: crate::Msg,
        orders: &mut impl Orders<crate::Msg>,
    ) {
        match msg {
            crate::Msg::List(sessions) => match sessions {
                Ok(sessions) => {
                    log::debug!("got sessions");
                    self.update_sessions(sessions);
                }
                Err(e) => {
                    log::error!("error getting sessions: {:?}", e);
                }
            },
            crate::Msg::Refresh => {
                log::debug!("refreshing");
                let url =
                    format!("http://{}/list", self.config.public_address);
                orders.perform_cmd(
                    seed::Request::new(url).fetch_json_data(crate::Msg::List),
                );
            }
            crate::Msg::StartWatching(id) => {
                log::debug!("watching {}", id);
                self.watch(&id, orders);
            }
            crate::Msg::Watch(id, event) => match event {
                crate::ws::WebSocketEvent::Connected(_) => {
                    log::info!("{}: connected", id);
                }
                crate::ws::WebSocketEvent::Disconnected(_) => {
                    log::info!("{}: disconnected", id);
                }
                crate::ws::WebSocketEvent::Message(msg) => {
                    log::info!("{}: message: {:?}", id, msg);
                    let json = msg.data().as_string().unwrap();
                    let msg: crate::protocol::Message =
                        serde_json::from_str(&json).unwrap();
                    match msg {
                        crate::protocol::Message::TerminalOutput { data } => {
                            self.process(&data);
                        }
                        crate::protocol::Message::Disconnected => {
                            self.disconnect_watch();
                            orders.send_msg(crate::Msg::Refresh);
                        }
                        crate::protocol::Message::Resize { size } => {
                            self.set_size(size.rows, size.cols);
                        }
                    }
                }
                crate::ws::WebSocketEvent::Error(e) => {
                    log::error!("{}: error: {:?}", id, e);
                }
            },
            crate::Msg::StopWatching => {
                self.disconnect_watch();
                orders.send_msg(crate::Msg::Refresh);
            }
        }
    }

    pub(crate) fn title(&self) -> &str {
        &self.config.title
    }

    pub(crate) fn screen(&self) -> Option<&vt100::Screen> {
        self.watch_conn.as_ref().map(|conn| conn.term.screen())
    }

    pub(crate) fn sessions(&self) -> &[crate::protocol::Session] {
        &self.sessions
    }

    pub(crate) fn watching(&self) -> bool {
        self.watch_conn.is_some()
    }

    pub(crate) fn received_data(&self) -> bool {
        self.watch_conn
            .as_ref()
            .map(|conn| conn.received_data)
            .unwrap_or(false)
    }

    fn watch(&mut self, id: &str, orders: &mut impl Orders<crate::Msg>) {
        let url =
            format!("ws://{}/watch?id={}", self.config.public_address, id);
        let ws = crate::ws::connect(&url, id, crate::Msg::Watch, orders);
        let term = vt100::Parser::default();
        self.watch_conn = Some(WatchConn {
            ws,
            term,
            received_data: false,
        })
    }

    fn update_sessions(&mut self, sessions: Vec<crate::protocol::Session>) {
        self.sessions = sessions;
    }

    fn disconnect_watch(&mut self) {
        self.watch_conn = None;
    }

    fn process(&mut self, bytes: &[u8]) {
        if let Some(conn) = &mut self.watch_conn {
            conn.term.process(bytes);
            conn.received_data = true;
        }
    }

    fn set_size(&mut self, rows: u16, cols: u16) {
        if let Some(conn) = &mut self.watch_conn {
            conn.term.set_size(rows, cols);
        }
    }
}