Files
2024-10-14 00:08:40 +02:00

138 lines
4.2 KiB
Perl

#!/bin/perl
use Thread;
use IO::Socket::Multicast;
use Win32::Console::ANSI qw /Cursor Title/;
use Term::ANSIScreen qw /:screen :cursor/;
use POE;
use POE::Component::Server::TCP;
use JSON::XS;
use strict;
my %mcasts :shared;
my %mcdata :shared;
my %mcopen :shared;
my $thr;
my $TimeOut = 20;
my $starttime;
my %JSONdata :shared;
$thr = new Thread (\&Main);
#$data{"0"}="hallo0";
#$data{"1"}="hallo1";
TCPServer ();
POE::Kernel->run();
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) { $thr = new Thread (\&WaitForMessage,$_); }
# Startzeit, lesbar
my $t=localtime;
# startzeit, als fortlaufende zahl
$starttime = time;
# Anzeige aufbauen
while (1) {
#print "\e[2J\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";
my $jcount=1;
foreach (sort keys %mcasts) {
my ($session,$IP,$PORT)=split /:/,$_;
my $open="no";
$open="yes" if ($mcopen{$_} == 1);
if ($mcdata{$_} eq "") {
#printf "%7d %15s %5s %4s %s\n", $session, $IP, $PORT, $open, 'NEVER';
$JSONdata{"$jcount"}="|||||||$session|$IP|$PORT|$open|NEVER";
}
else {
if ($atime-$mcasts{$_} > $TimeOut) {
#printf "%7d %15s %5s %4s more than %d sec ago\n", $session, $IP, $PORT, $open, $TimeOut;
$JSONdata{"$jcount"}="|||||||$session|$IP|$PORT|$open|>$TimeOut";
}
else {
#printf "%7d %15s %5s %4s %d seconds ago\n", $session, $IP, $PORT, $open,$atime-$mcasts{$_};
my $tdiff=$atime-$mcasts{$_};
$JSONdata{"$jcount"}="|||||||$session|$IP|$PORT|$open|>$tdiff";
}
}
$jcount++;
}
#printf "\nv1.4 by Andre Wisniewski\n";
sleep 1;
}
}
# Baut Sockets für Multicast Empfang auf und sichert die zuletzt empfangenen Daten und dazu einen Zeitstempel
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);
$mcdata{$IPPORT}="DATA received in the last $TimeOut seconds"; #$data;
$mcasts{$IPPORT}=time;
}
}
sub TCPServer {
#my %daten=%{$_[0]};
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);
}
}
);
}