basically working OK
[spider.git] / perl / Route / Node.pm
index 92ceba22577940c41cbba51faa732bd29834d1ef..0880622ea538c9739fe1bb958f95d8cec45bc450 100644 (file)
@@ -24,13 +24,13 @@ use vars qw(%list %valid @ISA $max $filterdef);
 @ISA = qw(Route);
 
 %valid = (
-                 dxchan => '0,Visible on DXChans,parray',
-                 nodes => '0,Nodes,parray',
-                 users => '0,Users,parray',
+                 dxchan => '0,DXChannel List,parray',
+                 nodes => '0,Node List,parray',
+                 users => '0,User List,parray',
                  usercount => '0,User Count',
                  version => '0,Version',
-                 np => '0,Using New Prot,yesno',
-                 lid => '0,Last Msgid',
+                 newroute => '0,New Routing?,yesno',
+                 pingtime => '0,Ping Time',
 );
 
 $filterdef = $Route::filterdef;
@@ -50,124 +50,114 @@ sub max
        return $max;
 }
 
+# link a node to this node and mark the route as available thru 
+# this dxchan, any users must be linked separately
 #
-# this routine handles the possible adding of an entry in the routing
-# table. It will only add an entry if it is new. It may have all sorts of
-# other side effects which may include fixing up other links.
-#
-# It will return a node object if (and only if) it is a completely new
-# object with that callsign. The upper layers are expected to do something
-# sensible with this!
-#
-# called as $dxchan->add(call, dxchan, version, flags) 
+# call as $node->link_node($neighbour, $dxchan);
 #
 
-sub add
+sub link_node
 {
-       my $dxchan = shift;
-       my $call = uc shift;
-       confess "Route::add trying to add $call to myself" if $call eq $dxchan->{call};
-       my $self = get($call);
-       if ($self) {
-               $self->_adddxchan($dxchan);
-               $dxchan->_addnode($self);
-               return undef;
-       }
-       $self = $dxchan->new($call, @_);
-       $dxchan->_addnode($self);
-       return $self;
+       my ($self, $neighbour, $dxchan) = @_;
+
+       my $r = $neighbour->is_empty('dxchan');
+       $self->_addlist('nodes', $neighbour);
+       $neighbour->_addlist('nodes', $self);
+       $neighbour->_addlist('dxchan', $dxchan);
+       return $r ? ($neighbour) : ();
 }
 
-#
-# this routine is the opposite of 'add' above.
-#
-# It will return an object if (and only if) this 'del' will remove
-# this object completely
+# unlink a node from a neighbour and remove any
+# routes, if this node becomes orphaned (no routes
+# and no nodes) then return it 
 #
 
-sub del
+sub unlink_node
 {
-       my $self = shift;
-       my $pref = shift;
-
-       # delete dxchan from this call's dxchan list
-       $pref->_delnode($self);
-    $self->_deldxchan($pref);
-       my @nodes;
-       my $ncall = $self->{call};
-       
-       # is this the last connection, I have no dxchan anymore?
-       unless (@{$self->{dxchan}}) {
-               foreach my $rcall (@{$self->{nodes}}) {
-                       next if grep $rcall eq $_, @_;
-                       my $r = Route::Node::get($rcall);
-                       push @nodes, $r->del($self, $ncall, @_) if $r;
-               }
-               $self->_del_users;
-               delete $list{$self->{call}};
-               push @nodes, $self;
-       }
-       return @nodes;
+       my ($self, $neighbour, $dxchan) = @_;
+       $self->_dellist('nodes', $neighbour);
+       $neighbour->_dellist('nodes', $self);
+       $neighbour->_dellist('dxchan', $dxchan) if $dxchan;
+       return $neighbour->is_empty('dxchan') ? ($neighbour) : ();
 }
 
-sub del_nodes
+sub remove_route
 {
-       my $dxchan = shift;
-       my @out;
-       foreach my $rcall (@{$dxchan->{nodes}}) {
-               my $r = get($rcall);
-               push @out, $r->del($dxchan, $dxchan->{call}, @_) if $r;
-       }
-       return @out;
-}
+       my ($self, $neighbour, $dxchan) = @_;
 
