Add perlcritic test and make code comply
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Runner / Interactive.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon::Runner::Interactive;
2
3use 5.014000;
4use strict;
5use warnings;
6
7use File::Slurp qw/slurp/;
8use Gruntmaster::Daemon::Constants qw/WA/;
9use Log::Log4perl qw/get_logger/;
10use POSIX qw/mkfifo/;
11use Try::Tiny;
12
3e7fd903 13our $VERSION = '5999.000_004';
bc372959 14
5c5cd38a
MG
15##################################################
16
17sub run{
a722431b
MG
18 my ($test, $meta) = @_;
19 get_logger->trace("Running on test $test...");
20
99d37110
MG
21 mkfifo 'fifo1', 0600 or die "$!\n" unless -e 'fifo1';
22 mkfifo 'fifo2', 0600 or die "$!\n" unless -e 'fifo2';
a722431b 23
fdb99417
MG
24 if ($test == 1 && $ENV{GRUNTMASTER_VM}) {
25 exec 'cat <prog.out >ver.in' if fork;
26 exec 'cat <ver.out >prog.in' if fork;
27 }
28
a722431b
MG
29 my $ret = fork // get_logger->logdie("Fork failed: $!");
30 if ($ret) {
c020b922 31 try {
fdb99417
MG
32 my @fds = $ENV{GRUNTMASTER_VM} ? qw,0 /dev/ttyS1 1 >/dev/ttyS1, : qw/0 fifo1 1 >fifo2/;
33 $meta->{files}{prog}{run}->($meta->{files}{prog}{name}, fds => \@fds, map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/);
c020b922 34 } catch {
99d37110 35 die $_ ## no critic (RequireCarping)
c020b922
MG
36 } finally {
37 waitpid $ret, 0;
38 };
99d37110 39 die [WA, 'Wrong Answer'] if $?; ## no critic (RequireCarping)
a722431b
MG
40 } else {
41 try {
fdb99417
MG
42 my @fds = $ENV{GRUNTMASTER_VM} ? qw,1 >/dev/ttyS1 0 /dev/ttyS1, : qw/1 >fifo1 0 fifo2/;
43 $meta->{files}{ver}{run}->($meta->{files}{ver}{name}, fds => [@fds, qw,4 >result,], args => [$test], map {defined $meta->{$_} ? ($_ => $meta->{$_}) : () } qw/timeout mlimit/);
a722431b
MG
44 } catch {
45 exit 1;
46 };
47 exit
48 }
49
40b036b5
MG
50 unlink 'fifo1';
51 unlink 'fifo2';
52
a722431b 53 scalar slurp 'result'
5c5cd38a
MG
54}
55
8aec2ffe
MG
561;
57__END__
58
59=encoding utf-8
60
61=head1 NAME
62
63Gruntmaster::Daemon::Runner::Interactive - Make an interactive verifier talk to the program
64
65=head1 SYNOPSIS
66
67 use Gruntmaster::Daemon::Runner::Interactive;
0005d3ad 68 Gruntmaster::Daemon::Runner::Interactive::run(5, $meta);
8aec2ffe
MG
69
70=head1 DESCRIPTION
71
72B<WARNING: This runner is experimental!>
73
0005d3ad 74Gruntmaster::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
75
76=head1 AUTHOR
77
78Marius Gavrilescu E<lt>marius@ieval.roE<gt>
79
80=head1 COPYRIGHT AND LICENSE
81
82Copyright (C) 2014 by Marius Gavrilescu
83
84This library is free software: you can redistribute it and/or modify
85it under the terms of the GNU Affero General Public License as published by
86the Free Software Foundation, either version 3 of the License, or
87(at your option) any later version.
88
89
90=cut
This page took 0.022193 seconds and 4 git commands to generate.