Add more backend logic
parent: tbd commit: 5060ca2
1 | use ; |
2 | |
3 | use ; |
4 | use |
5 | , |
6 | , | TcpStream
7 | |
8 | , |
9 | Mutex, |
10 | , |
11 | , | JoinHandle
12 | ; |
13 | use ; |
14 | |
15 | use crate:: |
16 | , |
17 | , |
18 | , | Listeners
19 | |
20 | , | IssuesCountResponse
21 | |
22 | RepositoryFileInspectionResponse, RepositoryIssueLabelsResponse, |
23 | RepositoryIssuesResponse, RepositoryMessage, RepositoryMessageKind, RepositoryRequest, |
24 | RepositoryResponse, |
25 | , |
26 | MessageKind, |
27 | , |
28 | |
29 | , |
30 | , |
31 | , | User
32 | , |
33 | ; |
34 | |
35 | |
36 | pub task: , |
37 | |
38 | |
39 | |
40 | pub instance: InstanceMeta, |
41 | pub sender: , |
42 | pub task: , |
43 | |
44 | |
45 | /// Represents a connection which hasn't finished the handshake. |
46 | |
47 | pub socket: , |
48 | |
49 | |
50 | |
51 | |
52 | pub connections: , |
53 | pub instance_connections: , |
54 | |
55 | |
56 | pub async |
57 | mut socket: , |
58 | listeners: , |
59 | mut connections: , |
60 | backend: , |
61 | addr: SocketAddr, |
62 | |
63 | let mut handshaked = false; |
64 | let this_instance = Instance |
65 | url: String from, |
66 | ; |
67 | |
68 | while let Some = socket.next .await |
69 | let message = match message |
70 | Ok => message, |
71 | Err => |
72 | error!; |
73 | continue; |
74 | |
75 | ; |
76 | |
77 | let payload = match message |
78 | => text.into_bytes, | Text
79 | => bytes, | Binary
80 | => continue, | Ping
81 | => continue, | Pong
82 | => | Close
83 | info!; |
84 | |
85 | return; |
86 | |
87 | _ => unreachable!, |
88 | ; |
89 | |
90 | let message = match |
91 | Ok => message, |
92 | Err => |
93 | error!; |
94 | continue; |
95 | |
96 | ; |
97 | |
98 | info!; |
99 | |
100 | if let Handshake = message |
101 | match handshake |
102 | => | Initiate
103 | // Send HandshakeMessage::Response |
104 | let message = HandshakeResponse |
105 | identity: Instance |
106 | url: String from, |
107 | , |
108 | version: String from, |
109 | ; |
110 | |
111 | socket |
112 | .send |
113 | to_vec |
114 | , | Response
115 | |
116 | .unwrap, |
117 | |
118 | .await |
119 | .unwrap; |
120 | |
121 | continue; |
122 | |
123 | => | Response
124 | // Send HandshakeMessage::Finalize |
125 | let message = HandshakeFinalize ; |
126 | |
127 | socket |
128 | .send |
129 | to_vec |
130 | , | Finalize
131 | |
132 | .unwrap, |
133 | |
134 | .await |
135 | .unwrap; |
136 | |
137 | continue; |
138 | |
139 | => | Finalize
140 | handshaked = true; |
141 | |
142 | // Send HandshakeMessage::Finalize |
143 | let message = HandshakeFinalize ; |
144 | |
145 | socket |
146 | .send |
147 | to_vec |
148 | , | Finalize
149 | |
150 | .unwrap, |
151 | |
152 | .await |
153 | .unwrap; |
154 | |
155 | continue; |
156 | |
157 | |
158 | |
159 | |
160 | if !handshaked |
161 | continue; |
162 | |
163 | |
164 | if let Repository = &message |
165 | if repository.target.instance != this_instance |
166 | info!; |
167 | // We need to send this command to a different instance |
168 | |
169 | let mut listener = send_and_get_listener .await; |
170 | |
171 | // Wait for response |
172 | while let Ok = listener.recv .await |
173 | if let Repository |
174 | command: Response, |
175 | .. |
176 | = message |
177 | |
178 | socket |
179 | .send |
180 | .await |
181 | .unwrap; |
182 | |
183 | |
184 | else |
185 | // This message is targeting this instance |
186 | match &repository.command |
187 | => match request | Request
188 | => | CreateRepository
189 | let mut backend = backend.lock .await; |
190 | let response = backend.create_repository; |
191 | |
192 | let response = match response |
193 | Ok => response, |
194 | Err => |
195 | error!; |
196 | continue; |
197 | |
198 | ; |
199 | drop; |
200 | |
201 | socket |
202 | .send |
203 | to_vec |
204 | RepositoryMessage |
205 | target: repository.target.clone, |
206 | command: Response |
207 | , | CreateRepository
208 | , |
209 | , |
210 | |
211 | .unwrap, |
212 | |
213 | .await |
214 | .unwrap; |
215 | |
216 | => | RepositoryFileInspect
217 | let mut backend = backend.lock .await; |
218 | let response = backend.repository_file_inspect; |
219 | |
220 | let response = match response |
221 | Ok => response, |
222 | Err => |
223 | error!; |
224 | continue; |
225 | |
226 | ; |
227 | drop; |
228 | |
229 | socket |
230 | .send |
231 | to_vec |
232 | RepositoryMessage |
233 | target: repository.target.clone, |
234 | command: Response |
235 | RepositoryFileInspection |
236 | response, |
237 | , |
238 | , |
239 | , |
240 | |
241 | .unwrap, |
242 | |
243 | .await |
244 | .unwrap; |
245 | |
246 | => | RepositoryInfo
247 | let mut backend = backend.lock .await; |
248 | let response = backend.repository_info; |
249 | |
250 | let response = match response |
251 | Ok => response, |
252 | Err => |
253 | error!; |
254 | continue; |
255 | |
256 | ; |
257 | drop; |
258 | |
259 | socket |
260 | .send |
261 | to_vec |
262 | RepositoryMessage |
263 | target: repository.target.clone, |
264 | command: Response |
265 | , | RepositoryInfo
266 | , |
267 | , |
268 | |
269 | .unwrap, |
270 | |
271 | .await |
272 | .unwrap; |
273 | |
274 | => | IssuesCount
275 | let mut backend = backend.lock .await; |
276 | let response = backend.issues_count; |
277 | |
278 | let response = match response |
279 | Ok => response, |
280 | Err => |
281 | error!; |
282 | continue; |
283 | |
284 | ; |
285 | drop; |
286 | |
287 | socket |
288 | .send |
289 | to_vec |
290 | RepositoryMessage |
291 | target: repository.target.clone, |
292 | command: Response |
293 | , | IssuesCount
294 | , |
295 | , |
296 | |
297 | .unwrap, |
298 | |
299 | .await |
300 | .unwrap; |
301 | |
302 | => | IssueLabels
303 | let mut backend = backend.lock .await; |
304 | let response = backend.issue_labels; |
305 | |
306 | let response = match response |
307 | Ok => response, |
308 | Err => |
309 | error!; |
310 | continue; |
311 | |
312 | ; |
313 | drop; |
314 | socket |
315 | .send |
316 | to_vec |
317 | RepositoryMessage |
318 | target: repository.target.clone, |
319 | command: Response |
320 | , | IssueLabels
321 | , |
322 | , |
323 | |
324 | .unwrap, |
325 | |
326 | .await |
327 | .unwrap; |
328 | |
329 | => | Issues
330 | let mut backend = backend.lock .await; |
331 | let response = backend.issues; |
332 | |
333 | let response = match response |
334 | Ok => response, |
335 | Err => |
336 | error!; |
337 | continue; |
338 | |
339 | ; |
340 | drop; |
341 | |
342 | socket |
343 | .send |
344 | to_vec |
345 | RepositoryMessage |
346 | target: repository.target.clone, |
347 | command: Response |
348 | , | Issues
349 | , |
350 | , |
351 | |
352 | .unwrap, |
353 | |
354 | .await |
355 | .unwrap; |
356 | |
357 | , |
358 | => | Response
359 | unreachable! |
360 | |
361 | |
362 | |
363 | |
364 | |
365 | |
366 | info!; |
367 | |
368 | |
369 | async |
370 | message: MessageKind, |
371 | listeners: & , |
372 | mut connections: & , |
373 | |
374 | let : = |
375 | match &message |
376 | => | Handshake
377 | todo! |
378 | |
379 | => , | Repository
380 | ; |
381 | |
382 | let target = match |
383 | => instance.clone, |
384 | => user.instance.clone, |
385 | => repository.instance.clone, |
386 | _ => unreachable!, |
387 | ; |
388 | |
389 | let mut listeners = listeners.lock .await; |
390 | let mut listener = listeners.add; |
391 | drop; |
392 | |
393 | let connections = connections.lock .await; |
394 | |
395 | if let Some = connections.instance_connections.get |
396 | connection.sender.send; |
397 | else |
398 | error!; |
399 | |
400 | panic!; |
401 | |
402 | |
403 | drop; |
404 | |
405 | listener |
406 | |
407 |