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