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