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