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 lastseen => '0,Last Seen,atime',
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 ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
73 Prefix::cty_data($call);
75 $self->{flags} = here(1);
81 # get a callsign from a passed reference or a string
88 $thingy = $self unless $thingy;
89 $thingy = $thingy->call if ref $thingy;
90 $thingy = uc $thingy if $thingy;
95 # add and delete a callsign to/from a list
104 confess "Need a ref here" unless ref($c);
106 my $call = $c->{call};
107 unless (grep $_ eq $call, @{$self->{$field}}) {
108 push @{$self->{$field}}, $call;
109 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
122 confess "Need a ref here" unless ref($c);
123 my $call = $c->{call};
124 if (grep $_ eq $call, @{$self->{$field}}) {
125 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
126 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
136 return @{$self->{$_[0]}} == 0;
140 # flag field constructors/enquirers
142 # These can be called in various ways:-
144 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
145 # Route::here(1) returns 2 (the bit value of the here flag)
146 # $ref->here(1) or $ref->here(0) sets the here flag
153 return $self ? 2 : 0 unless ref $self;
154 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
155 $self->{flags} &= ~2;
156 $self->{flags} |= $r ? 2 : 0;
164 return $self ? 1 : 0 unless ref $self;
165 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
166 $self->{flags} &= ~1;
167 $self->{flags} |= $r ? 1 : 0;
172 # pc59 entity encoding and decoding
177 my $sort = shift || 'N';
178 my $out = "$sort$self->{flag}$self->{call}";
179 if ($self->{build}) {
180 $out .= "b$self->{build}";
181 } elsif ($self->{version}) {
182 $out .= "v$self->{version}";
189 my $s = ref($node) ? shift : $node;
192 my ($sort, $here, $call) = unpack "A A A*", $s;
193 return unless is_callsign($call);
194 return unless $here =~ /^[0123]$/;
195 return unless $sort =~ /^[NUE]$/;
196 if ($sort eq 'E' || $sort eq 'N') {
197 $node = Route::Node::get($call) || Route::Node->new($call);
198 if ($s =~ /b([\d\.])/) {
201 if ($s =~ /v([\d\.])/) {
202 $node->{version} = $1;
204 } elsif ($sort eq 'U') {
205 $node = Route::User::get($call) || Route::User->new($call);
207 $node->flags = $here;
218 my $call = sprintf "%s", $self->{call};
219 return $self->here ? "$call" : "($call)";
225 my $nodes_only = shift;
230 my $call = $self->user_call;
235 $printit = grep $call =~ m|$_|, @_;
239 $line = ' ' x ($level*2) . "$call";
240 $call = ' ' x length $call;
243 if ((DXChannel->get($self->{call}) && $level > 1) || grep $self->{call} eq $_, @$seen) {
248 push @$seen, $self->{call};
251 unless ($nodes_only) {
252 if (@{$self->{users}}) {
254 foreach my $ucall (sort @{$self->{users}}) {
255 my $uref = Route::User::get($ucall);
258 $c = $uref->user_call;
262 if ((length $line) + (length $c) + 1 < 79) {
267 $line = ' ' x ($level*2) . "$call->$c ";
274 push @out, $line if length $line;
277 # deal with more nodes
278 foreach my $ncall (sort @{$self->{nodes}}) {
279 my $nref = Route::Node::get($ncall);
282 my $c = $nref->user_call;
283 # dbg("recursing from $call -> $c") if isdbg('routec');
284 push @out, $nref->config($nodes_only, $level+1, $seen, @_);
286 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
295 my $nodes = Route::Node::count();
296 my $tot = Route::User::count();
297 my $users = scalar DXCommandmode::get_all();
298 my $maxusers = Route::User::max();
299 my $uptime = main::uptime();
301 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
311 return Route::Node::get($call) || Route::User::get($call);
316 return (Route::Node::get_all(), Route::User::get_all());
319 # find all the possible dxchannels which this object might be on
324 my $dxchan = DXChannel->get($self->{call});
326 dbg("alldxchan for $self->{call} = $dxchan->{call}") if isdbg('routelow');
327 return $dxchan if $dxchan;
331 if ($self->isa('Route::User')) {
332 push @nodes, map{Route::Node::get($_)} @{$self->{nodes}};
333 } elsif ($self->isa('Route::Node')) {
337 # it isn't, build up a list of dxchannels and possible ping times
338 # for all the candidates.
340 foreach my $nref (@nodes) {
342 foreach my $p (@{$nref->{dxchan}}) {
343 # dbg("Trying dxchan $p") if isdbg('routech');
344 next if $p eq $main::mycall; # the root
345 my $dxchan = DXChannel->get($p);
347 push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
349 next if grep $p eq $_, @_;
350 my $ref = Route::Node::get($p);
351 # dbg("Next node $p " . ($ref ? 'Found' : 'NOT Found') if isdbg('routech') );
352 push @dxchan, $ref->alldxchan($self->{call}, @_) if $ref;
356 dbg("alldxchan for $self->{call} = (" . join(',', @dxchan) . ")") if isdbg('routelow');
364 # ALWAYS return the locally connected channel if present;
365 my $dxchan = DXChannel->get($self->call);
366 return $dxchan if $dxchan;
368 my @dxchan = sort { ($a->pingave || 9999999) <=> ($b->pingave || 9999999) } $self->alldxchan;
369 return undef unless @dxchan;
371 return shift @dxchan;
383 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
388 # return a list of valid elements
394 $pkg = ref $pkg if ref $pkg;
395 my $val = "${pkg}::valid";
396 my @out = keys %$val;
397 push @out, keys %valid;
402 # return a prompt for a field
407 my ($self, $ele) = @_;
409 my $val = "${pkg}::valid";
410 return $val->{$ele} || $valid{$ele};
414 # generic AUTOLOAD for accessors
419 my $name = $AUTOLOAD;
420 return if $name =~ /::DESTROY$/;
423 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
425 # this clever line of code creates a subroutine which takes over from autoload
426 # from OO Perl - Conway
427 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};