+sub get
+{
+ my $call = shift;
+ return Route::Node::get($call) || Route::User::get($call);
+}
+
+sub findroutes
+{
+ my $call = shift;
+ my $level = shift || 0;
+ my $seen = shift || {};
+ my @out;
+
+ dbg("findroutes: $call level: $level calls: " . join(',', @_)) if isdbg('routec');
+
+ # recursion detector
+ return () if $seen->{$call};
+
+ # return immediately if we are directly connected
+ if (my $dxchan = DXChannel::get($call)) {
+ $seen->{$call}++;
+ push @out, $level ? [$level, $dxchan] : $dxchan;
+ return @out;
+ }
+ $seen->{$call}++;
+
+ # deal with more nodes
+ my $nref = Route::get($call);
+ return () unless $nref;
+ foreach my $ncall (@{$nref->{parent}}) {
+ unless ($seen->{$ncall}) {
+
+ # put non-pc9x nodes to the back of the queue
+ my $l = $level + ($nref->{do_pc9x} && ($nref->{version}||5454) >= 5454 ? 0 : 30);
+ dbg("recursing from $call -> $ncall level $l") if isdbg('routec');
+ my @rout = findroutes($ncall, $l+1, $seen);
+ push @out, @rout;
+ }
+ }
+
+ if ($level == 0) {
+ my @nout = map {$_->[1]} sort {$a->[0] <=> $b->[0]} @out;
+ my $last;
+ if ($nref->isa('Route::Node')) {
+ my $ncall = $nref->PC92C_dxchan;
+ $last = DXChannel::get($ncall) if $ncall;
+ } else {
+ my $pcall = $nref->{parent}->[0];
+ my ($ref, $ncall);
+ $ref = Route::Node::get($pcall) if $pcall;
+ $ncall = $ref->PC92C_dxchan if $ref;
+ $last = DXChannel::get($ncall) if $ncall;
+ }
+
+ if (isdbg('findroutes')) {
+ if (@out) {
+ foreach (sort {$a->[0] <=> $b->[0]} @out) {
+ dbg("ROUTE: findroute $call -> $_->[0] " . $_->[1]->call);
+ }
+ } else {
+ dbg("ROUTE: findroute $call -> PC92C_dxchan " . $last->call) if $last;
+ }
+ }
+ push @nout, $last if @out == 0 && $last;
+ return @nout;
+ } else {
+ return @out;
+ }
+}
+
+# find all the possible dxchannels which this object might be on
+sub alldxchan
+{
+ my $self = shift;
+ my @dxchan = findroutes($self->{call});
+ return @dxchan;
+}
+
+sub dxchan
+{
+ my $self = shift;
+
+ # ALWAYS return the locally connected channel if present;
+ my $dxchan = DXChannel::get($self->call);
+ return $dxchan if $dxchan;
+
+ my @dxchan = $self->alldxchan;
+ return undef unless @dxchan;
+
+ # determine the minimum ping channel
+# my $minping = 99999999;
+# foreach my $dxc (@dxchan) {
+# my $p = $dxc->pingave;
+# if (defined $p && $p < $minping) {
+# $minping = $p;
+# $dxchan = $dxc;
+# }
+# }
+# $dxchan = shift @dxchan unless $dxchan;
+
+ # dxchannels are now returned in order of "closeness"
+ return $dxchan[0];
+}
+
+