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',
38 # tag, sort, field, priv, special parser
40 ['channel_dxcc', 'nc', 1],
41 ['channel_itu', 'ni', 2],
42 ['channel_zone', 'nz', 3],
45 ['call_dxcc', 'nc', 5],
47 ['call_itu', 'ni', 6],
49 ['call_zone', 'nz', 7],
51 ['channel_state', 'ns', 8],
52 ['call_state', 'ns', 9],
53 ['by_state', 'ns', 9],
56 $maxlevel = 25; # maximum recursion level in Route::config
60 my ($pkg, $call) = @_;
61 $pkg = ref $pkg if ref $pkg;
63 my $self = bless {call => $call}, $pkg;
64 dbg("create $pkg with $call") if isdbg('routelow');
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);
70 $self->{flags} = here(1);
76 # get a callsign from a passed reference or a string
83 $thingy = $self unless $thingy;
84 $thingy = $thingy->call if ref $thingy;
85 $thingy = uc $thingy if $thingy;
90 # add and delete a callsign to/from a list
99 confess "Need a ref here" unless ref($c);
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');
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');
131 return @{$self->{$_[0]}} == 0;
135 # flag field constructors/enquirers
137 # These can be called in various ways:-
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
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));
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));
167 return @{$self->{parent}};
177 my $call = sprintf "%s", $self->{call};
178 return $self->here ? "$call" : "($call)";
184 my $nodes_only = shift || 0;
185 my $width = shift || 79;
190 my $call = $self->{call};
193 dbg("config: $call nodes: $nodes_only level: $level calls: " . join(',', @_)) if isdbg('routec');
197 $printit = grep $call =~ m|$_|, @_;
201 my $pcall = $self->user_call;
202 $pcall .= ":" . $self->obscount if isdbg('obscount');
205 $line = ' ' x ($level*2) . $pcall;
206 $pcall = ' ' x length $pcall;
209 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
217 unless ($nodes_only) {
218 if (@{$self->{users}}) {
220 foreach my $ucall (sort @{$self->{users}}) {
221 my $uref = Route::User::get($ucall);
224 $c = $uref->user_call;
228 if ((length $line) + (length $c) + 1 < $width) {
233 $line = ' ' x ($level*2) . "$pcall->$c ";
240 push @out, $line if length $line;
243 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
249 # deal with more nodes
250 foreach my $ncall (sort @{$self->{nodes}}) {
251 my $nref = Route::Node::get($ncall);
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, @_);
258 push @out, ' ' x ($level*2) . $self->user_call unless grep /^\s+$call/, @out;
262 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
271 my $nodes = Route::Node::count();
272 my $tot = Route::User::count();
273 my ($users, $maxlocalusers) = DXCommandmode::user_count();
274 my $maxusers = Route::User::max();
275 my $uptime = main::uptime();
276 my $localnodes = $DXChannel::count - $users;
278 return ($nodes, $tot, $users, $maxlocalusers, $maxusers, $uptime, $localnodes);
290 return Route::Node::get($call) || Route::User::get($call);
299 dbg("ROUTE: findroutes $call") if isdbg('findroutes');
301 my $nref = Route::get($call);
302 return () unless $nref;
304 # we are directly connected, force "best possible" priority, but
305 # carry on in case user is connected on other nodes.
306 my $dxchan = DXChannel::get($call);
308 dbg("ROUTE: findroutes $call -> directly connected") if isdbg('findroutes');
312 # obtain the dxchannels that have seen this thingy
313 my @parent = $nref->isa('Route::User') ? @{$nref->{parent}} : $call;
314 foreach my $p (@parent) {
315 next if $p eq $main::mycall; # this is dealt with above
317 # deal with directly connected nodes, again "best priority"
318 $dxchan = DXChannel::get($p);
320 dbg("ROUTE: findroutes $call -> connected direct via parent $p") if isdbg('findroutes');
325 my $r = Route::Node::get($p);
327 my %r = $r->PC92C_dxchan;
328 while (my ($k, $v) = each %r) {
329 $cand{$k} = $v if $v > ($cand{$k} || 0);
334 # remove any dxchannels that have gone away
335 while (my ($k, $v) = each %cand) {
336 if (my $dxc = DXChannel::get($k)) {
337 push @out, [$v, $dxc];
341 # get a sorted list of dxchannels with the highest hop count first
342 my @nout = sort {$b->[0] <=> $a->[0]} @out;
343 if (isdbg('findroutes')) {
346 dbg("ROUTE: findroutes $call -> $_->[0] " . $_->[1]->call);
354 # find all the possible dxchannels which this object might be on
358 my @dxchan = findroutes($self->{call});
359 return map {$_->[1]} @dxchan;
366 # ALWAYS return the locally connected channel if present;
367 my $dxchan = DXChannel::get($self->call);
368 return $dxchan if $dxchan;
370 my @dxchan = $self->alldxchan;
371 return undef unless @dxchan;
373 # dxchannels are now returned in order of "closeness"
391 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
396 # return a list of valid elements
402 $pkg = ref $pkg if ref $pkg;
403 my $val = "${pkg}::valid";
404 my @out = keys %$val;
405 push @out, keys %valid;
410 # return a prompt for a field
415 my ($self, $ele) = @_;
417 my $val = "${pkg}::valid";
418 return $val->{$ele} || $valid{$ele};
422 # generic AUTOLOAD for accessors
427 my $name = $AUTOLOAD;
428 return if $name =~ /::DESTROY$/;
431 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
433 # this clever line of code creates a subroutine which takes over from autoload
434 # from OO Perl - Conway
435 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};