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
26 use vars qw($VERSION $BRANCH);
27 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /:\s+(\d+)\.(\d+)/ );
28 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /:\s+\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
29 $main::build += $VERSION;
30 $main::branch += $BRANCH;
32 use vars qw(%list %valid $filterdef);
36 flags => "0,Flags,phex",
37 dxcc => '0,Country Code',
42 lastseen => '0,Last Seen,atime',
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],
67 my ($pkg, $call) = @_;
68 $pkg = ref $pkg if ref $pkg;
70 my $self = bless {call => $call}, $pkg;
71 dbg("Create $pkg with $call") if isdbg('routelow');
73 # add in all the dxcc, itu, zone info
74 ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
75 Prefix::cty_data($call);
77 $self->{flags} = here(1);
83 # get a callsign from a passed reference or a string
90 $thingy = $self unless $thingy;
91 $thingy = $thingy->call if ref $thingy;
92 $thingy = uc $thingy if $thingy;
97 # add and delete a callsign to/from a list
106 confess "Need a ref here" unless ref($c);
108 my $call = $c->{call};
109 unless (grep $_ eq $call, @{$self->{$field}}) {
110 push @{$self->{$field}}, $call;
111 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
124 confess "Need a ref here" unless ref($c);
125 my $call = $c->{call};
126 if (grep $_ eq $call, @{$self->{$field}}) {
127 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
128 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
138 return @{$self->{$_[0]}} == 0;
142 # flag field constructors/enquirers
144 # These can be called in various ways:-
146 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
147 # Route::here(1) returns 2 (the bit value of the here flag)
148 # $ref->here(1) or $ref->here(0) sets the here flag
155 return $self ? 2 : 0 unless ref $self;
156 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
157 $self->{flags} &= ~2;
158 $self->{flags} |= $r ? 2 : 0;
166 return $self ? 1 : 0 unless ref $self;
167 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
168 $self->{flags} &= ~1;
169 $self->{flags} |= $r ? 1 : 0;
174 # pc59 entity encoding and decoding
179 my $sort = shift || 'N';
180 my $out = "$sort$self->{flag}$self->{call}";
181 if ($self->{build}) {
182 $out .= "b$self->{build}";
183 } elsif ($self->{version}) {
184 $out .= "v$self->{version}";
191 my $s = ref($node) ? shift : $node;
194 my ($sort, $here, $call) = unpack "A A A*", $s;
195 return unless is_callsign($call);
196 return unless $here =~ /^[0123]$/;
197 return unless $sort =~ /^[NUE]$/;
198 if ($sort eq 'E' || $sort eq 'N') {
199 $node = Route::Node::get($call) || Route::Node->new($call);
200 if ($s =~ /b([\d\.])/) {
203 if ($s =~ /v([\d\.])/) {
204 $node->{version} = $1;
206 } elsif ($sort eq 'U') {
207 $node = Route::User::get($call) || Route::User->new($call);
209 $node->{flags} = $here;
210 $node->{lastseen} = $main::systime;
221 my $call = sprintf "%s", $self->{call};
222 return $self->here ? "$call" : "($call)";
228 my $nodes_only = shift;
233 my $call = $self->user_call;
238 $printit = grep $call =~ m|$_|, @_;
242 $line = ' ' x ($level*2) . "$call";
243 $call = ' ' x length $call;
246 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
251 push @$seen, $self->{call};
254 unless ($nodes_only) {
255 if (@{$self->{users}}) {
257 foreach my $ucall (sort @{$self->{users}}) {
258 my $uref = Route::User::get($ucall);
261 $c = $uref->user_call;
265 if ((length $line) + (length $c) + 1 < 79) {
270 $line = ' ' x ($level*2) . "$call->$c ";
277 push @out, $line if length $line;
280 # deal with more nodes
281 foreach my $ncall (sort @{$self->{nodes}}) {
282 my $nref = Route::Node::get($ncall);
285 my $c = $nref->user_call;
286 # dbg("recursing from $call -> $c") if isdbg('routec');
287 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
289 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
298 my $nodes = Route::Node::count();
299 my $tot = Route::User::count();
300 my $users = scalar DXCommandmode::get_all();
301 my $maxusers = Route::User::max();
302 my $uptime = main::uptime();
304 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
314 return Route::Node::get($call) || Route::User::get($call);
319 return (Route::Node::get_all(), Route::User::get_all());
322 # find all the possible dxchannels which this object might be on
327 my $dxchan = DXChannel->get($self->{call});
329 dbg("alldxchan for $self->{call} = $dxchan->{call}") if isdbg('routelow');
330 return $dxchan if $dxchan;
334 if ($self->isa('Route::User')) {
335 push @nodes, map{Route::Node::get($_)} @{$self->{nodes}};
336 } elsif ($self->isa('Route::Node')) {
340 # it isn't, build up a list of dxchannels and possible ping times
341 # for all the candidates.
343 foreach my $nref (@nodes) {
345 foreach my $p (@{$nref->{dxchan}}) {
346 # dbg("Trying dxchan $p") if isdbg('routech');
347 next if $p eq $main::mycall; # the root
348 my $dxchan = DXChannel->get($p);
350 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
352 next if grep $p eq $_, @_;
353 my $ref = Route::Node::get($p);
354 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
355 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
359 dbg("alldxchan for $self->{call} = (" . join(',', @dxchan) . ")") if isdbg('routelow');
367 # ALWAYS return the locally connected channel if present;
368 my $dxchan = DXChannel->get($self->call);
369 return $dxchan if $dxchan;
371 my @dxchan = sort { ($a->pingave || 9999999) <=> ($b->pingave || 9999999) } $self->alldxchan;
372 return undef unless @dxchan;
374 return shift @dxchan;
386 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
391 # return a list of valid elements
397 $pkg = ref $pkg if ref $pkg;
398 my $val = "${pkg}::valid";
399 my @out = keys %$val;
400 push @out, keys %valid;
405 # return a prompt for a field
410 my ($self, $ele) = @_;
412 my $val = "${pkg}::valid";
413 return $val->{$ele} || $valid{$ele};
417 # generic AUTOLOAD for accessors
422 my $name = $AUTOLOAD;
423 return if $name =~ /::DESTROY$/;
426 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
428 # this clever line of code creates a subroutine which takes over from autoload
429 # from OO Perl - Conway
430 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};