+ # 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}} if exists $thing->{id};
+ $ref ||= $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;