Changes
parent: tbd commit: 5ede041
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 | , |
31 | ; |
32 | |
33 | |
34 | pub task: , |
35 | |
36 | |
37 | |
38 | pub instance: InstanceMeta, |
39 | pub sender: , |
40 | pub task: , |
41 | |
42 | |
43 | /// Represents a connection which hasn't finished the handshake. |
44 | |
45 | pub socket: , |
46 | |
47 | |
48 | |
49 | |
50 | pub connections: , |
51 | pub instance_connections: , |
52 | |
53 | |
54 | pub async |
55 | mut socket: , |
56 | listeners: , |
57 | mut connections: , |
58 | addr: SocketAddr, |
59 | |
60 | let mut handshaked = false; |
61 | let this_instance = Instance |
62 | url: String from, |
63 | ; |
64 | |
65 | while let Some = socket.next .await |
66 | let message = match message |
67 | Ok => message, |
68 | Err => |
69 | error!; |
70 | continue; |
71 | |
72 | ; |
73 | |
74 | let payload = match message |
75 | => text.into_bytes, | Text
76 | => bytes, | Binary
77 | => continue, | Ping
78 | => continue, | Pong
79 | => | Close
80 | info!; |
81 | |
82 | return; |
83 | |
84 | _ => unreachable!, |
85 | ; |
86 | |
87 | let message = match |
88 | Ok => message, |
89 | Err => |
90 | error!; |
91 | continue; |
92 | |
93 | ; |
94 | |
95 | if let Handshake = message |
96 | match handshake |
97 | => | Initiate
98 | // Send HandshakeMessage::Response |
99 | let message = HandshakeResponse |
100 | identity: Instance |
101 | url: String from, |
102 | , |
103 | version: String from, |
104 | ; |
105 | |
106 | socket |
107 | .send |
108 | unwrap, | to_vec .
109 | |
110 | .await |
111 | .unwrap; |
112 | |
113 | continue; |
114 | |
115 | => | Response
116 | // Send HandshakeMessage::Finalize |
117 | let message = HandshakeFinalize ; |
118 | |
119 | socket |
120 | .send |
121 | unwrap, | to_vec .
122 | |
123 | .await |
124 | .unwrap; |
125 | |
126 | continue; |
127 | |
128 | => | Finalize
129 | handshaked = true; |
130 | |
131 | continue; |
132 | |
133 | |
134 | |
135 | |
136 | if !handshaked |
137 | continue; |
138 | |
139 | |
140 | if let Repository = &message |
141 | if repository.target.instance != this_instance |
142 | // We need to send this command to a different instance |
143 | |
144 | let mut listener = send_and_get_listener .await; |
145 | |
146 | // Wait for response |
147 | while let Ok = listener.recv .await |
148 | if let Repository |
149 | command: Response, |
150 | .. |
151 | = message |
152 | |
153 | socket |
154 | .send |
155 | .await |
156 | .unwrap; |
157 | |
158 | |
159 | else |
160 | // This message is targeting this instance |
161 | match &repository.command |
162 | => match request | Request
163 | => todo!, | CreateRepository
164 | => | RepositoryFileInspection
165 | let response = File |
166 | commit_metadata: default, |
167 | ; |
168 | |
169 | => | RepositoryInfo
170 | let response = RepositoryView |
171 | name: String from, |
172 | description: String from, |
173 | default_branch: String from, |
174 | latest_commit: default, |
175 | files: vec!, |
176 | ; |
177 | |
178 | socket |
179 | .send |
180 | to_vec |
181 | RepositoryMessage |
182 | target: repository.target.clone, |
183 | command: Response |
184 | , | RepositoryInfo
185 | , |
186 | , |
187 | |
188 | .unwrap, |
189 | |
190 | .await |
191 | .unwrap; |
192 | |
193 | => | IssuesCount
194 | let response: IssuesCountResponse = |
195 | IssuesCountResponse ; |
196 | |
197 | socket |
198 | .send |
199 | to_vec |
200 | RepositoryMessage |
201 | target: repository.target.clone, |
202 | command: Response |
203 | , | IssuesCount
204 | , |
205 | , |
206 | |
207 | .unwrap, |
208 | |
209 | .await |
210 | .unwrap; |
211 | |
212 | => | IssueLabels
213 | let response = RepositoryIssueLabelsResponse ; |
214 | |
215 | socket |
216 | .send |
217 | to_vec |
218 | RepositoryMessage |
219 | target: repository.target.clone, |
220 | command: Response |
221 | , | IssueLabels
222 | , |
223 | , |
224 | |
225 | .unwrap, |
226 | |
227 | .await |
228 | .unwrap; |
229 | |
230 | => | Issues
231 | let response = RepositoryIssuesResponse ; |
232 | |
233 | socket |
234 | .send |
235 | to_vec |
236 | RepositoryMessage |
237 | target: repository.target.clone, |
238 | command: Response |
239 | , | Issues
240 | , |
241 | , |
242 | |
243 | .unwrap, |
244 | |
245 | .await |
246 | .unwrap; |
247 | |
248 | , |
249 | => | Response
250 | unreachable! |
251 | |
252 | |
253 | |
254 | |
255 | |
256 | |
257 | |
258 | async |
259 | message: MessageKind, |
260 | listeners: & , |
261 | mut connections: & , |
262 | |
263 | let = match &message |
264 | => | Handshake
265 | todo! |
266 | |
267 | => , | Repository
268 | ; |
269 | |
270 | let target = todo!; |
271 | |
272 | let mut listeners = listeners.lock .await; |
273 | let mut listener = listeners.add; |
274 | drop; |
275 | |
276 | let connections = connections.lock .await; |
277 | |
278 | if let Some = connections.instance_connections.get |
279 | connection.sender.send; |
280 | else |
281 | error!; |
282 | |
283 | panic!; |
284 | |
285 | |
286 | drop; |
287 | |
288 | listener |
289 | |
290 |