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
23 use vars qw(%list %valid $filterdef);
27 flags => "0,Flags,phex",
28 dxcc => '0,Country Code',
34 # tag, sort, field, priv, special parser
36 ['channel_dxcc', 'n', 1],
37 ['channel_itu', 'n', 2],
38 ['channel_zone', 'n', 3],
40 ['call_dxcc', 'n', 5],
42 ['call_zone', 'n', 7],
48 my ($pkg, $call) = @_;
49 $pkg = ref $pkg if ref $pkg;
51 my $self = bless {call => $call}, $pkg;
52 dbg("create $pkg with $call") if isdbg('routelow');
54 # add in all the dxcc, itu, zone info
55 my @dxcc = Prefix::extract($call);
57 $self->{dxcc} = $dxcc[1]->dxcc;
58 $self->{itu} = $dxcc[1]->itu;
59 $self->{cq} = $dxcc[1]->cq;
61 $self->{flags} = here(1);
67 # get a callsign from a passed reference or a string
74 $thingy = $self unless $thingy;
75 $thingy = $thingy->call if ref $thingy;
76 $thingy = uc $thingy if $thingy;
81 # add and delete a callsign to/from a list
89 my $call = _getcall($c);
90 unless (grep {$_ eq $call} @{$self->{$field}}) {
91 push @{$self->{$field}}, $call;
92 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
95 return $self->{$field};
103 my $call = _getcall($c);
104 if (grep {$_ eq $call} @{$self->{$field}}) {
105 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
106 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
109 return $self->{$field};
113 # flag field constructors/enquirers
115 # These can be called in various ways:-
117 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
118 # Route::here(1) returns 2 (the bit value of the here flag)
119 # $ref->here(1) or $ref->here(0) sets the here flag
126 return $self ? 2 : 0 unless ref $self;
127 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
128 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
136 return $self ? 1 : 0 unless ref $self;
137 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
138 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
145 return @{$self->{parent}};
155 my $call = sprintf "%s", $self->{call};
156 return $self->here ? "$call" : "($call)";
162 my $nodes_only = shift;
167 my $call = $self->user_call;
172 $printit = grep $call =~ m|$_|, @_;
176 $line = ' ' x ($level*2) . "$call";
177 $call = ' ' x length $call;
180 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
185 push @$seen, $self->{call};
188 unless ($nodes_only) {
189 if (@{$self->{users}}) {
191 foreach my $ucall (sort @{$self->{users}}) {
192 my $uref = Route::User::get($ucall);
195 $c = $uref->user_call;
199 if ((length $line) + (length $c) + 1 < 79) {
204 $line = ' ' x ($level*2) . "$call->$c ";
211 push @out, $line if length $line;
214 # deal with more nodes
215 foreach my $ncall (sort @{$self->{nodes}}) {
216 my $nref = Route::Node::get($ncall);
219 my $c = $nref->user_call;
220 # dbg("recursing from $call -> $c") if isdbg('routec');
221 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
223 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
232 my $nodes = Route::Node::count();
233 my $tot = Route::User::count();
234 my $users = scalar DXCommandmode::get_all();
235 my $maxusers = Route::User::max();
236 my $uptime = main::uptime();
238 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
248 return Route::Node::get($call) || Route::User::get($call);
251 # find all the possible dxchannels which this object might be on
256 # dbg("Trying node $self->{call}") if isdbg('routech');
257 my $dxchan = DXChannel->get($self->{call});
258 push @dxchan, $dxchan if $dxchan;
260 # it isn't, build up a list of dxchannels and possible ping times
261 # for all the candidates.
263 foreach my $p (@{$self->{parent}}) {
264 # dbg("Trying parent $p") if isdbg('routech');
265 next if $p eq $main::mycall; # the root
266 my $dxchan = DXChannel->get($p);
268 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
270 next if grep $p eq $_, @_;
271 my $ref = Route::Node::get($p);
272 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
273 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
277 # dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
284 my @dxchan = $self->alldxchan;
285 return undef unless @dxchan;
287 # determine the minimum ping channel
288 my $minping = 99999999;
290 foreach my $dxc (@dxchan) {
291 my $p = $dxc->pingave;
292 if (defined $p && $p < $minping) {
297 $dxchan = shift @dxchan unless $dxchan;
310 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
315 # return a list of valid elements
321 $pkg = ref $pkg if ref $pkg;
322 my $val = "${pkg}::valid";
323 my @out = keys %$val;
324 push @out, keys %valid;
329 # return a prompt for a field
334 my ($self, $ele) = @_;
336 my $val = "${pkg}::valid";
337 return $val->{$ele} || $valid{$ele};
341 # generic AUTOLOAD for accessors
346 my $name = $AUTOLOAD;
347 return if $name =~ /::DESTROY$/;
350 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
352 # this clever line of code creates a subroutine which takes over from autoload
353 # from OO Perl - Conway
354 # *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
355 @_ ? $self->{$name} = shift : $self->{$name} ;