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