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);
42 handle_ping_reply($dxchan, $self->{o}, $self->{ot}, $self->{oid});
45 $self->route($dxchan);
52 unless (exists $self->{'-pcxx'}) {
53 $self->{'-pcxx'} = DXProt::pc51($self->{to}, $self->{o}, $self->{s});
55 return $self->{'-pcxx'};
58 # add a ping request to the ping queues
61 my ($dxchan, $to, $via) = @_;
62 my $from = $dxchan->call;
63 my $ref = $pings{$to} || [];
65 my $self = DXXml::Ping->new(to=>$to, '-hirestime'=>[ gettimeofday ], s=>'1');
67 $self->{'-via'} = $via if $via && DXChannel::get($via);
68 $self->{o} = $main::mycall;
69 $self->route($dxchan);
73 my $u = DXUser->get_current($to);
75 $u->lastping(($via || $from), $main::systime);
82 my $fromdxchan = shift;
93 # it's a reply, look in the ping list for this one
94 my $ref = $pings{$from};
97 my $tochan = DXChannel::get($from);
100 my $dxchan = DXChannel::get($r->{u});
102 my $t = tv_interval($r->{'-hirestime'}, [ gettimeofday ]);
103 if ($dxchan->is_node) {
105 my $nopings = $tochan->user->nopings || $DXProt::obscount;
106 push @{$tochan->{pingtime}}, $t;
107 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
109 # cope with a missed ping, this means you must set the pingint large enough
110 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
111 $t -= $tochan->{pingint};
114 # calc smoothed RTT a la TCP
115 if (@{$tochan->{pingtime}} == 1) {
116 $tochan->{pingave} = $t;
118 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
120 $tochan->{nopings} = $nopings; # pump up the timer
122 _handle_believe($from, $fromdxchan->{call});
123 } elsif ($dxchan->is_user) {
124 my $s = sprintf "%.2f", $t;
125 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
126 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
133 my ($from, $via) = @_;
135 if (my $ivp = Investigate::get($from, $via)) {
138 my $user = DXUser->get_current($from);
140 $user->set_believe($via);