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
188
189
190
191
192
193
use anyhow::Result;
use nanoserde::{DeJson, SerJson};
use std::{
    net::{TcpListener, TcpStream},
    str::FromStr,
};
use tiny_http::{Header, Response as HttpResponse, Server};
use tuna::Tuneable;
use tungstenite::{accept, WebSocket};

use include_dir::{include_dir, Dir};

static PROJECT_DIR: Dir = include_dir!("html");

fn content_type(url: &str) -> Option<Header> {
    if url.ends_with(".js") {
        return Header::from_str("Content-Type: application/javascript; charset=UTF=8").ok();
    }

    if url.ends_with(".css") {
        return Header::from_str("Content-Type: text/css; charset=UTF=8").ok();
    }

    if url.ends_with(".html") {
        return Header::from_str("Content-Type: text/html; charset=UTF=8").ok();
    }

    None
}

#[derive(DeJson, SerJson, Debug)]
enum TunaMessage {
    ListAll,
    Tuneables(tuna::TunaState),
    Delta((String, String, Tuneable)),
    Ok((String, String)),
}

struct TunaClient {
    websocket: WebSocket<TcpStream>,
}

impl TunaClient {
    fn new(stream: TcpStream) -> Result<Self> {
        let websocket = accept(stream)?;

        Ok(Self { websocket })
    }

    fn poll(&mut self) -> bool {
        let msg = self.websocket.read_message().unwrap();

        if msg.is_text() {
            let contents = msg.into_text().unwrap();
            let message: TunaMessage = match DeJson::deserialize_json(&contents) {
                Ok(v) => v,
                Err(e) => {
                    log::error!("failed deserialization: {}, {}", e, contents);
                    return true;
                }
            };

            match message {
                TunaMessage::ListAll => {
                    let state = tuna::TUNA_STATE.read();
                    let res = TunaMessage::Tuneables((*state).clone());

                    let response = SerJson::serialize_json(&res);
                    self.websocket
                        .write_message(tungstenite::Message::Text(response))
                        .unwrap();
                }

                TunaMessage::Delta((category, name, tuneable)) => {
                    tuneable.apply_to(&category, &name);

                    let response = SerJson::serialize_json(&TunaMessage::Ok((category, name)));
                    self.websocket
                        .write_message(tungstenite::Message::Text(response))
                        .unwrap();
                }
                TunaMessage::Tuneables(_) | TunaMessage::Ok((_, _)) => {
                    panic!("unexpected message kind")
                }
            }
        } else if msg.is_close() {
            return false;
        } else {
            log::error!("received non-string message: {:?}", msg);
        }

        true
    }
}

/// The server to tuna web. Will deal with both serving of HTTP content and the
/// websockets used for management.
pub struct TunaServer {
    server: TcpListener,
    http_server: Server,
}

impl TunaServer {
    /// Create a new Tuna Web server. Will serve HTTP on the specified port, and
    /// websocket traffic on the subsequent port (`port + 1`).
    pub fn new(port: u16) -> anyhow::Result<Self> {
        let http_server = Server::http(("0.0.0.0", port))
            .map_err(|e| anyhow::format_err!("http server error: {}", e))?;

        let server = TcpListener::bind(("127.0.0.1", port + 1))?;

        server.set_nonblocking(true)?;

        Ok(Self {
            server,
            http_server,
        })
    }

    /// Update the HTTP server, draining the request queue before returning.
    pub fn work_http(&mut self) {
        loop {
            match self.http_server.try_recv() {
                Ok(Some(req)) => {
                    log::debug!("request: {:#?}", req);
                    let response = match req.url() {
                        "/" => HttpResponse::from_string(
                            PROJECT_DIR
                                .get_file("index.html")
                                .unwrap()
                                .contents_utf8()
                                .unwrap(),
                        )
                        .with_status_code(200)
                        .with_header(content_type(".html").unwrap()),
                        _ => match PROJECT_DIR.get_file(&req.url()[1..]) {
                            Some(contents) => {
                                HttpResponse::from_string(contents.contents_utf8().unwrap())
                                    .with_status_code(200)
                                    .with_header(content_type(req.url()).unwrap())
                            }
                            _ => HttpResponse::from_string("not found").with_status_code(404),
                        },
                    };

                    let _ = req.respond(response);
                }
                Ok(None) => {
                    break;
                }
                Err(e) => {
                    log::error!("Http Error: {:?}", e);
                    break;
                }
            }
        }
    }

    /// Update the websocket server, draining the connection queue before returning.
    pub fn work_websocket(&mut self) {
        loop {
            match self.server.accept() {
                Ok((stream, addr)) => {
                    log::debug!("New Tuna client from: {:?}", addr);

                    match TunaClient::new(stream) {
                        Ok(mut client) => {
                            std::thread::spawn(move || loop {
                                if !client.poll() {
                                    break;
                                }
                            });
                        }
                        Err(e) => log::error!("failed to accept client: {}", e),
                    }
                }
                Err(e) if e.kind() != std::io::ErrorKind::WouldBlock => {
                    log::error!("Error during accept: {:?}", e);
                    break;
                }
                _ => {
                    break;
                }
            }
        }
    }
    /// Update all connections and servers in one go. Note that the clients will
    /// not be polled here as they are blocking.
    pub fn loop_once(&mut self) {
        self.work_http();
        self.work_websocket();
    }
}