02fc5cef51bea6caf1176bce93e4a98d4729d08a
[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);
26 @ISA = qw(Thingy);
27
28 my $id;
29
30 sub gen_Aranea
31 {
32         my $thing = shift;
33         unless ($thing->{Aranea}) {
34                 $thing->{Aranea} = Aranea::genmsg($thing, qw(id out o));
35         }
36         return $thing->{Aranea};
37 }
38
39 sub from_Aranea
40 {
41         my $thing = shift;
42         return unless $thing;
43         return $thing;
44 }
45
46 sub gen_DXProt
47 {
48         my $thing = shift;
49         my $dxchan = shift;
50         unless ($thing->{DXProt}) {
51                 # we need to tease out the nodes out of all of this.
52                 # bear in mind that a proxied PC prot node could be in
53                 # {user} as well as a true user and also it may not
54                 # have originated here.
55
56                 my $from = $thing->{o};
57             $from ||= $thing->{user} if Route::Node::get($thing->{user});
58                 $from ||= $thing->{origin};
59                 my $to = $thing->{touser} if Route::Node::get($thing->{touser});
60                 $to ||= $thing->{group};
61
62                 
63                 $thing->{DXProt} = DXProt::pc51($to, $from, $thing->{out});
64         }
65         return $thing->{DXProt};
66 }
67
68 sub gen_DXCommandmode
69 {
70         my $thing = shift;
71         my $dxchan = shift;
72         my $buf;
73
74         return $buf;
75 }
76
77 sub from_DXProt
78 {
79         my $thing = ref $_[0] ? shift : $_[0]->SUPER::new();
80         
81         while (@_) {
82                 my $k = shift;
83                 $thing->{$k} = shift;
84         }
85         return $thing;
86 }
87
88 sub handle
89 {
90         my $thing = shift;
91         my $dxchan = shift;
92
93         # is it for us?
94         if ($thing->{group} eq $main::mycall) {
95                 if ($thing->{out} == 1) {
96                         my $repthing = $thing->new_reply;
97                         $repthing->{out} = 0;
98                         $repthing->{id} = $thing->{id};
99                         $repthing->{o} = $thing->{o} if $thing->{o};
100                         $repthing->send($dxchan) if $repthing;
101                 } else {
102
103                         # it's a reply, look in the ping list for this one
104                         my $ref = $ping{$thing->{id}} if exists $thing->{id};
105                         $ref ||= find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group}));
106                         if ($ref) {
107                                 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
108                                 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
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                                 }
127                                 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
128                                         if ($dxc->is_user) {
129                                                 my $s = sprintf "%.2f", $t; 
130                                                 my $ave = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
131                                                 $dxc->send($dxc->msg('pingi', ($ref->{touser} || $ref->{group}), $s, $ave))
132                                         } 
133                                 }
134                                 delete $ping{$ref->{id}};
135                         }
136                 }
137         } else {
138                 $thing->{route} = $thing->{o} if $thing->{o};
139                 $thing->broadcast($dxchan);
140         }
141 }
142
143 # this just creates a ping for onward transmission
144 # remember it if you want to ping someone from here     
145 sub new_ping
146 {
147         my $pkg = shift;
148         my $thing = $pkg->SUPER::new(@_);
149 }
150
151 # do this for pings we generate ourselves
152 sub remember
153 {
154         my $thing = shift;
155         $thing->{t} = [ gettimeofday ];
156         $thing->{out} = 1;
157         $thing->{id} = ++$id;
158         my $u = DXUser->get_current($thing->{to});
159         if ($u) {
160                 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
161                 $u->put;
162         }
163         $ping{$id} = $thing;
164 }
165
166 # remove any pings outstanding that we have remembered for this
167 # callsign, return the number of forgotten pings
168 sub forget
169 {
170         my $call = shift;
171         my $count = 0;
172         my @out;        
173         foreach my $thing (values %ping) {
174                 if (($thing->{user} || $thing->{group}) eq $call) {
175                         $count++;
176                         delete $ping{$thing->{id}};
177                 }
178         }
179         return $count;
180 }
181
182 sub find
183 {
184         my $to = shift;
185         my $from = shift;
186         my $user = shift;
187         
188         foreach my $thing (values %ping) {
189                 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
190                         if ($user) {
191                                 return if $thing->{user} && $thing->{user} eq $user; 
192                         } else {
193                                 return $thing;
194                         }
195                 }
196         }
197         return undef;
198 }
199 1;