make it a pure b/c if it isn't for me
[spider.git] / perl / Thingy / Ping.pm
1 #
2 # Ping Thingy handling
3 #
4 # $Id$
5 #
6 # Copyright (c) 2005 Dirk Koopman G1TLH
7 #
8
9 use strict;
10
11 package Thingy::Ping;
12
13 use vars qw($VERSION $BRANCH);
14
15 main::mkver($VERSION = q$Revision$);
16
17 use DXChannel;
18 use DXDebug;
19 use DXUtil;
20 use Thingy;
21 use Spot;
22 use Time::HiRes qw(gettimeofday tv_interval);
23
24
25 use vars qw(@ISA %ping $ping_ttl);
26 @ISA = qw(Thingy);
27
28 my $id;
29 $ping_ttl = 300;                                # default ping ttl
30
31
32 sub gen_Aranea
33 {
34         my $thing = shift;
35         unless ($thing->{Aranea}) {
36                 $thing->{Aranea} = Aranea::genmsg($thing, qw(id out o));
37         }
38         return $thing->{Aranea};
39 }
40
41 sub from_Aranea
42 {
43         my $thing = shift;
44         return unless $thing;
45         return $thing;
46 }
47
48 sub gen_DXProt
49 {
50         my $thing = shift;
51         my $dxchan = shift;
52         unless ($thing->{DXProt}) {
53                 # we need to tease out the nodes out of all of this.
54                 # bear in mind that a proxied PC prot node could be in
55                 # {user} as well as a true user and also it may not
56                 # have originated here.
57
58                 my $from = $thing->{o} if $thing->{out};
59             $from ||= $thing->{user} if Route::Node::get($thing->{user});
60                 $from ||= $thing->{origin};
61                 my $to = $thing->{o} unless $thing->{out};
62                 $to ||= $thing->{touser} if Route::Node::get($thing->{touser});
63                 $to ||= $thing->{group};
64
65                 
66                 $thing->{DXProt} = DXProt::pc51($to, $from, $thing->{out});
67         }
68         return $thing->{DXProt};
69 }
70
71 sub gen_DXCommandmode
72 {
73         my $thing = shift;
74         my $dxchan = shift;
75         my $buf = $dxchan->msg('pingi', ($thing->{user} || $thing->{origin}), $thing->{ft}, $thing->{fave});
76         return $buf;
77 }
78
79 # called with the dxchan, line and the split out arguments
80 sub from_DXProt
81 {
82         my $thing = ref $_[0] ? shift : $_[0]->SUPER::new(origin=>$main::mycall);
83         my $dxchan = shift;
84         $thing->{DXProt} = shift;
85         shift;                                          # PC51
86         $thing->{group} = shift;        # to call
87         my $from = shift;
88         $thing->{out} = shift;          # 1 = ping, 0 = pong;
89         $thing->{user} = $dxchan->{call};
90         $thing->{o} = $from unless $from eq $dxchan->{call};
91         $thing->remember if $thing->{out} && $thing->{group} ne $main::mycall;
92         return $thing;
93 }
94
95 sub handle
96 {
97         my $thing = shift;
98         my $dxchan = shift;
99
100         # is it for us?
101         if ($thing->{group} eq $main::mycall) {
102                 if ($thing->{out} == 1) {
103                         my $repthing = $thing->new_reply;
104                         $repthing->{out} = 0;
105                         $repthing->{id} = $thing->{id};
106                         $repthing->{o} = $thing->{o} if $thing->{o};
107                         $repthing->send($dxchan) if $repthing;
108                 } else {
109
110                         # it's a reply, look in the ping list for this one
111                         my $ref = $ping{$thing->{id}} if exists $thing->{id};
112                         $ref ||= find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group}));
113                         if ($ref) {
114                                 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
115                                 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
116                                 if ($tochan) {
117                                         my $nopings = $tochan->user->nopings || $DXProt::obscount;
118                                         push @{$tochan->{pingtime}}, $t;
119                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
120                                         
121                                         # cope with a missed ping, this means you must set the pingint large enough
122                                         if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
123                                                 $t -= $tochan->{pingint};
124                                         }
125                                         
126                                         # calc smoothed RTT a la TCP
127                                         if (@{$tochan->{pingtime}} == 1) {
128                                                 $tochan->{pingave} = $t;
129                                         } else {
130                                                 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
131                                         }
132                                         $tochan->{nopings} = $nopings; # pump up the timer
133                                 }
134                                 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
135                                         $thing->{ft} = sprintf "%.2f", $t;
136                                         $thing->{fave} = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
137                                         $thing->send($dxc);
138                                 }
139                                 delete $ping{$ref->{id}};
140                         }
141                 }
142         } else {
143                 $thing->broadcast($dxchan);
144         }
145 }
146
147 # this just creates a ping for onward transmission
148 # remember it if you want to ping someone from here     
149 sub new_ping
150 {
151         my $pkg = shift;
152         my $thing = $pkg->SUPER::new(@_);
153 }
154
155 # do this for pings we generate ourselves
156 sub remember
157 {
158         my $thing = shift;
159         $thing->{t} = [ gettimeofday ];
160         $thing->{out} = 1;
161         $thing->{id} = ++$id;
162         my $u = DXUser->get_current($thing->{to});
163         if ($u) {
164                 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
165                 $u->put;
166         }
167         $ping{$id} = $thing;
168 }
169
170 # remove any pings outstanding that we have remembered for this
171 # callsign, return the number of forgotten pings
172 sub forget
173 {
174         my $call = shift;
175         my $count = 0;
176         my @out;        
177         foreach my $thing (values %ping) {
178                 if (($thing->{user} || $thing->{group}) eq $call) {
179                         $count++;
180                         delete $ping{$thing->{id}};
181                 }
182         }
183         return $count;
184 }
185
186 sub find
187 {
188         my $to = shift;
189         my $from = shift;
190         my $user = shift;
191         
192         foreach my $thing (values %ping) {
193                 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
194                         if ($user) {
195                                 return if $thing->{user} && $thing->{user} eq $user; 
196                         } else {
197                                 return $thing;
198                         }
199                 }
200         }
201         return undef;
202 }
203 1;