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
25 use vars qw(%list %valid $filterdef);
29 flags => "0,Flags,phex",
30 dxcc => '0,Country Code',
38 # tag, sort, field, priv, special parser
40 ['channel_dxcc', 'nc', 1],
41 ['channel_itu', 'ni', 2],
42 ['channel_zone', 'nz', 3],
45 ['call_dxcc', 'nc', 5],
47 ['call_itu', 'ni', 6],
49 ['call_zone', 'nz', 7],
51 ['channel_state', 'ns', 8],
52 ['call_state', 'ns', 9],
53 ['by_state', 'ns', 9],
59 my ($pkg, $call) = @_;
60 $pkg = ref $pkg if ref $pkg;
62 my $self = bless {call => $call}, $pkg;
63 dbg("create $pkg with $call") if isdbg('routelow');
65 # add in all the dxcc, itu, zone info
66 ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
67 Prefix::cty_data($call);
69 $self->{flags} = here(1);
75 # get a callsign from a passed reference or a string
82 $thingy = $self unless $thingy;
83 $thingy = $thingy->call if ref $thingy;
84 $thingy = uc $thingy if $thingy;
89 # add and delete a callsign to/from a list
98 confess "Need a ref here" unless ref($c);
100 my $call = $c->{call};
101 unless (grep $_ eq $call, @{$self->{$field}}) {
102 push @{$self->{$field}}, $call;
103 dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
116 confess "Need a ref here" unless ref($c);
117 my $call = $c->{call};
118 if (grep $_ eq $call, @{$self->{$field}}) {
119 $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
120 dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
130 return @{$self->{$_[0]}} == 0;
134 # flag field constructors/enquirers
136 # These can be called in various ways:-
138 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
139 # Route::here(1) returns 2 (the bit value of the here flag)
140 # $ref->here(1) or $ref->here(0) sets the here flag
147 return $self ? 2 : 0 unless ref $self;
148 return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
149 $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
157 return $self ? 1 : 0 unless ref $self;
158 return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
159 $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
166 return @{$self->{parent}};
176 my $call = sprintf "%s", $self->{call};
177 return $self->here ? "$call" : "($call)";
183 my $nodes_only = shift || 0;
184 my $width = shift || 79;
189 my $call = $self->{call};
192 dbg("config: $call nodes: $nodes_only level: $level calls: " . join(',', @_)) if isdbg('routec');
196 $printit = grep $call =~ m|$_|, @_;
200 my $pcall = $self->user_call;
201 $pcall .= ":" . $self->obscount if isdbg('obscount');
204 $line = ' ' x ($level*2) . $pcall;
205 $pcall = ' ' x length $pcall;
208 if ((DXChannel::get($call) && $level > 1) || $seen->{$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 < $width) {
232 $line = ' ' x ($level*2) . "$pcall->$c ";
239 push @out, $line if length $line;
242 if ((DXChannel::get($call) && $level > 1) || $seen->{$call}) {
248 # deal with more nodes
249 foreach my $ncall (sort @{$self->{nodes}}) {
250 my $nref = Route::Node::get($ncall);
253 my $c = $nref->user_call;
254 dbg("recursing from $call -> $c") if isdbg('routec');
255 my @rout = $nref->config($nodes_only, $width, $level+1, $seen, @_);
257 push @out, ' ' x ($level*2) . $self->user_call unless grep /^\s+$call/, @out;
261 push @out, ' ' x (($level+1)*2) . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
270 my $nodes = Route::Node::count();
271 my $tot = Route::User::count();
272 my $users = scalar DXCommandmode::get_all();
273 my $maxusers = Route::User::max();
274 my $uptime = main::uptime();
276 return " $nodes nodes, $users local / $tot total users Max users $maxusers Uptime $uptime";
286 return Route::Node::get($call) || Route::User::get($call);
289 # this may be a better algorithm
290 #start = {start node}
293 #marked(n) = false for all nodes n
295 #while queue is not empty:
298 # for each node n in queue:
299 # for each edge from node n to node m:
303 # -- We've found the end node
304 # -- it's a distance "dist" from the start
312 my $level = shift || 0;
313 my $seen = shift || {};
316 dbg("findroutes: $call level: $level calls: " . join(',', @_)) if isdbg('routec');
318 # recursion detector (no point in recursing that deeply)
319 return () if $seen->{$call};
321 # dbg("Route::findroutes: recursion limit reached looking for $call");
325 # return immediately if we are directly connected
326 if (my $dxchan = DXChannel::get($call)) {
328 push @out, $level ? [$level, $dxchan] : $dxchan;
333 # deal with more nodes
334 my $nref = Route::get($call);
335 return () unless $nref;
336 foreach my $ncall (@{$nref->{parent}}) {
337 unless ($seen->{$ncall}) {
339 # put non-pc9x nodes to the back of the queue
340 my $l = $level + ($nref->{do_pc9x} && ($nref->{version}||5454) >= 5454 ? 0 : 30);
341 dbg("recursing from $call -> $ncall level $l") if isdbg('routec');
342 my @rout = findroutes($ncall, $l+1, $seen);
348 my @nout = map {$_->[1]} sort {$a->[0] <=> $b->[0]} @out;
350 if ($nref->isa('Route::Node')) {
351 my $ncall = $nref->PC92C_dxchan;
352 $last = DXChannel::get($ncall) if $ncall;
354 my $pcall = $nref->{parent}->[0];
356 $ref = Route::Node::get($pcall) if $pcall;
357 $ncall = $ref->PC92C_dxchan if $ref;
358 $last = DXChannel::get($ncall) if $ncall;
361 if (isdbg('findroutes')) {
363 foreach (sort {$a->[0] <=> $b->[0]} @out) {
364 dbg("ROUTE: findroute $call -> $_->[0] " . $_->[1]->call);
367 dbg("ROUTE: findroute $call -> PC92C_dxchan " . $last->call) if $last;
370 push @nout, $last if @out == 0 && $last;
377 # find all the possible dxchannels which this object might be on
381 my @dxchan = findroutes($self->{call});
389 # ALWAYS return the locally connected channel if present;
390 my $dxchan = DXChannel::get($self->call);
391 return $dxchan if $dxchan;
393 my @dxchan = $self->alldxchan;
394 return undef unless @dxchan;
396 # determine the minimum ping channel
397 # my $minping = 99999999;
398 # foreach my $dxc (@dxchan) {
399 # my $p = $dxc->pingave;
400 # if (defined $p && $p < $minping) {
405 # $dxchan = shift @dxchan unless $dxchan;
407 # dxchannels are now returned in order of "closeness"
422 dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
427 # return a list of valid elements
433 $pkg = ref $pkg if ref $pkg;
434 my $val = "${pkg}::valid";
435 my @out = keys %$val;
436 push @out, keys %valid;
441 # return a prompt for a field
446 my ($self, $ele) = @_;
448 my $val = "${pkg}::valid";
449 return $val->{$ele} || $valid{$ele};
453 # generic AUTOLOAD for accessors
458 my $name = $AUTOLOAD;
459 return if $name =~ /::DESTROY$/;
462 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
464 # this clever line of code creates a subroutine which takes over from autoload
465 # from OO Perl - Conway
466 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};