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