521068b023cba2c2e1358140f549d667d8b1d0c9
[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('routelow', "create $pkg with $call");
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('routelow', ref($self) . " adding $call to " . $self->{call} . "->\{$field\}");
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('routelow', ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}");
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('routec', "recursing from $call -> $c");
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         my $dxchan = DXChannel->get($self->{call});
257         push @dxchan, $dxchan if $dxchan;
258         
259         # it isn't, build up a list of dxchannels and possible ping times 
260         # for all the candidates.
261         foreach my $p (@{$self->{parent}}) {
262                 my $dxchan = DXChannel->get($p);
263                 if ($dxchan) {
264                         push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
265                 } else {
266                         next if $p eq $main::mycall; # the root
267                         my $ref = $self->get($p);
268                         push @dxchan, $ref->alldxchan if $ref;
269                 }
270         }
271         return @dxchan;
272 }
273
274 sub dxchan
275 {
276         my $self = shift;
277         my $dxchan = DXChannel->get($self->{call});
278         return $dxchan if $dxchan;
279         
280         my @dxchan = $self->alldxchan;
281         return undef unless @dxchan;
282         
283         # determine the minimum ping channel
284         my $minping = 99999999;
285         foreach my $dxc (@dxchan) {
286                 my $p = $dxc->pingave;
287                 if (defined $p  && $p < $minping) {
288                         $minping = $p;
289                         $dxchan = $dxc;
290                 }
291         }
292         $dxchan = shift @dxchan unless $dxchan;
293         return $dxchan;
294 }
295
296 #
297 # track destruction
298 #
299
300 sub DESTROY
301 {
302         my $self = shift;
303         my $pkg = ref $self;
304         
305         dbg('routelow', "$pkg $self->{call} destroyed");
306 }
307
308 no strict;
309 #
310 # return a list of valid elements 
311
312
313 sub fields
314 {
315         my $pkg = shift;
316         $pkg = ref $pkg if ref $pkg;
317     my $val = "${pkg}::valid";
318         my @out = keys %$val;
319         push @out, keys %valid;
320         return @out;
321 }
322
323 #
324 # return a prompt for a field
325 #
326
327 sub field_prompt
328
329         my ($self, $ele) = @_;
330         my $pkg = ref $self;
331     my $val = "${pkg}::valid";
332         return $val->{$ele} || $valid{$ele};
333 }
334
335 #
336 # generic AUTOLOAD for accessors
337 #
338 sub AUTOLOAD
339 {
340         my $self = shift;
341         my $name = $AUTOLOAD;
342         return if $name =~ /::DESTROY$/;
343         $name =~ s/.*:://o;
344   
345         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
346
347         # this clever line of code creates a subroutine which takes over from autoload
348         # from OO Perl - Conway
349 #       *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
350     @_ ? $self->{$name} = shift : $self->{$name} ;
351 }
352
353 1;