init III
This commit is contained in:
223
Perl JSON Server Client/MCast Receiver/mcastrcv_universal.pl
Normal file
223
Perl JSON Server Client/MCast Receiver/mcastrcv_universal.pl
Normal file
@@ -0,0 +1,223 @@
|
||||
#!/bin/perl
|
||||
use strict "vars";
|
||||
#use strict "refs";
|
||||
use strict "subs";
|
||||
|
||||
|
||||
use Thread;
|
||||
#use threads::shared;
|
||||
use Time::HiRes qw /usleep/;
|
||||
|
||||
use IO::Socket::Multicast;
|
||||
use POE;
|
||||
use POE::Loop::Select;
|
||||
use POE::Resource::Aliases;
|
||||
use POE::Resource::Events;
|
||||
use POE::Resource::Extrefs;
|
||||
use POE::Resource::FileHandles;
|
||||
use POE::Resource::SIDs;
|
||||
use POE::Resource::Sessions;
|
||||
use POE::Resource::Signals;
|
||||
#use POE::Resource::Statistics;
|
||||
|
||||
|
||||
my @a10; my @a11; my @a12; my @a13; my @a14; my @a15; my @a16; my @a17; my @a18; my @a19;
|
||||
my @a20; my @a21; my @a22; my @a23; my @a24; my @a25; my @a26; my @a27; my @a28; my @a29;
|
||||
my @a30; my @a31; my @a32; my @a33; my @a34; my @a35; my @a36; my @a37; my @a38; my @a39;
|
||||
|
||||
my @arrays = qw /a10 a11 a12 a13 a14 a15 a16 a17 a18 a19 a20 a21 a22 a23 a24 a25 a26 a27 a28 a29 a30 a31 a32 a33 a34 a35 a36 a37 a38 a39/;
|
||||
|
||||
use POE::Component::Server::TCP;
|
||||
use JSON::XS;
|
||||
|
||||
my %mcasts :shared;
|
||||
my %mcdata :shared;
|
||||
my %mcopen :shared;
|
||||
|
||||
my $TimeOut = 20;
|
||||
my $starttime;
|
||||
|
||||
my %JSONdata :shared;
|
||||
|
||||
my $TCPSERVERIP;
|
||||
my $TCPSERVERPORT;
|
||||
|
||||
if ($ARGV[0] eq "") {
|
||||
$TCPSERVERIP="0.0.0.0"; $TCPSERVERPORT="11211";
|
||||
}
|
||||
else {
|
||||
if ($ARGV[1] eq "") { wrong_start("Kein Port"); }
|
||||
|
||||
my $e=$ARGV[1];
|
||||
if ($e<1024 or $e>65535) {wrong_start("Ungültiger Port");}
|
||||
$TCPSERVERPORT = $e;
|
||||
|
||||
my ($a,$b,$c,$d) = split/\./,$ARGV[0];
|
||||
if ($ARGV[0] eq "0.0.0.0") {
|
||||
$TCPSERVERIP=0.0.0.0;
|
||||
}
|
||||
else {
|
||||
if ($a<1 or $a>255 or $b<0 or $b>255 or $c<0 or $c>255 or $d<1 or $d>255) { wrong_start("Ungültige IP"); }
|
||||
$TCPSERVERIP = $a . "." . $b . "." . $c . "." . "$d";
|
||||
}
|
||||
}
|
||||
|
||||
my $thr = new Thread(\&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
|
||||
#share(@seq2);
|
||||
my $i=0;
|
||||
my @aref;
|
||||
foreach (keys %mcasts) {
|
||||
my $arr=$arrays[$i];
|
||||
$aref[$i]=\@$arr;
|
||||
my $thr1 = new Thread(\&WaitForMessage,$_,$aref[$i]);
|
||||
# my $thr2 = new Thread(\&Write_Gaps,$i,$aref[$i]);
|
||||
$i++;
|
||||
}
|
||||
|
||||
# Startzeit, lesbar
|
||||
my $t=localtime;
|
||||
|
||||
$starttime = time;
|
||||
|
||||
POE::Component::Server::TCP->new(
|
||||
Alias => "SERVER",
|
||||
Port => $TCPSERVERPORT,
|
||||
Address => $TCPSERVERIP,
|
||||
ClientInput => sub {
|
||||
my ($session, $heap, $input) = @_[SESSION, HEAP, ARG0];
|
||||
print "Session ", $session->ID(), " got input: $input\n";
|
||||
CheckInput($session, $heap, $input);
|
||||
}
|
||||
);
|
||||
|
||||
POE::Kernel->run();
|
||||
|
||||
sub Main {
|
||||
# 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";
|
||||
# sp. zeil
|
||||
BuildJsonLine(0,undef,undef,undef,undef,$atime,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;
|
||||
my $jcountt="text".$jcount;
|
||||
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,$ARR)=@_;
|
||||
my ($SESSION, $IP, $PORT) = split/:/,$IPPORT;
|
||||
#print "Öffne $IP $PORT\n"; <STDIN>;
|
||||
my $sock;
|
||||
my @ta=@{$ARR};
|
||||
|
||||
$mcopen{$IPPORT}=0;
|
||||
while (!$sock) {
|
||||
$sock = IO::Socket::Multicast->new(LocalPort=>$PORT,ReuseAddr=>1);
|
||||
Time::HiRes::sleep(0.1);
|
||||
}
|
||||
$sock->mcast_add($IP);
|
||||
$mcopen{$IPPORT}=1;
|
||||
my $data;
|
||||
while (1) {
|
||||
$sock->recv($data,4096);
|
||||
|
||||
#print "$data\n";
|
||||
# my @a=split//,$data;
|
||||
|
||||
# my $b=$a[10];
|
||||
# my $c=$a[11];
|
||||
|
||||
# $b=unpack("Cn",$b);
|
||||
# $c=unpack("Cn",$c);
|
||||
|
||||
# my $d=($b<<16)+$c;
|
||||
|
||||
# push(@ta,$d);
|
||||
# print "\nRCV SEQ on $IPPORT\n$d\n@ta\n";
|
||||
|
||||
$mcasts{$IPPORT}=time;
|
||||
$mcdata{$IPPORT}=time;
|
||||
###
|
||||
sleep 1;
|
||||
}
|
||||
}
|
||||
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";
|
||||
#print "$count\n";
|
||||
$JSONdata{$count}=$line;
|
||||
return $line;
|
||||
}
|
||||
sub CheckInput {
|
||||
my ($session, $heap,$input)=@_;
|
||||
if ($input eq "GETDATA") {
|
||||
my $JSONObject = JSON::XS->new->ascii->pretty->allow_nonref();
|
||||
my $senddata=$JSONObject->encode(\%JSONdata);
|
||||
$heap->{client}->put($senddata);
|
||||
}
|
||||
}
|
||||
sub wrong_start {
|
||||
print "\n\n\t$_[0]\n\nAufruf:\n\nmcastrcv3 [<IP> <PORT>]\n\nOhne Angabe von IP und Port wird auf 0.0.0.0 11211 gehört\n\n";
|
||||
exit;
|
||||
}
|
||||
sub Write_Gaps {
|
||||
my ($a,$aref)=@_;
|
||||
my @ta=@{$aref};
|
||||
|
||||
while (1) {
|
||||
open FH, ">$a.txt";
|
||||
#print "@arr\n";
|
||||
foreach (@ta) {
|
||||
print FH "$_\n";
|
||||
print "\nWR SEQ\n$_\n";
|
||||
}
|
||||
close FH;
|
||||
sleep (30);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user