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',
28 handle_xml => '0,Using XML,yesno',
29 lastmsg => '0,Last Route Msg,atime',
30 lastid => '0,Last Route MsgID',
31 do_pc9x => '0,Uses pc9x,yesno',
32 via_pc92 => '0,Came in via pc92,yesno',
33 obscount => '0,Obscount',
34 last_PC92C => '9,Last PC92C',
35 PC92C_dxchan => '9,Channel of PC92C',
38 $filterdef = $Route::filterdef;
45 my $n = scalar (keys %list);
46 $max = $n if $n > $max;
57 # this routine handles the possible adding of an entry in the routing
58 # table. It will only add an entry if it is new. It may have all sorts of
59 # other side effects which may include fixing up other links.
61 # It will return a node object if (and only if) it is a completely new
62 # object with that callsign. The upper layers are expected to do something
65 # called as $parent->add(call, dxchan, version, flags)
72 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
73 my $self = get($call);
75 $self->_addparent($parent);
76 $parent->_addnode($self);
79 $self = $parent->new($call, @_);
80 $parent->_addnode($self);
85 # this routine is the opposite of 'add' above.
87 # It will return an object if (and only if) this 'del' will remove
88 # this object completely
96 # delete parent from this call's parent list
97 $pref->_delnode($self);
98 $self->_delparent($pref);
100 my $ncall = $self->{call};
102 # is this the last connection, I have no parents anymore?
103 unless (@{$self->{parent}}) {
104 foreach my $rcall (@{$self->{nodes}}) {
105 next if grep $rcall eq $_, @_;
106 my $r = Route::Node::get($rcall);
107 push @nodes, $r->del($self, $ncall, @_) if $r;
110 delete $list{$self->{call}};
116 # this deletes this node completely by grabbing the parents
117 # and deleting me from them
124 foreach my $call (@{$self->{parent}}) {
125 my $parent = Route::Node::get($call);
126 push @out, $parent->del($self) if $parent;
135 foreach my $rcall (@{$parent->{nodes}}) {
137 push @out, $r->del($parent, $parent->{call}, @_) if $r;
145 for (@{$self->{users}}) {
146 my $ref = Route::User::get($_);
147 $ref->del($self) if $ref;
152 # add a user to this node
158 confess "Trying to add NULL User call to routing tables" unless $ucall;
160 my $uref = Route::User::get($ucall);
163 @out = $uref->addparent($self);
165 $uref = Route::User->new($ucall, $self->{call}, @_);
168 $self->_adduser($uref);
169 $self->{usercount} = scalar @{$self->{users}};
174 # delete a user from this node
182 @out = $self->_deluser($ref);
185 confess "tried to delete non-existant $ref->{call} from $self->{call}";
187 $self->{usercount} = scalar @{$self->{users}};
194 if (@_ && @{$self->{users}} == 0) {
195 $self->{usercount} = shift;
197 return $self->{usercount};
203 return @{$self->{users}};
209 return @{$self->{nodes}};
215 return @{$self->{parent}};
222 foreach my $call (@{$self->{nodes}}) {
223 next if grep $call eq $_, @_;
226 push @out, $r->rnodes($call, @_) if $r;
231 # this takes in a list of node and user calls (not references) from
232 # a config type update for a node and returns
233 # the differences as lists of things that have gone away
234 # and things that have been added.
235 sub calc_config_changes
238 my %nodes = map {$_ => 1} @{$self->{nodes}};
239 my %users = map {$_ => 1} @{$self->{users}};
242 if (isdbg('route')) {
243 dbg("ROUTE: start calc_config_changes");
244 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
245 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
246 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
247 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
249 my (@dnodes, @dusers, @nnodes, @nusers);
250 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
251 push @dnodes, keys %nodes;
252 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
253 push @dusers, keys %users;
254 if (isdbg('route')) {
255 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
256 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
257 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort @nnodes));
258 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
259 dbg("ROUTE: end calc_config_changes");
261 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
269 confess "already have $call in $pkg" if $list{$call};
271 my $self = $pkg->SUPER::new($call);
272 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
273 $self->{version} = shift || 5401;
274 $self->{flags} = shift || Route::here(1);
277 $self->{lastid} = {};
278 $self->reset_obs; # by definition
280 $list{$call} = $self;
288 $call = shift if ref $call;
289 my $ref = $list{uc $call};
290 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
302 return $self->_addlist('parent', @_);
308 return $self->_dellist('parent', @_);
315 return $self->_addlist('nodes', @_);
321 return $self->_dellist('nodes', @_);
328 return $self->_addlist('users', @_);
334 return $self->_dellist('users', @_);
341 return $self->{obscount};
347 $self->{obscount} = $obscount;
354 my $call = $self->{call} || "Unknown";
356 dbg("destroying $pkg with $call") if isdbg('routelow');
360 # generic AUTOLOAD for accessors
366 my $name = $AUTOLOAD;
367 return if $name =~ /::DESTROY$/;
370 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
372 # this clever line of code creates a subroutine which takes over from autoload
373 # from OO Perl - Conway
374 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};