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',
40 lastupdate => '0,Last Update,cldatetime',
44 # tag, sort, field, priv, special parser
46 ['channel_dxcc', 'nc', 1],
47 ['channel_itu', 'ni', 2],
48 ['channel_zone', 'nz', 3],
51 ['call_dxcc', 'nc', 5],
53 ['call_itu', 'ni', 6],
55 ['call_zone', 'nz', 7],
57 ['channel_state', 'ns', 8],
58 ['call_state', 'ns', 9],
59 ['by_state', 'ns', 9],
65 my ($pkg, $call) = @_;
66 $pkg = ref $pkg if ref $pkg;
68 my $self = bless {call => $call}, $pkg;
69 dbg("create $pkg with $call") if isdbg('routelow');
71 # add in all the dxcc, itu, zone info
72 my @dxcc = Prefix::extract($call);
74 $self->{dxcc} = $dxcc[1]->dxcc;
75 $self->{itu} = $dxcc[1]->itu;
76 $self->{cq} = $dxcc[1]->cq;
77 $self->{state} = $dxcc[1]->state;
78 $self->{city} = $dxcc[1]->city;
80 $self->{flags} = here(1);
86 # get a callsign from a passed reference or a string
93 $thingy = $self unless $thingy;
94 $thingy = $thingy->call if ref $thingy;
95 $thingy = uc $thingy if $thingy;
100 # add and delete a callsign to/from a list
109 confess "Need a ref here" unless ref($c);
111 my $call = $c->{call};
112 unless (grep $_ eq $call, @{$self->{$field}}) {
113 push @{$self->{$field}}, $call;
114 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
127 confess "Need a ref here" unless ref($c);
128 my $call = $c->{call};
129 if (grep $_ eq $call, @{$self->{$field}}) {
130 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
131 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
141 return @{$self->{$_[0]}} == 0;
145 # flag field constructors/enquirers
147 # These can be called in various ways:-
149 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
150 # Route::here(1) returns 2 (the bit value of the here flag)
151 # $ref->here(1) or $ref->here(0) sets the here flag
158 return $self ? 2 : 0 unless ref $self;
159 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
160 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
168 return $self ? 1 : 0 unless ref $self;
169 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
170 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
177 return @{$self->{parent}};
187 my $call = sprintf "%s", $self->{call};
188 return $self->here ? "$call" : "($call)";
194 my $nodes_only = shift;
199 my $call = $self->user_call;
204 $printit = grep $call =~ m|$_|, @_;
208 $line = ' ' x ($level*2) . "$call";
209 $call = ' ' x length $call;
212 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
217 push @$seen, $self->{call};
220 unless ($nodes_only) {
221 if (@{$self->{users}}) {
223 foreach my $ucall (sort @{$self->{users}}) {
224 my $uref = Route::User::get($ucall);
227 $c = $uref->user_call;
231 if ((length $line) + (length $c) + 1 < 79) {
236 $line = ' ' x ($level*2) . "$call->$c ";
243 push @out, $line if length $line;
246 # deal with more nodes
247 foreach my $ncall (sort @{$self->{nodes}}) {
248 my $nref = Route::Node::get($ncall);
251 my $c = $nref->user_call;
252 # dbg("recursing from $call -> $c") if isdbg('routec');
253 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
255 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
264 my $nodes = Route::Node::count();
265 my $tot = Route::User::count();
266 my $users = scalar DXCommandmode::get_all();
267 my $maxusers = Route::User::max();
268 my $uptime = main::uptime();
270 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
280 return Route::Node::get($call) || Route::User::get($call);
283 # find all the possible dxchannels which this object might be on
288 # dbg("Trying node $self->{call}") if isdbg('routech');
290 my $dxchan = DXChannel->get($self->{call});
291 push @dxchan, $dxchan if $dxchan;
293 # it isn't, build up a list of dxchannels and possible ping times
294 # for all the candidates.
296 foreach my $p (@{$self->{parent}}) {
297 # dbg("Trying parent $p") if isdbg('routech');
298 next if $p eq $main::mycall; # the root
299 my $dxchan = DXChannel->get($p);
301 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
303 next if grep $p eq $_, @_;
304 my $ref = Route::Node::get($p);
305 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
306 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
310 # dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
318 # ALWAYS return the locally connected channel if present;
319 my $dxchan = DXChannel->get($self->call);
320 return $dxchan if $dxchan;
322 my @dxchan = $self->alldxchan;
323 return undef unless @dxchan;
325 # determine the minimum ping channel
326 my $minping = 99999999;
327 foreach my $dxc (@dxchan) {
328 my $p = $dxc->pingave;
329 if (defined $p && $p < $minping) {
334 $dxchan = shift @dxchan unless $dxchan;
349 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
354 # return a list of valid elements
360 $pkg = ref $pkg if ref $pkg;
361 my $val = "${pkg}::valid";
362 my @out = keys %$val;
363 push @out, keys %valid;
368 # return a prompt for a field
373 my ($self, $ele) = @_;
375 my $val = "${pkg}::valid";
376 return $val->{$ele} || $valid{$ele};
380 # generic AUTOLOAD for accessors
385 my $name = $AUTOLOAD;
386 return if $name =~ /::DESTROY$/;
389 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
391 # this clever line of code creates a subroutine which takes over from autoload
392 # from OO Perl - Conway
393 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};