3 # This module impliments the abstracted routing for all protocols and
4 # is probably what I SHOULD have done the first time.
8 # This is just a container class which I expect to subclass
10 # Copyright (c) 2001 Dirk Koopman G1TLH
25 use vars qw(%list %valid $filterdef $maxlevel);
28 parent => '0,Parent Calls,parray',
30 flags => "0,Flags,phex",
31 dxcc => '0,Country Code',
40 # tag, sort, field, priv, special parser
42 ['channel_dxcc', 'nc', 1],
43 ['channel_itu', 'ni', 2],
44 ['channel_zone', 'nz', 3],
47 ['call_dxcc', 'nc', 5],
49 ['call_itu', 'ni', 6],
51 ['call_zone', 'nz', 7],
53 ['channel_state', 'ns', 8],
54 ['call_state', 'ns', 9],
55 ['by_state', 'ns', 9],
58 $maxlevel = 25; # maximum recursion level in Route::config
62 my ($pkg, $call) = @_;
63 $pkg = ref $pkg if ref $pkg;
65 my $self = bless {call => $call}, $pkg;
66 dbg("create $pkg with $call") if isdbg('routelow');
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);
72 $self->{flags} = here(1);
78 # get a callsign from a passed reference or a string
85 $thingy = $self unless $thingy;
86 $thingy = $thingy->call if ref $thingy;
87 $thingy = uc $thingy if $thingy;
92 # add and delete a callsign to/from a list
101 confess "Need a ref here" unless ref($c);
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');
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');
133 return @{$self->{$_[0]}} == 0;
137 # flag field constructors/enquirers
139 # These can be called in various ways:-
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
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));
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));
169 return @{$self->{parent}};
179 my $call = sprintf "%s", $self->{call};
180 return $self->here ? "$call" : "($call)";
186 my $nodes_only = shift || 0;
187 my $width = shift || 79;
192 my $call = $self->{call};
195 dbg("config: $call nodes: $nodes_only level: $level calls: " . join(',', @_)) if isdbg('routec');
199 $printit = grep $call =~ m|$_|, @_;
203 my $pcall = $self->user_call;
204 $pcall .= ":" . $self->obscount if isdbg('obscount');
207 $line = ' ' x ($level*2) . $pcall;
208 $pcall = ' ' x length $pcall;
211 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
219 unless ($nodes_only) {
220 if (@{$self->{users}}) {
222 foreach my $ucall (sort @{$self->{users}}) {
223 my $uref = Route::User::get($ucall);
226 $c = $uref->user_call;
231 if ((length $line) + (length $c) + 1 < $width) {
237 $line = ' ' x ($level*2) . "$pcall->$c ";
244 push @out, $line if length $line;
248 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
254 # deal with more nodes
255 foreach my $ncall (sort @{$self->{nodes}}) {
256 my $nref = Route::Node::get($ncall);
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, @_);
263 push @out, ' ' x ($level*2) . $self->user_call unless grep /^\s+$call/, @out;
267 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
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
283 return ($nodes, $tot, $users, $maxlocalusers, $maxusers, $uptime, $localnodes);
295 return Route::Node::get($call) || Route::User::get($call);
304 dbg("ROUTE: findroutes $call") if isdbg('findroutes');
306 my $nref = Route::get($call);
307 return () unless $nref;
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);
313 dbg("ROUTE: findroutes $call -> directly connected") if isdbg('findroutes');
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
322 # deal with directly connected nodes, again "best priority"
323 $dxchan = DXChannel::get($p);
325 dbg("ROUTE: findroutes $call -> connected direct via parent $p") if isdbg('findroutes');
330 my $r = Route::Node::get($p);
332 my %r = $r->PC92C_dxchan;
333 while (my ($k, $v) = each %r) {
334 $cand{$k} = $v if $v > ($cand{$k} || 0);
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];
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')) {
351 dbg("ROUTE: findroutes $call -> $_->[0] " . $_->[1]->call);
359 # find all the possible dxchannels which this object might be on
363 my @dxchan = findroutes($self->{call});
364 return map {$_->[1]} @dxchan;
371 # ALWAYS return the locally connected channel if present;
372 my $dxchan = DXChannel::get($self->call);
373 return $dxchan if $dxchan;
375 my @dxchan = $self->alldxchan;
376 return undef unless @dxchan;
378 # dxchannels are now returned in order of "closeness"
396 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
401 # return a list of valid elements
407 $pkg = ref $pkg if ref $pkg;
408 my $val = "${pkg}::valid";
409 my @out = keys %$val;
410 push @out, keys %valid;
415 # return a prompt for a field
420 my ($self, $ele) = @_;
422 my $val = "${pkg}::valid";
423 return $val->{$ele} || $valid{$ele};
427 # generic AUTOLOAD for accessors
432 my $name = $AUTOLOAD;
433 return if $name =~ /::DESTROY$/;
436 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
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}};