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);
26 main::mkver($VERSION = q$Revision$);
28 use vars qw(%list %valid $filterdef);
32 flags => "0,Flags,phex",
33 dxcc => '0,Country Code',
41 # tag, sort, field, priv, special parser
43 ['channel_dxcc', 'nc', 1],
44 ['channel_itu', 'ni', 2],
45 ['channel_zone', 'nz', 3],
48 ['call_dxcc', 'nc', 5],
50 ['call_itu', 'ni', 6],
52 ['call_zone', 'nz', 7],
54 ['channel_state', 'ns', 8],
55 ['call_state', 'ns', 9],
56 ['by_state', 'ns', 9],
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 1 (the bit value of the here flag)
143 # $ref->here(1) or $ref->here(0) sets the here flag
145 # these are now redundant really as we are not interested in conferences
146 # and here is back to being '1'.
152 return $self ? 2 : 0 unless ref $self;
153 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
154 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
162 return $self ? 2 : 0 unless ref $self;
163 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
164 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
171 return @{$self->{parent}};
181 my $call = sprintf "%s", $self->{call};
182 return $self->here ? "$call" : "($call)";
188 my $nodes_only = shift;
193 my $call = $self->user_call;
198 $printit = grep $call =~ m|$_|, @_;
202 $line = ' ' x ($level*2) . "$call";
203 $call = ' ' x length $call;
206 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
211 push @$seen, $self->{call};
214 unless ($nodes_only) {
215 if (@{$self->{users}}) {
217 foreach my $ucall (sort @{$self->{users}}) {
218 my $uref = Route::User::get($ucall);
221 $c = $uref->user_call;
225 if ((length $line) + (length $c) + 1 < 79) {
230 $line = ' ' x ($level*2) . "$call->$c ";
237 push @out, $line if length $line;
240 # deal with more nodes
241 foreach my $ncall (sort @{$self->{nodes}}) {
242 my $nref = Route::Node::get($ncall);
245 my $c = $nref->user_call;
246 # dbg("recursing from $call -> $c") if isdbg('routec');
247 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
249 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
258 my $nodes = Route::Node::count();
259 my $tot = Route::User::count();
260 my $users = scalar DXCommandmode::get_all();
261 my $maxusers = Route::User::max();
262 my $uptime = main::uptime();
264 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
274 return Route::Node::get($call) || Route::User::get($call);
277 # find all the possible dxchannels which this object might be on
282 # dbg("Trying node $self->{call}") if isdbg('routech');
284 my $dxchan = DXChannel->get($self->{call});
285 push @dxchan, $dxchan if $dxchan;
287 # it isn't, build up a list of dxchannels and possible ping times
288 # for all the candidates.
290 foreach my $p (@{$self->{parent}}) {
291 # dbg("Trying parent $p") if isdbg('routech');
292 next if $p eq $main::mycall; # the root
293 my $dxchan = DXChannel->get($p);
295 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
297 next if grep $p eq $_, @_;
298 my $ref = Route::Node::get($p);
299 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
300 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
304 # dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
312 # ALWAYS return the locally connected channel if present;
313 my $dxchan = DXChannel->get($self->call);
314 return $dxchan if $dxchan;
316 my @dxchan = $self->alldxchan;
317 return undef unless @dxchan;
319 # determine the minimum ping channel
320 my $minping = 99999999;
321 foreach my $dxc (@dxchan) {
322 my $p = $dxc->pingave;
323 if (defined $p && $p < $minping) {
328 $dxchan = shift @dxchan unless $dxchan;
343 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
348 # return a list of valid elements
354 $pkg = ref $pkg if ref $pkg;
355 my $val = "${pkg}::valid";
356 my @out = keys %$val;
357 push @out, keys %valid;
362 # return a prompt for a field
367 my ($self, $ele) = @_;
369 my $val = "${pkg}::valid";
370 return $val->{$ele} || $valid{$ele};
374 # generic AUTOLOAD for accessors
379 my $name = $AUTOLOAD;
380 return if $name =~ /::DESTROY$/;
383 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
385 # this clever line of code creates a subroutine which takes over from autoload
386 # from OO Perl - Conway
387 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};