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
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
30 use vars qw(%list %valid $filterdef);
34 flags => "0,Flags,phex",
35 dxcc => '0,Country Code',
41 # tag, sort, field, priv, special parser
43 ['channel_dxcc', 'n', 1],
44 ['channel_itu', 'n', 2],
45 ['channel_zone', 'n', 3],
47 ['call_dxcc', 'n', 5],
49 ['call_zone', 'n', 7],
55 my ($pkg, $call) = @_;
56 $pkg = ref $pkg if ref $pkg;
58 my $self = bless {call => $call}, $pkg;
59 dbg("create $pkg with $call") if isdbg('routelow');
61 # add in all the dxcc, itu, zone info
62 my @dxcc = Prefix::extract($call);
64 $self->{dxcc} = $dxcc[1]->dxcc;
65 $self->{itu} = $dxcc[1]->itu;
66 $self->{cq} = $dxcc[1]->cq;
68 $self->{flags} = here(1);
74 # get a callsign from a passed reference or a string
81 $thingy = $self unless $thingy;
82 $thingy = $thingy->call if ref $thingy;
83 $thingy = uc $thingy if $thingy;
88 # add and delete a callsign to/from a list
97 confess "Need a ref here" unless ref($c);
99 my $call = $c->{call};
100 unless (grep $_ eq $call, @{$self->{$field}}) {
101 push @{$self->{$field}}, $call;
102 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
115 confess "Need a ref here" unless ref($c);
116 my $call = $c->{call};
117 if (grep $_ eq $call, @{$self->{$field}}) {
118 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
119 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
129 return @{$self->{$_[0]}} == 0;
133 # flag field constructors/enquirers
135 # These can be called in various ways:-
137 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
138 # Route::here(1) returns 2 (the bit value of the here flag)
139 # $ref->here(1) or $ref->here(0) sets the here flag
146 return $self ? 2 : 0 unless ref $self;
147 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
148 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
156 return $self ? 1 : 0 unless ref $self;
157 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
158 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
165 return @{$self->{parent}};
175 my $call = sprintf "%s", $self->{call};
176 return $self->here ? "$call" : "($call)";
182 my $nodes_only = shift;
187 my $call = $self->user_call;
192 $printit = grep $call =~ m|$_|, @_;
196 $line = ' ' x ($level*2) . "$call";
197 $call = ' ' x length $call;
200 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
205 push @$seen, $self->{call};
208 unless ($nodes_only) {
209 if (@{$self->{users}}) {
211 foreach my $ucall (sort @{$self->{users}}) {
212 my $uref = Route::User::get($ucall);
215 $c = $uref->user_call;
219 if ((length $line) + (length $c) + 1 < 79) {
224 $line = ' ' x ($level*2) . "$call->$c ";
231 push @out, $line if length $line;
234 # deal with more nodes
235 foreach my $ncall (sort @{$self->{nodes}}) {
236 my $nref = Route::Node::get($ncall);
239 my $c = $nref->user_call;
240 # dbg("recursing from $call -> $c") if isdbg('routec');
241 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
243 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
252 my $nodes = Route::Node::count();
253 my $tot = Route::User::count();
254 my $users = scalar DXCommandmode::get_all();
255 my $maxusers = Route::User::max();
256 my $uptime = main::uptime();
258 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
268 return Route::Node::get($call) || Route::User::get($call);
271 # find all the possible dxchannels which this object might be on
276 # dbg("Trying node $self->{call}") if isdbg('routech');
278 my $dxchan = DXChannel->get($self->{call});
279 push @dxchan, $dxchan if $dxchan;
281 # it isn't, build up a list of dxchannels and possible ping times
282 # for all the candidates.
284 foreach my $p (@{$self->{parent}}) {
285 # dbg("Trying parent $p") if isdbg('routech');
286 next if $p eq $main::mycall; # the root
287 my $dxchan = DXChannel->get($p);
289 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
291 next if grep $p eq $_, @_;
292 my $ref = Route::Node::get($p);
293 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
294 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
298 # dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
306 # ALWAYS return the locally connected channel if present;
307 my $dxchan = DXChannel->get($self->call);
308 return $dxchan if $dxchan;
310 my @dxchan = $self->alldxchan;
311 return undef unless @dxchan;
313 # determine the minimum ping channel
314 my $minping = 99999999;
315 foreach my $dxc (@dxchan) {
316 my $p = $dxc->pingave;
317 if (defined $p && $p < $minping) {
322 $dxchan = shift @dxchan unless $dxchan;
335 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
340 # return a list of valid elements
346 $pkg = ref $pkg if ref $pkg;
347 my $val = "${pkg}::valid";
348 my @out = keys %$val;
349 push @out, keys %valid;
354 # return a prompt for a field
359 my ($self, $ele) = @_;
361 my $val = "${pkg}::valid";
362 return $val->{$ele} || $valid{$ele};
366 # generic AUTOLOAD for accessors
371 my $name = $AUTOLOAD;
372 return if $name =~ /::DESTROY$/;
375 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
377 # this clever line of code creates a subroutine which takes over from autoload
378 # from OO Perl - Conway
379 # *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
380 @_ ? $self->{$name} = shift : $self->{$name} ;