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