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 = scalar DXCommandmode::get_all();
274 my $maxusers = Route::User::max();
275 my $uptime = main::uptime();
277 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
287 return Route::Node::get($call) || Route::User::get($call);
296 dbg("ROUTE: findroutes $call") if isdbg('findroutes');
298 my $nref = Route::get($call);
299 return () unless $nref;
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);
305 dbg("ROUTE: findroutes $call -> directly connected") if isdbg('findroutes');
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
314 # deal with directly connected nodes, again "best priority"
315 $dxchan = DXChannel::get($p);
317 dbg("ROUTE: findroutes $call -> connected direct via parent $p") if isdbg('findroutes');
322 my $r = Route::Node::get($p);
324 my %r = $r->PC92C_dxchan;
325 while (my ($k, $v) = each %r) {
326 $cand{$k} = $v if $v > ($cand{$k} || 0);
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];
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')) {
343 dbg("ROUTE: findroutes $call -> $_->[0] " . $_->[1]->call);
351 # find all the possible dxchannels which this object might be on
355 my @dxchan = findroutes($self->{call});
356 return map {$_->[1]} @dxchan;
363 # ALWAYS return the locally connected channel if present;
364 my $dxchan = DXChannel::get($self->call);
365 return $dxchan if $dxchan;
367 my @dxchan = $self->alldxchan;
368 return undef unless @dxchan;
370 # dxchannels are now returned in order of "closeness"
388 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
393 # return a list of valid elements
399 $pkg = ref $pkg if ref $pkg;
400 my $val = "${pkg}::valid";
401 my @out = keys %$val;
402 push @out, keys %valid;
407 # return a prompt for a field
412 my ($self, $ele) = @_;
414 my $val = "${pkg}::valid";
415 return $val->{$ele} || $valid{$ele};
419 # generic AUTOLOAD for accessors
424 my $name = $AUTOLOAD;
425 return if $name =~ /::DESTROY$/;
428 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
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}};