2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
18 use vars qw(%list %valid @ISA $max $filterdef $obscount);
22 nodes => '0,Nodes,parray',
23 users => '0,Users,parray',
24 usercount => '0,User Count',
25 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,In via pc92?,yesno',
32 obscount => '0,Obscount',
33 last_PC92C => '9,Last PC92C',
34 PC92C_dxchan => '9,PC92C hops,phash',
37 $filterdef = $Route::filterdef;
44 my $n = scalar (keys %list);
45 $max = $n if $n > $max;
56 # this routine handles the possible adding of an entry in the routing
57 # table. It will only add an entry if it is new. It may have all sorts of
58 # other side effects which may include fixing up other links.
60 # It will return a node object if (and only if) it is a completely new
61 # object with that callsign. The upper layers are expected to do something
64 # called as $parent->add(call, dxchan, version, flags)
71 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
72 my $self = get($call);
74 $self->_addparent($parent);
75 $parent->_addnode($self);
78 $self = $parent->new($call, @_);
79 $parent->_addnode($self);
80 dbg("CLUSTER: node $call added") if isdbg('cluster');
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{$ncall};
112 dbg("CLUSTER: node $ncall deleted") if isdbg('cluster');
117 # this deletes this node completely by grabbing the parents
118 # and deleting me from them, then deleting me from all the
124 my $ncall = $self->{call};
126 # get rid of users and parents
128 if (@{$self->{parent}}) {
129 foreach my $call (@{$self->{parent}}) {
130 my $parent = Route::Node::get($call);
131 push @out, $parent->del($self) if $parent;
134 # get rid of my nodes
135 push @out, $self->del_nodes;
136 # this only happens if we a orphan with no parents
139 delete $list{$ncall};
148 foreach my $rcall (@{$parent->{nodes}}) {
150 push @out, $r->del($parent, $parent->{call}, @_) if $r;
158 for (@{$self->{users}}) {
159 my $ref = Route::User::get($_);
160 $ref->del($self) if $ref;
165 # add a user to this node
173 confess "Trying to add NULL User call to routing tables" unless $ucall;
175 my $uref = Route::User::get($ucall);
178 @out = $uref->addparent($self);
180 $uref = Route::User->new($ucall, $self->{call}, $here, $ip);
183 $self->_adduser($uref);
184 $self->{usercount} = scalar @{$self->{users}};
189 # delete a user from this node
197 @out = $self->_deluser($ref);
200 confess "tried to delete non-existant $ref->{call} from $self->{call}";
202 $self->{usercount} = scalar @{$self->{users}};
209 if (@_ && @{$self->{users}} == 0) {
210 $self->{usercount} = shift;
212 return $self->{usercount};
218 return @{$self->{users}};
224 return @{$self->{nodes}};
231 foreach my $call (@{$self->{nodes}}) {
232 next if grep $call eq $_, @_;
235 push @out, $r->rnodes($call, @_) if $r;
240 # this takes in a list of node and user calls (not references) from
241 # a config type update for a node and returns
242 # the differences as lists of things that have gone away
243 # and things that have been added.
244 sub calc_config_changes
247 my %nodes = map {$_ => 1} @{$self->{nodes}};
248 my %users = map {$_ => 1} @{$self->{users}};
251 if (isdbg('route')) {
252 dbg("ROUTE: start calc_config_changes");
253 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
254 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
255 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
256 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
258 my (@dnodes, @dusers, @nnodes, @nusers);
259 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
260 push @dnodes, keys %nodes;
261 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
262 push @dusers, keys %users;
263 if (isdbg('route')) {
264 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
265 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
266 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort @nnodes));
267 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
268 dbg("ROUTE: end calc_config_changes");
270 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
278 confess "already have $call in $pkg" if $list{$call};
280 my $self = $pkg->SUPER::new($call);
281 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
282 $self->{version} = shift || 5401;
283 $self->{flags} = shift || Route::here(1);
286 $self->{PC92C_dxchan} = {};
288 $self->{ip} = $ip if defined $ip;
289 $self->reset_obs; # by definition
291 $list{$call} = $self;
299 $call = shift if ref $call;
300 my $ref = $list{uc $call};
301 dbg("ROUTE: Failed to get Node $call" ) if !$ref && isdbg('routerr');
313 return $self->_addlist('parent', @_);
319 return $self->_dellist('parent', @_);
326 return $self->_addlist('nodes', @_);
332 return $self->_dellist('nodes', @_);
339 return $self->_addlist('users', @_);
345 return $self->_dellist('users', @_);
352 return $self->{obscount};
358 $self->{obscount} = $obscount;
365 my $lastid = $parent->{lastid};
367 return ($t < $lastid) ? $t+86400-$lastid : $t - $lastid;
378 if ($call && $hops) {
380 $parent->{PC92C_dxchan}->{$call} = $hops;
383 return (%{$parent->{PC92C_dxchan}});
390 my $call = $self->{call} || "Unknown";
392 dbg("ROUTE: destroying $pkg with $call") if isdbg('routelow');
396 # generic AUTOLOAD for accessors
402 my $name = $AUTOLOAD;
403 return if $name =~ /::DESTROY$/;
406 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
408 # this clever line of code creates a subroutine which takes over from autoload
409 # from OO Perl - Conway
410 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};