Connection
parent: tbd commit: 415ff8d
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 = IssuesCountResponse ; |
195 | |
196 | socket |
197 | .send |
198 | to_vec |
199 | RepositoryMessage |
200 | target: repository.target.clone, |
201 | command: Response |
202 | , | IssuesCount
203 | , |
204 | , |
205 | |
206 | .unwrap, |
207 | |
208 | .await |
209 | .unwrap; |
210 | |
211 | => | IssueLabels
212 | let response = RepositoryIssueLabelsResponse ; |
213 | |
214 | socket |
215 | .send |
216 | to_vec |
217 | RepositoryMessage |
218 | target: repository.target.clone, |
219 | command: Response |
220 | , | IssueLabels
221 | , |
222 | , |
223 | |
224 | .unwrap, |
225 | |
226 | .await |
227 | .unwrap; |
228 | |
229 | => | Issues
230 | let response = RepositoryIssuesResponse ; |
231 | |
232 | socket |
233 | .send |
234 | to_vec |
235 | RepositoryMessage |
236 | target: repository.target.clone, |
237 | command: Response |
238 | , | Issues
239 | , |
240 | , |
241 | |
242 | .unwrap, |
243 | |
244 | .await |
245 | .unwrap; |
246 | |
247 | , |
248 | => | Response
249 | unreachable! |
250 | |
251 | |
252 | |
253 | |
254 | |
255 | |
256 | |
257 | async |
258 | message: MessageKind, |
259 | listeners: & , |
260 | mut connections: & , |
261 | |
262 | let = match &message |
263 | => | Handshake
264 | todo! |
265 | |
266 | => , | Repository
267 | ; |
268 | |
269 | let target = todo!; |
270 | |
271 | let mut listeners = listeners.lock .await; |
272 | let mut listener = listeners.add; |
273 | drop; |
274 | |
275 | let connections = connections.lock .await; |
276 | |
277 | if let Some = connections.instance_connections.get |
278 | connection.sender.send; |
279 | else |
280 | error!; |
281 | |
282 | panic!; |
283 | |
284 | |
285 | drop; |
286 | |
287 | listener |
288 | |
289 |