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
//! Web interface to view and control running services
//!
//! Uses the Iron web framework, Handlebars templates, and Twitter Boostrap.

extern crate iron;
extern crate handlebars_iron as hbs;
extern crate staticfile;
extern crate mount;
extern crate router;
extern crate hyper;
extern crate rustc_serialize as serialize;
extern crate websocket;

use std::io::Write;
use std::path::Path;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::{channel, Sender};
use std::collections::BTreeMap;
use super::comms::{Controllable, CmdFrom};
use self::iron::prelude::*;
use self::iron::{status, typemap};
use self::iron::middleware::Handler;
use self::hbs::{Template, HandlebarsEngine, Watchable};
use self::serialize::json::{ToJson, Json};
use self::staticfile::Static;
use self::mount::Mount;
use self::router::Router;
use self::hyper::server::Listening;
use self::websocket::Server;

/// Service descriptor
///
/// Unlike the one in main.rs, this descriptor only needs to contain things that are useful for
/// display in the interface. However, they should probably be unified (TODO). The "web descriptor"
/// could be just a subfield of the main.rs service descriptor, and then those could get passed in
/// here (somehow).
struct Service {
    name: String,
    shortname: String,
    extra: String
}

impl Service {
    /// Create a new service descriptor with the given name
    fn new(s: &str, t: &str, e: &str) -> Service {
        Service { name: s.to_string(), shortname: t.to_string(), extra: e.to_string() }
    }
}

macro_rules! jsonize {
    ($map:ident, $selph:ident, $var:ident) => {{
        $map.insert(stringify!($var).to_string(), $selph.$var.to_json())
    }};
    ($map:ident, $selph:ident; $($var:ident),+) => {{
        $(jsonize!($map, $selph, $var));+
    }}
}

impl ToJson for Service {
    fn to_json(&self) -> Json {
        let mut m: BTreeMap<String, Json> = BTreeMap::new();
        jsonize!(m, self; name, shortname, extra);
        m.to_json()
    }
}

/// Make a path relative to the current file's directory
fn relpath(path: &str) -> String {
    String::from(Path::new(file!()).parent().unwrap().join(path).to_str().unwrap())
}

/// Handler for the main page of the web interface
fn index(req: &mut Request) -> IronResult<Response> {
    let mut data = BTreeMap::<String, Json>::new();
    data.insert("services".to_string(), vec![ Service::new("Structure Sensor", "structure", "<img id=\"structure\" class=\"latest\" src=\"img/structure_latest.png\" />"),
                                              Service::new("mvBlueFOX3",       "bluefox"  , "<img id=\"bluefox\" class=\"latest\" src=\"img/bluefox_latest.png\" />"),
                                              Service::new("OptoForce",        "optoforce", ""),
                                              Service::new("SynTouch BioTac",  "biotac"   , ""),
                                            ].to_json());

    let mut resp = Response::new();
    resp.set_mut(Template::new("index", data)).set_mut(status::Ok);
    Ok(resp)
}

/// Handler for starting/stopping a service
fn control(tx: Sender<CmdFrom>) -> Box<Handler> {
    let mtx = Mutex::new(tx);
    Box::new(move |req: &mut Request| -> IronResult<Response> {
        let params = req.extensions.get::<Router>().unwrap();
        let service = params.find("service").unwrap();
        let action = params.find("action").unwrap();

        match action {
            "start" =>
                if rpc!(mtx.lock().unwrap(), CmdFrom::Start, service.to_string()).unwrap() {
                    Ok(Response::with((status::Ok, format!("Started {}", service))))
                } else {
                    Ok(Response::with((status::InternalServerError, format!("Failed to start {}", service))))
                },
            "stop" =>
                if rpc!(mtx.lock().unwrap(), CmdFrom::Stop, service.to_string()).unwrap() {
                    Ok(Response::with((status::Ok, format!("Stopped {}", service))))
                } else {
                    Ok(Response::with((status::InternalServerError, format!("Failed to stop {}", service))))
                },
            _ => Ok(Response::with((status::BadRequest, format!("What does {} mean?", action))))
        }
    })
}

/// Controllable struct for the web server
pub struct Web<'a> {
    /// Private handle to the HTTP server
    listening: Listening,

    /// Private handle to the websocket server
    websocket: Server<'a>,
}

impl<'a> Controllable for Web<'a> {
    fn setup(tx: Sender<CmdFrom>) -> Web<'a> {
        let mut mount = Mount::new();
        for p in ["css", "fonts", "js", "img"].iter() {
            mount.mount(&format!("/{}/", p),
                        Static::new(Path::new(&relpath("bootstrap")).join(p)));
        }

        let mut router = Router::new();
        router.get("/", index);
        router.post("/control/:service/:action", control(tx));

        mount.mount("/", router);

        let mut chain = Chain::new(mount);

        let watcher = Arc::new(HandlebarsEngine::new(&relpath("templates"), ".hbs"));
        watcher.watch();

        chain.link_after(watcher);

        let listening = Iron::new(chain).http("0.0.0.0:3000").unwrap();

        let ws = Server::bind("0.0.0.0:3001").unwrap();

        Web { listening: listening, websocket: ws }
    }

    fn step(&mut self) -> bool {
        true
    }
    
    fn teardown(&mut self) {
        self.listening.close().unwrap(); // FIXME this does not do anything (known bug in hyper)
        // FIXME no way to close the websocket server?
    }
}