Files
scripts/Perl Multicast Receiver/client.pl_1.9
2024-10-14 00:08:40 +02:00

119 lines
3.4 KiB
Groff

#!/usr/bin/perl
$|=1;
use Thread;
use IO::Socket::Multicast;
#use Win32::Console::ANSI qw /Cursor Title/;
#use Term::ANSIScreen qw /:screen :cursor/;
use strict;
my %mcasts :shared;
my %mcdata :shared;
my %mcopen :shared;
my %stream :shared;
my $thr;
my $TimeOut = 10;
my $starttime;
my $lastdown;
my $lastdowns;
# Konfigdatei einlesen
open CONF, "<$0.conf";
my @config=<CONF>;
close CONF;
foreach (@config) {
chomp;
next if ($_ =~ /^#/);
next if (length($_)==0);
$_ =~ s/\r//g;
$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
open LOG, ">$0.log";
printf LOG "%15s | %15s | %5s | %15s | %s | %s\n", "SESSION", "MCAST Grp", "Port", "Interface", "Open", "Last Data";
close LOG;
while (1) {
print "\e[2J\n";
open LOG, ">>$0.log";
#aktuelle zeit, als fortlaufende zahl
my $atime=time;
printf "program runs since %s (for %d seconds)\n\n", $t, $atime-$starttime;
printf "%15s | %15s | %5s | %15s | %s | %s\n", "SESSION", "MCAST Grp", "Port", "Interface", "Open", "Last Data";
foreach (sort keys %mcasts) {
my ($session,$IP,$PORT,$IF)=split /:/,$_;
my $open="no";
$open="yes" if ($mcopen{$_} == 1);
if ($mcdata{$_} eq "") {
printf "%15s | %15s | %5s | %15s | %4s | %s\n", $session, $IP, $PORT, $IF, $open, 'NEVER';
}
else {
if ($atime-$mcasts{$_} > $TimeOut) {
printf "%15s | %15s | %5s | %15s | %4s | more than %d sec ago\n", $session, $IP, $PORT, $IF, $open, $TimeOut;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
$lastdown=localtime;
$lastdowns=$session;
if ($hour>=7 and $hour<=19) {
if ($stream{$_} == 1) {
#printf LOG "%s\n", scalar localtime;
printf LOG "%15s | %15s | %5s | %15s | %4s | at %s down for %d seconds\n", $session, $IP, $PORT, $IF, $open, scalar localtime, $TimeOut-1;
}
$stream{$_}=0;
}
}
else {
printf "%15s | %15s | %5s | %15s | %4s | %d seconds ago\n", $session, $IP, $PORT, $IF, $open, $atime-$mcasts{$_};
if ($stream{$_} == 0) {
#printf LOG "%s\n", scalar localtime;
printf LOG "%15s | %15s | %5s | %15s | %4s | live since %s\n", $session, $IP, $PORT, $IF, $open, scalar localtime;
}
$stream{$_}=1;
}
}
}
close LOG;
printf "\nLetzter Fehler: Session $lastdowns am $lastdown\n" if ($lastdowns ne "");
printf "\nKein Fehler seit Programmstart aufgetreten\n" if ($lastdowns eq "");
printf "\nv1.9 by Andre Geissler\n";
sleep 1;
}
close LOG;
# 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, $IF) = split/:/,$IPPORT;
my $sock;
$mcopen{$IPPORT}=0;
while (!$sock) {
$sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1);
sleep 1;
}
$sock->mcast_add($IP, $IF);
$mcopen{$IPPORT}=1;
#my $data;
while (1) {
my $data;
$sock->recv($data,0);
$mcdata{$IPPORT}="DATA"; # received in the last $TimeOut seconds"; #$data;
$mcasts{$IPPORT}=time;
}
}