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');
66 $self->{u} = $from unless $from eq $main::mycall;
67 $self->{'-via'} = $via if $via && DXChannel::get($via);
68 $self->{o} = $main::mycall;
69 $self->{id} = $self->nextid;
70 $self->route($dxchan);
74 my $u = DXUser->get_current($to);
76 $u->lastping(($via || $from), $main::systime);
83 my $fromdxchan = shift;
94 # it's a reply, look in the ping list for this one
95 my $ref = $pings{$from};
98 my $tochan = DXChannel::get($from);
101 my $dxchan = DXChannel::get($r->{to});
103 my $t = tv_interval($r->{'-hirestime'}, [ gettimeofday ]);
104 if ($dxchan->is_user) {
105 my $s = sprintf "%.2f", $t;
106 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
107 $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
108 } elsif ($dxchan->is_node) {
110 my $nopings = $tochan->user->nopings || $DXProt::obscount;
111 push @{$tochan->{pingtime}}, $t;
112 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
114 # cope with a missed ping, this means you must set the pingint large enough
115 if ($t > $tochan->{pingint} && $t < 2 * $tochan->{pingint} ) {
116 $t -= $tochan->{pingint};
119 # calc smoothed RTT a la TCP
120 if (@{$tochan->{pingtime}} == 1) {
121 $tochan->{pingave} = $t;
123 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
125 $tochan->{nopings} = $nopings; # pump up the timer
126 if (my $ivp = Investigate::get($from, $fromdxchan->{call})) {
129 } elsif (my $rref = Route::Node::get($r->{to})) {
130 if (my $ivp = Investigate::get($from, $fromdxchan->{to})) {