From 2b27340b4675c337e6dcb8d90635091f3f1a363d Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Mon, 20 Jan 2014 05:53:31 +0100 Subject: [PATCH] Add socket communication --- comm-socket.pl | 36 ++++++++++++++++++++++++++ gen_site.pl | 68 ++++++++++++++++++++++++++++++++++++++++++-------- launch.sh | 4 +-- nginx.conf | 1 - synchro.sh | 2 +- 5 files changed, 96 insertions(+), 15 deletions(-) create mode 100644 comm-socket.pl diff --git a/comm-socket.pl b/comm-socket.pl new file mode 100644 index 00000000..e9c1b0f3 --- /dev/null +++ b/comm-socket.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use v5.10.1; +use IO::Select; +use IO::Socket::UNIX; +use threads; + +die("Give at least the socket file as argument") if (! @ARGV); + +my $socket = IO::Socket::UNIX->new( + Type => SOCK_STREAM, + Peer => $ARGV[0], + ); + +die "Can't create socket: $!" unless $socket; + +my $s = IO::Select->new(); + +$s->add(\*STDIN); +$s->add($socket); + +while ($s->count()) +{ + for my $rd ($s->can_read(0.25)) + { + my $line = <$rd>; + chomp($line); + + if ($rd == \*STDIN) { + say $socket $line; + } + elsif ($rd == $socket) { + say $line; + } + } +} diff --git a/gen_site.pl b/gen_site.pl index adc75523..6052d85c 100755 --- a/gen_site.pl +++ b/gen_site.pl @@ -13,6 +13,7 @@ use File::Find; use File::Path qw/make_path remove_tree/; use File::Temp "tempdir"; use Getopt::Long; +use IO::Socket; use Thread::Queue; our $outdir = "outest"; @@ -26,7 +27,6 @@ our $basehome = "/"; our $baseteams = "/connected/"; our $threads = 6; - sub genHome(;$) { my $m = shift // Mirror->new(); @@ -188,7 +188,7 @@ sub sync $todir .= $2; } - make_path($todir, { mode => 0711 }) if (! -d $todir ); + make_path($todir, { mode => 0751 }) if (! -d $todir ); copy($File::Find::name, $todir) or warn(q{copy failed:} . $!); } @@ -200,8 +200,9 @@ sub sync abs_path($tmpcopy); remove_tree($main::outdir); + mkdir($main::outdir); - system("mv '$tmpcopy' '$main::outdir'"); + system("mv '$tmpcopy'/* '$main::outdir/'"); } else { @@ -223,7 +224,7 @@ sub sync $todir .= $2; } - make_path($todir, { mode => 0711 }) if (! -d $todir ); + make_path($todir, { mode => 0751 }) if (! -d $todir ); say "$File::Find::name -> $todir"; @@ -235,12 +236,14 @@ sub sync } } -sub parse($$) +sub parse($$;$) { my $m = shift; my $change_current = 0; + my $cmds = shift; + my $chan_output = shift // \*STDOUT; - for my $cmd ($_[0] =~ /([^:]+)/g) + for my $cmd ($cmds =~ /([^:]+)/g) { my $len = length($cmd); @@ -250,7 +253,7 @@ sub parse($$) { last if ($len > length($queue->peek($i))); } - #say "Inserting $cmd at position $i/".$queue->pending(); + say $chan_output "Inserting $cmd at position $i/".$queue->pending(); $queue->insert($i, $cmd); $change_current = 1 if $i == 0 && $queue->pending() != 1; } @@ -271,24 +274,65 @@ sub parse($$) # Parse arguments -my $help; my $deamon; +my $help; my $deamon; my $socket; GetOptions ("threads|thread|t=i" => \$threads, "baseadmin|ba=s" => \$baseadmin, "basehome|bh=s" => \$basehome, "baseteams|bt=s" => \$baseteams, "outdir|out|o=s" => \$outdir, "deamon|d" => \$deamon, + "socket|s=s" => \$socket, "help|h|?" => \$help); $outdir = abs_path($outdir); +sub create_socket +{ + my $m = shift; + my $socket_path = abs_path( shift ); + + unlink($socket_path) if -e $socket_path; + + my $socket = IO::Socket::UNIX->new( + Local => $socket_path, + Type => SOCK_STREAM, + Listen => SOMAXCONN, + ); + say "Socket listening on $socket_path; waiting for connections..."; + + while(my $connection = $socket->accept) + { + say "New connexion, new thread ready for parsing actions!"; + threads->create(\&socket_run, $m, $connection); + } +} + +sub socket_run +{ + my $m = shift; + my $connection = shift; + + $connection->autoflush(1); + say $connection "You are connected to gen_site.pl, please enter command:"; + while (<$connection>) + { + chomp $_; + parse($m, $_, $connection); + } + say "Closing socket connection; stopping thread."; + close $connection; +} + if ($deamon) { my $m :shared = Mirror->new(); $main_thread = threads->create(\&manage, $m); - while(<>) { + threads->create(\&create_socket, $m, $socket) if ($socket); + + while(<>) + { chomp $_; parse($m, $_); } @@ -302,6 +346,8 @@ elsif (@ARGV) $main_thread = threads->create(\&manage, $m); + threads->create(\&create_socket, $m, $socket) if ($socket); + while ($_ = shift) { parse($m, $_); } @@ -626,7 +672,7 @@ sub save($;$) if ($path =~ /\.[a-z0-9]{2,4}$/) { eval { - make_path( dirname("$path") , { mode => 0711 }) if (! -d dirname("$path") ); + make_path( dirname("$path") , { mode => 0751 }) if (! -d dirname("$path") ); }; open my $fd, ">", $path or die "$path: $!"; @@ -636,7 +682,7 @@ sub save($;$) else { eval { - make_path "$path", { mode => 0711 } if (! -d "$path"); + make_path "$path", { mode => 0751 } if (! -d "$path"); }; open my $fd, ">", "$path/index.html" or die "$path: $!"; diff --git a/launch.sh b/launch.sh index d85c3953..eb2ee30a 100755 --- a/launch.sh +++ b/launch.sh @@ -15,11 +15,11 @@ tail -f ./logs/checks.log & TMPF=`mktemp` -tail -f "$TMPF" | ./gen_site.pl -d -o ./out/ & +tail -f "$TMPF" | ./gen_site.pl -d -s /tmp/test.sock -o ./out & while ! [ -f /tmp/stop ]; do - ./synchro.sh delete > /dev/null + ./synchro.sh delete if [ `ls submission | wc -l` -gt 1 ] then diff --git a/nginx.conf b/nginx.conf index 13d2d3d9..07ba2533 100644 --- a/nginx.conf +++ b/nginx.conf @@ -79,5 +79,4 @@ server { fastcgi_pass unix:/var/run/php-fpm.sock; break; } - } diff --git a/synchro.sh b/synchro.sh index 844daa65..4ecb9cd0 100755 --- a/synchro.sh +++ b/synchro.sh @@ -22,6 +22,6 @@ rsync -e ssh -av $OPTS misc phobos:~/ scp nginx.conf submission.php phobos:~/ rsync -e ssh -av phobos:~/submission/ submission/ -ssh phobos "rm ~/submission/*" +ssh phobos "rm -fv ~/submission/*" exit $?