]>
Commit | Line | Data |
---|---|---|
5c5cd38a MG |
1 | package Gruntmaster::Daemon::Runner::Interactive; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | use File::Slurp qw/slurp/; | |
8 | use Gruntmaster::Daemon::Constants qw/WA/; | |
9 | use Log::Log4perl qw/get_logger/; | |
10 | use POSIX qw/mkfifo/; | |
11 | use Try::Tiny; | |
12 | ||
3e7fd903 | 13 | our $VERSION = '5999.000_004'; |
bc372959 | 14 | |
5c5cd38a MG |
15 | ################################################## |
16 | ||
17 | sub run{ | |
a722431b MG |
18 | my ($test, $meta) = @_; |
19 | get_logger->trace("Running on test $test..."); | |
20 | ||
21 | mkfifo 'fifo1', 0600 or die $! unless -e 'fifo1'; | |
22 | mkfifo 'fifo2', 0600 or die $! unless -e 'fifo2'; | |
23 | ||
24 | my $ret = fork // get_logger->logdie("Fork failed: $!"); | |
25 | if ($ret) { | |
c020b922 MG |
26 | try { |
27 | $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => [qw/0 fifo1 1 >fifo2/], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/); | |
28 | } catch { | |
29 | die $_ | |
30 | } finally { | |
31 | waitpid $ret, 0; | |
32 | }; | |
a722431b MG |
33 | die [WA, "Wrong Answer"] if $?; |
34 | } else { | |
35 | try { | |
5f8de888 | 36 | $meta->{files}{ver}{run}->($meta->{files}{ver}{name}, fds => [qw/1 >fifo1 0 fifo2 4 >result/], args => [$test], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/); |
a722431b MG |
37 | } catch { |
38 | exit 1; | |
39 | }; | |
40 | exit | |
41 | } | |
42 | ||
40b036b5 MG |
43 | unlink 'fifo1'; |
44 | unlink 'fifo2'; | |
45 | ||
a722431b | 46 | scalar slurp 'result' |
5c5cd38a MG |
47 | } |
48 | ||
8aec2ffe MG |
49 | 1; |
50 | __END__ | |
51 | ||
52 | =encoding utf-8 | |
53 | ||
54 | =head1 NAME | |
55 | ||
56 | Gruntmaster::Daemon::Runner::Interactive - Make an interactive verifier talk to the program | |
57 | ||
58 | =head1 SYNOPSIS | |
59 | ||
60 | use Gruntmaster::Daemon::Runner::Interactive; | |
0005d3ad | 61 | Gruntmaster::Daemon::Runner::Interactive::run(5, $meta); |
8aec2ffe MG |
62 | |
63 | =head1 DESCRIPTION | |
64 | ||
65 | B<WARNING: This runner is experimental!> | |
66 | ||
0005d3ad | 67 | Gruntmaster::Daemon::Runner::Interactive is a runner which runs the program and an interactive verifier in parallel, connecting each program's STDIN to the other's STDOUT. The verifier, C<< $meta->{files}{ver} >>, should return nonzero if the program gives an incorrect answer, or print the test score to fd 4 then return 0 if the answer is correct. |
8aec2ffe MG |
68 | |
69 | =head1 AUTHOR | |
70 | ||
71 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
72 | ||
73 | =head1 COPYRIGHT AND LICENSE | |
74 | ||
75 | Copyright (C) 2014 by Marius Gavrilescu | |
76 | ||
77 | This library is free software: you can redistribute it and/or modify | |
78 | it under the terms of the GNU Affero General Public License as published by | |
79 | the Free Software Foundation, either version 3 of the License, or | |
80 | (at your option) any later version. | |
81 | ||
82 | ||
83 | =cut |