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;
230 if ((length $line) + (length $c) + 1 < $width) {
235 $line = ' ' x ($level*2) . "$pcall->$c ";
242 push @out, $line if length $line;
245 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
251 # deal with more nodes
252 foreach my $ncall (sort @{$self->{nodes}}) {
253 my $nref = Route::Node::get($ncall);
256 my $c = $nref->user_call;
257 dbg("recursing from $call -> $c") if isdbg('routec');
258 my @rout = $nref->config($nodes_only, $width, $level+1, $seen, @_);
260 push @out, ' ' x ($level*2) . $self->user_call unless grep /^\s+$call/, @out;
264 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
273 my $nodes = Route::Node::count();
274 my $tot = Route::User::count();
275 my ($users, $maxlocalusers) = DXCommandmode::user_count();
276 my $maxusers = Route::User::max();
277 my $uptime = main::uptime();
278 my $localnodes = $DXChannel::count - $users;
280 return ($nodes, $tot, $users, $maxlocalusers, $maxusers, $uptime, $localnodes);
292 return Route::Node::get($call) || Route::User::get($call);
301 dbg("ROUTE: findroutes $call") if isdbg('findroutes');
303 my $nref = Route::get($call);
304 return () unless $nref;
306 # we are directly connected, force "best possible" priority, but
307 # carry on in case user is connected on other nodes.
308 my $dxchan = DXChannel::get($call);
310 dbg("ROUTE: findroutes $call -> directly connected") if isdbg('findroutes');
314 # obtain the dxchannels that have seen this thingy
315 my @parent = $nref->isa('Route::User') ? @{$nref->{parent}} : $call;
316 foreach my $p (@parent) {
317 next if $p eq $main::mycall; # this is dealt with above
319 # deal with directly connected nodes, again "best priority"
320 $dxchan = DXChannel::get($p);
322 dbg("ROUTE: findroutes $call -> connected direct via parent $p") if isdbg('findroutes');
327 my $r = Route::Node::get($p);
329 my %r = $r->PC92C_dxchan;
330 while (my ($k, $v) = each %r) {
331 $cand{$k} = $v if $v > ($cand{$k} || 0);
336 # remove any dxchannels that have gone away
337 while (my ($k, $v) = each %cand) {
338 if (my $dxc = DXChannel::get($k)) {
339 push @out, [$v, $dxc];
343 # get a sorted list of dxchannels with the highest hop count first
344 my @nout = sort {$b->[0] <=> $a->[0]} @out;
345 if (isdbg('findroutes')) {
348 dbg("ROUTE: findroutes $call -> $_->[0] " . $_->[1]->call);
356 # find all the possible dxchannels which this object might be on
360 my @dxchan = findroutes($self->{call});
361 return map {$_->[1]} @dxchan;
368 # ALWAYS return the locally connected channel if present;
369 my $dxchan = DXChannel::get($self->call);
370 return $dxchan if $dxchan;
372 my @dxchan = $self->alldxchan;
373 return undef unless @dxchan;
375 # dxchannels are now returned in order of "closeness"
393 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
398 # return a list of valid elements
404 $pkg = ref $pkg if ref $pkg;
405 my $val = "${pkg}::valid";
406 my @out = keys %$val;
407 push @out, keys %valid;
412 # return a prompt for a field
417 my ($self, $ele) = @_;
419 my $val = "${pkg}::valid";
420 return $val->{$ele} || $valid{$ele};
424 # generic AUTOLOAD for accessors
429 my $name = $AUTOLOAD;
430 return if $name =~ /::DESTROY$/;
433 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
435 # this clever line of code creates a subroutine which takes over from autoload
436 # from OO Perl - Conway
437 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};