-sub _del_users
-{
-       my $self = shift;
-       for (@{$self->{users}}) {
-               my $ref = Route::User::get($_);
-               $ref->del($self) if $ref;
+       # cut the dxchan link
+       # cut the node link
+       my @rout;
+       push @rout, $self->unlink_node($neighbour, $dxchan);
+       dbg("Orphanning $neighbour->{call}") if isdbg('routelow');
+       
+       # then run down the tree removing this dxchan link from
+       # all the referenced nodes that use this interface
+       my %visited;
+       my @in = map { Route::Node::get($_) } $neighbour->nodes;
+       foreach my $r (@in) {
+               next unless $r;
+               next if $visited{$r->call};
+               my ($o) = $r->del_dxchan($dxchan);
+               if ($o) {
+                       dbg("Orphanning $o->{call}") if isdbg('routelow');
+                       push @rout, $o;
+               }
+               push @in, map{ Route::Node::get($_) } $r->nodes;
+               $visited{$r->call} = $r;
        }
-       $self->{users} = [];
+       
+       # in @rout there should be a list of orphaned (in dxchan terms)
+       # nodes. Now go thru and make sure that all their links are
+       # broken (they should be, but this is to check).
+       
+       foreach my $r (@rout) {
+               my @nodes = map { Route::Node::get($_)} $r->nodes;
+               for (@nodes) {
+                       next unless $_;
+                       dbg("Orphaned node $_->{call}: breaking link to $_->{call}") if isdbg('routelow');
+                       $r->unlink_node($_);
+               }
+       }
+       return @rout;
 }
 
 # add a user to this node
+# returns Route::User if it is a new user;
 sub add_user
 {
-       my $self = shift;
-       my $ucall = shift;
-
-       confess "Trying to add NULL User call to routing tables" unless $ucall;
-
-       my $uref = Route::User::get($ucall);
-       my @out;
-       if ($uref) {
-               @out = $uref->adddxchan($self);
-       } else {
-               $uref = Route::User->new($ucall, $self->{call}, @_);
-               @out = $uref;
-       }
-       $self->_adduser($uref);
+       my ($self, $uref) = @_;
+       my $r = $uref->is_empty('nodes');
+       $self->_addlist('users', $uref);
+       $uref->_addlist('nodes', $self);
        $self->{usercount} = scalar @{$self->{users}};
-
-       return @out;
+       return $r ? ($uref) : ();
 }
 
 # delete a user from this node
 sub del_user
 {
-       my $self = shift;
-       my $ref = shift;
-       my @out;
-       
-       if ($ref) {
-               @out = $self->_deluser($ref);
-               $ref->del($self);
-       } else {
-               confess "tried to delete non-existant $ref->{call} from $self->{call}";
-       }
+       my ($self, $uref) = @_;
+
+       $self->_dellist('users', $uref);
+       $uref->_dellist('nodes', $self);
        $self->{usercount} = scalar @{$self->{users}};
-       return @out;
+       return $uref->is_empty('nodes') ? ($uref) : ();
+}
+
+# add a single dxchan link
+sub add_dxchan
+{
+       my ($self, $dxchan) = @_;
+       return $self->_addlist('dxchan', $dxchan);
+}
+
+# remove a single dxchan link
+sub del_dxchan
+{
+       my ($self, $dxchan) = @_;
+       $self->_dellist('dxchan', $dxchan);
+       return $self->is_empty('dxchan') ? ($self) : ();
 }
 
 sub usercount
@@ -191,20 +181,17 @@ sub nodes
        return @{$self->{nodes}};
 }
 
-sub rnodes
+sub unlink_all_users
 {
        my $self = shift;
-       my @out;
-       foreach my $call (@{$self->{nodes}}) {
-               next if grep $call eq $_, @_;
-               push @out, $call;
-               my $r = get($call);
-               push @out, $r->rnodes($call, @_) if $r;
+       my @rout;
+       foreach my $u (@{$self->{users}}) {
+               my $uref = Route::User::get($u);
+               push @rout, $self->del_user($uref) if $uref;
        }
-       return @out;
+       return @rout;
 }
 
-
 sub new
 {
        my $pkg = shift;
@@ -213,18 +200,28 @@ sub new
        confess "already have $call in $pkg" if $list{$call};
        
        my $self = $pkg->SUPER::new($call);
-       $self->{dxchan} = ref $pkg ? [ $pkg->{call} ] : [ ];
-       $self->{version} = shift;
-       $self->{flags} = shift;
+       $self->{dxchan} = [ ];
+       $self->{version} = shift || 5000;
+       $self->{flags} = shift || Route::here(1);
        $self->{users} = [];
        $self->{nodes} = [];
-       $self->{lid} = 0;
        
        $list{$call} = $self;
+       dbg("creating Route::Node $self->{call}") if isdbg('routelow');
        
        return $self;
 }
 
+sub delete
+{
+       my $self = shift;
+       dbg("Deleting Route::Node $self->{call}") if isdbg('routelow');
+       for ($self->unlink_all_users) {
+               $_->delete;
+       }
+       delete $list{$self->{call}};
+}
+
 sub get
 {
        my $call = shift;
@@ -239,19 +236,6 @@ sub get_all
        return values %list;
 }
 
-
-sub _adduser
-{
-       my $self = shift;
-    return $self->_addlist('users', @_);
-}
-
-sub _deluser
-{
-       my $self = shift;
-    return $self->_dellist('users', @_);
-}
-
 sub DESTROY
 {
        my $self = shift;
@@ -259,6 +243,7 @@ sub DESTROY
        my $call = $self->{call} || "Unknown";
        
        dbg("destroying $pkg with $call") if isdbg('routelow');
+       $self->unlink_all_users if @{$self->{users}};
 }
 
 #