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