155 lines
4.4 KiB
Perl
155 lines
4.4 KiB
Perl
#!/bin/perl
|
|
use strict;
|
|
|
|
use Thread;
|
|
|
|
use IO::Socket::Multicast;
|
|
use POE;
|
|
use POE::Component::Server::TCP;
|
|
use JSON::XS;
|
|
|
|
my %mcasts :shared;
|
|
my %mcdata :shared;
|
|
my %mcopen :shared;
|
|
|
|
my $TimeOut = 20;
|
|
my $starttime;
|
|
|
|
#fix
|
|
my %JSONdata :shared;
|
|
#TCPServer();
|
|
my $thr = new Thread (\&Main);
|
|
|
|
POE::Session->create(
|
|
inline_states => {
|
|
#loop => \&loop,
|
|
main => \&Main,
|
|
tcpserver => \&TCPServer,
|
|
_start => sub {
|
|
$_[KERNEL]->yield($_) for qw(main tcpserver);
|
|
}
|
|
}
|
|
);
|
|
|
|
|
|
POE::Kernel->run();
|
|
#StartMain();
|
|
#fix ende
|
|
|
|
sub Main {
|
|
# Konfigdatei einlesen
|
|
open CONF, "<config.txt";
|
|
my @config=<CONF>;
|
|
close CONF;
|
|
|
|
foreach (@config) {
|
|
chomp;
|
|
next if ($_ =~ /^#/);
|
|
next if (length($_)==0);
|
|
$mcasts{$_}='';
|
|
}
|
|
|
|
# Threads für die einzelnen Multicastgruppen anstarten
|
|
foreach (keys %mcasts) { my $thr = new Thread (\&WaitForMessage,$_); print "$_ gestartet\n";}
|
|
|
|
# Startzeit, lesbar
|
|
my $t=localtime;
|
|
|
|
# startzeit, als fortlaufende zahl
|
|
$starttime = time;
|
|
|
|
# Anzeige aufbauen
|
|
while (1) {
|
|
#print "\e[2J\n";
|
|
print ".\n";
|
|
|
|
#aktuelle zeit, als fortlaufende zahl
|
|
my $atime=time;
|
|
|
|
#printf "program runs since %s (for %d seconds)\n\n", $t, $atime-$starttime;
|
|
#printf "%7s %15s %5s %s %s\n", "SESSION", "MCAST Grp", "Port", "Open", "Last Data";
|
|
|
|
%JSONdata=();
|
|
# "Status" | "Reserve" | "Reserve" | "Reserve" | "Reserve" | "Spaltenanzahl" | "Zeilenanzahl" | "Beschriftung" | "Spalte1" | "Spalte2";
|
|
#$JSONdata{'0'}="|||||||Session|Gruppe|Port|Open|Last";
|
|
BuildJsonLine(0,undef,undef,undef,undef,undef,undef,undef,"SESSION","IP","PORT","OPEN","LAST UPDATE");
|
|
my $jcount=1;
|
|
foreach (sort keys %mcasts) {
|
|
my ($session,$IP,$PORT)=split /:/,$_;
|
|
my $open="no";
|
|
$open="yes" if ($mcopen{$_} == 1);
|
|
my $line;
|
|
if ($mcdata{$_} eq "") {
|
|
$line = BuildJsonLine($jcount,undef,undef,undef,undef,undef,undef,undef,$session,$IP,$PORT,$open,"NEVER");
|
|
print "$line\n";
|
|
}
|
|
else {
|
|
if ($atime-$mcasts{$_} > $TimeOut) {
|
|
$line = BuildJsonLine($jcount,undef,undef,undef,undef,undef,undef,undef,$session,$IP,$PORT,$open,"> $TimeOut");
|
|
print "$line\n";
|
|
}
|
|
else {
|
|
my $tdiff=$atime-$mcasts{$_};
|
|
$line = BuildJsonLine($jcount,undef,undef,undef,undef,undef,undef,undef,$session,$IP,$PORT,$open,$tdiff);
|
|
print "$line\n";
|
|
}
|
|
}
|
|
$jcount++;
|
|
}
|
|
sleep 1;
|
|
}
|
|
}
|
|
sub WaitForMessage {
|
|
my ($IPPORT)=@_;
|
|
my ($SESSION, $IP, $PORT) = split/:/,$IPPORT;
|
|
#print "Öffne $IP $PORT\n"; <STDIN>;
|
|
my $sock;
|
|
|
|
$mcopen{$IPPORT}=0;
|
|
while (!$sock) {
|
|
$sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1);
|
|
sleep 1;
|
|
}
|
|
$sock->mcast_add($IP);
|
|
$mcopen{$IPPORT}=1;
|
|
my $data;
|
|
while (1) {
|
|
$sock->recv($data,4096);
|
|
$mcasts{$IPPORT}=time;
|
|
}
|
|
}
|
|
|
|
sub TCPServer {
|
|
POE::Component::Server::TCP->new(
|
|
Alias => "SERVER",
|
|
Port => 11211,
|
|
ClientInput => sub {
|
|
my ($session, $heap, $input) = @_[SESSION, HEAP, ARG0];
|
|
print "Session ", $session->ID(), " got input: $input\n";
|
|
#$heap->{client}->put($input);
|
|
#my $rip0 = $heap->{remote_ip};
|
|
#my $rport = $heap->{remote_port};
|
|
#my $id = $session->ID();
|
|
#my $log = scalar localtime(time).": Send data to session $id, client: $rip0, clientport: $rport";
|
|
#$logbox->insert('end', $log);
|
|
#$logbox->see('end');
|
|
if ($input eq "GETDATA") {
|
|
my $JSONObject = JSON::XS->new->ascii->pretty->allow_nonref();
|
|
my $senddata=$JSONObject->encode(\%JSONdata);
|
|
$heap->{client}->put($senddata);
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
sub BuildJsonLine {
|
|
my ($count,$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l)=@_;
|
|
my $line="$a|$b|$c|$d|$e|$f|$g|$h|$i|$j|$k|$l";
|
|
$JSONdata{"$count"}=$line;
|
|
return $line;
|
|
}
|
|
#fix {
|
|
sub StartMain {
|
|
|
|
}
|
|
#} fix |