Add socket communication

This commit is contained in:
nemunaire 2014-01-20 05:53:31 +01:00
parent dee5b5fbb7
commit 2b27340b46
5 changed files with 96 additions and 15 deletions

36
comm-socket.pl Normal file
View file

@ -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;
}
}
}

View file

@ -13,6 +13,7 @@ use File::Find;
use File::Path qw/make_path remove_tree/; use File::Path qw/make_path remove_tree/;
use File::Temp "tempdir"; use File::Temp "tempdir";
use Getopt::Long; use Getopt::Long;
use IO::Socket;
use Thread::Queue; use Thread::Queue;
our $outdir = "outest"; our $outdir = "outest";
@ -26,7 +27,6 @@ our $basehome = "/";
our $baseteams = "/connected/"; our $baseteams = "/connected/";
our $threads = 6; our $threads = 6;
sub genHome(;$) sub genHome(;$)
{ {
my $m = shift // Mirror->new(); my $m = shift // Mirror->new();
@ -188,7 +188,7 @@ sub sync
$todir .= $2; $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:} . $!); copy($File::Find::name, $todir) or warn(q{copy failed:} . $!);
} }
@ -200,8 +200,9 @@ sub sync
abs_path($tmpcopy); abs_path($tmpcopy);
remove_tree($main::outdir); remove_tree($main::outdir);
mkdir($main::outdir);
system("mv '$tmpcopy' '$main::outdir'"); system("mv '$tmpcopy'/* '$main::outdir/'");
} }
else else
{ {
@ -223,7 +224,7 @@ sub sync
$todir .= $2; $todir .= $2;
} }
make_path($todir, { mode => 0711 }) if (! -d $todir ); make_path($todir, { mode => 0751 }) if (! -d $todir );
say "$File::Find::name -> $todir"; say "$File::Find::name -> $todir";
@ -235,12 +236,14 @@ sub sync
} }
} }
sub parse($$) sub parse($$;$)
{ {
my $m = shift; my $m = shift;
my $change_current = 0; 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); my $len = length($cmd);
@ -250,7 +253,7 @@ sub parse($$)
{ {
last if ($len > length($queue->peek($i))); 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); $queue->insert($i, $cmd);
$change_current = 1 if $i == 0 && $queue->pending() != 1; $change_current = 1 if $i == 0 && $queue->pending() != 1;
} }
@ -271,24 +274,65 @@ sub parse($$)
# Parse arguments # Parse arguments
my $help; my $deamon; my $help; my $deamon; my $socket;
GetOptions ("threads|thread|t=i" => \$threads, GetOptions ("threads|thread|t=i" => \$threads,
"baseadmin|ba=s" => \$baseadmin, "baseadmin|ba=s" => \$baseadmin,
"basehome|bh=s" => \$basehome, "basehome|bh=s" => \$basehome,
"baseteams|bt=s" => \$baseteams, "baseteams|bt=s" => \$baseteams,
"outdir|out|o=s" => \$outdir, "outdir|out|o=s" => \$outdir,
"deamon|d" => \$deamon, "deamon|d" => \$deamon,
"socket|s=s" => \$socket,
"help|h|?" => \$help); "help|h|?" => \$help);
$outdir = abs_path($outdir); $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) if ($deamon)
{ {
my $m :shared = Mirror->new(); my $m :shared = Mirror->new();
$main_thread = threads->create(\&manage, $m); $main_thread = threads->create(\&manage, $m);
while(<>) { threads->create(\&create_socket, $m, $socket) if ($socket);
while(<>)
{
chomp $_; chomp $_;
parse($m, $_); parse($m, $_);
} }
@ -302,6 +346,8 @@ elsif (@ARGV)
$main_thread = threads->create(\&manage, $m); $main_thread = threads->create(\&manage, $m);
threads->create(\&create_socket, $m, $socket) if ($socket);
while ($_ = shift) { while ($_ = shift) {
parse($m, $_); parse($m, $_);
} }
@ -626,7 +672,7 @@ sub save($;$)
if ($path =~ /\.[a-z0-9]{2,4}$/) if ($path =~ /\.[a-z0-9]{2,4}$/)
{ {
eval { 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: $!"; open my $fd, ">", $path or die "$path: $!";
@ -636,7 +682,7 @@ sub save($;$)
else else
{ {
eval { 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: $!"; open my $fd, ">", "$path/index.html" or die "$path: $!";

View file

@ -15,11 +15,11 @@ tail -f ./logs/checks.log &
TMPF=`mktemp` 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 ]; while ! [ -f /tmp/stop ];
do do
./synchro.sh delete > /dev/null ./synchro.sh delete
if [ `ls submission | wc -l` -gt 1 ] if [ `ls submission | wc -l` -gt 1 ]
then then

View file

@ -79,5 +79,4 @@ server {
fastcgi_pass unix:/var/run/php-fpm.sock; fastcgi_pass unix:/var/run/php-fpm.sock;
break; break;
} }
} }

View file

@ -22,6 +22,6 @@ rsync -e ssh -av $OPTS misc phobos:~/
scp nginx.conf submission.php phobos:~/ scp nginx.conf submission.php phobos:~/
rsync -e ssh -av phobos:~/submission/ submission/ rsync -e ssh -av phobos:~/submission/ submission/
ssh phobos "rm ~/submission/*" ssh phobos "rm -fv ~/submission/*"
exit $? exit $?