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