-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathrenderer_server.rs
More file actions
179 lines (157 loc) · 5.88 KB
/
Copy pathrenderer_server.rs
File metadata and controls
179 lines (157 loc) · 5.88 KB
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
mod state;
mod app;
mod coords;
mod renderer;
mod backend;
mod animation;
mod handlers;
mod start;
cfg_if::cfg_if! {
if #[cfg(any(feature = "test", test))] {
mod test_event_loop_notifier;
use test_event_loop_notifier as event_loop_notifier;
} else {
mod event_loop_notifier;
mod main;
}
}
pub(crate) use app::TurtleId;
pub(crate) use backend::RendererServer;
pub use renderer::export::ExportError;
pub use start::start;
use ipc_channel::ipc::IpcError;
use tokio::sync::mpsc;
use parking_lot::{RwLock, Mutex};
use crate::ipc_protocol::{ServerSender, ServerOneshotSender, ServerReceiver, ClientRequest};
use crate::Event;
use app::{SharedApp, App};
use renderer::display_list::{SharedDisplayList, DisplayList};
use event_loop_notifier::EventLoopNotifier;
use animation::AnimationRunner;
/// Serves requests from the client forever
async fn serve(
conn: ServerSender,
client_requests: ServerReceiver,
app: SharedApp,
display_list: SharedDisplayList,
event_loop: EventLoopNotifier,
mut events_receiver: mpsc::UnboundedReceiver<Event>,
mut server_shutdown_receiver: mpsc::Receiver<()>,
) {
let anim_runner = AnimationRunner::new(
conn.clone(),
app.clone(),
display_list.clone(),
event_loop.clone(),
);
loop {
// This will either receive the next request or end this task
let (client_id, request) = tokio::select! {
// If the main thread shuts down successfully, this will receive Some(()). If the main
// thread panics, this will return None. In either case, this loop needs to stop.
_ = server_shutdown_receiver.recv() => break,
req = client_requests.recv() => match req {
Ok(req) => req,
// Client has disconnected completely, no purpose in continuing this loop
Err(IpcError::Disconnected) => break,
Err(err) => panic!("unable to receive request from IPC client: {:?}", err),
},
};
// Each request is executed immediately, in the order it arrives
handle_handler_result(dispatch_request(
ServerOneshotSender::new(client_id, &conn),
&app,
&display_list,
&event_loop,
&mut events_receiver,
&anim_runner,
request,
));
}
}
fn dispatch_request(
conn: ServerOneshotSender,
app: &RwLock<App>,
display_list: &Mutex<DisplayList>,
event_loop: &EventLoopNotifier,
events_receiver: &mut mpsc::UnboundedReceiver<Event>,
anim_runner: &AnimationRunner,
request: ClientRequest,
) -> Result<(), handlers::HandlerError> {
use ClientRequest::*;
match request {
CreateTurtle => {
handlers::create_turtle(conn, &mut app.write(), event_loop)
},
Export(path, format) => {
handlers::export_drawings(conn, &app.read(), &display_list.lock(), &path, format)
},
PollEvent => {
handlers::poll_event(conn, events_receiver)
},
DrawingProp(prop) => {
handlers::drawing_prop(conn, &app.read(), prop)
},
SetDrawingProp(prop_value) => {
handlers::set_drawing_prop(&mut app.write(), event_loop, prop_value)
},
ResetDrawingProp(prop) => {
handlers::reset_drawing_prop(&mut app.write(), event_loop, prop)
},
TurtleProp(id, prop) => {
handlers::turtle_prop(conn, &app.read(), id, prop)
},
SetTurtleProp(id, prop_value) => {
handlers::set_turtle_prop(&mut app.write(), &mut display_list.lock(), event_loop, id, prop_value)
},
ResetTurtleProp(id, prop) => {
handlers::reset_turtle_prop(&mut app.write(), &mut display_list.lock(), event_loop, id, prop)
},
ResetTurtle(id) => {
handlers::reset_turtle(&mut app.write(), &mut display_list.lock(), event_loop, id)
},
MoveForward(id, distance) => {
handlers::move_forward(conn, &mut app.write(), &mut display_list.lock(), event_loop, anim_runner, id, distance)
},
MoveTo(id, target_pos) => {
handlers::move_to(conn, &mut app.write(), &mut display_list.lock(), event_loop, anim_runner, id, target_pos)
},
RotateInPlace(id, angle, direction) => {
handlers::rotate_in_place(conn, &mut app.write(), event_loop, anim_runner, id, angle, direction)
},
BeginFill(id) => {
handlers::begin_fill(&mut app.write(), &mut display_list.lock(), event_loop, id)
},
EndFill(id) => {
handlers::end_fill(&mut app.write(), id)
},
ClearAll => {
handlers::clear_all(&mut app.write(), &mut display_list.lock(), event_loop, anim_runner)
},
ClearTurtle(id) => {
handlers::clear_turtle(&mut app.write(), &mut display_list.lock(), event_loop, id)
},
DebugTurtle(id, angle_unit) => {
handlers::debug_turtle(conn, &app.read(), id, angle_unit)
},
DebugDrawing => {
handlers::debug_drawing(conn, &app.read())
},
DestroyDrawing => {
handlers::destroy_drawing(event_loop)
},
}
}
fn handle_handler_result(res: Result<(), handlers::HandlerError>) {
use handlers::HandlerError::*;
match res {
Ok(_) => {},
Err(IpcChannelError(err)) => panic!("Error while serializing response: {}", err),
// Task managing window has ended, this task will end soon too.
//TODO: This potentially leaves the turtle/drawing state in an inconsistent state. Should
// we deal with that somehow? Panicking doesn't seem appropriate since this probably isn't
// an error, but we should definitely stop processing commands and make sure the process
// ends shortly after.
Err(EventLoopClosed(_)) => {},
}
}