X-Git-Url: http://gb7djk.dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FRoute%2FNode.pm;h=f3072db121cb747b440c838244cc51025606cf3c;hb=8c23f70c2487e906a80ef323763cb7cb123baf28;hp=9e1f3c04fecf7ba100ca35ed4c5594dc82578e91;hpb=b67b50de92dbf61ce939b42f7c74e30dc58eba41;p=spider.git diff --git a/perl/Route/Node.pm b/perl/Route/Node.pm index 9e1f3c04..f3072db1 100644 --- a/perl/Route/Node.pm +++ b/perl/Route/Node.pm @@ -14,203 +14,260 @@ use Route::User; use strict; -use vars qw(%list %valid @ISA $max); +use vars qw($VERSION $BRANCH); +$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /:\s+(\d+)\.(\d+)/ ); +$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /:\s+\d+\.\d+\.(\d+)\.(\d+)/ || (0,0)); +$main::build += $VERSION; +$main::branch += $BRANCH; + +use vars qw(%list %valid @ISA $max $filterdef); @ISA = qw(Route); %valid = ( - parent => '0,Parent Calls,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', + newroute => '0,New Routing?,yesno', + pingtime => '0,Ping Time', ); +$filterdef = $Route::filterdef; %list = (); $max = 0; sub count { - my $n = scalar %list; + my $n = scalar (keys %list); $max = $n if $n > $max; return $n; } sub max { + count(); 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 $parent->add(call, dxchan, version, flags) +# call as $node->link_node($neighbour, $dxchan); # -sub add +sub link_node { - my $parent = shift; - my $call = uc shift; - my $self = get($call); - if ($self) { - $self->_addparent($parent->{call}); - return undef; - } - $parent->_addnode($call); - $self = $parent->new($call, @_); - 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 parent from this call's parent list - my $pcall = $pref->{call}; - my $ref = $self->_delparent($pcall); - my @nodes; - - # is this the last connection? - $self->_del_users; - unless (@$ref) { - push @nodes, $self->del_nodes; - 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_users +sub add_route { - my $self = shift; - for (@{$self->{users}}) { - my $ref = Route::User::get($_); - $ref->del($self) if $ref; + my ($self, $neighbour, $dxchan) = @_; + + # add the dxchan link + # add the node link + my @rout; + push @rout, $self->link_node($neighbour, $dxchan); + dbg("Adding $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}; + next if $r->{call} eq $main::mycall; + next if $r->{call} eq $self->{call}; + my ($o) = $r->add_dxchan($dxchan); + if ($o) { + dbg("Connecting new node $o->{call}") if isdbg('routelow'); + push @rout, $o; + } + push @in, map{ Route::Node::get($_) } $r->nodes; + $visited{$r->call} = $r; } - $self->{users} = []; + + # @rout should contain any nodes that have now been de-orphaned + # ie have had their first dxchan added. + return @rout; } -# remove all sub nodes from this parent -sub del_nodes +sub remove_route { - my $self = shift; - my @nodes; + my ($self, $neighbour, $dxchan) = @_; + + # 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}; + next if $r->{call} eq $main::mycall; + next if $r->{call} eq $self->{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; + } + + # 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). - for (@{$self->{nodes}}) { - next if $self->{call} eq $_; - push @nodes, $self->del_node($_); + 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 @nodes; + 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; - $self->_adduser($ucall); - - my $uref = Route::User::get($ucall); - return $uref ? () : (Route::User->new($ucall, $self->{call}, @_)); + my ($self, $uref) = @_; + my $r = $uref->is_empty('nodes'); + $self->_addlist('users', $uref); + $uref->_addlist('nodes', $self); + $self->{usercount} = scalar @{$self->{users}}; + return $r ? ($uref) : (); } # delete a user from this node sub del_user { - my $self = shift; - my $ucall = shift; - my $ref = Route::User::get($ucall); - $self->_deluser($ucall); - return ($ref->del($self)) if $ref; - return (); -} + my ($self, $uref) = @_; -# delete a node from this node (ie I am a parent) -sub del_node -{ - my $self = shift; - my $ncall = shift; - $self->_delnode($ncall); - my $ref = get($ncall); - return ($ref->del($self)) if $ref; - return (); + $self->_dellist('users', $uref); + $uref->_dellist('nodes', $self); + $self->{usercount} = scalar @{$self->{users}}; + return $uref->is_empty('nodes') ? ($uref) : (); } -sub new +# add a single dxchan link +sub add_dxchan { - my $pkg = shift; - my $call = uc shift; - - confess "already have $call in $pkg" if $list{$call}; - - my $self = $pkg->SUPER::new($call); - $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ]; - $self->{version} = shift; - $self->{flags} = shift; - $self->{users} = []; - $self->{nodes} = []; - - $list{$call} = $self; - - return $self; + my ($self, $dxchan) = @_; + return $self->_addlist('dxchan', $dxchan); } -sub get +# remove a single dxchan link +sub del_dxchan { - my $call = shift; - $call = shift if ref $call; - return $list{uc $call}; + my ($self, $dxchan) = @_; + $self->_dellist('dxchan', $dxchan); + return $self->is_empty('dxchan') ? ($self) : (); } -sub _addparent +sub usercount { my $self = shift; - return $self->_addlist('parent', @_); + if (@_ && @{$self->{users}} == 0) { + $self->{usercount} = shift; + } + return $self->{usercount}; } -sub _delparent +sub users { my $self = shift; - return $self->_dellist('parent', @_); + return @{$self->{users}}; } - -sub _addnode +sub nodes { my $self = shift; - return $self->_addlist('nodes', @_); + return @{$self->{nodes}}; } -sub _delnode +sub unlink_all_users { my $self = shift; - return $self->_dellist('nodes', @_); + my @rout; + foreach my $u (@{$self->{users}}) { + my $uref = Route::User::get($u); + push @rout, $self->del_user($uref) if $uref; + } + return @rout; } +sub new +{ + my $pkg = shift; + my $call = uc shift; + + confess "already have $call in $pkg" if $list{$call}; + + my $self = $pkg->SUPER::new($call); + $self->{dxchan} = [ ]; + $self->{version} = shift || 5000; + $self->{flags} = shift || Route::here(1); + $self->{users} = []; + $self->{nodes} = []; + + $list{$call} = $self; + + return $self; +} -sub _adduser +sub delete { my $self = shift; - return $self->_addlist('users', @_); + dbg("Deleting Route::Node $self->{call}") if isdbg('routelow'); + for ($self->unlink_all_users) { + $_->delete; + } + delete $list{$self->{call}}; } -sub _deluser +sub get { - my $self = shift; - return $self->_dellist('users', @_); + my $call = shift; + $call = shift if ref $call; + my $ref = $list{uc $call}; + dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr'); + return $ref; +} + +sub get_all +{ + return values %list; } sub DESTROY @@ -219,7 +276,8 @@ sub DESTROY my $pkg = ref $self; my $call = $self->{call} || "Unknown"; - dbg('route', "destroying $pkg with $call"); + dbg("destroying $pkg with $call") if isdbg('routelow'); + $self->unlink_all_users if @{$self->{users}}; } # @@ -229,19 +287,16 @@ sub DESTROY sub AUTOLOAD { no strict; - - my $self = shift; - $name = $AUTOLOAD; + my $name = $AUTOLOAD; return if $name =~ /::DESTROY$/; - $name =~ s/.*:://o; + $name =~ s/^.*:://o; confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name}; # this clever line of code creates a subroutine which takes over from autoload # from OO Perl - Conway -# print "AUTOLOAD: $AUTOLOAD\n"; -# *{$AUTOLOAD} = sub {my $self = shift; @_ ? $self->{$name} = shift : $self->{$name}} ; - @_ ? $self->{$name} = shift : $self->{$name} ; + *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}}; + goto &$AUTOLOAD; } 1;