diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-01-06 15:29:05 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-01-06 15:29:05 +0100 |
| commit | 3505458bd75381935344354cf0451e5ea13ada3b (patch) | |
| tree | 92f6bc41698bb2d41bf195a02a0c1c32e31d05e2 /src/process | |
| parent | c0f537e06778191fa5af2a115a3edf84c25aa6b9 (diff) | |
| download | mullvadvpn-3505458bd75381935344354cf0451e5ea13ada3b.tar.xz mullvadvpn-3505458bd75381935344354cf0451e5ea13ada3b.zip | |
Add ChildMonitor, the public interface of monitor
Diffstat (limited to 'src/process')
| -rw-r--r-- | src/process/monitor.rs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/process/monitor.rs b/src/process/monitor.rs index 96a747964f..f4b501e630 100644 --- a/src/process/monitor.rs +++ b/src/process/monitor.rs @@ -278,3 +278,45 @@ impl<C: MonitorChild, B: ChildSpawner<C>> Drop for StateMachine<C, B> { drop(self.stop()) } } + +/// A child process monitor. Takes care of starting and monitoring a child process and sends +/// out events about it to a registered lisener. +pub struct ChildMonitor { + state_machine: Sender<MonitorMsg>, +} + +impl ChildMonitor { + /// Creates a new `ChildMonitor` that spawn processes with the given `builder`. The new + /// `ChildMonitor` will be in the stopped state and not start any process until you call + /// `start()` + pub fn new<C: MonitorChild, B: ChildSpawner<C>>(builder: B) -> Self { + ChildMonitor { state_machine: spawn_state_machine(builder) } + } + + /// Set the event listener to `listener`. Note that events might not show up on the new + /// listener imediately after this call since the listener change message must be processed by + /// the backend first. + pub fn set_listener<L>(&self, listener: L) + where L: MonitorEventListener + { + self.state_machine.send(MonitorMsg::AddListener(Box::new(listener))).unwrap(); + } + + /// Start the process to monitor. This will trigger an `MonitorEventListener::started` event. + pub fn start(&self) { + self.state_machine.send(MonitorMsg::Start).unwrap(); + } + + /// Stop the monitored process. This will trigger an `MonitorEventListener::stopped` event. + pub fn stop(&self) { + self.state_machine.send(MonitorMsg::Stop).unwrap(); + } +} + +impl Drop for ChildMonitor { + fn drop(&mut self) { + self.state_machine + .send(MonitorMsg::Shutdown) + .expect("Internal error, not able to send Shutdown"); + } +} |
