6 # Copyright (c) Dirk Koopman, G1TLH
17 use Time::HiRes qw(gettimeofday tv_interval);
19 use vars qw(@ISA %pings);
21 %pings = (); # outstanding ping requests outbound
28 if ($self->{to} eq $main::mycall) {
29 if ($self->{s} eq '1') {
30 my $rep = DXXml::Ping->new(to=>$self->{o},
35 $dxchan->send($rep->toxml);
36 if ($dxchan->{outgoing} && abs($dxchan->{lastping} - $main::systime) < 15) {
37 $dxchan->{lastping} += $dxchan->{pingint} / 2;
40 handle_ping_reply($dxchan, $self->{o}, $self->{ot}, $self->{oid});
43 $self->route($dxchan);
50 unless (exists $self->{'-pcxx'}) {
51 $self->{'-pcxx'} = DXProt::pc51($self->{to}, $self->{o}, $self->{s});
53 return $self->{'-pcxx'};
56 # add a ping request to the ping queues
59 my ($dxchan, $to, $via) = @_;
60 my $from = $dxchan->call;
61 my $ref = $pings{$to} || [];
63 my $self = DXXml::Ping->new(to=>$to, '-hirestime'=>[ gettimeofday ], s=>'1');
64 $self->{u} = $from unless $from eq $main::mycall;
65 $self->{'-via'} = $via if $via && DXChannel::get($via);
66 $self->{o} = $main::mycall;
67 $self->route($dxchan);
71 my $u = DXUser->get_current($to);
73 $u->lastping(($via || $from), $main::systime);
80 my $fromdxchan = shift;
91 # it's a reply, look in the ping list for this one
92 my $ref = $pings{$from};
95 my $tochan = DXChannel::get($from);
98 my $dxchan = DXChannel::get($r->{o});
100 my $t = tv_interval($r->{'-hirestime'}, [ gettimeofday ]);
101 if ($dxchan->is_node) {
103 my $nopings = $tochan->user->nopings || $DXProt::obscount;
104 push @{$tochan->{pingtime}}, $t;
105 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
107 # cope with a missed ping, this means you must set the pingint large enough
108 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
109 $t -= $tochan->{pingint};
112 # calc smoothed RTT a la TCP
113 if (@{$tochan->{pingtime}} == 1) {
114 $tochan->{pingave} = $t;
116 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
118 $tochan->{nopings} = $nopings; # pump up the timer
120 _handle_believe($from, $fromdxchan->{call});
122 if (exists $r->{u} && ($dxchan = DXChannel::get($r->{u})) && $dxchan->is_user) {
123 my $s = sprintf "%.2f", $t;
124 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
125 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
132 my ($from, $via) = @_;
134 if (my $ivp = Investigate::get($from, $via)) {
137 my $user = DXUser->get_current($from);
139 $user->set_believe($via);