2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
17 use vars qw($VERSION $BRANCH);
19 main::mkver($VERSION = q$Revision$);
21 use vars qw(%list %valid @ISA $max $filterdef);
25 parent => '0,Parent Calls,parray',
26 nodes => '0,Nodes,parray',
27 users => '0,Users,parray',
28 usercount => '0,User Count',
29 version => '0,Version',
30 np => '0,Using New Prot,yesno',
31 lid => '0,Last Msgid',
34 $filterdef = $Route::filterdef;
40 my $n = scalar (keys %list);
41 $max = $n if $n > $max;
52 # this routine handles the possible adding of an entry in the routing
53 # table. It will only add an entry if it is new. It may have all sorts of
54 # other side effects which may include fixing up other links.
56 # It will return a node object if (and only if) it is a completely new
57 # object with that callsign. The upper layers are expected to do something
60 # called as $parent->add(call, dxchan, version, flags)
67 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
68 my $self = get($call);
70 $self->_addparent($parent);
71 $parent->_addnode($self);
74 $self = $parent->new($call, @_);
75 $parent->_addnode($self);
80 # this routine is the opposite of 'add' above.
82 # It will return an object if (and only if) this 'del' will remove
83 # this object completely
91 # delete parent from this call's parent list
92 $pref->_delnode($self);
93 $self->_delparent($pref);
95 my $ncall = $self->{call};
97 # is this the last connection, I have no parents anymore?
98 unless (@{$self->{parent}}) {
99 foreach my $rcall (@{$self->{nodes}}) {
100 next if grep $rcall eq $_, @_;
101 my $r = Route::Node::get($rcall);
102 push @nodes, $r->del($self, $ncall, @_) if $r;
105 delete $list{$self->{call}};
115 foreach my $rcall (@{$parent->{nodes}}) {
117 push @out, $r->del($parent, $parent->{call}, @_) if $r;
125 for (@{$self->{users}}) {
126 my $ref = Route::User::get($_);
127 $ref->del($self) if $ref;
132 # add a user to this node
138 confess "Trying to add NULL User call to routing tables" unless $ucall;
140 my $uref = Route::User::get($ucall);
143 @out = $uref->addparent($self);
145 $uref = Route::User->new($ucall, $self->{call}, @_);
148 $self->_adduser($uref);
149 $self->{usercount} = scalar @{$self->{users}};
154 # delete a user from this node
162 @out = $self->_deluser($ref);
165 confess "tried to delete non-existant $ref->{call} from $self->{call}";
167 $self->{usercount} = scalar @{$self->{users}};
174 if (@_ && @{$self->{users}} == 0) {
175 $self->{usercount} = shift;
177 return $self->{usercount};
183 return @{$self->{users}};
189 return @{$self->{nodes}};
195 return @{$self->{parent}};
202 foreach my $call (@{$self->{nodes}}) {
203 next if grep $call eq $_, @_;
206 push @out, $r->rnodes($call, @_) if $r;
217 confess "already have $call in $pkg" if $list{$call};
219 my $self = $pkg->SUPER::new($call);
220 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
221 $self->{version} = shift;
222 $self->{flags} = shift;
227 $list{$call} = $self;
235 $call = shift if ref $call;
236 my $ref = $list{uc $call};
237 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
251 return 0 if $id == $self->{lid};
252 if ($id > $self->{lid}) {
255 } elsif ($self->{lid} - $id > 500) {
265 return $self->_addlist('parent', @_);
271 return $self->_dellist('parent', @_);
278 return $self->_addlist('nodes', @_);
284 return $self->_dellist('nodes', @_);
291 return $self->_addlist('users', @_);
297 return $self->_dellist('users', @_);
304 my $call = $self->{call} || "Unknown";
306 dbg("destroying $pkg with $call") if isdbg('routelow');
310 # generic AUTOLOAD for accessors
316 my $name = $AUTOLOAD;
317 return if $name =~ /::DESTROY$/;
320 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
322 # this clever line of code creates a subroutine which takes over from autoload
323 # from OO Perl - Conway
324 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};