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 $default_metric);
34 flags => "0,Flags,phex",
35 dxcc => '0,Country Code',
40 dxchan => '0,DXChans,parray',
41 links => '0,Node Links,parray',
46 # tag, sort, field, priv, special parser
48 ['channel_dxcc', 'nc', 1],
49 ['channel_itu', 'ni', 2],
50 ['channel_zone', 'nz', 3],
53 ['call_dxcc', 'nc', 5],
55 ['call_itu', 'ni', 6],
57 ['call_zone', 'nz', 7],
59 ['channel_state', 'ns', 8],
60 ['call_state', 'ns', 9],
61 ['by_state', 'ns', 9],
68 my ($pkg, $call) = @_;
69 $pkg = ref $pkg if ref $pkg;
71 my $self = bless {call => $call}, $pkg;
72 dbg("create $pkg with $call") if isdbg('routelow');
74 # add in all the dxcc, itu, zone info
75 my @dxcc = Prefix::extract($call);
77 $self->{dxcc} = $dxcc[1]->dxcc;
78 $self->{itu} = $dxcc[1]->itu;
79 $self->{cq} = $dxcc[1]->cq;
80 $self->{state} = $dxcc[1]->state;
81 $self->{city} = $dxcc[1]->city;
83 $self->{flags} = here(1);
89 # get a callsign from a passed reference or a string
96 $thingy = $self unless $thingy;
97 $thingy = $thingy->call if ref $thingy;
98 $thingy = uc $thingy if $thingy;
103 # add and delete a callsign to/from a list
112 confess "Need a ref here" unless ref($c);
114 my $call = $c->{call};
115 unless (grep $_ eq $call, @{$self->{$field}}) {
116 push @{$self->{$field}}, $call;
117 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
130 confess "Need a ref here" unless ref($c);
131 my $call = $c->{call};
132 if (grep $_ eq $call, @{$self->{$field}}) {
133 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
134 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
144 return @{$self->{$_[0]}} == 0;
148 # flag field constructors/enquirers
150 # These can be called in various ways:-
152 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
153 # Route::here(1) returns 2 (the bit value of the here flag)
154 # $ref->here(1) or $ref->here(0) sets the here flag
161 return $self ? 2 : 0 unless ref $self;
162 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
163 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
171 return $self ? 1 : 0 unless ref $self;
172 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
173 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
184 my $call = sprintf "%s", $self->{call};
185 return $self->here ? "$call" : "($call)";
191 my $nodes_only = shift;
196 my $call = $self->user_call;
201 $printit = grep $call =~ m|$_|, @_;
205 $line = ' ' x ($level*2) . "$call";
206 $call = ' ' x length $call;
209 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
214 push @$seen, $self->{call};
217 unless ($nodes_only) {
218 if (@{$self->{users}}) {
220 foreach my $ucall (sort @{$self->{users}}) {
221 my $uref = Route::User::get($ucall);
224 $c = $uref->user_call;
228 if ((length $line) + (length $c) + 1 < 79) {
233 $line = ' ' x ($level*2) . "$call->$c ";
240 push @out, $line if length $line;
243 # deal with more nodes
244 foreach my $ncall (sort @{$self->{links}}) {
245 my $nref = Route::Node::get($ncall);
248 my $c = $nref->user_call;
249 # dbg("recursing from $call -> $c") if isdbg('routec');
250 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
252 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
261 my $nodes = Route::Node::count();
262 my $tot = Route::User::count();
263 my $users = scalar DXCommandmode::get_all();
264 my $maxusers = Route::User::max();
265 my $uptime = main::uptime();
267 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
277 return Route::Node::get($call) || Route::User::get($call);
284 my $call = $self->{call};
286 if (DXChannel->get($call)) {
287 my $n = scalar @_ || 0;
288 my $o = $ah->{$call} || 9999;
289 $ah->{$call} = $n if $n < $o;
290 dbg("_distance hit: $call = $n") if isdbg('routech');
294 dbg("_distance miss $call: " . join(',', @_)) if isdbg('routech');
296 foreach my $c (@{$self->{links}}) {
297 next if $c eq $call || $c eq $main::mycall;
298 next if grep $c eq $_, @_;
301 _distance($n, $ah, @_, $c);
311 if (exists $self->{dxchan}) {
312 dbg("stored routes for $self->{call}: " . join(',', @{$self->{dxchan}})) if isdbg('routech');
313 return @{$self->{dxchan}} if exists $self->{dxchan};
317 _distance($self, \%ah);
319 @routes = sort {$ah{$a} <=> $ah{$b}} keys %ah;
320 $self->{dxchan} = \@routes;
321 dbg("new routes for $self->{call}: " . join(',', @routes)) if isdbg('routech');
325 # find all the possible dxchannels which this object might be on
331 my $dxchan = DXChannel->get($self->{call});
332 push @dxchan, $dxchan if $dxchan;
334 @dxchan = map {DXChannel->get($_)} _ordered_routes($self) unless @dxchan;
342 # ALWAYS return the locally connected channel if present;
343 my $dxchan = DXChannel->get($self->call);
344 return $dxchan if $dxchan;
346 my @dxchan = $self->alldxchan;
347 return undef unless @dxchan;
349 # determine the minimum ping channel
350 my $minping = 99999999;
351 foreach my $dxc (@dxchan) {
352 my $p = $dxc->pingave;
353 if (defined $p && $p < $minping) {
358 $dxchan = shift @dxchan unless $dxchan;
365 delete $self->{dxchan};
366 return $self->_addlist('links', @_);
372 delete $self->{dxchan};
373 return $self->_dellist('links', @_);
385 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
390 # return a list of valid elements
396 $pkg = ref $pkg if ref $pkg;
397 my $val = "${pkg}::valid";
398 my @out = keys %$val;
399 push @out, keys %valid;
404 # return a prompt for a field
409 my ($self, $ele) = @_;
411 my $val = "${pkg}::valid";
412 return $val->{$ele} || $valid{$ele};
416 # generic AUTOLOAD for accessors
421 my $name = $AUTOLOAD;
422 return if $name =~ /::DESTROY$/;
425 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
427 # this clever line of code creates a subroutine which takes over from autoload
428 # from OO Perl - Conway
429 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};