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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::thread;
use std::fmt;
use std::error::Error;
use std::sync::mpsc;
use std::cell::Cell;
use traits::{Runner, Process, Sender, Receiver, Signal};
use {MaridError};

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
enum ProcState {
    Init,
    SetupDone,
    Finished,
}


#[derive(Debug, Eq, PartialEq, Clone)]
/// Error type for a running Process.
pub enum ProcessError<E> {
    /// The associated runner returned an error which can be found as the enclosed argument.
    RunnerError(E),
    /// The associated result has already been received by a caller.
    ///
    /// This occurs when calling wait or ready more than once.
    ResultAlreadyGiven,
    /// The process was not able to recieve a result from the runner. Something has gone wrong
    /// on the runner's thread.
    CouldNotRecvResult,
}

impl<E: Error> fmt::Display for ProcessError<E> {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ProcessError::RunnerError(ref e) => {
                write!(fmt, "{}", e)
            },
            ProcessError::ResultAlreadyGiven => {
                write!(fmt, "Already returned result to caller")
            },
            ProcessError::CouldNotRecvResult => {
                write!(fmt, "Could not receive result from thread")
            }
        }
    }
}

impl<E: Error> Error for ProcessError<E> {
    fn description(&self) -> &str {
        match *self {
            ProcessError::RunnerError(ref e) => {
                e.description()
            },
            ProcessError::ResultAlreadyGiven => {
                "Already returned result to caller"
            },
            ProcessError::CouldNotRecvResult => {
                "Could not receive result from thread"
            }
        }
    }
}

impl<E: Error> From<E> for ProcessError<E> {
    fn from(err: E) -> ProcessError<E> {
        ProcessError::RunnerError(err)
    }
}

/// Signifying the running state of a unit of work, a MaridProcess will spawn a
/// new thread in order to not block the current thread.
///
/// Upon dropping, an instance of a MaridProcess will join on the running thread,
/// potentially blocking.
pub struct MaridProcess {
    setup_chan: mpsc::Receiver<Result<(), ProcessError<MaridError>>>,
    run_chan: mpsc::Receiver<Result<(), ProcessError<MaridError>>>,

    signaler: Sender<Signal>,
    runner: Option<thread::JoinHandle<()>>,
    state: Cell<ProcState>,
}

// Be aware, ready/wait
impl MaridProcess {
    /// Starts the specified runner with the given signal receiver.
    pub fn start(runner: Box<Runner + Send>, signaler: Sender<Signal>, recv: Receiver<Signal>) -> MaridProcess {
        let (setup_sn, setup_rc) = mpsc::channel();
        let (run_sn, run_rc) = mpsc::channel();

        let handle = MaridProcess::spawn_run_thread(runner, recv, setup_sn, run_sn);

        MaridProcess {
            setup_chan: setup_rc,
            run_chan: run_rc,

            runner: Some(handle),
            signaler: signaler,
            state: Cell::new(ProcState::Init),
        }
    }

    fn spawn_run_thread(mut runner: Box<Runner + Send>,
                           recv: Receiver<Signal>,
                           setup: mpsc::Sender<Result<(), ProcessError<MaridError>>>,
                           run: mpsc::Sender<Result<(), ProcessError<MaridError>>>)
        -> thread::JoinHandle<()> {
        thread::spawn(move || {
            let res = runner.setup().map_err(ProcessError::RunnerError);
            let is_err = res.is_err();
            setup.send(res).expect("Could not send setup result");

            if !is_err {
                let err = runner.run(recv).map_err(ProcessError::RunnerError);
                run.send(err).expect("Could not send run result");
            }
        })
    }
}


impl Process for MaridProcess {
    type Error = ProcessError<MaridError>;

    fn ready(&self) -> Result<(), Self::Error> {
        match self.state.get() {
            ProcState::Init => {
                self.state.set(ProcState::SetupDone);
                self.setup_chan.recv().unwrap_or(Err(ProcessError::CouldNotRecvResult))
            },
            _ => {
                Err(ProcessError::ResultAlreadyGiven)
            }
        }
    }

    fn wait(&self) -> Result<(), Self::Error> {
        match self.state.get() {
            ProcState::Init | ProcState::SetupDone => {
                self.state.set(ProcState::Finished);
                self.run_chan.recv().unwrap_or(Err(ProcessError::CouldNotRecvResult))
            },
            ProcState::Finished => {
                Err(ProcessError::ResultAlreadyGiven)
            }
        }
    }

    fn signal(&self, signal: Signal) {
        self.signaler.send(signal)
    }
}

impl Drop for MaridProcess {
    fn drop(&mut self) {
        let runner = self.runner.take();
        runner.expect("No runner").
            join().expect("Runner panicked");
    }
}

#[cfg(test)]
mod tests {
    use test_helpers::{TestRunner};
    use super::{MaridProcess, ProcessError};
    use traits::{Runner, Process, Signal};
    use chan;

    #[test]
    fn test_ready_process() {
        let (sn, rc) = chan::sync(0);
        let runner = Box::new(TestRunner::new(0, sn)) as Box<Runner + Send>;

        let (signal_sn, signal_rc) = chan::sync(9);
        let process = MaridProcess::start(runner, signal_sn, signal_rc);
        let res = process.ready();
        assert!(res.is_ok());

        let res = process.ready();
        assert!(res.is_err());
        match res {
            Ok(_) => unreachable!(),
            Err(ProcessError::ResultAlreadyGiven) => {},
            _ => assert!(false, "Wrong error type"),
        }

        // Finish workflow
        process.signal(Signal::INT);
        assert!(rc.recv().unwrap());
        assert!(process.wait().is_ok());
    }

    #[test]
    fn test_wait_and_signal_process() {
        let (sn, rc) = chan::sync(0);
        let runner = Box::new(TestRunner::new(0, sn)) as Box<Runner + Send>;

        let (signal_sn, signal_rc) = chan::sync(9);
        let process = MaridProcess::start(runner, signal_sn, signal_rc);
        process.signal(Signal::INT);

        assert!(rc.recv().unwrap()); // rendevous channel goes first
        let res = process.wait();
        assert!(res.is_ok());

        let res = process.wait();
        assert!(res.is_err());
        match res {
            Ok(_) => unreachable!(),
            Err(ProcessError::ResultAlreadyGiven) => {},
            _ => assert!(false, "Wrong error type"),
        }
    }
}