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