fix all DXChannel->get to ::get
[spider.git] / perl / Route.pm
1 #!/usr/bin/perl
2 #
3 # This module impliments the abstracted routing for all protocols and
4 # is probably what I SHOULD have done the first time. 
5 #
6 # Heyho.
7 #
8 # This is just a container class which I expect to subclass 
9 #
10 # Copyright (c) 2001 Dirk Koopman G1TLH
11 #
12 # $Id$
13
14
15 package Route;
16
17 use DXDebug;
18 use DXChannel;
19 use Prefix;
20
21 use strict;
22
23
24 use vars qw($VERSION $BRANCH);
25
26 main::mkver($VERSION = q$Revision$);
27
28 use vars qw(%list %valid $filterdef);
29
30 %valid = (
31                   call => "0,Callsign",
32                   flags => "0,Flags,phex",
33                   dxcc => '0,Country Code',
34                   itu => '0,ITU Zone',
35                   cq => '0,CQ Zone',
36                   state => '0,State',
37                   city => '0,City',
38                   aranea => '0, By Aranea,yesno',
39                  );
40
41 $filterdef = bless ([
42                           # tag, sort, field, priv, special parser 
43                           ['channel', 'c', 0],
44                           ['channel_dxcc', 'nc', 1],
45                           ['channel_itu', 'ni', 2],
46                           ['channel_zone', 'nz', 3],
47                           ['call', 'c', 4],
48                           ['by', 'c', 4],
49                           ['call_dxcc', 'nc', 5],
50                           ['by_dxcc', 'nc', 5],
51                           ['call_itu', 'ni', 6],
52                           ['by_itu', 'ni', 6],
53                           ['call_zone', 'nz', 7],
54                           ['by_zone', 'nz', 7],
55                           ['channel_state', 'ns', 8],
56                           ['call_state', 'ns', 9],
57                           ['by_state', 'ns', 9],
58                          ], 'Filter::Cmd');
59
60
61 sub new
62 {
63         my ($pkg, $call) = @_;
64         $pkg = ref $pkg if ref $pkg;
65
66         my $self = bless {call => $call}, $pkg;
67         dbg("create $pkg with $call") if isdbg('routelow');
68
69         # add in all the dxcc, itu, zone info
70         ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
71                 Prefix::cty_data($call);
72
73         $self->{flags} = here(1);
74         
75         return $self; 
76 }
77
78 #
79 # get a callsign from a passed reference or a string
80 #
81
82 sub _getcall
83 {
84         my $self = shift;
85         my $thingy = shift;
86         $thingy = $self unless $thingy;
87         $thingy = $thingy->call if ref $thingy;
88         $thingy = uc $thingy if $thingy;
89         return $thingy;
90 }
91
92
93 # add and delete a callsign to/from a list
94 #
95
96 sub _addlist
97 {
98         my $self = shift;
99         my $field = shift;
100         my @out;
101         foreach my $c (@_) {
102                 confess "Need a ref here" unless ref($c);
103                 
104                 my $call = $c->{call};
105                 unless (grep $_ eq $call, @{$self->{$field}}) {
106                         push @{$self->{$field}}, $call;
107                         dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
108                         push @out, $c;
109                 }
110         }
111         return @out;
112 }
113
114 sub _dellist
115 {
116         my $self = shift;
117         my $field = shift;
118         my @out;
119         foreach my $c (@_) {
120                 confess "Need a ref here" unless ref($c);
121                 my $call = $c->{call};
122                 if (grep $_ eq $call, @{$self->{$field}}) {
123                         $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
124                         dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
125                         push @out, $c;
126                 }
127         }
128         return @out;
129 }
130
131 sub is_empty
132 {
133         my $self = shift;
134         return @{$self->{$_[0]}} == 0;
135 }
136
137 sub is_aranea
138 {
139         my $self = shift;
140         $self->{aranea} = shift if @_;
141         return $self->{aranea};
142 }
143
144 #
145 # flag field constructors/enquirers
146 #
147 # These can be called in various ways:-
148 #
149 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
150 # Route::here(1) returns 1 (the bit value of the here flag)
151 # $ref->here(1) or $ref->here(0) sets the here flag
152 #
153 # these are now redundant really as we are not interested in conferences
154 # and here is back to being '1'.
155
156 sub here
157 {
158         my $self = shift;
159         my $r = shift;
160         return $self ? 2 : 0 unless ref $self;
161         return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
162         $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
163         return $r ? 1 : 0;
164 }
165
166 sub conf
167 {
168         my $self = shift;
169         my $r = shift;
170         return $self ? 2 : 0 unless ref $self;
171         return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
172         $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
173         return $r ? 2 : 0;
174 }
175
176 sub parents
177 {
178         my $self = shift;
179         return @{$self->{parent}};
180 }
181
182
183 # display routines
184 #
185
186 sub user_call
187 {
188         my $self = shift;
189         my $call = sprintf "%s", $self->{call};
190         return $self->here ? "$call" : "($call)";
191 }
192
193 sub config
194 {
195         my $self = shift;
196         my $nodes_only = shift;
197         my $level = shift;
198         my $seen = shift;
199         my @out;
200         my $line;
201         my $call = $self->user_call;
202         my $printit = 1;
203
204         # allow ranges
205         if (@_) {
206                 $printit = grep $call =~ m|$_|, @_;
207         }
208
209         if ($printit) {
210                 $line = ' ' x ($level*2) . "$call";
211                 $call = ' ' x length $call; 
212                 
213                 # recursion detector
214                 if ((DXChannel::get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
215                         $line .= ' ...';
216                         push @out, $line;
217                         return @out;
218                 }
219                 push @$seen, $self->{call};
220
221                 # print users
222                 unless ($nodes_only) {
223                         if (@{$self->{users}}) {
224                                 $line .= '->';
225                                 foreach my $ucall (sort @{$self->{users}}) {
226                                         my $uref = Route::User::get($ucall);
227                                         my $c;
228                                         if ($uref) {
229                                                 $c = $uref->user_call;
230                                         } else {
231                                                 $c = "$ucall?";
232                                         }
233                                         if ((length $line) + (length $c) + 1 < 79) {
234                                                 $line .= $c . ' ';
235                                         } else {
236                                                 $line =~ s/\s+$//;
237                                                 push @out, $line;
238                                                 $line = ' ' x ($level*2) . "$call->$c ";
239                                         }
240                                 }
241                         }
242                 }
243                 $line =~ s/->$//g;
244                 $line =~ s/\s+$//;
245                 push @out, $line if length $line;
246         }
247         
248         # deal with more nodes
249         foreach my $ncall (sort @{$self->{nodes}}) {
250                 my $nref = Route::Node::get($ncall);
251
252                 if ($nref) {
253                         my $c = $nref->user_call;
254 #                       dbg("recursing from $call -> $c") if isdbg('routec');
255                         push @out, $nref->config($nodes_only, $level+1, $seen, @_);
256                 } else {
257                         push @out, ' ' x (($level+1)*2)  . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_); 
258                 }
259         }
260
261         return @out;
262 }
263
264 sub cluster
265 {
266         my $nodes = Route::Node::count();
267         my $tot = Route::User::count();
268         my $users = scalar DXCommandmode::get_all();
269         my $maxusers = Route::User::max();
270         my $uptime = main::uptime();
271         
272         return " $nodes nodes, $users local / $tot total users  Max users $maxusers  Uptime $uptime";
273 }
274
275 #
276 # routing things
277 #
278
279 sub get
280 {
281         my $call = shift;
282         return Route::Node::get($call) || Route::User::get($call);
283 }
284
285 # find all the possible dxchannels which this object might be on
286 sub alldxchan
287 {
288         my $self = shift;
289         my @dxchan;
290 #       dbg("Trying node $self->{call}") if isdbg('routech');
291
292         my $dxchan = DXChannel::get($self->{call});
293         push @dxchan, $dxchan if $dxchan;
294         
295         # it isn't, build up a list of dxchannels and possible ping times 
296         # for all the candidates.
297         unless (@dxchan) {
298                 foreach my $p (@{$self->{parent}}) {
299 #                       dbg("Trying parent $p") if isdbg('routech');
300                         next if $p eq $main::mycall; # the root
301                         my $dxchan = DXChannel::get($p);
302                         if ($dxchan) {
303                                 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
304                         } else {
305                                 next if grep $p eq $_, @_;
306                                 my $ref = Route::Node::get($p);
307 #                               dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
308                                 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
309                         }
310                 }
311         }
312 #       dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
313         return @dxchan;
314 }
315
316 sub dxchan
317 {
318         my $self = shift;
319         
320         # ALWAYS return the locally connected channel if present;
321         my $dxchan = DXChannel::get($self->call);
322         return $dxchan if $dxchan;
323         
324         my @dxchan = $self->alldxchan;
325         return undef unless @dxchan;
326         
327         # determine the minimum ping channel
328         my $minping = 99999999;
329         foreach my $dxc (@dxchan) {
330                 my $p = $dxc->pingave;
331                 if (defined $p  && $p < $minping) {
332                         $minping = $p;
333                         $dxchan = $dxc;
334                 }
335         }
336         $dxchan = shift @dxchan unless $dxchan;
337         return $dxchan;
338 }
339
340
341
342 #
343 # track destruction
344 #
345
346 sub DESTROY
347 {
348         my $self = shift;
349         my $pkg = ref $self;
350         
351         dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
352 }
353
354 no strict;
355 #
356 # return a list of valid elements 
357
358
359 sub fields
360 {
361         my $pkg = shift;
362         $pkg = ref $pkg if ref $pkg;
363     my $val = "${pkg}::valid";
364         my @out = keys %$val;
365         push @out, keys %valid;
366         return @out;
367 }
368
369 #
370 # return a prompt for a field
371 #
372
373 sub field_prompt
374
375         my ($self, $ele) = @_;
376         my $pkg = ref $self;
377     my $val = "${pkg}::valid";
378         return $val->{$ele} || $valid{$ele};
379 }
380
381 #
382 # generic AUTOLOAD for accessors
383 #
384 sub AUTOLOAD
385 {
386         no strict;
387         my $name = $AUTOLOAD;
388         return if $name =~ /::DESTROY$/;
389         $name =~ s/^.*:://o;
390   
391         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
392
393         # this clever line of code creates a subroutine which takes over from autoload
394         # from OO Perl - Conway
395         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
396        goto &$AUTOLOAD;
397
398 }
399
400 1;