Skip to content

Commit 84e9a16

Browse files
committed
Fixed admin socket management
1 parent 9a2e91b commit 84e9a16

File tree

2 files changed

+41
-36
lines changed

2 files changed

+41
-36
lines changed

backend/src/routes/sockets.route.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const router = Router();
88
router.get(
99
'/:type',
1010
AuthUtil.requireSuperUser,
11-
async (req: Request, res: Response) => {
11+
(req: Request, res: Response) => {
1212
const sockets: SocketConnection[] = [];
1313

1414
if (req.params.type === 'teams') {
@@ -22,16 +22,16 @@ router.get(
2222
}
2323

2424
res.json(sockets);
25-
}
25+
},
2626
);
2727

2828
router.delete(
2929
'/:id',
3030
AuthUtil.requireSuperUser,
31-
async (req: Request, res: Response) => {
31+
(req: Request, res: Response) => {
3232
SocketManager.instance.kick(req.params.id);
33-
res.status(200);
34-
}
33+
res.sendStatus(200);
34+
},
3535
);
3636

3737
export default router;

backend/src/socket.manager.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
ConnectPacket,
33
ReplayPacket,
4-
SubmissionPacket
4+
SubmissionPacket,
55
} from '@codelm/common/src/packets/client.packet';
66
import { Packet } from '@codelm/common/src/packets/packet';
77
import { SubmissionStatus } from '@codelm/common/src/packets/server.packet';
@@ -11,19 +11,19 @@ import {
1111
isGradedProblem,
1212
isOpenEndedProblem,
1313
OpenEndedProblemModel,
14-
ProblemType
14+
ProblemType,
1515
} from '@codelm/common/src/models/problem.model';
1616
import {
1717
isFalse,
1818
isTestCaseSubmissionCorrect,
1919
SubmissionDao,
20-
submissionResult
20+
submissionResult,
2121
} from './daos/submission.dao';
2222
import {
2323
GradedSubmissionModel,
2424
isGradedSubmission,
2525
SubmissionModel,
26-
TestCaseSubmissionModel
26+
TestCaseSubmissionModel,
2727
} from '@codelm/common/src/models/submission.model';
2828
import * as WebSocket from 'ws';
2929
import { Express } from 'express';
@@ -114,7 +114,12 @@ export class SocketManager {
114114
}
115115

116116
public kick(id: string) {
117-
this.sockets = this.sockets.filter(socket => socket._id !== id);
117+
const index = this.sockets.findIndex(socket => socket._id === id);
118+
119+
if (index !== -1) {
120+
this.sockets[index].close();
121+
this.sockets.splice(index, 1);
122+
}
118123
}
119124

120125
public kickTeams() {
@@ -135,7 +140,7 @@ export class SocketManager {
135140
this.sockets = this.sockets.filter(socket => socket.ping());
136141
}, 15 * 1000);
137142

