Functionaltiy
parent: tbd commit: 5646846
1 | use ; |
2 | |
3 | use ; |
4 | use |
5 | , |
6 | , | TcpStream
7 | |
8 | , |
9 | Mutex, |
10 | , |
11 | , | JoinHandle
12 | ; |
13 | use ; |
14 | |
15 | use crate:: |
16 | |
17 | , | IssuesCountResponse
18 | |
19 | RepositoryFileInspectionResponse, RepositoryIssueLabelsResponse, |
20 | RepositoryIssuesResponse, RepositoryMessage, RepositoryMessageKind, RepositoryRequest, |
21 | RepositoryResponse, |
22 | , |
23 | MessageKind, |
24 | , |
25 | , |
26 | , | Listeners
27 | |
28 | , |
29 | , |
30 | , | User
31 | , |
32 | ; |
33 | |
34 | |
35 | pub task: , |
36 | |
37 | |
38 | |
39 | pub instance: InstanceMeta, |
40 | pub sender: , |
41 | pub task: , |
42 | |
43 | |
44 | /// Represents a connection which hasn't finished the handshake. |
45 | |
46 | pub socket: , |
47 | |
48 | |
49 | |
50 | |
51 | pub connections: , |
52 | pub instance_connections: , |
53 | |
54 | |
55 | pub async |
56 | mut socket: , |
57 | listeners: , |
58 | mut connections: , |
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 | => todo!, | CreateRepository
187 | => | RepositoryFileInspection
188 | let response = File |
189 | commit_metadata: default, |
190 | ; |
191 | |
192 | => | RepositoryInfo
193 | let response = RepositoryView |
194 | name: String from, |
195 | description: String from, |
196 | default_branch: String from, |
197 | latest_commit: default, |
198 | files: vec!, |
199 | ; |
200 | |
201 | socket |
202 | .send |
203 | to_vec |
204 | RepositoryMessage |
205 | target: repository.target.clone, |
206 | command: Response |
207 | , | RepositoryInfo
208 | , |
209 | , |
210 | |
211 | .unwrap, |
212 | |
213 | .await |
214 | .unwrap; |
215 | |
216 | => | IssuesCount
217 | let response: IssuesCountResponse = |
218 | IssuesCountResponse ; |
219 | |
220 | socket |
221 | .send |
222 | to_vec |
223 | RepositoryMessage |
224 | target: repository.target.clone, |
225 | command: Response |
226 | , | IssuesCount
227 | , |
228 | , |
229 | |
230 | .unwrap, |
231 | |
232 | .await |
233 | .unwrap; |
234 | |
235 | => | IssueLabels
236 | let response = RepositoryIssueLabelsResponse ; |
237 | |
238 | socket |
239 | .send |
240 | to_vec |
241 | RepositoryMessage |
242 | target: repository.target.clone, |
243 | command: Response |
244 | , | IssueLabels
245 | , |
246 | , |
247 | |
248 | .unwrap, |
249 | |
250 | .await |
251 | .unwrap; |
252 | |
253 | => | Issues
254 | let response = RepositoryIssuesResponse ; |
255 | |
256 | socket |
257 | .send |
258 | to_vec |
259 | RepositoryMessage |
260 | target: repository.target.clone, |
261 | command: Response |
262 | , | Issues
263 | , |
264 | , |
265 | |
266 | .unwrap, |
267 | |
268 | .await |
269 | .unwrap; |
270 | |
271 | , |
272 | => | Response
273 | unreachable! |
274 | |
275 | |
276 | |
277 | |
278 | |
279 | |
280 | info!; |
281 | |
282 | |
283 | async |
284 | message: MessageKind, |
285 | listeners: & , |
286 | mut connections: & , |
287 | |
288 | let : = |
289 | match &message |
290 | => | Handshake
291 | todo! |
292 | |
293 | => , | Repository
294 | ; |
295 | |
296 | let target = match |
297 | => instance.clone, |
298 | => user.instance.clone, |
299 | => repository.instance.clone, |
300 | _ => unreachable!, |
301 | ; |
302 | |
303 | let mut listeners = listeners.lock .await; |
304 | let mut listener = listeners.add; |
305 | drop; |
306 | |
307 | let connections = connections.lock .await; |
308 | |
309 | if let Some = connections.instance_connections.get |
310 | connection.sender.send; |
311 | else |
312 | error!; |
313 | |
314 | panic!; |
315 | |
316 | |
317 | drop; |
318 | |
319 | listener |
320 | |
321 |