@@ -26,6 +26,8 @@ import { stat } from "./Statistics";
26
26
import * as crypto from "crypto" ;
27
27
import { FileHandle } from "fs/promises" ;
28
28
import { EmptyMeterResult , MeterResult } from "../Spawn/Meter" ;
29
+ import { SerialPort } from "serialport" ;
30
+ import { closePort } from "./Serial" ;
29
31
30
32
const UsrCompileResultTransformer = {
31
33
mle : JudgeResultKind . CompileMemoryLimitExceeded ,
@@ -134,7 +136,7 @@ export abstract class JudgeAgent {
134
136
const compileLog = await executableAgent . fileAgent . getPath (
135
137
CompileLogName
136
138
) ;
137
- const compileLogSize = fs . statSync ( compileLog ) . size ;
139
+ const compileLogSize = ( await fs . promises . stat ( compileLog ) ) . size ;
138
140
const exteaInfo = {
139
141
compileTime : this . transformTime ( compileSumTime ) ,
140
142
compileMessage : await readStream (
@@ -199,7 +201,10 @@ export abstract class JudgeAgent {
199
201
200
202
protected async runJudge (
201
203
executableAgent : ExecutableAgent ,
202
- judgeFunction : ( testCase : TestCase ) => Promise < JudgeCaseResult >
204
+ judgeFunction : (
205
+ testCase : TestCase ,
206
+ judgeTimeout : number
207
+ ) => Promise < JudgeCaseResult >
203
208
) : Promise < JudgeCaseResult [ ] > {
204
209
const programResult = await executableAgent . program ( ) ;
205
210
let runResult : JudgeResult | undefined = undefined ;
@@ -217,7 +222,10 @@ export abstract class JudgeAgent {
217
222
const judgeCaseResults : JudgeCaseResult [ ] = [ ] ;
218
223
if ( this . judge . test ) {
219
224
for ( const testCase of this . judge . test . cases ) {
220
- const caseResult = await judgeFunction ( testCase ) ;
225
+ const caseResult = await judgeFunction (
226
+ testCase ,
227
+ executableAgent . configuredLanguage . judgeTimeout
228
+ ) ;
221
229
judgeCaseResults . push ( caseResult ) ;
222
230
if (
223
231
caseResult . kind !== JudgeResultKind . Accepted &&
@@ -468,11 +476,50 @@ export class NormalJudgeAgent extends JudgeAgent {
468
476
469
477
const caseResults = await this . runJudge (
470
478
userExecutableAgent ,
471
- async ( testCase ) => {
479
+ async ( testCase , judgeTimeout ) => {
480
+ let kind = JudgeResultKind . SystemError ;
481
+ let time = 0 ;
482
+ let memory = 0 ;
483
+ const path = ( await SerialPort . list ( ) ) . find (
484
+ ( info ) =>
485
+ info . serialNumber &&
486
+ getConfig ( ) . fpga . serial . includes ( info . serialNumber )
487
+ ) ?. path ;
488
+ if ( path ) {
489
+ const [ port , input , output ] = await Promise . all ( [
490
+ SerialPort . binding . open ( {
491
+ baudRate : 250000 ,
492
+ path,
493
+ } ) ,
494
+ this . fileAgent . getBuffer ( testCase . input ) ,
495
+ this . fileAgent . getBuffer ( testCase . output ) ,
496
+ ] ) ;
497
+ const start = Date . now ( ) ;
498
+ port . write ( input ) ;
499
+ const timeout = setTimeout ( closePort , judgeTimeout , port ) ;
500
+ try {
501
+ const { buffer, bytesRead } = await port . read (
502
+ Buffer . alloc ( output . length ) ,
503
+ 0 ,
504
+ output . length
505
+ ) ;
506
+ if ( buffer . equals ( output ) ) {
507
+ kind = JudgeResultKind . Accepted ;
508
+ } else {
509
+ kind = JudgeResultKind . WrongAnswer ;
510
+ }
511
+ memory = bytesRead ;
512
+ } catch {
513
+ kind = JudgeResultKind . TimeLimitExceeded ;
514
+ }
515
+ clearTimeout ( timeout ) ;
516
+ closePort ( port ) ;
517
+ time = Date . now ( ) - start ;
518
+ }
472
519
return {
473
- kind : JudgeResultKind . OutpuLimitExceeded ,
474
- time : 0 ,
475
- memory : 0 ,
520
+ kind,
521
+ time,
522
+ memory,
476
523
} ;
477
524
}
478
525
) ;
0 commit comments