3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 22:09:26 +01:00

Use JSON to communicate wtih compiler VM

This commit is contained in:
Pragmatic Software 2017-09-14 19:10:21 -07:00
parent cf960261aa
commit 76764a06bf
3 changed files with 62 additions and 77 deletions

View File

@ -13,6 +13,7 @@ use warnings;
use strict; use strict;
use IO::Socket; use IO::Socket;
use JSON;
my $sock = IO::Socket::INET->new( my $sock = IO::Socket::INET->new(
PeerAddr => '127.0.0.1', PeerAddr => '127.0.0.1',
@ -24,19 +25,18 @@ if(not defined $sock) {
die $!; die $!;
} }
my $nick = shift @ARGV; my $json = join ' ', @ARGV;
my $channel = shift @ARGV; my $h = decode_json $json;
my $code = join ' ', @ARGV; my $lang = $h->{lang} // "c11";
my $lang = "c11"; if ($h->{code} =~ s/-lang=([^ ]+)//) {
if($code =~ s/-lang=([^ ]+)//) {
$lang = lc $1; $lang = lc $1;
} }
print $sock "compile:$nick:$channel:$lang\n"; $h->{lang} = $lang;
print $sock "$code\n"; $json = encode_json $h;
print $sock "compile:end\n";
print $sock "$json\n";
while(my $line = <$sock>) { while(my $line = <$sock>) {
print "$line"; print "$line";

View File

@ -9,6 +9,7 @@ use strict;
use File::Basename; use File::Basename;
use POSIX; use POSIX;
use JSON;
my $USERNAME = 'compiler'; my $USERNAME = 'compiler';
my $USE_LOCAL = defined $ENV{'CC_LOCAL'}; my $USE_LOCAL = defined $ENV{'CC_LOCAL'};
@ -62,12 +63,12 @@ sub run_server {
while(my $line = <$input>) { while(my $line = <$input>) {
chomp $line; chomp $line;
print "-" x 40, "\n";
print "Got [$line]\n"; print "Got [$line]\n";
if($line =~ m/^compile:\s*end/) { my $compile_in = decode_json($line);
next if not defined $lang or not defined $code;
print "Attempting compile [$lang] ...\n"; print "Attempting compile [$compile_in->{lang}] ...\n";
my $pid = fork; my $pid = fork;
@ -83,14 +84,16 @@ sub run_server {
exit; exit;
} }
POSIX::setgid($gid); POSIX::setgid($gid);
POSIX::setuid($uid); POSIX::setuid($uid);
my $result = interpret($lang, $sourcefile, $execfile, $code, $cmdline, $user_input, $date); my $result = interpret(%$compile_in);
print "Done compiling; result: [$result]\n"; my $compile_out = { result => $result };
print $output "result:$result\n"; my $json = encode_json($compile_out);
print "Done compiling; result: [$result] [$json]\n";
print $output "result:$json\n";
print $output "result:end\n"; print $output "result:end\n";
exit; exit;
} else { } else {
@ -104,31 +107,6 @@ sub run_server {
exit; exit;
} }
} }
if($line =~ m/^compile:\s*(.*)/) {
my $options = $1;
$cmdline = undef;
$user_input = undef;
$lang = undef;
$sourcefile = undef;
$execfile = undef;
($lang, $sourcefile, $execfile, $cmdline, $user_input, $date) = split /:/, $options;
$code = "";
$sourcefile = "/dev/null" if not defined $sourcefile;
$execfile = "/dev/null" if not defined $execfile;
$lang = "unknown" if not defined $lang;
$cmdline = "echo No cmdline specified!" if not defined $cmdline;
$user_input = "" if not defined $user_input;
print "Setting lang [$lang]; [$sourcefile]; [$cmdline]; [$user_input]; [$date]\n";
next;
}
$code .= $line . "\n";
}
} else { } else {
while(1) { while(1) {
print $heartbeat "\n"; print $heartbeat "\n";
@ -142,17 +120,16 @@ sub run_server {
} }
sub interpret { sub interpret {
my ($lang, $sourcefile, $execfile, $code, $cmdline, $input, $date) = @_; my %h = @_;
print "lang: [$lang], sourcefile: [$sourcefile], execfile [$execfile], code: [$code], cmdline: [$cmdline], input: [$input], date: [$date]\n"; print "lang: [$h{lang}], sourcefile: [$h{sourcefile}], execfile [$h{execfile}], code: [$h{code}], cmdline: [$h{cmdline}], input: [$h{input}], date: [$h{date}]\n";
$lang = '_default' if not exists $languages{$lang}; $h{lang} = '_default' if not exists $languages{$h{lang}};
system("chmod -R 755 /home/compiler"); system("chmod -R 755 /home/compiler");
system("rm -rf /home/compiler/prog*"); system("rm -rf /home/compiler/prog*");
my $mod = $lang->new(sourcefile => $sourcefile, execfile => $execfile, code => $code, my $mod = $h{lang}->new(%h);
cmdline => $cmdline, input => $input, date => $date);
$mod->preprocess; $mod->preprocess;

View File

@ -17,6 +17,7 @@ use IO::Socket;
use LWP::UserAgent; use LWP::UserAgent;
use Time::HiRes qw/gettimeofday/; use Time::HiRes qw/gettimeofday/;
use Text::Balanced qw/extract_delimited/; use Text::Balanced qw/extract_delimited/;
use JSON;
my $EXECUTE_PORT = '3333'; my $EXECUTE_PORT = '3333';
@ -367,21 +368,28 @@ sub execute {
print FILE "$cmdline\n$input\n$pretty_code\n"; print FILE "$cmdline\n$input\n$pretty_code\n";
close FILE; close FILE;
print $compiler "compile:$self->{lang}:$self->{sourcefile}:$self->{execfile}:$cmdline:$input:$date\n"; my $compile_in = { lang => $self->{lang}, sourcefile => $self->{sourcefile}, execfile => $self->{execfile},
print $compiler "$pretty_code\n"; cmdline => $cmdline, input => $input, date => $date, arguments => $self->{arguments}, code => $pretty_code };
print $compiler "compile:end\n"; my $compile_json = encode_json($compile_in);
#print STDERR "Sending [$compile_json] to vm_server\n";
print $compiler "$compile_json\n";
my $result = ""; my $result = "";
my $got_result = 0; my $got_result = 0;
while(my $line = <$compiler_output>) { while(my $line = <$compiler_output>) {
$line =~ s/[\r\n]+$//;
#print STDERR "Read [$line]\n";
$line =~ s/[\r\n]+$//;
last if $line =~ /^result:end$/; last if $line =~ /^result:end$/;
if($line =~ /^result:/) { if($line =~ /^result:/) {
$line =~ s/^result://; $line =~ s/^result://;
$result .= "$line\n"; my $compile_out = decode_json($line);
$result .= "$compile_out->{result}\n";
$got_result = 1; $got_result = 1;
next; next;
} }