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
96 my $call = _getcall($c);
97 unless (grep {$_ eq $call} @{$self->{$field}}) {
98 push @{$self->{$field}}, $call;
99 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
102 return $self->{$field};
110 my $call = _getcall($c);
111 if (grep {$_ eq $call} @{$self->{$field}}) {
112 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
113 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
116 return $self->{$field};
120 # flag field constructors/enquirers
122 # These can be called in various ways:-
124 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
125 # Route::here(1) returns 2 (the bit value of the here flag)
126 # $ref->here(1) or $ref->here(0) sets the here flag
133 return $self ? 2 : 0 unless ref $self;
134 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
135 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
143 return $self ? 1 : 0 unless ref $self;
144 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
145 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
152 return @{$self->{parent}};
162 my $call = sprintf "%s", $self->{call};
163 return $self->here ? "$call" : "($call)";
169 my $nodes_only = shift;
174 my $call = $self->user_call;
179 $printit = grep $call =~ m|$_|, @_;
183 $line = ' ' x ($level*2) . "$call";
184 $call = ' ' x length $call;
187 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
192 push @$seen, $self->{call};
195 unless ($nodes_only) {
196 if (@{$self->{users}}) {
198 foreach my $ucall (sort @{$self->{users}}) {
199 my $uref = Route::User::get($ucall);
202 $c = $uref->user_call;
206 if ((length $line) + (length $c) + 1 < 79) {
211 $line = ' ' x ($level*2) . "$call->$c ";
218 push @out, $line if length $line;
221 # deal with more nodes
222 foreach my $ncall (sort @{$self->{nodes}}) {
223 my $nref = Route::Node::get($ncall);
226 my $c = $nref->user_call;
227 # dbg("recursing from $call -> $c") if isdbg('routec');
228 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
230 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
239 my $nodes = Route::Node::count();
240 my $tot = Route::User::count();
241 my $users = scalar DXCommandmode::get_all();
242 my $maxusers = Route::User::max();
243 my $uptime = main::uptime();
245 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
255 return Route::Node::get($call) || Route::User::get($call);
258 # find all the possible dxchannels which this object might be on
263 # dbg("Trying node $self->{call}") if isdbg('routech');
264 my $dxchan = DXChannel->get($self->{call});
265 push @dxchan, $dxchan if $dxchan;
267 # it isn't, build up a list of dxchannels and possible ping times
268 # for all the candidates.
270 foreach my $p (@{$self->{parent}}) {
271 # dbg("Trying parent $p") if isdbg('routech');
272 next if $p eq $main::mycall; # the root
273 my $dxchan = DXChannel->get($p);
275 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
277 next if grep $p eq $_, @_;
278 my $ref = Route::Node::get($p);
279 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
280 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
284 # dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
291 my @dxchan = $self->alldxchan;
292 return undef unless @dxchan;
294 # determine the minimum ping channel
295 my $minping = 99999999;
297 foreach my $dxc (@dxchan) {
298 my $p = $dxc->pingave;
299 if (defined $p && $p < $minping) {
304 $dxchan = shift @dxchan unless $dxchan;
317 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
322 # return a list of valid elements
328 $pkg = ref $pkg if ref $pkg;
329 my $val = "${pkg}::valid";
330 my @out = keys %$val;
331 push @out, keys %valid;
336 # return a prompt for a field
341 my ($self, $ele) = @_;
343 my $val = "${pkg}::valid";
344 return $val->{$ele} || $valid{$ele};
348 # generic AUTOLOAD for accessors
353 my $name = $AUTOLOAD;
354 return if $name =~ /::DESTROY$/;
357 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
359 # this clever line of code creates a subroutine which takes over from autoload
360 # from OO Perl - Conway
361 # *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
362 @_ ? $self->{$name} = shift : $self->{$name} ;