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