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}};
206 # is a user on this node
211 return scalar grep {$_ eq $call} @{$self->{users}};
217 if (@_ && @{$self->{users}} == 0) {
218 $self->{usercount} = shift;
220 return $self->{usercount};
226 return @{$self->{users}};
232 return @{$self->{nodes}};
239 foreach my $call (@{$self->{nodes}}) {
240 next if grep $call eq $_, @_;
243 push @out, $r->rnodes($call, @_) if $r;
248 # this takes in a list of node and user calls (not references) from
249 # a config type update for a node and returns
250 # the differences as lists of things that have gone away
251 # and things that have been added.
252 sub calc_config_changes
255 my %nodes = map {$_ => 1} @{$self->{nodes}};
256 my %users = map {$_ => 1} @{$self->{users}};
259 if (isdbg('route')) {
260 dbg("ROUTE: start calc_config_changes");
261 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
262 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
263 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
264 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
266 my (@dnodes, @dusers, @nnodes, @nusers);
267 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
268 push @dnodes, keys %nodes;
269 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
270 push @dusers, keys %users;
271 if (isdbg('route')) {
272 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
273 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
274 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort @nnodes));
275 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
276 dbg("ROUTE: end calc_config_changes");
278 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
287 confess "already have $call in $pkg" if $list{$call};
289 my $self = $pkg->SUPER::new($call);
290 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
291 $self->{version} = shift || 5401;
292 $self->{flags} = shift || Route::here(1);
295 $self->{PC92C_dxchan} = {};
297 $self->{ip} = $ip if defined $ip;
298 $self->reset_obs; # by definition
300 $list{$call} = $self;
308 $call = shift if ref $call;
309 my $ref = $list{uc $call};
310 dbg("ROUTE: Failed to get Node $call" ) if !$ref && isdbg('routerr');
322 return $self->_addlist('parent', @_);
328 return $self->_dellist('parent', @_);
335 return $self->_addlist('nodes', @_);
341 return $self->_dellist('nodes', @_);
348 return $self->_addlist('users', @_);
354 return $self->_dellist('users', @_);
361 return $self->{obscount};
367 $self->{obscount} = $obscount;
374 my $lastid = $parent->{lastid};
376 return ($t < $lastid) ? $t+86400-$lastid : $t - $lastid;
387 if ($call && $hops) {
389 $parent->{PC92C_dxchan}->{$call} = $hops;
392 return (%{$parent->{PC92C_dxchan}});
399 my $call = $self->{call} || "Unknown";
401 dbg("ROUTE: destroying $pkg with $call") if isdbg('routelow');
405 # generic AUTOLOAD for accessors
411 my $name = $AUTOLOAD;
412 return if $name =~ /::DESTROY$/;
415 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
417 # this clever line of code creates a subroutine which takes over from autoload
418 # from OO Perl - Conway
419 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};