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,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',
43 # tag, sort, field, priv, special parser
45 ['channel_dxcc', 'nc', 1],
46 ['channel_itu', 'ni', 2],
47 ['channel_zone', 'nz', 3],
50 ['call_dxcc', 'nc', 5],
52 ['call_itu', 'ni', 6],
54 ['call_zone', 'nz', 7],
56 ['channel_state', 'ns', 8],
57 ['call_state', 'ns', 9],
58 ['by_state', 'ns', 9],
64 my ($pkg, $call) = @_;
65 $pkg = ref $pkg if ref $pkg;
67 my $self = bless {call => $call}, $pkg;
68 dbg("create $pkg with $call") if isdbg('routelow');
70 # add in all the dxcc, itu, zone info
71 my @dxcc = Prefix::extract($call);
73 $self->{dxcc} = $dxcc[1]->dxcc;
74 $self->{itu} = $dxcc[1]->itu;
75 $self->{cq} = $dxcc[1]->cq;
76 $self->{state} = $dxcc[1]->state;
77 $self->{city} = $dxcc[1]->city;
79 $self->{flags} = here(1);
85 # get a callsign from a passed reference or a string
92 $thingy = $self unless $thingy;
93 $thingy = $thingy->call if ref $thingy;
94 $thingy = uc $thingy if $thingy;
99 # add and delete a callsign to/from a list
108 confess "Need a ref here" unless ref($c);
110 my $call = $c->{call};
111 unless (grep $_ eq $call, @{$self->{$field}}) {
112 push @{$self->{$field}}, $call;
113 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
126 confess "Need a ref here" unless ref($c);
127 my $call = $c->{call};
128 if (grep $_ eq $call, @{$self->{$field}}) {
129 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
130 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
140 return @{$self->{$_[0]}} == 0;
144 # flag field constructors/enquirers
146 # These can be called in various ways:-
148 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
149 # Route::here(1) returns 2 (the bit value of the here flag)
150 # $ref->here(1) or $ref->here(0) sets the here flag
157 return $self ? 2 : 0 unless ref $self;
158 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
159 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
167 return $self ? 1 : 0 unless ref $self;
168 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
169 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
176 return @{$self->{parent}};
186 my $call = sprintf "%s", $self->{call};
187 return $self->here ? "$call" : "($call)";
193 my $nodes_only = shift;
198 my $call = $self->user_call;
203 $printit = grep $call =~ m|$_|, @_;
207 $line = ' ' x ($level*2) . "$call";
208 $call = ' ' x length $call;
211 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
216 push @$seen, $self->{call};
219 unless ($nodes_only) {
220 if (@{$self->{users}}) {
222 foreach my $ucall (sort @{$self->{users}}) {
223 my $uref = Route::User::get($ucall);
226 $c = $uref->user_call;
230 if ((length $line) + (length $c) + 1 < 79) {
235 $line = ' ' x ($level*2) . "$call->$c ";
242 push @out, $line if length $line;
245 # deal with more nodes
246 foreach my $ncall (sort @{$self->{nodes}}) {
247 my $nref = Route::Node::get($ncall);
250 my $c = $nref->user_call;
251 # dbg("recursing from $call -> $c") if isdbg('routec');
252 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
254 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
263 my $nodes = Route::Node::count();
264 my $tot = Route::User::count();
265 my $users = scalar DXCommandmode::get_all();
266 my $maxusers = Route::User::max();
267 my $uptime = main::uptime();
269 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
279 return Route::Node::get($call) || Route::User::get($call);
282 # find all the possible dxchannels which this object might be on
287 # dbg("Trying node $self->{call}") if isdbg('routech');
289 my $dxchan = DXChannel->get($self->{call});
290 push @dxchan, $dxchan if $dxchan;
292 # it isn't, build up a list of dxchannels and possible ping times
293 # for all the candidates.
295 foreach my $p (@{$self->{parent}}) {
296 # dbg("Trying parent $p") if isdbg('routech');
297 next if $p eq $main::mycall; # the root
298 my $dxchan = DXChannel->get($p);
300 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
302 next if grep $p eq $_, @_;
303 my $ref = Route::Node::get($p);
304 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
305 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
309 # dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
317 # ALWAYS return the locally connected channel if present;
318 my $dxchan = DXChannel->get($self->call);
319 return $dxchan if $dxchan;
321 my @dxchan = $self->alldxchan;
322 return undef unless @dxchan;
324 # determine the minimum ping channel
325 my $minping = 99999999;
326 foreach my $dxc (@dxchan) {
327 my $p = $dxc->pingave;
328 if (defined $p && $p < $minping) {
333 $dxchan = shift @dxchan unless $dxchan;
348 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
353 # return a list of valid elements
359 $pkg = ref $pkg if ref $pkg;
360 my $val = "${pkg}::valid";
361 my @out = keys %$val;
362 push @out, keys %valid;
367 # return a prompt for a field
372 my ($self, $ele) = @_;
374 my $val = "${pkg}::valid";
375 return $val->{$ele} || $valid{$ele};
379 # generic AUTOLOAD for accessors
384 my $name = $AUTOLOAD;
385 return if $name =~ /::DESTROY$/;
388 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
390 # this clever line of code creates a subroutine which takes over from autoload
391 # from OO Perl - Conway
392 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};