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);
29 flags => "0,Flags,phex",
30 dxcc => '0,Country Code',
39 # tag, sort, field, priv, special parser
41 ['channel_dxcc', 'nc', 1],
42 ['channel_itu', 'ni', 2],
43 ['channel_zone', 'nz', 3],
46 ['call_dxcc', 'nc', 5],
48 ['call_itu', 'ni', 6],
50 ['call_zone', 'nz', 7],
52 ['channel_state', 'ns', 8],
53 ['call_state', 'ns', 9],
54 ['by_state', 'ns', 9],
57 $maxlevel = 25; # maximum recursion level in Route::config
61 my ($pkg, $call) = @_;
62 $pkg = ref $pkg if ref $pkg;
64 my $self = bless {call => $call}, $pkg;
65 dbg("create $pkg with $call") if isdbg('routelow');
67 # add in all the dxcc, itu, zone info
68 ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
69 Prefix::cty_data($call);
71 $self->{flags} = here(1);
77 # get a callsign from a passed reference or a string
84 $thingy = $self unless $thingy;
85 $thingy = $thingy->call if ref $thingy;
86 $thingy = uc $thingy if $thingy;
91 # add and delete a callsign to/from a list
100 confess "Need a ref here" unless ref($c);
102 my $call = $c->{call};
103 unless (grep $_ eq $call, @{$self->{$field}}) {
104 push @{$self->{$field}}, $call;
105 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
118 confess "Need a ref here" unless ref($c);
119 my $call = $c->{call};
120 if (grep $_ eq $call, @{$self->{$field}}) {
121 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
122 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
132 return @{$self->{$_[0]}} == 0;
136 # flag field constructors/enquirers
138 # These can be called in various ways:-
140 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
141 # Route::here(1) returns 2 (the bit value of the here flag)
142 # $ref->here(1) or $ref->here(0) sets the here flag
149 return $self ? 2 : 0 unless ref $self;
150 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
151 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
159 return $self ? 1 : 0 unless ref $self;
160 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
161 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
168 return @{$self->{parent}};
178 my $call = sprintf "%s", $self->{call};
179 return $self->here ? "$call" : "($call)";
185 my $nodes_only = shift || 0;
186 my $width = shift || 79;
191 my $call = $self->{call};
194 dbg("config: $call nodes: $nodes_only level: $level calls: " . join(',', @_)) if isdbg('routec');
198 $printit = grep $call =~ m|$_|, @_;
202 my $pcall = $self->user_call;
203 $pcall .= ":" . $self->obscount if isdbg('obscount');
206 $line = ' ' x ($level*2) . $pcall;
207 $pcall = ' ' x length $pcall;
210 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
218 unless ($nodes_only) {
219 if (@{$self->{users}}) {
221 foreach my $ucall (sort @{$self->{users}}) {
222 my $uref = Route::User::get($ucall);
225 $c = $uref->user_call;
229 if ((length $line) + (length $c) + 1 < $width) {
234 $line = ' ' x ($level*2) . "$pcall->$c ";
241 push @out, $line if length $line;
244 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
250 # deal with more nodes
251 foreach my $ncall (sort @{$self->{nodes}}) {
252 my $nref = Route::Node::get($ncall);
255 my $c = $nref->user_call;
256 dbg("recursing from $call -> $c") if isdbg('routec');
257 my @rout = $nref->config($nodes_only, $width, $level+1, $seen, @_);
259 push @out, ' ' x ($level*2) . $self->user_call unless grep /^\s+$call/, @out;
263 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
272 my $nodes = Route::Node::count();
273 my $tot = Route::User::count();
274 my ($users, $maxlocalusers) = DXCommandmode::user_count();
275 my $maxusers = Route::User::max();
276 my $uptime = main::uptime();
277 my $localnodes = $DXChannel::count - $users;
279 return ($nodes, $tot, $users, $maxlocalusers, $maxusers, $uptime, $localnodes);
291 return Route::Node::get($call) || Route::User::get($call);
300 dbg("ROUTE: findroutes $call") if isdbg('findroutes');
302 my $nref = Route::get($call);
303 return () unless $nref;
305 # we are directly connected, force "best possible" priority, but
306 # carry on in case user is connected on other nodes.
307 my $dxchan = DXChannel::get($call);
309 dbg("ROUTE: findroutes $call -> directly connected") if isdbg('findroutes');
313 # obtain the dxchannels that have seen this thingy
314 my @parent = $nref->isa('Route::User') ? @{$nref->{parent}} : $call;
315 foreach my $p (@parent) {
316 next if $p eq $main::mycall; # this is dealt with above
318 # deal with directly connected nodes, again "best priority"
319 $dxchan = DXChannel::get($p);
321 dbg("ROUTE: findroutes $call -> connected direct via parent $p") if isdbg('findroutes');
326 my $r = Route::Node::get($p);
328 my %r = $r->PC92C_dxchan;
329 while (my ($k, $v) = each %r) {
330 $cand{$k} = $v if $v > ($cand{$k} || 0);
335 # remove any dxchannels that have gone away
336 while (my ($k, $v) = each %cand) {
337 if (my $dxc = DXChannel::get($k)) {
338 push @out, [$v, $dxc];
342 # get a sorted list of dxchannels with the highest hop count first
343 my @nout = sort {$b->[0] <=> $a->[0]} @out;
344 if (isdbg('findroutes')) {
347 dbg("ROUTE: findroutes $call -> $_->[0] " . $_->[1]->call);
355 # find all the possible dxchannels which this object might be on
359 my @dxchan = findroutes($self->{call});
360 return map {$_->[1]} @dxchan;
367 # ALWAYS return the locally connected channel if present;
368 my $dxchan = DXChannel::get($self->call);
369 return $dxchan if $dxchan;
371 my @dxchan = $self->alldxchan;
372 return undef unless @dxchan;
374 # dxchannels are now returned in order of "closeness"
392 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
397 # return a list of valid elements
403 $pkg = ref $pkg if ref $pkg;
404 my $val = "${pkg}::valid";
405 my @out = keys %$val;
406 push @out, keys %valid;
411 # return a prompt for a field
416 my ($self, $ele) = @_;
418 my $val = "${pkg}::valid";
419 return $val->{$ele} || $valid{$ele};
423 # generic AUTOLOAD for accessors
428 my $name = $AUTOLOAD;
429 return if $name =~ /::DESTROY$/;
432 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
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}};