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