]> gb7djk.dxcluster.net Git - spider.git/blob - perl/DXXml/Ping.pm
1e7aea6fb85d219be60ee3fceadf87313256043b
[spider.git] / perl / DXXml / Ping.pm
1 #
2 # XML Ping handler
3 #
4 # $Id$
5 #
6 # Copyright (c) Dirk Koopman, G1TLH
7 #
8
9 use strict;
10
11 package DXXml::Ping;
12
13 use DXDebug;
14 use DXProt;
15 use IsoTime;
16 use Investigate;
17 use Time::HiRes qw(gettimeofday tv_interval);
18
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;
24
25 @ISA = qw(DXXml);
26 %pings = ();                    # outstanding ping requests outbound
27
28 sub handle_input
29 {
30         my $self = shift;
31         my $dxchan = shift;
32         
33         if ($self->{to} eq $main::mycall) {
34                 if ($self->{s} eq '1') {
35                         my $rep = DXXml::Ping->new(to=>$self->{o}, 
36                                                                            s=>'0',
37                                                                            oid=>$self->{id},
38                                                                            ot=>$self->{t}
39                                                                           );
40                         $dxchan->send($rep->toxml);
41                 } else {
42                         handle_ping_reply($dxchan, $self->{o}, $self->{ot}, $self->{oid});
43                 }
44         } else {
45                 $self->route($dxchan);
46         }
47 }
48
49 sub topcxx
50 {
51         my $self = shift;
52         unless (exists $self->{'-pcxx'}) {
53                 $self->{'-pcxx'} = DXProt::pc51($self->{to}, $self->{o}, $self->{s});
54         }
55         return $self->{'-pcxx'};
56 }
57
58 # add a ping request to the ping queues
59 sub add
60 {
61         my ($dxchan, $to, $via) = @_;
62         my $from = $dxchan->call;
63         my $ref = $pings{$to} || [];
64         my $r = {};
65         my $self = DXXml::Ping->new(to=>$to, '-hirestime'=>[ gettimeofday ], s=>'1');
66         $self->{u} = $from;
67         $self->{'-via'} = $via if $via && DXChannel::get($via);
68         $self->{o} = $main::mycall;
69         $self->{id} = $self->nextid;
70         $self->route($dxchan);
71
72         push @$ref, $self;
73         $pings{$to} = $ref;
74         my $u = DXUser->get_current($to);
75         if ($u) {
76                 $u->lastping(($via || $from), $main::systime);
77                 $u->put;
78         }
79 }
80
81 sub handle_ping_reply
82 {
83         my $fromdxchan = shift;
84         my $from = shift;
85         my $ot = shift;
86         my $oid = shift;
87         my $fromxml;
88         
89         if (ref $from) {
90                 $fromxml = $from;
91                 $from = $from->{o};
92         }
93
94         # it's a reply, look in the ping list for this one
95         my $ref = $pings{$from};
96         return unless $ref;
97
98         my $tochan = DXChannel::get($from);
99         while (@$ref) {
100                 my $r = shift @$ref;
101                 my $dxchan = DXChannel::get($r->{u});
102                 next unless $dxchan;
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) {
109                         if ($tochan) {
110                                 my $nopings = $tochan->user->nopings || $DXProt::obscount;
111                                 push @{$tochan->{pingtime}}, $t;
112                                 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
113                                 
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};
117                                 }
118                                 
119                                 # calc smoothed RTT a la TCP
120                                 if (@{$tochan->{pingtime}} == 1) {
121                                         $tochan->{pingave} = $t;
122                                 } else {
123                                         $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
124                                 }
125                                 $tochan->{nopings} = $nopings; # pump up the timer
126                                 if (my $ivp = Investigate::get($from, $fromdxchan->{call})) {
127                                         $ivp->handle_ping;
128                                 }
129                         } elsif (my $rref = Route::Node::get($r->{to})) {
130                                 if (my $ivp = Investigate::get($from, $fromdxchan->{to})) {
131                                         $ivp->handle_ping;
132                                 }
133                         }
134                 }
135         }
136 }
137
138 1;