0e9b61395da39891f3bcf0a1957e7a6e8abdd87a
[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                   lastseen => 'Last Seen,atime',
41                  );
42
43 $filterdef = bless ([
44                           # tag, sort, field, priv, special parser 
45                           ['channel', 'c', 0],
46                           ['channel_dxcc', 'nc', 1],
47                           ['channel_itu', 'ni', 2],
48                           ['channel_zone', 'nz', 3],
49                           ['call', 'c', 4],
50                           ['by', 'c', 4],
51                           ['call_dxcc', 'nc', 5],
52                           ['by_dxcc', 'nc', 5],
53                           ['call_itu', 'ni', 6],
54                           ['by_itu', 'ni', 6],
55                           ['call_zone', 'nz', 7],
56                           ['by_zone', 'nz', 7],
57                           ['channel_state', 'ns', 8],
58                           ['call_state', 'ns', 9],
59                           ['by_state', 'ns', 9],
60                          ], 'Filter::Cmd');
61
62
63 sub new
64 {
65         my ($pkg, $call) = @_;
66         $pkg = ref $pkg if ref $pkg;
67
68         my $self = bless {call => $call}, $pkg;
69         dbg("create $pkg with $call") if isdbg('routelow');
70
71         # add in all the dxcc, itu, zone info
72         ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
73                 Prefix::cty_data($call);
74
75         $self->{flags} = here(1);
76         
77         return $self; 
78 }
79
80 #
81 # get a callsign from a passed reference or a string
82 #
83
84 sub _getcall
85 {
86         my $self = shift;
87         my $thingy = shift;
88         $thingy = $self unless $thingy;
89         $thingy = $thingy->call if ref $thingy;
90         $thingy = uc $thingy if $thingy;
91         return $thingy;
92 }
93
94
95 # add and delete a callsign to/from a list
96 #
97
98 sub _addlist
99 {
100         my $self = shift;
101         my $field = shift;
102         my @out;
103         foreach my $c (@_) {
104                 confess "Need a ref here" unless ref($c);
105                 
106                 my $call = $c->{call};
107                 unless (grep $_ eq $call, @{$self->{$field}}) {
108                         push @{$self->{$field}}, $call;
109                         dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
110                         push @out, $c;
111                 }
112         }
113         return @out;
114 }
115
116 sub _dellist
117 {
118         my $self = shift;
119         my $field = shift;
120         my @out;
121         foreach my $c (@_) {
122                 confess "Need a ref here" unless ref($c);
123                 my $call = $c->{call};
124                 if (grep $_ eq $call, @{$self->{$field}}) {
125                         $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
126                         dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
127                         push @out, $c;
128                 }
129         }
130         return @out;
131 }
132
133 sub is_empty
134 {
135         my $self = shift;
136         return @{$self->{$_[0]}} == 0;
137 }
138
139 #
140 # flag field constructors/enquirers
141 #
142 # These can be called in various ways:-
143 #
144 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
145 # Route::here(1) returns 2 (the bit value of the here flag)
146 # $ref->here(1) or $ref->here(0) sets the here flag
147 #
148
149 sub here
150 {
151         my $self = shift;
152         my $r = shift;
153         return $self ? 2 : 0 unless ref $self;
154         return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
155         $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
156         return $r ? 1 : 0;
157 }
158
159 sub conf
160 {
161         my $self = shift;
162         my $r = shift;
163         return $self ? 1 : 0 unless ref $self;
164         return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
165         $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
166         return $r ? 1 : 0;
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->{dxchan}}) {
286 #                       dbg("Trying dxchan $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 bestdxchan
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 sub _adddxchan
328 {
329         my $self = shift;
330     return $self->_addlist('dxchan', @_);
331 }
332
333 sub _deldxchan
334 {
335         my $self = shift;
336     return $self->_dellist('dxchan', @_);
337 }
338
339 sub _addnode
340 {
341         my $self = shift;
342     return $self->_addlist('nodes', @_);
343 }
344
345 sub _delnode
346 {
347         my $self = shift;
348     return $self->_dellist('nodes', @_);
349 }
350
351
352 #
353 # track destruction
354 #
355
356 sub DESTROY
357 {
358         my $self = shift;
359         my $pkg = ref $self;
360         
361         dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
362 }
363
364 no strict;
365 #
366 # return a list of valid elements 
367
368
369 sub fields
370 {
371         my $pkg = shift;
372         $pkg = ref $pkg if ref $pkg;
373     my $val = "${pkg}::valid";
374         my @out = keys %$val;
375         push @out, keys %valid;
376         return @out;
377 }
378
379 #
380 # return a prompt for a field
381 #
382
383 sub field_prompt
384
385         my ($self, $ele) = @_;
386         my $pkg = ref $self;
387     my $val = "${pkg}::valid";
388         return $val->{$ele} || $valid{$ele};
389 }
390
391 #
392 # generic AUTOLOAD for accessors
393 #
394 sub AUTOLOAD
395 {
396         no strict;
397         my $name = $AUTOLOAD;
398         return if $name =~ /::DESTROY$/;
399         $name =~ s/^.*:://o;
400   
401         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
402
403         # this clever line of code creates a subroutine which takes over from autoload
404         # from OO Perl - Conway
405         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
406        goto &$AUTOLOAD;
407
408 }
409
410 1;