138-
((app as any) as WithWebsocketMethod).ws('/', webSocket => {
143+
(app as any as WithWebsocketMethod).ws('/', webSocket => {
139144
let socket: Socket;
140145

141146
webSocket.onmessage = data => {
@@ -147,7 +152,7 @@ export class SocketManager {
147152
this.onConnectPacket(packet as ConnectPacket, webSocket).then(
148153
_socket => {
149154
socket = _socket;
150-
}
155+
},
151156
);
152157
});
153158

@@ -162,7 +167,7 @@ export class SocketManager {
162167
webSocket.onclose = () => {
163168
if (socket) {
164169
this.sockets = this.sockets.filter(
165-
_socket => _socket._id !== socket._id
170+
_socket => _socket._id !== socket._id,
166171
);
167172
}
168173
};
@@ -175,13 +180,13 @@ export class SocketManager {
175180

176181
onConnectPacket(
177182
packet: ConnectPacket,
178-
webSocket: WebSocket
183+
webSocket: WebSocket,
179184
): Promise<Socket> {
180185
return new Promise<Socket>((resolve, reject) => {
181186
jwt.verify(packet.jwt, JWT_PRIVATE_KEY, (err, decoded) => {
182187
if (err) {
183188
webSocket.send(
184-
JSON.stringify({ name: 'connectResponse', success: false })
189+
JSON.stringify({ name: 'connectResponse', success: false }),
185190
);
186191
webSocket.close();
187192
reject(err);
@@ -204,13 +209,13 @@ export class SocketManager {
204209
problemTitle: problem.title,
205210
type: problem.type,
206211
language: problemSubmission.language,
207-
code: problemSubmission.code
212+
code: problemSubmission.code,
208213
};
209214

210215
if (isGradedProblem(problem)) {
211216
serverProblemSubmission.testCases = problem.testCases.filter(
212217
testCase =>
213-
isFalse(problemSubmission.test.toString()) || !testCase.hidden
218+
isFalse(problemSubmission.test.toString()) || !testCase.hidden,
214219
);
215220
} else if (isOpenEndedProblem(problem)) {
216221
serverProblemSubmission.gameType = problem.gameType;
@@ -219,7 +224,7 @@ export class SocketManager {
219224

220225
const { testCases, compilationError } = await this.runSubmission(
221226
serverProblemSubmission,
222-
socket
227+
socket,
223228
);
224229

225230
let submission: SubmissionModel = {
@@ -230,7 +235,7 @@ export class SocketManager {
230235
code: problemSubmission.code,
231236
testCases: testCases ?? [],
232237
compilationError,
233-
test: problemSubmission.test
238+
test: problemSubmission.test,
234239
} as GradedSubmissionModel;
235240

236241
if (problemSubmission.test) {
@@ -251,14 +256,14 @@ export class SocketManager {
251256
// TODO: Combine this with onSubmissionPacket
252257
async onReplayPacket(packet: ReplayPacket, socket: Socket) {
253258
const submission = await SubmissionDao.getSubmission(
254-
packet.replayRequest._id
259+
packet.replayRequest._id,
255260
);
256261

257262
const serverProblemSubmission: ServerProblemSubmission = {
258263
problemTitle: submission.problem.title,
259264
type: submission.problem.type,
260265
language: submission.language,
261-
code: submission.code
266+
code: submission.code,
262267
};
263268

264269
const problem = submission.problem as OpenEndedProblemModel;
@@ -271,7 +276,7 @@ export class SocketManager {
271276

272277
private async runGradedSubmission(
273278
serverProblemSubmission: ServerProblemSubmission,
274-
connection: CodeRunnerConnection
279+
connection: CodeRunnerConnection,
275280
): Promise<{ testCases: TestCaseSubmissionModel[] }> {
276281
const testCases: TestCaseSubmissionModel[] = [];
277282

@@ -289,7 +294,7 @@ export class SocketManager {
289294
output: '',
290295
correctOutput: testCase.output,
291296
inputDisplay: testCase.inputDisplay,
292-
outputDisplay: testCase.outputDisplay
297+
outputDisplay: testCase.outputDisplay,
293298
};
294299

295300
if (e.name === 'dockerKilled') {
@@ -321,7 +326,7 @@ export class SocketManager {
321326
private runOpenEndedSubmission(
322327
serverProblemSubmission: ServerProblemSubmission,
323328
connection: CodeRunnerConnection,
324-
socket: Socket
329+
socket: Socket,
325330
): Promise<{}> {
326331
return new Promise<{}>(resolve => {
327332
let packetStream: Observable<CodeRunnerPacket>;
@@ -333,7 +338,7 @@ export class SocketManager {
333338
}
334339
case GameType.Timesweeper: {
335340
packetStream = connection.runGame(
336-
new Timesweeper(serverProblemSubmission.problemExtras)
341+
new Timesweeper(serverProblemSubmission.problemExtras),
337342
);
338343
break;
339344
}
@@ -348,21 +353,21 @@ export class SocketManager {
348353
},
349354
() => {
350355
resolve({});
351-
}
356+
},
352357
);
353358
});
354359
}
355360

356361
private async runSubmission(
357362
serverProblemSubmission: ServerProblemSubmission,
358-
socket: Socket
363+
socket: Socket,
359364
): Promise<{
360365
testCases?: TestCaseSubmissionModel[];
361366
compilationError?: string;
362367
}> {
363368
socket.send({
364369
name: 'submissionExtras',
365-
extras: serverProblemSubmission.problemExtras
370+
extras: serverProblemSubmission.problemExtras,
366371
});
367372

368373
let connection: CodeRunnerConnection;
@@ -373,17 +378,17 @@ export class SocketManager {
373378
return {
374379
compilationError:
375380
'The server is overloaded. Please send the following error to a judge: ' +
376-
JSON.stringify(e)
381+
JSON.stringify(e),
377382
};
378383
}
379384

380385
try {
381386
socket.send({
382387
name: 'submissionStatus',
383-
status: SubmissionStatus.Compiling
388+
status: SubmissionStatus.Compiling,
384389
});
385390
const compilationResult = await connection.compile(
386-
serverProblemSubmission
391+
serverProblemSubmission,
387392
);
388393

389394
if (!compilationResult.success) {
@@ -396,19 +401,19 @@ export class SocketManager {
396401
if (e.name === 'dockerKilled') {
397402
return {
398403
compilationError:
399-
'Your code could not compile. Maybe the server is overloaded?'
404+
'Your code could not compile. Maybe the server is overloaded?',
400405
};
401406
} else if (e.name === 'unexpectedData') {
402407
return {
403408
compilationError:
404409
'An unexpected error occurred. Maybe the server is overloaded? Please send the following error to a judge: ' +
405-
e.data
410+
e.data,
406411
};
407412
} else {
408413
return {
409414
compilationError:
410415
'An unknown occurred. Please send the following error to a judge: ' +
411-
JSON.stringify(e)
416+
JSON.stringify(e),
412417
};
413418
}
414419
}
@@ -418,13 +423,13 @@ export class SocketManager {
418423
if (serverProblemSubmission.type === ProblemType.Graded) {
419424
return await this.runGradedSubmission(
420425
serverProblemSubmission,
421-
connection
426+
connection,
422427
);
423428
} else {
424429
return await this.runOpenEndedSubmission(
425430
serverProblemSubmission,
426431
connection,
427-
socket
432+
socket,
428433
);
429434
}
430435
}

0 commit comments

Comments
 (0)