2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
18 use vars qw(%list %valid @ISA $max $filterdef $obscount);
22 parent => '0,Parent Calls,parray',
23 nodes => '0,Nodes,parray',
24 users => '0,Users,parray',
25 usercount => '0,User Count',
26 version => '0,Version',
27 handle_xml => '0,Using XML,yesno',
28 lastmsg => '0,Last Route Msg,atime',
29 lastid => '0,Last Route MsgID',
30 do_pc9x => '0,Uses pc9x,yesno',
31 via_pc92 => '0,Came in via pc92,yesno',
32 obscount => '0,Obscount',
35 $filterdef = $Route::filterdef;
42 my $n = scalar (keys %list);
43 $max = $n if $n > $max;
54 # this routine handles the possible adding of an entry in the routing
55 # table. It will only add an entry if it is new. It may have all sorts of
56 # other side effects which may include fixing up other links.
58 # It will return a node object if (and only if) it is a completely new
59 # object with that callsign. The upper layers are expected to do something
62 # called as $parent->add(call, dxchan, version, flags)
69 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
70 my $self = get($call);
72 $self->_addparent($parent);
73 $parent->_addnode($self);
76 $self = $parent->new($call, @_);
77 $parent->_addnode($self);
82 # this routine is the opposite of 'add' above.
84 # It will return an object if (and only if) this 'del' will remove
85 # this object completely
93 # delete parent from this call's parent list
94 $pref->_delnode($self);
95 $self->_delparent($pref);
97 my $ncall = $self->{call};
99 # is this the last connection, I have no parents anymore?
100 unless (@{$self->{parent}}) {
101 foreach my $rcall (@{$self->{nodes}}) {
102 next if grep $rcall eq $_, @_;
103 my $r = Route::Node::get($rcall);
104 push @nodes, $r->del($self, $ncall, @_) if $r;
107 delete $list{$self->{call}};
113 # this deletes this node completely by grabbing the parents
114 # and deleting me from them
121 foreach my $call (@{$self->{parent}}) {
122 my $parent = Route::Node::get($call);
123 push @out, $parent->del($self) if $parent;
132 foreach my $rcall (@{$parent->{nodes}}) {
134 push @out, $r->del($parent, $parent->{call}, @_) if $r;
142 for (@{$self->{users}}) {
143 my $ref = Route::User::get($_);
144 $ref->del($self) if $ref;
149 # add a user to this node
155 confess "Trying to add NULL User call to routing tables" unless $ucall;
157 my $uref = Route::User::get($ucall);
160 @out = $uref->addparent($self);
162 $uref = Route::User->new($ucall, $self->{call}, @_);
165 $self->_adduser($uref);
166 $self->{usercount} = scalar @{$self->{users}};
171 # delete a user from this node
179 @out = $self->_deluser($ref);
182 confess "tried to delete non-existant $ref->{call} from $self->{call}";
184 $self->{usercount} = scalar @{$self->{users}};
191 if (@_ && @{$self->{users}} == 0) {
192 $self->{usercount} = shift;
194 return $self->{usercount};
200 return @{$self->{users}};
206 return @{$self->{nodes}};
212 return @{$self->{parent}};
219 foreach my $call (@{$self->{nodes}}) {
220 next if grep $call eq $_, @_;
223 push @out, $r->rnodes($call, @_) if $r;
228 # this takes in a list of node and user calls (not references) from
229 # a config type update for a node and returns
230 # the differences as lists of things that have gone away
231 # and things that have been added.
232 sub calc_config_changes
235 my %nodes = map {$_ => 1} @{$self->{nodes}};
236 my %users = map {$_ => 1} @{$self->{users}};
239 if (isdbg('route')) {
240 dbg("ROUTE: start calc_config_changes");
241 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
242 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
243 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
244 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
246 my (@dnodes, @dusers, @nnodes, @nusers);
247 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
248 push @dnodes, keys %nodes;
249 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
250 push @dusers, keys %users;
251 if (isdbg('route')) {
252 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
253 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
254 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort @nnodes));
255 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
256 dbg("ROUTE: end calc_config_changes");
258 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
266 confess "already have $call in $pkg" if $list{$call};
268 my $self = $pkg->SUPER::new($call);
269 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
270 $self->{version} = shift || 5401;
271 $self->{flags} = shift || Route::here(1);
274 $self->{lastid} = {};
275 $self->reset_obs; # by definition
277 $list{$call} = $self;
285 $call = shift if ref $call;
286 my $ref = $list{uc $call};
287 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
299 return $self->_addlist('parent', @_);
305 return $self->_dellist('parent', @_);
312 return $self->_addlist('nodes', @_);
318 return $self->_dellist('nodes', @_);
325 return $self->_addlist('users', @_);
331 return $self->_dellist('users', @_);
338 return $self->{obscount};
344 $self->{obscount} = $obscount;
351 my $call = $self->{call} || "Unknown";
353 dbg("destroying $pkg with $call") if isdbg('routelow');
357 # generic AUTOLOAD for accessors
363 my $name = $AUTOLOAD;
364 return if $name =~ /::DESTROY$/;
367 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
369 # this clever line of code creates a subroutine which takes over from autoload
370 # from OO Perl - Conway
371 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};