K records only come from $mycall
[spider.git] / perl / DXXml / Ping.pm
1 #
2 # XML Ping handler
3 #
4 #
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 Time::HiRes qw(gettimeofday tv_interval);
17
18 use vars qw(@ISA %pings);
19 @ISA = qw(DXXml);
20 %pings = ();                    # outstanding ping requests outbound
21
22 sub handle_input
23 {
24         my $self = shift;
25         my $dxchan = shift;
26         
27         if ($self->{to} eq $main::mycall) {
28                 if ($self->{s} eq '1') {
29                         my $rep = DXXml::Ping->new(to=>$self->{o}, 
30                                                                            s=>'0',
31                                                                            oid=>$self->{id},
32                                                                            ot=>$self->{t}
33                                                                           );
34                         $dxchan->send($rep->toxml);
35                         if ($dxchan->{outgoing} && abs($dxchan->{lastping} - $main::systime) < 15) {
36                                 $dxchan->{lastping} += $dxchan->{pingint} / 2; 
37                         }
38                 } else {
39                         handle_ping_reply($dxchan, $self->{o}, $self->{ot}, $self->{oid});
40                 }
41         } else {
42                 $self->route($dxchan);
43         }
44 }
45
46 sub topcxx
47 {
48         my $self = shift;
49         unless (exists $self->{'-pcxx'}) {
50                 $self->{'-pcxx'} = DXProt::pc51($self->{to}, $self->{o}, $self->{s});
51         }
52         return $self->{'-pcxx'};
53 }
54
55 # add a ping request to the ping queues
56 sub add
57 {
58         my ($dxchan, $to, $via) = @_;
59         my $from = $dxchan->call;
60         my $ref = $pings{$to} || [];
61         my $r = {};
62         my $self = DXXml::Ping->new(to=>$to, '-hirestime'=>[ gettimeofday ], s=>'1');
63         $self->{u} = $from unless $from eq $main::mycall;
64         $self->{'-via'} = $via if $via && DXChannel::get($via);
65         $self->{o} = $main::mycall;
66         $self->route($dxchan);
67
68         push @$ref, $self;
69         $pings{$to} = $ref;
70         my $u = DXUser->get_current($to);
71         if ($u) {
72                 $u->lastping(($via || $from), $main::systime);
73                 $u->put;
74         }
75 }
76
77 sub handle_ping_reply
78 {
79         my $fromdxchan = shift;
80         my $from = shift;
81         my $ot = shift;
82         my $oid = shift;
83         my $fromxml;
84         
85         if (ref $from) {
86                 $fromxml = $from;
87                 $from = $from->{o};
88         }
89
90         # it's a reply, look in the ping list for this one
91         my $ref = $pings{$from};
92         return unless $ref;
93
94         my $tochan = DXChannel::get($from);
95         while (@$ref) {
96                 my $r = shift @$ref;
97                 my $dxchan = DXChannel::get($r->{o});
98                 next unless $dxchan;
99                 my $t = tv_interval($r->{'-hirestime'}, [ gettimeofday ]);
100                 if ($dxchan->is_node) {
101                         if ($tochan) {
102                                 my $nopings = $tochan->user->nopings || $DXProt::obscount;
103                                 push @{$tochan->{pingtime}}, $t;
104                                 shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
105                                 
106                                 # cope with a missed ping, this means you must set the pingint large enough
107                                 if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
108                                         $t -= $tochan->{pingint};
109                                 }
110                                 
111                                 # calc smoothed RTT a la TCP
112                                 if (@{$tochan->{pingtime}} == 1) {
113                                         $tochan->{pingave} = $t;
114                                 } else {
115                                         $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
116                                 }
117                                 $tochan->{nopings} = $nopings; # pump up the timer
118                         }
119                         _handle_believe($from, $fromdxchan->{call});
120                 } 
121                 if (exists $r->{u} && ($dxchan = DXChannel::get($r->{u})) && $dxchan->is_user) {
122                         my $s = sprintf "%.2f", $t; 
123                         my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
124                         $dxchan->send($dxchan->msg('pingi', $from, $s, $ave))
125                 } 
126         }
127 }
128
129 sub _handle_believe
130 {
131         my ($from, $via) = @_;
132         
133         my $user = DXUser->get_current($from);
134         if ($user) {
135                 $user->set_believe($via);
136                 $user->put;
137         }
138 }
139 1;