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
// Copyright (C) 2015-2016 Mickaël Salaün
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#![allow(deprecated)]

/// `Request::call(&self, PortalFsmInit)` use `PortalFsm`

use bincode::rustc_serialize::{DecodingResult, EncodingResult};
use getopts::Options;
use self::fsm_kage::KageFsm;
use self::fsm_portal::{PortalFsmInit, PortalFsm};
use srv::{GetDotRequest, ManagerAction};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::sync::mpsc::{Sender, channel};
use unix_socket::UnixStream;

mod fsm_kage;
mod fsm_portal;

#[derive(Debug, RustcDecodable, RustcEncodable)]
pub enum InfoAction {
    GetDot(DotRequest),
}

impl InfoAction {
    pub fn call(&self, stream: UnixStream, manager_tx: Sender<ManagerAction>) -> Result<(), String> {
        match self {
            &InfoAction::GetDot(ref req) => req.call(PortalFsm::new(stream), manager_tx),
        }
    }
}

#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
pub struct DotResponse {
    pub result: Option<String>,
}
impl_encdec!(DotResponse);

#[derive(Clone, Debug, RustcDecodable, RustcEncodable)]
pub struct DotRequest;

impl DotRequest {
    fn call(&self, machine: PortalFsmInit, manager_tx: Sender<ManagerAction>) -> Result<(), String> {
        let (response_tx, response_rx) = channel();
        let action = ManagerAction::GetDot(GetDotRequest {
            response: response_tx,
        });
        // TODO: Add error typing
        match manager_tx.send(action) {
            Ok(()) => {},
            Err(e) => return Err(format!("Failed to send the dot request: {}", e)),
        };
        let dot = match response_rx.recv() {
            Ok(r) => r.dot,
            Err(e) => return Err(format!("Failed to receive the dot response: {}", e)),
        };
        try!(machine.send_dot_response(DotResponse { result: dot }));
        Ok(())
    }
}

pub struct InfoKageCmd {
    name: String,
    opts: Options,
}

impl InfoKageCmd {
    pub fn new() -> InfoKageCmd {
        let mut opts = Options::new();
        opts.optflag("h", "help", "Print this message");
        opts.optflag("d", "dot", "Export the StemFlow graph to DOT");
        opts.optopt("o", "output", "Write the information to a file", "PATH");
        InfoKageCmd {
            name: "info".to_string(),
            opts: opts,
        }
    }

    pub fn do_dot<T>(out: Option<T>) -> Result<(), String> where T: AsRef<Path> {
        let machine = try!(KageFsm::new());
        let machine = try!(machine.send_dot_request(DotRequest));
        let dot = match try!(machine.recv_dot_response()).result {
            Some(dot) => dot,
            None => return Err("Empty graph".into()),
        };
        match out {
            None => {
                println!("{}", dot);
                Ok(())
            }
            Some(path) => match File::create(path) {
                Ok(mut f) => match f.write_all(dot.as_bytes()) {
                    Ok(()) => Ok(()),
                    Err(e) => return Err(format!("Failed to write to the file: {}", e)),
                },
                Err(e) => return Err(format!("Failed to open the file: {}", e)),
            }
        }
    }
}

impl super::KageCommand for InfoKageCmd {
    fn get_name<'a>(&'a self) -> &'a String {
        &self.name
    }

    fn get_usage(&self) -> String {
        let msg = format!("Usage for the {} command", self.name);
        format!("{}", self.opts.usage(msg.as_ref()))
    }

    fn call(&mut self, args: &Vec<String>) -> Result<(), String> {
        let matches = match self.opts.parse(args.as_slice()) {
            Ok(m) => m,
            Err(e) => return Err(format!("{}", e)),
        };
        if matches.opt_present("help") {
            println!("{}", self.get_usage());
            return Ok(());
        }

        if matches.opt_present("dot") {
            check_remaining!(matches);
            return InfoKageCmd::do_dot(matches.opt_str("output"));
        }

        Err("No command".into())
    }
}