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