X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FThingy%2FPing.pm;h=edcd8fe741927acec6282f0ecd666cd195582ce6;hb=dca951f539a50c396d7a94ad9f513a19892d54ad;hp=6fe808b53423a45d23b6712191dba3d9a44bc3ea;hpb=4d22d5fd3874e8292d82f84a777b99ff7d10402a;p=spider.git diff --git a/perl/Thingy/Ping.pm b/perl/Thingy/Ping.pm index 6fe808b5..edcd8fe7 100644 --- a/perl/Thingy/Ping.pm +++ b/perl/Thingy/Ping.pm @@ -19,15 +19,19 @@ use DXDebug; use DXUtil; use Thingy; use Spot; +use Time::HiRes qw(gettimeofday tv_interval); -use vars qw(@ISA); + +use vars qw(@ISA %ping); @ISA = qw(Thingy); +my $id; + sub gen_Aranea { my $thing = shift; unless ($thing->{Aranea}) { - $thing->{Aranea} = Aranea::genmsg($thing); + $thing->{Aranea} = Aranea::genmsg($thing, qw(id)); } return $thing->{Aranea}; } @@ -57,7 +61,8 @@ sub gen_DXCommandmode sub from_DXProt { - my $thing = shift; + my $thing = ref $_[0] ? shift : $_[0]->SUPER::new(); + while (@_) { my $k = shift; $thing->{$k} = shift; @@ -70,7 +75,102 @@ sub handle my $thing = shift; my $dxchan = shift; - $thing->broadcast($dxchan); + # is it for us? + if ($thing->{group} eq $main::mycall) { + if ($thing->{out} == 1) { + my $repthing = $thing->new_reply; + $repthing->{out} = 0; + $repthing->{id} = $thing->{id}; + $repthing->send($dxchan) if $repthing; + } else { + + # it's a reply, look in the ping list for this one + my $ref = $ping{$thing->{id}} || $thing->find; + if ($ref) { + my $t = tv_interval($thing->{t}, [ gettimeofday ]); + if (my $dxc = DXChannel::get($thing->{user} || $thing->{origin})) { + + my $tochan = DXChannel::get($thing->{touser} || $thing->{group}); + + if ($dxc->is_user) { + my $s = sprintf "%.2f", $t; + my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t; + $dxc->send($dxc->msg('pingi', ($thing->{touser} || $thing->{group}), $s, $ave)) + } elsif ($dxc->is_node) { + if ($tochan ) { + my $nopings = $tochan->user->nopings || $DXProt::obscount; + push @{$tochan->{pingtime}}, $t; + shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6; + + # cope with a missed ping, this means you must set the pingint large enough + if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) { + $t -= $tochan->{pingint}; + } + + # calc smoothed RTT a la TCP + if (@{$tochan->{pingtime}} == 1) { + $tochan->{pingave} = $t; + } else { + $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6); + } + $tochan->{nopings} = $nopings; # pump up the timer + } + } + } + } + } + } else { + $thing->broadcast($dxchan); + } +} + +# this just creates a ping for onward transmission +# remember it if you want to ping someone from here +sub new_ping +{ + my $pkg = shift; + my $thing = $pkg->SUPER::new(@_); +} + +# do this for pings we generate ourselves +sub remember +{ + my $thing = shift; + $thing->{t} = [ gettimeofday ]; + $thing->{out} = 1; + $thing->{id} = ++$id; + my $u = DXUser->get_current($thing->{to}); + if ($u) { + $u->lastping(($thing->{user} || $thing->{group}), $main::systime); + $u->put; + } + $ping{$id} = $thing; +} + +# remove any pings outstanding that we have remembered for this +# callsign, return the number of forgotten pings +sub forget +{ + my $call = shift; + my $count = 0; + my @out; + foreach my $thing (values %ping) { + if (($thing->{user} || $thing->{group}) eq $call) { + $count++; + delete $ping{$thing->{id}}; + } + } + return $count; } +sub find +{ + my $call = shift; + foreach my $thing (values %ping) { + if (($thing->{user} || $thing->{origin}) eq $call) { + return $thing; + } + } + return undef; +} 1;