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 ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
72 Prefix::cty_data($call);
74 $self->{flags} = here(1);
80 # get a callsign from a passed reference or a string
87 $thingy = $self unless $thingy;
88 $thingy = $thingy->call if ref $thingy;
89 $thingy = uc $thingy if $thingy;
94 # add and delete a callsign to/from a list
103 confess "Need a ref here" unless ref($c);
105 my $call = $c->{call};
106 unless (grep $_ eq $call, @{$self->{$field}}) {
107 push @{$self->{$field}}, $call;
108 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
121 confess "Need a ref here" unless ref($c);
122 my $call = $c->{call};
123 if (grep $_ eq $call, @{$self->{$field}}) {
124 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
125 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
135 return @{$self->{$_[0]}} == 0;
139 # flag field constructors/enquirers
141 # These can be called in various ways:-
143 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
144 # Route::here(1) returns 1 (the bit value of the here flag)
145 # $ref->here(1) or $ref->here(0) sets the here flag
147 # these are now redundant really as we are not interested in conferences
148 # and here is back to being '1'.
154 return $self ? 2 : 0 unless ref $self;
155 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
156 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
164 return $self ? 2 : 0 unless ref $self;
165 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
166 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
173 return @{$self->{parent}};
183 my $call = sprintf "%s", $self->{call};
184 return $self->here ? "$call" : "($call)";
190 my $nodes_only = shift;
195 my $call = $self->user_call;
200 $printit = grep $call =~ m|$_|, @_;
204 $line = ' ' x ($level*2) . "$call";
205 $call = ' ' x length $call;
208 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
213 push @$seen, $self->{call};
216 unless ($nodes_only) {
217 if (@{$self->{users}}) {
219 foreach my $ucall (sort @{$self->{users}}) {
220 my $uref = Route::User::get($ucall);
223 $c = $uref->user_call;
227 if ((length $line) + (length $c) + 1 < 79) {
232 $line = ' ' x ($level*2) . "$call->$c ";
239 push @out, $line if length $line;
242 # deal with more nodes
243 foreach my $ncall (sort @{$self->{nodes}}) {
244 my $nref = Route::Node::get($ncall);
247 my $c = $nref->user_call;
248 # dbg("recursing from $call -> $c") if isdbg('routec');
249 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
251 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
260 my $nodes = Route::Node::count();
261 my $tot = Route::User::count();
262 my $users = scalar DXCommandmode::get_all();
263 my $maxusers = Route::User::max();
264 my $uptime = main::uptime();
266 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
276 return Route::Node::get($call) || Route::User::get($call);
279 # find all the possible dxchannels which this object might be on
284 # dbg("Trying node $self->{call}") if isdbg('routech');
286 my $dxchan = DXChannel->get($self->{call});
287 push @dxchan, $dxchan if $dxchan;
289 # it isn't, build up a list of dxchannels and possible ping times
290 # for all the candidates.
292 foreach my $p (@{$self->{parent}}) {
293 # dbg("Trying parent $p") if isdbg('routech');
294 next if $p eq $main::mycall; # the root
295 my $dxchan = DXChannel->get($p);
297 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
299 next if grep $p eq $_, @_;
300 my $ref = Route::Node::get($p);
301 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
302 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
306 # dbg('routech', "Got dxchan: " . join(',', (map{ $_->call } @dxchan)) );
314 # ALWAYS return the locally connected channel if present;
315 my $dxchan = DXChannel->get($self->call);
316 return $dxchan if $dxchan;
318 my @dxchan = $self->alldxchan;
319 return undef unless @dxchan;
321 # determine the minimum ping channel
322 my $minping = 99999999;
323 foreach my $dxc (@dxchan) {
324 my $p = $dxc->pingave;
325 if (defined $p && $p < $minping) {
330 $dxchan = shift @dxchan unless $dxchan;
345 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
350 # return a list of valid elements
356 $pkg = ref $pkg if ref $pkg;
357 my $val = "${pkg}::valid";
358 my @out = keys %$val;
359 push @out, keys %valid;
364 # return a prompt for a field
369 my ($self, $ele) = @_;
371 my $val = "${pkg}::valid";
372 return $val->{$ele} || $valid{$ele};
376 # generic AUTOLOAD for accessors
381 my $name = $AUTOLOAD;
382 return if $name =~ /::DESTROY$/;
385 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
387 # this clever line of code creates a subroutine which takes over from autoload
388 # from OO Perl - Conway
389 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};