2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
17 use vars qw(%list %valid @ISA $max $filterdef);
21 parent => '0,Parent Calls,parray',
22 nodes => '0,Nodes,parray',
23 users => '0,Users,parray',
24 usercount => '0,User Count',
25 version => '0,Version',
26 handle_xml => '0,Using XML,yesno',
27 lastmsg => '0,Last Route Msg,atime',
28 lastid => '0,Last Route MsgID',
31 $filterdef = $Route::filterdef;
37 my $n = scalar (keys %list);
38 $max = $n if $n > $max;
49 # this routine handles the possible adding of an entry in the routing
50 # table. It will only add an entry if it is new. It may have all sorts of
51 # other side effects which may include fixing up other links.
53 # It will return a node object if (and only if) it is a completely new
54 # object with that callsign. The upper layers are expected to do something
57 # called as $parent->add(call, dxchan, version, flags)
64 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
65 my $self = get($call);
67 $self->_addparent($parent);
68 $parent->_addnode($self);
71 $self = $parent->new($call, @_);
72 $parent->_addnode($self);
77 # this routine is the opposite of 'add' above.
79 # It will return an object if (and only if) this 'del' will remove
80 # this object completely
88 # delete parent from this call's parent list
89 $pref->_delnode($self);
90 $self->_delparent($pref);
92 my $ncall = $self->{call};
94 # is this the last connection, I have no parents anymore?
95 unless (@{$self->{parent}}) {
96 foreach my $rcall (@{$self->{nodes}}) {
97 next if grep $rcall eq $_, @_;
98 my $r = Route::Node::get($rcall);
99 push @nodes, $r->del($self, $ncall, @_) if $r;
102 delete $list{$self->{call}};
108 # this deletes this node completely by grabbing the parents
109 # and deleting me from them
116 foreach my $call (@{$self->{parent}}) {
117 my $parent = Route::Node::get($call);
118 push @out, $parent->del($self) if $parent;
127 foreach my $rcall (@{$parent->{nodes}}) {
129 push @out, $r->del($parent, $parent->{call}, @_) if $r;
137 for (@{$self->{users}}) {
138 my $ref = Route::User::get($_);
139 $ref->del($self) if $ref;
144 # add a user to this node
150 confess "Trying to add NULL User call to routing tables" unless $ucall;
152 my $uref = Route::User::get($ucall);
155 @out = $uref->addparent($self);
157 $uref = Route::User->new($ucall, $self->{call}, @_);
160 $self->_adduser($uref);
161 $self->{usercount} = scalar @{$self->{users}};
166 # delete a user from this node
174 @out = $self->_deluser($ref);
177 confess "tried to delete non-existant $ref->{call} from $self->{call}";
179 $self->{usercount} = scalar @{$self->{users}};
186 if (@_ && @{$self->{users}} == 0) {
187 $self->{usercount} = shift;
189 return $self->{usercount};
195 return @{$self->{users}};
201 return @{$self->{nodes}};
207 return @{$self->{parent}};
214 foreach my $call (@{$self->{nodes}}) {
215 next if grep $call eq $_, @_;
218 push @out, $r->rnodes($call, @_) if $r;
229 confess "already have $call in $pkg" if $list{$call};
231 my $self = $pkg->SUPER::new($call);
232 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
233 $self->{version} = shift;
234 $self->{flags} = shift;
238 $list{$call} = $self;
246 $call = shift if ref $call;
247 my $ref = $list{uc $call};
248 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
260 return $self->_addlist('parent', @_);
266 return $self->_dellist('parent', @_);
273 return $self->_addlist('nodes', @_);
279 return $self->_dellist('nodes', @_);
286 return $self->_addlist('users', @_);
292 return $self->_dellist('users', @_);
299 my $call = $self->{call} || "Unknown";
301 dbg("destroying $pkg with $call") if isdbg('routelow');
305 # generic AUTOLOAD for accessors
311 my $name = $AUTOLOAD;
312 return if $name =~ /::DESTROY$/;
315 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
317 # this clever line of code creates a subroutine which takes over from autoload
318 # from OO Perl - Conway
319 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};