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