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
use std::any::Any;
use std::sync::{mpsc, Mutex};
use std::thread;
use super::super::state::StateFlag;
use super::message::{Message, Response};
use crate::error::RuntimeError;
pub struct Controller<M>
where
M: PartialEq + Eq + Send + Sync,
{
tx: Mutex<mpsc::Sender<Message<M>>>,
rx: Mutex<mpsc::Receiver<Response>>,
pub(super) t: Mutex<Option<thread::JoinHandle<Result<(), RuntimeError>>>>,
pub(super) state: StateFlag<ControllerState>,
}
impl<M> Controller<M>
where
M: 'static + PartialEq + Eq + Send + Sync,
{
#[inline]
pub(super) fn new(
tx: mpsc::Sender<Message<M>>,
rx: mpsc::Receiver<Response>,
t: thread::JoinHandle<Result<(), RuntimeError>>,
) -> Self {
Self {
tx: Mutex::new(tx),
rx: Mutex::new(rx),
t: Mutex::new(Some(t)),
state: StateFlag::default(),
}
}
#[inline]
pub fn wait(&self) -> Result<(), RuntimeError> {
match self.t.lock().unwrap().take() {
Some(t) => match t.join() {
Ok(r) => r,
Err(_) => RuntimeError::unexpected(),
},
None => RuntimeError::expect("Already stopped"),
}
}
#[inline]
pub fn stop(&self) -> Result<(), RuntimeError> {
self.state.stop()?;
self.send(Message::Stop)?;
match self.t.lock().unwrap().take().unwrap().join() {
Ok(r) => r,
Err(_) => RuntimeError::unexpected(),
}
}
#[inline]
pub fn pause(&self) -> Result<(), RuntimeError> {
self.set_state(ControllerState::Running, ControllerState::Paused)?;
self.send(Message::Pause)
}
#[inline]
pub fn resume(&self) -> Result<(), RuntimeError> {
self.set_state(ControllerState::Paused, ControllerState::Running)?;
self.send(Message::Resume)
}
#[inline]
pub fn hibernate(&self) -> Result<(), RuntimeError> {
self.set_state(ControllerState::Running, ControllerState::Hibernated)?;
self.send(Message::Hibernate)
}
#[inline]
pub fn wake_up(&self) -> Result<(), RuntimeError> {
self.set_state(ControllerState::Hibernated, ControllerState::Running)?;
self.send(Message::WakeUp)
}
#[inline]
pub fn request<R>(&self, m: M) -> Result<R, RuntimeError>
where
R: 'static,
{
self.assert_state(ControllerState::Running)?;
self.send(Message::Custom(m))
}
#[inline]
fn send<R>(&self, m: Message<M>) -> Result<R, RuntimeError>
where
R: 'static,
{
let (tx, rx) = {
let rx = self.rx.lock().unwrap();
let tx = self.tx.lock().unwrap();
(tx, rx)
};
tx.send(m)?;
let m = match (rx.recv()? as Box<dyn Any>).downcast() {
Ok(m) => Ok(*m),
Err(_) => RuntimeError::unexpected(),
};
{
drop(tx);
drop(rx);
}
m
}
#[inline]
fn assert_state(&self, state: ControllerState) -> Result<(), RuntimeError> {
match self.state.status()? == state {
true => Ok(()),
false => RuntimeError::expect("Unexpected flag"),
}
}
#[inline]
fn set_state(&self, last: ControllerState, now: ControllerState) -> Result<(), RuntimeError> {
self.assert_state(last)?;
self.state.start(now)
}
}
#[derive(PartialEq, Eq)]
pub enum ControllerState {
Running,
Paused,
Hibernated,
}
impl Default for ControllerState {
#[inline]
fn default() -> Self {
Self::Running
}
}
impl From<usize> for ControllerState {
#[inline]
fn from(state: usize) -> Self {
match state {
1 => Self::Paused,
2 => Self::Hibernated,
0 | _ => Self::Running,
}
}
}
impl Into<usize> for ControllerState {
#[inline]
fn into(self) -> usize {
match self {
Self::Running => 0,
Self::Paused => 1,
Self::Hibernated => 2,
}
}
}