add some handler code
[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                 } else {
41                         handle_ping_reply($dxchan, $self->{o}, $self->{ot}, $self->{oid});
42                 }
43         } else {
44                 $self->route($dxchan);
45         }
46 }
47
48 sub topcxx
49 {
50         my $self = shift;
51         unless (exists $self->{'-pcxx'}) {
52                 $self->{'-pcxx'} = DXProt::pc51($self->{to}, $self->{o}, $self->{s});
53         }
54         return $self->{'-pcxx'};
55 }
56
57 # add a ping request to the ping queues
58 sub add
59 {
60         my ($dxchan, $to, $via) = @_;
61         my $from = $dxchan->call;
62         my $ref = $pings{$to} || [];
63         my $r = {};
64         my $self = DXXml::Ping->new(to=>$to, '-hirestime'=>[ gettimeofday ], s=>'1');
65         $self->{u} = $from unless $from eq $main::mycall;
66         $self->{'-via'} = $via if $via && DXChannel::get($via);
67         $self->{o} = $main::mycall;
68         $self->{id} = $self->nextid;
69         $self->route($dxchan);
70
71         push @$ref, $self;
72         $pings{$to} = $ref;
73         my $u = DXUser->get_current($to);
74         if ($u) {
75                 $u->lastping(($via || $from), $main::systime);
76                 $u->put;
77         }
78 }
79
80 sub handle_ping_reply
81 {
82         my $fromdxchan = shift;
83         my $from = shift;
84         my $ot = shift;
85         my $oid = shift;
86         my $fromxml;
87         
88         if (ref $from) {
89                 $fromxml = $from;
90                 $from = $from->{o};
91         }
92
93         # it's a reply, look in the ping list for this one
94         my $ref = $pings{$from};
95         return unless $ref;
96
97         my $tochan = DXChannel::get($from);
98         while (@$ref) {
99                 my $r = shift @$ref;
100                 my $dxchan = DXChannel::get($r->{to});
101                 next unless $dxchan;
102                 my $t = tv_interval($r->{'-hirestime'}, [ gettimeofday ]);
103                 if ($dxchan->is_user) {
104                         my $s = sprintf "%.2f", $t; 
105                         my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
106                         $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
107                 } elsif ($dxchan->is_node) {
108                         if ($tochan) {
109                                 my $nopings = $tochan->user->nopings || $DXProt::obscount;
110                                 push @{$tochan->{pingtime}}, $t;
111                                 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
112                                 
113                                 # cope with a missed ping, this means you must set the pingint large enough
114                                 if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
115                                         $t -= $tochan->{pingint};
116                                 }
117                                 
118                                 # calc smoothed RTT a la TCP
119                                 if (@{$tochan->{pingtime}} == 1) {
120                                         $tochan->{pingave} = $t;
121                                 } else {
122                                         $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
123                                 }
124                                 $tochan->{nopings} = $nopings; # pump up the timer
125                                 if (my $ivp = Investigate::get($from, $fromdxchan->{call})) {
126                                         $ivp->handle_ping;
127                                 }
128                         } elsif (my $rref = Route::Node::get($r->{to})) {
129                                 if (my $ivp = Investigate::get($from, $fromdxchan->{to})) {
130                                         $ivp->handle_ping;
131                                 }
132                         }
133                 }
134         }
135 }
136
137 1;