0676139aa033647f434bc557a6c6a4cdfc179c82
[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} = 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 #
138 # flag field constructors/enquirers
139 #
140 # These can be called in various ways:-
141 #
142 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
143 # Route::here(1) returns 1 (the bit value of the here flag)
144 # $ref->here(1) or $ref->here(0) sets the here flag
145 #
146 # these are now redundant really as we are not interested in conferences
147 # and here is back to being '1'.
148
149 sub here
150 {
151         my $self = shift;
152         $self->{flags} = shift if @_;
153         return $self->{flags};
154 }
155
156 # conferencing in the ak1a sense is not supported 
157 sub conf
158 {
159         my $self = shift;
160         return 0;
161 }
162
163 sub parents
164 {
165         my $self = shift;
166         return @{$self->{parent}};
167 }
168
169
170 # display routines
171 #
172
173 sub user_call
174 {
175         my $self = shift;
176         my $call = sprintf "%s", $self->{call};
177         return $self->here ? "$call" : "($call)";
178 }
179
180 sub config
181 {
182         my $self = shift;
183         my $nodes_only = shift;
184         my $level = shift;
185         my $seen = shift;
186         my @out;
187         my $line;
188         my $call = $self->user_call;
189         my $printit = 1;
190
191         # allow ranges
192         if (@_) {
193                 $printit = grep $call =~ m|$_|, @_;
194         }
195
196         if ($printit) {
197                 $line = ' ' x ($level*2) . "$call";
198                 $call = ' ' x length $call; 
199                 
200                 # recursion detector
201                 if ((DXChannel::get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
202                         $line .= ' ...';
203                         push @out, $line;
204                         return @out;
205                 }
206                 push @$seen, $self->{call};
207
208                 # print users
209                 unless ($nodes_only) {
210                         if (@{$self->{users}}) {
211                                 $line .= '->';
212                                 foreach my $ucall (sort @{$self->{users}}) {
213                                         my $uref = Route::User::get($ucall);
214                                         my $c;
215                                         if ($uref) {
216                                                 $c = $uref->user_call;
217                                         } else {
218                                                 $c = "$ucall?";
219                                         }
220                                         if ((length $line) + (length $c) + 1 < 79) {
221                                                 $line .= $c . ' ';
222                                         } else {
223                                                 $line =~ s/\s+$//;
224                                                 push @out, $line;
225                                                 $line = ' ' x ($level*2) . "$call->$c ";
226                                         }
227                                 }
228                         }
229                 }
230                 $line =~ s/->$//g;
231                 $line =~ s/\s+$//;
232                 push @out, $line if length $line;
233         }
234         
235         # deal with more nodes
236         foreach my $ncall (sort @{$self->{nodes}}) {
237                 my $nref = Route::Node::get($ncall);
238
239                 if ($nref) {
240                         my $c = $nref->user_call;
241 #                       dbg("recursing from $call -> $c") if isdbg('routec');
242                         push @out, $nref->config($nodes_only, $level+1, $seen, @_);
243                 } else {
244                         push @out, ' ' x (($level+1)*2)  . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_); 
245                 }
246         }
247
248         return @out;
249 }
250
251 sub cluster
252 {
253         my $nodes = Route::Node::count();
254         my $tot = Route::User::count();
255         my $users = scalar DXCommandmode::get_all();
256         my $maxusers = Route::User::max();
257         my $uptime = main::uptime();
258         
259         return " $nodes nodes, $users local / $tot total users  Max users $maxusers  Uptime $uptime";
260 }
261
262 #
263 # routing things
264 #
265
266 sub get
267 {
268         my $call = shift;
269         return Route::Node::get($call) || Route::User::get($call);
270 }
271
272 # find all the possible dxchannels which this object might be on
273 sub alldxchan
274 {
275         my $self = shift;
276         my @dxchan;
277 #       dbg("Trying node $self->{call}") if isdbg('routech');
278
279         my $dxchan = DXChannel::get($self->{call});
280         push @dxchan, $dxchan if $dxchan;
281         
282         # it isn't, build up a list of dxchannels and possible ping times 
283         # for all the candidates.
284         unless (@dxchan) {
285                 foreach my $p (@{$self->{parent}}) {
286 #                       dbg("Trying parent $p") if isdbg('routech');
287                         next if $p eq $main::mycall; # the root
288                         my $dxchan = DXChannel::get($p);
289                         if ($dxchan) {
290                                 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
291                         } else {
292                                 next if grep $p eq $_, @_;
293                                 my $ref = Route::Node::get($p);
294 #                               dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
295                                 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
296                         }
297                 }
298         }
299 #       dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
300         return @dxchan;
301 }
302
303 sub dxchan
304 {
305         my $self = shift;
306         
307         # ALWAYS return the locally connected channel if present;
308         my $dxchan = DXChannel::get($self->call);
309         return $dxchan if $dxchan;
310         
311         my @dxchan = $self->alldxchan;
312         return undef unless @dxchan;
313         
314         # determine the minimum ping channel
315         my $minping = 99999999;
316         foreach my $dxc (@dxchan) {
317                 my $p = $dxc->pingave;
318                 if (defined $p  && $p < $minping) {
319                         $minping = $p;
320                         $dxchan = $dxc;
321                 }
322         }
323         $dxchan = shift @dxchan unless $dxchan;
324         return $dxchan;
325 }
326
327
328
329 #
330 # track destruction
331 #
332
333 sub DESTROY
334 {
335         my $self = shift;
336         my $pkg = ref $self;
337         
338         dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
339 }
340
341 no strict;
342 #
343 # return a list of valid elements 
344
345
346 sub fields
347 {
348         my $pkg = shift;
349         $pkg = ref $pkg if ref $pkg;
350     my $val = "${pkg}::valid";
351         my @out = keys %$val;
352         push @out, keys %valid;
353         return @out;
354 }
355
356 #
357 # return a prompt for a field
358 #
359
360 sub field_prompt
361
362         my ($self, $ele) = @_;
363         my $pkg = ref $self;
364     my $val = "${pkg}::valid";
365         return $val->{$ele} || $valid{$ele};
366 }
367
368 #
369 # generic AUTOLOAD for accessors
370 #
371 sub AUTOLOAD
372 {
373         no strict;
374         my $name = $AUTOLOAD;
375         return if $name =~ /::DESTROY$/;
376         $name =~ s/^.*:://o;
377   
378         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
379
380         # this clever line of code creates a subroutine which takes over from autoload
381         # from OO Perl - Conway
382         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
383        goto &$AUTOLOAD;
384
385 }
386
387 1;