1. Added an efficiency thing for AUTOLOADed accessors from OO Perl by Conway.
[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,DXCluster::showcall',
27                   call => '0,Callsign',
28                   confmode => '0,Conference Mode,yesno',
29                   here => '0,Here?,yesno',
30                   dxchan => '5,Channel ref,DXCluster::showcall',
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->{dxchan} = $dxchan;
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 no strict;
147 sub AUTOLOAD
148 {
149         my $self = shift;
150         my $name = $AUTOLOAD;
151   
152         return if $name =~ /::DESTROY$/;
153         $name =~ s/.*:://o;
154   
155         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
156         # this clever line of code creates a subroutine which takes over from autoload
157         # from OO Perl - Conway
158         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
159         @_ ? $self->{$name} = shift : $self->{$name} ;
160 }
161
162 #
163 # USER special routines
164 #
165
166 package DXNodeuser;
167
168 @ISA = qw(DXCluster);
169
170 use DXDebug;
171
172 use strict;
173
174 sub new 
175 {
176         my ($pkg, $dxchan, $node, $call, $confmode, $here) = @_;
177
178         die "tried to add $call when it already exists" if DXCluster->get_exact($call);
179   
180         my $self = $pkg->alloc($dxchan, $call, $confmode, $here);
181         $self->{mynode} = $node;
182         $node->add_user($call, $self);
183         dbg('cluster', "allocating user $call to $node->{call} in cluster\n");
184         return $self;
185 }
186
187 sub del
188 {
189         my $self = shift;
190         my $call = $self->{call};
191         my $node = $self->{mynode};
192
193         $node->del_user($call);
194         dbg('cluster', "deleting user $call from $node->{call} in cluster\n");
195 }
196
197 sub count
198 {
199         return $DXNode::users;          # + 1 for ME (naf eh!)
200 }
201
202 no strict;
203
204 #
205 # NODE special routines
206 #
207
208 package DXNode;
209
210 @ISA = qw(DXCluster);
211
212 use DXDebug;
213
214 use strict;
215 use vars qw($nodes $users $maxusers);
216
217 $nodes = 0;
218 $users = 0;
219 $maxusers = 0;
220
221
222 sub new 
223 {
224         my ($pkg, $dxchan, $call, $confmode, $here, $pcversion) = @_;
225         my $self = $pkg->alloc($dxchan, $call, $confmode, $here);
226         $self->{pcversion} = $pcversion;
227         $self->{list} = { } ;
228         $self->{mynode} = $self;        # for sh/station
229         $self->{users} = 0;
230         $nodes++;
231         dbg('cluster', "allocating node $call to cluster\n");
232         return $self;
233 }
234
235 # get all the nodes
236 sub get_all
237 {
238         my $list;
239         my @out;
240         foreach $list (values(%DXCluster::cluster)) {
241                 push @out, $list if $list->{pcversion};
242         }
243         return @out;
244 }
245
246 sub del
247 {
248         my $self = shift;
249         my $call = $self->{call};
250         my $ref;
251
252         # delete all the listed calls
253         foreach $ref (values %{$self->{list}}) {
254                 $ref->del();                    # this also takes them out of this list
255         }
256         delete $DXCluster::cluster{$call}; # remove me from the cluster table
257         dbg('cluster', "deleting node $call from cluster\n"); 
258         $users -= $self->{users};    # it may be PC50 updated only therefore > 0
259         $users = 0 if $users < 0;
260         $nodes--;
261         $nodes = 0 if $nodes < 0;
262 }
263
264 sub add_user
265 {
266         my $self = shift;
267         my $call = shift;
268         my $ref = shift;
269         
270         $self->{list}->{$call} = $ref; # add this user to the list on this node
271         $self->{users} = keys %{$self->{list}};
272         $users++;
273         $maxusers = $users+$nodes if $users+$nodes > $maxusers;
274 }
275
276 sub del_user
277 {
278         my $self = shift;
279         my $call = shift;
280
281         delete $self->{list}->{$call};
282         delete $DXCluster::cluster{$call}; # remove me from the cluster table
283         $self->{users} = keys %{$self->{list}};
284         $users--;
285         $users = 0, warn "\$users gone neg, reset" if $users < 0;
286         $maxusers = $users+$nodes if $users+$nodes > $maxusers;
287 }
288
289 sub update_users
290 {
291         my $self = shift;
292         my $count = shift;
293         $count = 0 unless $count;
294         
295         $users -= $self->{users};
296         $self->{users} = $count unless keys %{$self->{list}};
297         $users += $self->{users};
298         $maxusers = $users+$nodes if $users+$nodes > $maxusers;
299 }
300
301 sub count
302 {
303         return $nodes;                          # + 1 for ME!
304 }
305
306 sub dolist
307 {
308
309 }
310
311 sub DESTROY
312 {
313         my $self = shift;
314         undef $self->{list} if $self->{list};
315 }
316
317
318 1;
319 __END__