remove Prot.pm, sort %valid fields
[spider.git] / perl / Route.pm
1 #
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 #
13 #
14
15 package Route;
16
17 use DXDebug;
18 use DXChannel;
19 use Prefix;
20 use DXUtil;
21
22 use strict;
23
24
25 use vars qw(%list %valid $filterdef $maxlevel);
26
27 %valid = (
28                   call => "0,Callsign",
29                   city => '0,City',
30                   cq => '0,CQ Zone',
31                   dxcc => '0,Country Code',
32                   flags => "0,Flags,phex",
33                   ip => '0,IP Address',
34                   itu => '0,ITU Zone',
35                   parent => '0,Parent Calls,parray',
36                   state => '0,State',
37                  );
38
39 $filterdef = bless ([
40                           # tag, sort, field, priv, special parser
41                           ['channel', 'c', 0],
42                           ['channel_dxcc', 'nc', 1],
43                           ['channel_itu', 'ni', 2],
44                           ['channel_zone', 'nz', 3],
45                           ['call', 'c', 4],
46                           ['by', 'c', 4],
47                           ['call_dxcc', 'nc', 5],
48                           ['by_dxcc', 'nc', 5],
49                           ['call_itu', 'ni', 6],
50                           ['by_itu', 'ni', 6],
51                           ['call_zone', 'nz', 7],
52                           ['by_zone', 'nz', 7],
53                           ['channel_state', 'ns', 8],
54                           ['call_state', 'ns', 9],
55                           ['by_state', 'ns', 9],
56                          ], 'Filter::Cmd');
57
58 $maxlevel = 25;                 # maximum recursion level in Route::config
59
60 sub new
61 {
62         my ($pkg, $call) = @_;
63         $pkg = ref $pkg if ref $pkg;
64
65         my $self = bless {call => $call}, $pkg;
66         dbg("create $pkg with $call") if isdbg('routelow');
67
68         # add in all the dxcc, itu, zone info
69         ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
70                 Prefix::cty_data($call);
71
72         $self->{flags} = here(1);
73
74         return $self;
75 }
76
77 #
78 # get a callsign from a passed reference or a string
79 #
80
81 sub _getcall
82 {
83         my $self = shift;
84         my $thingy = shift;
85         $thingy = $self unless $thingy;
86         $thingy = $thingy->call if ref $thingy;
87         $thingy = uc $thingy if $thingy;
88         return $thingy;
89 }
90
91 #
92 # add and delete a callsign to/from a list
93 #
94
95 sub _addlist
96 {
97         my $self = shift;
98         my $field = shift;
99         my @out;
100         foreach my $c (@_) {
101                 confess "Need a ref here" unless ref($c);
102
103                 my $call = $c->{call};
104                 unless (grep $_ eq $call, @{$self->{$field}}) {
105                         push @{$self->{$field}}, $call;
106                         dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
107                         push @out, $c;
108                 }
109         }
110         return @out;
111 }
112
113 sub _dellist
114 {
115         my $self = shift;
116         my $field = shift;
117         my @out;
118         foreach my $c (@_) {
119                 confess "Need a ref here" unless ref($c);
120                 my $call = $c->{call};
121                 if (grep $_ eq $call, @{$self->{$field}}) {
122                         $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
123                         dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
124                         push @out, $c;
125                 }
126         }
127         return @out;
128 }
129
130 sub is_empty
131 {
132         my $self = shift;
133         return @{$self->{$_[0]}} == 0;
134 }
135
136 #
137 # flag field constructors/enquirers
138 #
139 # These can be called in various ways:-
140 #
141 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
142 # Route::here(1) returns 2 (the bit value of the here flag)
143 # $ref->here(1) or $ref->here(0) sets the here flag
144 #
145
146 sub here
147 {
148         my $self = shift;
149         my $r = shift;
150         return $self ? 2 : 0 unless ref $self;
151         return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
152         $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
153         return $r ? 1 : 0;
154 }
155
156 sub conf
157 {
158         my $self = shift;
159         my $r = shift;
160         return $self ? 1 : 0 unless ref $self;
161         return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
162         $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
163         return $r ? 1 : 0;
164 }
165
166 sub parents
167 {
168         my $self = shift;
169         return @{$self->{parent}};
170 }
171
172 #
173 # display routines
174 #
175
176 sub user_call
177 {
178         my $self = shift;
179         my $call = sprintf "%s", $self->{call};
180         return $self->here ? "$call" : "($call)";
181 }
182
183 sub config
184 {
185         my $self = shift;
186         my $nodes_only = shift || 0;
187         my $width = shift || 79;
188         my $level = shift;
189         my $seen = shift;
190         my @out;
191         my $line;
192         my $call = $self->{call};
193         my $printit = 1;
194
195         dbg("config: $call nodes: $nodes_only level: $level calls: " . join(',', @_)) if isdbg('routec');
196
197         # allow ranges
198         if (@_) {
199                 $printit = grep $call =~ m|$_|, @_;
200         }
201
202         if ($printit) {
203                 my $pcall = $self->user_call;
204                 $pcall .= ":" . $self->obscount if isdbg('obscount');
205
206
207                 $line = ' ' x ($level*2) . $pcall;
208                 $pcall = ' ' x length $pcall;
209
210                 # recursion detector
211                 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
212                         $line .= ' ...';
213                         push @out, $line;
214                         return @out;
215                 }
216                 $seen->{$call}++;
217
218                 # print users
219                 unless ($nodes_only) {
220                         if (@{$self->{users}}) {
221                                 $line .= '->';
222                                 foreach my $ucall (sort @{$self->{users}}) {
223                                         my $uref = Route::User::get($ucall);
224                                         my $c;
225                                         if ($uref) {
226                                                 $c = $uref->user_call;
227                                         }
228                                         else {
229                                                 $c = "$ucall?";
230                                         }
231                                         if ((length $line) + (length $c) + 1 < $width) {
232                                                 $line .= $c . ' ';
233                                         }
234                                         else {
235                                                 $line =~ s/\s+$//;
236                                                 push @out, $line;
237                                                 $line = ' ' x ($level*2) . "$pcall->$c ";
238                                         }
239                                 }
240                         }
241                 }
242                 $line =~ s/->$//g;
243                 $line =~ s/\s+$//;
244                 push @out, $line if length $line;
245         }
246         else {
247                 # recursion detector
248                 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
249                         return @out;
250                 }
251                 $seen->{$call}++;
252         }
253
254         # deal with more nodes
255         foreach my $ncall (sort @{$self->{nodes}}) {
256                 my $nref = Route::Node::get($ncall);
257
258                 if ($nref) {
259                         my $c = $nref->user_call;
260                         dbg("recursing from $call -> $c") if isdbg('routec');
261                         my @rout = $nref->config($nodes_only, $width, $level+1, $seen, @_);
262                         if (@rout && @_) {
263                                 push @out, ' ' x ($level*2) . $self->user_call unless grep /^\s+$call/, @out;
264                         }
265                         push @out, @rout;
266                 } else {
267                         push @out, ' ' x (($level+1)*2)  . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
268                 }
269         }
270
271         return @out;
272 }
273
274 sub cluster
275 {
276         my $nodes = Route::Node::count();
277         my $tot = Route::User::count();
278         my ($users, $maxlocalusers) = DXCommandmode::user_count(); # the user count is wrong because of skimmers
279         my $maxusers = Route::User::max();
280         my $uptime = main::uptime();
281         my $localnodes = $DXChannel::count - $users;   # this is now wrong because of skimmers
282         
283         return ($nodes, $tot, $users, $maxlocalusers, $maxusers, $uptime, $localnodes);
284         
285
286 }
287
288 #
289 # routing things
290 #
291
292 sub get
293 {
294         my $call = shift;
295         return Route::Node::get($call) || Route::User::get($call);
296 }
297
298 sub findroutes
299 {
300         my $call = shift;
301         my %cand;
302         my @out;
303
304         dbg("ROUTE: findroutes $call") if isdbg('findroutes');
305
306         my $nref = Route::get($call);
307         return () unless $nref;
308
309         # we are directly connected, force "best possible" priority, but
310         # carry on in case user is connected on other nodes.
311         my $dxchan = DXChannel::get($call);
312         if ($dxchan) {
313                 dbg("ROUTE: findroutes $call -> directly connected") if isdbg('findroutes');
314                 $cand{$call} = 99;
315         }
316
317         # obtain the dxchannels that have seen this thingy
318         my @parent = $nref->isa('Route::User') ? @{$nref->{parent}} : $call;
319         foreach my $p (@parent) {
320                 next if $p eq $main::mycall; # this is dealt with above
321
322                 # deal with directly connected nodes, again "best priority"
323                 $dxchan = DXChannel::get($p);
324                 if ($dxchan) {
325                         dbg("ROUTE: findroutes $call -> connected direct via parent $p") if isdbg('findroutes');
326                         $cand{$p} = 99;
327                         next;
328                 }
329
330                 my $r = Route::Node::get($p);
331                 if ($r) {
332                         my %r = $r->PC92C_dxchan;
333                         while (my ($k, $v) = each %r) {
334                                 $cand{$k} = $v if $v > ($cand{$k} || 0);
335                         }
336                 }
337         }
338
339         # remove any dxchannels that have gone away
340         while (my ($k, $v) = each %cand) {
341                 if (my $dxc = DXChannel::get($k)) {
342                         push @out, [$v, $dxc];
343                 }
344         }
345
346         # get a sorted list of dxchannels with the highest hop count first
347         my @nout = sort {$b->[0] <=> $a->[0]} @out;
348         if (isdbg('findroutes')) {
349                 if (@nout) {
350                         for (@nout) {
351                                 dbg("ROUTE: findroutes $call -> $_->[0] " . $_->[1]->call);
352                         }
353                 }
354         }
355
356         return @nout;
357 }
358
359 # find all the possible dxchannels which this object might be on
360 sub alldxchan
361 {
362         my $self = shift;
363         my @dxchan = findroutes($self->{call});
364         return map {$_->[1]} @dxchan;
365 }
366
367 sub dxchan
368 {
369         my $self = shift;
370
371         # ALWAYS return the locally connected channel if present;
372         my $dxchan = DXChannel::get($self->call);
373         return $dxchan if $dxchan;
374
375         my @dxchan = $self->alldxchan;
376         return undef unless @dxchan;
377
378         # dxchannels are now returned in order of "closeness"
379         return $dxchan[0];
380 }
381
382 sub delete_interface
383 {
384
385 }
386
387 #
388 # track destruction
389 #
390
391 sub DESTROY
392 {
393         my $self = shift;
394         my $pkg = ref $self;
395
396         dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
397 }
398
399 no strict;
400 #
401 # return a list of valid elements
402 #
403
404 sub fields
405 {
406         my $pkg = shift;
407         $pkg = ref $pkg if ref $pkg;
408     my $val = "${pkg}::valid";
409         my @out = keys %$val;
410         push @out, keys %valid;
411         return @out;
412 }
413
414 #
415 # return a prompt for a field
416 #
417
418 sub field_prompt
419 {
420         my ($self, $ele) = @_;
421         my $pkg = ref $self;
422     my $val = "${pkg}::valid";
423         return $val->{$ele} || $valid{$ele};
424 }
425
426 #
427 # generic AUTOLOAD for accessors
428 #
429 sub AUTOLOAD
430 {
431         no strict;
432         my $name = $AUTOLOAD;
433         return if $name =~ /::DESTROY$/;
434         $name =~ s/^.*:://o;
435
436         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
437
438         # this clever line of code creates a subroutine which takes over from autoload
439         # from OO Perl - Conway
440         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
441        goto &$AUTOLOAD;
442
443 }
444
445 1;