36d1d20f9db5e20e5d09a75a5dabbde5d078342c
[spider.git] / perl / DXCluster.pm
1 #
2 # DX database control routines
3 #
4 # This manages the on-line cluster user 'database'
5 #
6 # This should all be pretty trees and things, but for now I
7 # just can't be bothered. If it becomes an issue I shall
8 # address it.
9 #
10 # Copyright (c) 1998 - Dirk Koopman G1TLH
11 #
12 # $Id$
13 #
14
15 package DXCluster;
16
17 use DXDebug;
18 use DXUtil;
19
20 use strict;
21 use vars qw(%cluster %valid);
22
23 %cluster = ();                                  # this is where we store the dxcluster database
24
25 %valid = (
26                   mynode => '0,Parent Node',
27                   call => '0,Callsign',
28                   confmode => '0,Conference Mode,yesno',
29                   here => '0,Here?,yesno',
30                   dxchancall => '5,Channel Call',
31                   pcversion => '5,Node Version',
32                   list => '5,User List,DXCluster::dolist',
33                   users => '0,No of Users',
34                  );
35
36 sub alloc
37 {
38         my ($pkg, $dxchan, $call, $confmode, $here) = @_;
39         die "$call is already alloced" if $cluster{$call};
40         my $self = {};
41         $self->{call} = $call;
42         $self->{confmode} = $confmode;
43         $self->{here} = $here;
44         $self->{dxchancall} = $dxchan->call;
45
46         $cluster{$call} = bless $self, $pkg;
47         return $self;
48 }
49
50 # get an entry exactly as it is
51 sub get_exact
52 {
53         my ($pkg, $call) = @_;
54
55         # belt and braces
56         $call = uc $call;
57   
58         # search for 'as is' only
59         return $cluster{$call}; 
60 }
61
62 #
63 # search for a call in the cluster
64 # taking into account SSIDs
65 #
66 sub get
67 {
68         my ($pkg, $call) = @_;
69
70         # belt and braces
71         $call = uc $call;
72   
73         # search for 'as is'
74         my $ref = $cluster{$call}; 
75         return $ref if $ref;
76
77         # search for the unSSIDed one
78         $call =~ s/-\d+$//o;
79         $ref = $cluster{$call};
80         return $ref if $ref;
81   
82         # search for the SSIDed one
83         my $i;
84         for ($i = 1; $i < 17; $i++) {
85                 $ref = $cluster{"$call-$i"};
86                 return $ref if $ref;
87         }
88         return undef;
89 }
90
91 # get all 
92 sub get_all
93 {
94         return values(%cluster);
95 }
96
97 # return a prompt for a field
98 sub field_prompt
99
100         my ($self, $ele) = @_;
101         return $valid{$ele};
102 }
103 #
104 # return a list of valid elements 
105
106
107 sub fields
108 {
109         return keys(%valid);
110 }
111
112 # this expects a reference to a list in a node NOT a ref to a node 
113 sub dolist
114 {
115         my $self = shift;
116         my $out;
117         my $ref;
118   
119         foreach my $call (keys %{$self}) {
120                 $ref = $$self{$call};
121                 my $s = $ref->{call};
122                 $s = "($s)" if !$ref->{here};
123                 $out .= "$s ";
124         }
125         chop $out;
126         return $out;
127 }
128
129 # this expects a reference to a node 
130 sub showcall
131 {
132         my $self = shift;
133         return $self->{call};
134 }
135
136 # the answer required by show/cluster
137 sub cluster
138 {
139         my $users = DXCommandmode::get_all();
140         my $uptime = main::uptime();
141         my $tot = $DXNode::users;
142                 
143         return " $DXNode::nodes nodes, $users local / $tot total users  Max users $DXNode::maxusers  Uptime $uptime";
144 }
145
146 sub mynode
147 {
148         my $self = shift;
149         my $noderef = shift;
150         
151         if ($noderef) {
152                 $self->{mynode} = $noderef->call;
153         } else {
154                 $noderef = DXCluster->get_exact($self->{mynode});
155                 unless ($noderef) {
156                         my $mynode = $self->{mynode};
157                         my $call = $self->{call};
158                         dbg("parent node $mynode has disappeared from $call") if isdbg('err');
159                 }
160         }
161         return $noderef;
162 }
163
164 sub dxchan
165 {
166         my $self = shift;
167         my $dxchan = shift;
168
169         if ($dxchan) {
170                 $self->{dxchancall} = $dxchan->call;
171         } else {
172                 $dxchan = DXChannel->get($self->{dxchancall});
173                 unless ($dxchan) {
174                         my $dxcall = $self->{dxchancall};
175                         my $call = $self->{call};
176                         dbg("parent dxchan $dxcall has disappeared from $call") if isdbg('err');
177                 }
178         }
179         return $dxchan;
180 }
181
182 no strict;
183 sub AUTOLOAD
184 {
185         my $self = shift;
186         my $name = $AUTOLOAD;
187   
188         return if $name =~ /::DESTROY$/;
189         $name =~ s/.*:://o;
190   
191         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
192         # this clever line of code creates a subroutine which takes over from autoload
193         # from OO Perl - Conway
194         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
195         @_ ? $self->{$name} = shift : $self->{$name} ;
196 }
197
198 #
199 # USER special routines
200 #
201
202 package DXNodeuser;
203
204 @ISA = qw(DXCluster);
205
206 use DXDebug;
207
208 use strict;
209
210 sub new 
211 {
212         my ($pkg, $dxchan, $node, $call, $confmode, $here) = @_;
213
214         die "tried to add $call when it already exists" if DXCluster->get_exact($call);
215   
216         my $self = $pkg->alloc($dxchan, $call, $confmode, $here);
217         $self->{mynode} = $node->call;
218         $node->add_user($call, $self);
219         dbg("allocating user $call to $node->{call} in cluster\n") if isdbg('cluster');
220         return $self;
221 }
222
223 sub del
224 {
225         my $self = shift;
226         my $call = $self->{call};
227         my $node = $self->mynode;
228
229         $node->del_user($call);
230         dbg("deleting user $call from $node->{call} in cluster\n") if isdbg('cluster');
231 }
232
233 sub count
234 {
235         return $DXNode::users;          # + 1 for ME (naf eh!)
236 }
237
238 no strict;
239
240 #
241 # NODE special routines
242 #
243
244 package DXNode;
245
246 @ISA = qw(DXCluster);
247
248 use DXDebug;
249
250 use strict;
251 use vars qw($nodes $users $maxusers);
252
253 $nodes = 0;
254 $users = 0;
255 $maxusers = 0;
256
257
258 sub new 
259 {
260         my ($pkg, $dxchan, $call, $confmode, $here, $pcversion) = @_;
261         my $self = $pkg->alloc($dxchan, $call, $confmode, $here);
262         $self->{pcversion} = $pcversion;
263         $self->{list} = { } ;
264         $self->{mynode} = $self->call;  # for sh/station
265         $self->{users} = 0;
266         $nodes++;
267         dbg("allocating node $call to cluster\n") if isdbg('cluster');
268         return $self;
269 }
270
271 # get all the nodes
272 sub get_all
273 {
274         my $list;
275         my @out;
276         foreach $list (values(%DXCluster::cluster)) {
277                 push @out, $list if $list->{pcversion};
278         }
279         return @out;
280 }
281
282 sub del
283 {
284         my $self = shift;
285         my $call = $self->{call};
286         my $ref;
287
288         # delete all the listed calls
289         foreach $ref (values %{$self->{list}}) {
290                 $ref->del();                    # this also takes them out of this list
291         }
292         delete $DXCluster::cluster{$call}; # remove me from the cluster table
293         dbg("deleting node $call from cluster\n") if isdbg('cluster'); 
294         $users -= $self->{users};    # it may be PC50 updated only therefore > 0
295         $users = 0 if $users < 0;
296         $nodes--;
297         $nodes = 0 if $nodes < 0;
298 }
299
300 sub add_user
301 {
302         my $self = shift;
303         my $call = shift;
304         my $ref = shift;
305         
306         $self->{list}->{$call} = $ref; # add this user to the list on this node
307         $self->{users} = keys %{$self->{list}};
308         $users++;
309         $maxusers = $users+$nodes if $users+$nodes > $maxusers;
310 }
311
312 sub del_user
313 {
314         my $self = shift;
315         my $call = shift;
316
317         delete $self->{list}->{$call};
318         delete $DXCluster::cluster{$call}; # remove me from the cluster table
319         $self->{users} = keys %{$self->{list}};
320         $users--;
321         $users = 0, warn "\$users gone neg, reset" if $users < 0;
322         $maxusers = $users+$nodes if $users+$nodes > $maxusers;
323 }
324
325 sub update_users
326 {
327         my $self = shift;
328         my $count = shift;
329         $count = 0 unless $count;
330         
331         $users -= $self->{users};
332         $self->{users} = $count unless keys %{$self->{list}};
333         $users += $self->{users};
334         $maxusers = $users+$nodes if $users+$nodes > $maxusers;
335 }
336
337 sub count
338 {
339         return $nodes;                          # + 1 for ME!
340 }
341
342 sub dolist
343 {
344
345 }
346
347 sub DESTROY
348 {
349         my $self = shift;
350         undef $self->{list} if $self->{list};
351 }
352
353
354 1;
355 __END__