]> gb7djk.dxcluster.net Git - spider.git/blob - perl/Thingy/Ping.pm
change touser login in gen_DXProt
[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} unless Route::User::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;
76         if ($dxchan->{call} eq $thing->{touser}) {
77                 $buf = $dxchan->msg('pingi', ($thing->{user} || $thing->{origin}), $thing->{ft}, $thing->{fave});
78         }
79         return $buf;
80 }
81
82 # called with the dxchan, line and the split out arguments
83 sub from_DXProt
84 {
85         my $thing = ref $_[0] ? shift : $_[0]->SUPER::new(origin=>$main::mycall);
86         my $dxchan = shift;
87         $thing->{DXProt} = shift;
88         shift;                                          # PC51
89         $thing->{group} = shift;        # to call
90         my $from = shift;
91         $thing->{out} = shift;          # 1 = ping, 0 = pong;
92         $thing->{user} = $dxchan->{call};
93         $thing->{o} = $from unless $from eq $dxchan->{call};
94         $thing->remember if $thing->{out} && $thing->{group} ne $main::mycall;
95         return $thing;
96 }
97
98 sub handle
99 {
100         my $thing = shift;
101         my $dxchan = shift;
102
103         # is it for us?
104         if ($thing->{group} eq $main::mycall) {
105                 if ($thing->{out} == 1) {
106                         my $repthing = $thing->new_reply;
107                         $repthing->{out} = 0;
108                         $repthing->{id} = $thing->{id};
109                         $repthing->{o} = $thing->{o} if $thing->{o};
110                         $repthing->send($dxchan) if $repthing;
111                 } else {
112
113                         # it's a reply, look in the ping list for this one
114                         my $ref = $ping{$thing->{id}} if exists $thing->{id};
115                         $ref ||= find(($thing->{user}||$thing->{origin}), ($thing->{touser}||$thing->{group}));
116                         if ($ref) {
117                                 my $t = tv_interval($ref->{t}, [ gettimeofday ]);
118                                 my $tochan = DXChannel::get($ref->{touser} || $ref->{group});
119                                 if ($tochan) {
120                                         my $nopings = $tochan->user->nopings || $DXProt::obscount;
121                                         push @{$tochan->{pingtime}}, $t;
122                                         shift @{$tochan->{pingtime}} if @{$tochan->{pingtime}} > 6;
123                                         
124                                         # cope with a missed ping, this means you must set the pingint large enough
125                                         if ($t > $tochan->{pingint}  && $t < 2 * $tochan->{pingint} ) {
126                                                 $t -= $tochan->{pingint};
127                                         }
128                                         
129                                         # calc smoothed RTT a la TCP
130                                         if (@{$tochan->{pingtime}} == 1) {
131                                                 $tochan->{pingave} = $t;
132                                         } else {
133                                                 $tochan->{pingave} = $tochan->{pingave} + (($t - $tochan->{pingave}) / 6);
134                                         }
135                                         $tochan->{nopings} = $nopings; # pump up the timer
136                                 }
137                                 if (my $dxc = DXChannel::get($ref->{user} || $ref->{origin})) {
138                                         $thing->{ft} = sprintf "%.2f", $t;
139                                         $thing->{fave} = sprintf "%.2f", $tochan ? ($tochan->{pingave} || $t) : $t;
140                                         $thing->send($dxc);
141                                 }
142                                 delete $ping{$ref->{id}};
143                         }
144                 }
145         } else {
146                 $thing->broadcast($dxchan);
147         }
148 }
149
150 # this just creates a ping for onward transmission
151 # remember it if you want to ping someone from here     
152 sub new_ping
153 {
154         my $pkg = shift;
155         my $thing = $pkg->SUPER::new(@_);
156 }
157
158 # do this for pings we generate ourselves
159 sub remember
160 {
161         my $thing = shift;
162         $thing->{t} = [ gettimeofday ];
163         $thing->{out} = 1;
164         $thing->{id} = ++$id;
165         my $u = DXUser->get_current($thing->{to});
166         if ($u) {
167                 $u->lastping(($thing->{user} || $thing->{group}), $main::systime);
168                 $u->put;
169         }
170         $ping{$id} = $thing;
171 }
172
173 # remove any pings outstanding that we have remembered for this
174 # callsign, return the number of forgotten pings
175 sub forget
176 {
177         my $call = shift;
178         my $count = 0;
179         my @out;        
180         foreach my $thing (values %ping) {
181                 if (($thing->{user} || $thing->{group}) eq $call) {
182                         $count++;
183                         delete $ping{$thing->{id}};
184                 }
185         }
186         return $count;
187 }
188
189 sub find
190 {
191         my $to = shift;
192         my $from = shift;
193         my $user = shift;
194         
195         foreach my $thing (values %ping) {
196                 if ($thing->{origin} eq $from && $thing->{group} eq $to) {
197                         if ($user) {
198                                 return if $thing->{user} && $thing->{user} eq $user; 
199                         } else {
200                                 return $thing;
201                         }
202                 }
203         }
204         return undef;
205 }
206 1;