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