1extern crate alloc;
8
9use alloc::collections::{BTreeMap, VecDeque};
10use alloc::string::String;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum WorkerState {
15 Running,
17 Terminated,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum WorkerError {
24 AlreadyTerminated,
26 QueueEmpty,
28}
29
30#[derive(Debug, Clone)]
32pub struct WorkerCore {
33 pub id: usize,
34 pub script_url: String,
35 pub state: WorkerState,
36 pub to_worker_queue: VecDeque<String>,
37 pub to_main_queue: VecDeque<String>,
38}
39
40impl WorkerCore {
41 pub fn new(id: usize, script_url: &str) -> Self {
43 Self {
44 id,
45 script_url: String::from(script_url),
46 state: WorkerState::Running,
47 to_worker_queue: VecDeque::new(),
48 to_main_queue: VecDeque::new(),
49 }
50 }
51
52 pub fn is_running(&self) -> bool {
54 self.state == WorkerState::Running
55 }
56
57 pub fn post_to_worker(&mut self, message: &str) -> Result<(), WorkerError> {
59 if self.state == WorkerState::Terminated {
60 return Err(WorkerError::AlreadyTerminated);
61 }
62 self.to_worker_queue.push_back(String::from(message));
63 Ok(())
64 }
65
66 pub fn post_to_main(&mut self, message: &str) -> Result<(), WorkerError> {
68 if self.state == WorkerState::Terminated {
69 return Err(WorkerError::AlreadyTerminated);
70 }
71 self.to_main_queue.push_back(String::from(message));
72 Ok(())
73 }
74
75 pub fn pop_worker_message(&mut self) -> Option<String> {
77 if self.state == WorkerState::Terminated {
78 return None;
79 }
80 self.to_worker_queue.pop_front()
81 }
82
83 pub fn pop_main_message(&mut self) -> Option<String> {
85 if self.state == WorkerState::Terminated {
86 return None;
87 }
88 self.to_main_queue.pop_front()
89 }
90
91 pub fn terminate(&mut self) {
93 self.state = WorkerState::Terminated;
94 self.to_worker_queue.clear();
95 self.to_main_queue.clear();
96 }
97}
98
99#[derive(Debug, Clone)]
101pub struct SharedPortPair {
102 pub client_id: usize,
103 pub to_worker_queue: VecDeque<String>,
104 pub to_client_queue: VecDeque<String>,
105 pub is_closed: bool,
106}
107
108impl SharedPortPair {
109 pub fn new(client_id: usize) -> Self {
110 Self {
111 client_id,
112 to_worker_queue: VecDeque::new(),
113 to_client_queue: VecDeque::new(),
114 is_closed: false,
115 }
116 }
117}
118
119#[derive(Debug, Clone)]
121pub struct SharedWorkerCore {
122 pub id: usize,
123 pub script_url: String,
124 pub name: String,
125 pub state: WorkerState,
126 pub ports: BTreeMap<usize, SharedPortPair>,
127 pub pending_connects: VecDeque<usize>,
128}
129
130impl SharedWorkerCore {
131 pub fn new(id: usize, script_url: &str, name: &str) -> Self {
132 Self {
133 id,
134 script_url: String::from(script_url),
135 name: String::from(name),
136 state: WorkerState::Running,
137 ports: BTreeMap::new(),
138 pending_connects: VecDeque::new(),
139 }
140 }
141
142 pub fn is_running(&self) -> bool {
143 self.state == WorkerState::Running
144 }
145
146 pub fn connect_client(&mut self, client_id: usize) -> Result<(), WorkerError> {
148 if self.state == WorkerState::Terminated {
149 return Err(WorkerError::AlreadyTerminated);
150 }
151 self.ports.insert(client_id, SharedPortPair::new(client_id));
152 self.pending_connects.push_back(client_id);
153 Ok(())
154 }
155
156 pub fn pop_pending_connect(&mut self) -> Option<usize> {
158 if self.state == WorkerState::Terminated {
159 return None;
160 }
161 self.pending_connects.pop_front()
162 }
163
164 pub fn post_to_worker(&mut self, client_id: usize, message: &str) -> Result<(), WorkerError> {
166 if self.state == WorkerState::Terminated {
167 return Err(WorkerError::AlreadyTerminated);
168 }
169 let port = self.ports.get_mut(&client_id).ok_or(WorkerError::QueueEmpty)?;
170 if port.is_closed {
171 return Err(WorkerError::AlreadyTerminated);
172 }
173 port.to_worker_queue.push_back(String::from(message));
174 Ok(())
175 }
176
177 pub fn post_to_client(&mut self, client_id: usize, message: &str) -> Result<(), WorkerError> {
179 if self.state == WorkerState::Terminated {
180 return Err(WorkerError::AlreadyTerminated);
181 }
182 let port = self.ports.get_mut(&client_id).ok_or(WorkerError::QueueEmpty)?;
183 if port.is_closed {
184 return Err(WorkerError::AlreadyTerminated);
185 }
186 port.to_client_queue.push_back(String::from(message));
187 Ok(())
188 }
189
190 pub fn pop_worker_message(&mut self, client_id: usize) -> Option<String> {
192 if self.state == WorkerState::Terminated {
193 return None;
194 }
195 let port = self.ports.get_mut(&client_id)?;
196 if port.is_closed {
197 return None;
198 }
199 port.to_worker_queue.pop_front()
200 }
201
202 pub fn pop_client_message(&mut self, client_id: usize) -> Option<String> {
204 if self.state == WorkerState::Terminated {
205 return None;
206 }
207 let port = self.ports.get_mut(&client_id)?;
208 if port.is_closed {
209 return None;
210 }
211 port.to_client_queue.pop_front()
212 }
213
214 pub fn close_port(&mut self, client_id: usize) {
216 if let Some(port) = self.ports.get_mut(&client_id) {
217 port.is_closed = true;
218 port.to_worker_queue.clear();
219 port.to_client_queue.clear();
220 }
221 }
222
223 pub fn terminate(&mut self) {
225 self.state = WorkerState::Terminated;
226 self.pending_connects.clear();
227 for port in self.ports.values_mut() {
228 port.is_closed = true;
229 port.to_worker_queue.clear();
230 port.to_client_queue.clear();
231 }
232 self.ports.clear();
233 }
234}