+sub handle_ping_reply
+{
+ my $fromdxchan = shift;
+ my $from = shift;
+ my $ot = shift;
+ my $oid = shift;
+ my $fromxml;
+
+ if (ref $from) {
+ $fromxml = $from;
+ $from = $from->{o};
+ }
+
+ # it's a reply, look in the ping list for this one
+ my $ref = $pings{$from};
+ return unless $ref;
+
+ my $tochan = DXChannel::get($from);
+ while (@$ref) {
+ my $r = shift @$ref;
+ my $dxchan = DXChannel::get($r->{o});
+ next unless $dxchan;
+ my $t = tv_interval($r->{'-hirestime'}, [ gettimeofday ]);
+ if ($dxchan->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
+ dbg("ROUTE: $tochan->{call} ping obscount reset to $tochan->{nopings}") if isdbg('obscount');
+ my $nref = Route::Node::get($tochan->{call});
+ if ($nref) {
+ my $n = $nref->reset_obs;
+ dbg("ROUTE: reset obscount on $tochan->{call} to $n (ping)") if isdbg('obscount');
+ }
+ }
+ _handle_believe($from, $fromdxchan->{call});
+ }
+ if (exists $r->{u} && ($dxchan = DXChannel::get($r->{u})) && $dxchan->is_user) {
+ my $s = sprintf "%.2f", $t;
+ my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
+ $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
+ }
+ }
+}
+
+sub _handle_believe
+{
+ my ($from, $via) = @_;
+
+ my $dxchan = DXChannel::get($from);
+ my $user = $dxchan->user || DXUser::get($from);
+ if ($user) {
+ $user->set_believe($via);
+ $user->put unless $dxchan->{_nospawn};
+ }
+}