6 # Copyright (c) Dirk Koopman, G1TLH
17 use Time::HiRes qw(gettimeofday tv_interval);
19 use vars qw($VERSION $BRANCH @ISA %pings);
20 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
21 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
22 $main::build += $VERSION;
23 $main::branch += $BRANCH;
26 %pings = (); # outstanding ping requests outbound
33 if ($self->{to} eq $main::mycall) {
34 if ($self->{s} eq '1') {
35 my $rep = DXXml::Ping->new(to=>$self->{o},
40 $dxchan->send($rep->toxml);
41 if ($dxchan->{outgoing} && abs($dxchan->{lastping} - $main::systime) < 15) {
42 $dxchan->{lastping} += $dxchan->{pingint} / 2;
45 handle_ping_reply($dxchan, $self->{o}, $self->{ot}, $self->{oid});
48 $self->route($dxchan);
55 unless (exists $self->{'-pcxx'}) {
56 $self->{'-pcxx'} = DXProt::pc51($self->{to}, $self->{o}, $self->{s});
58 return $self->{'-pcxx'};
61 # add a ping request to the ping queues
64 my ($dxchan, $to, $via) = @_;
65 my $from = $dxchan->call;
66 my $ref = $pings{$to} || [];
68 my $self = DXXml::Ping->new(to=>$to, '-hirestime'=>[ gettimeofday ], s=>'1');
69 $self->{u} = $from unless $from eq $main::mycall;
70 $self->{'-via'} = $via if $via && DXChannel::get($via);
71 $self->{o} = $main::mycall;
72 $self->route($dxchan);
76 my $u = DXUser->get_current($to);
78 $u->lastping(($via || $from), $main::systime);
85 my $fromdxchan = shift;
96 # it's a reply, look in the ping list for this one
97 my $ref = $pings{$from};
100 my $tochan = DXChannel::get($from);
103 my $dxchan = DXChannel::get($r->{o});
105 my $t = tv_interval($r->{'-hirestime'}, [ gettimeofday ]);
106 if ($dxchan->is_node) {
108 my $nopings = $tochan->user->nopings || $DXProt::obscount;
109 push @{$tochan->{pingtime}}, $t;
110 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
112 # cope with a missed ping, this means you must set the pingint large enough
113 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
114 $t -= $tochan->{pingint};
117 # calc smoothed RTT a la TCP
118 if (@{$tochan->{pingtime}} == 1) {
119 $tochan->{pingave} = $t;
121 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
123 $tochan->{nopings} = $nopings; # pump up the timer
125 _handle_believe($from, $fromdxchan->{call});
127 if (exists $r->{u} && ($dxchan = DXChannel::get($r->{u})) && $dxchan->is_user) {
128 my $s = sprintf "%.2f", $t;
129 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
130 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
137 my ($from, $via) = @_;
139 if (my $ivp = Investigate::get($from, $via)) {
142 my $user = DXUser->get_current($from);
144 $user->set_believe($via);