2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
18 use vars qw(%list %valid @ISA $max $filterdef $obscount);
22 K => '9,Seen on PC92K,yesno',
23 PC92C_dxchan => '9,PC92C hops,phash',
25 do_pc9x => '0,Uses pc9x,yesno',
26 handle_xml => '0,Using XML,yesno',
27 last_PC92C => '9,Last PC92C',
28 lastid => '0,Last Route MsgID',
29 lastmsg => '0,Last Route Msg,atime',
30 nodes => '0,Nodes,parray',
31 obscount => '0,Obscount',
32 usercount => '0,User Count',
33 users => '0,Users,parray',
34 version => '0,Version',
35 via_pc92 => '0,In via pc92?,yesno',,
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);
81 dbg("CLUSTER: node $call added") if isdbg('cluster');
86 # this routine is the opposite of 'add' above.
88 # It will return an object if (and only if) this 'del' will remove
89 # this object completely
97 # delete parent from this call's parent list
98 $pref->_delnode($self);
99 $self->_delparent($pref);
101 my $ncall = $self->{call};
103 # is this the last connection, I have no parents anymore?
104 unless (@{$self->{parent}}) {
105 foreach my $rcall (@{$self->{nodes}}) {
106 next if grep $rcall eq $_, @_;
107 my $r = Route::Node::get($rcall);
108 push @nodes, $r->del($self, $ncall, @_) if $r;
111 delete $list{$ncall};
113 dbg("CLUSTER: node $ncall deleted") if isdbg('cluster');
118 # this deletes this node completely by grabbing the parents
119 # and deleting me from them, then deleting me from all the
125 my $ncall = $self->{call};
127 # get rid of users and parents
129 if (@{$self->{parent}}) {
130 foreach my $call (@{$self->{parent}}) {
131 my $parent = Route::Node::get($call);
132 push @out, $parent->del($self) if $parent;
135 # get rid of my nodes
136 push @out, $self->del_nodes;
137 # this only happens if we a orphan with no parents
140 delete $list{$ncall};
149 foreach my $rcall (@{$parent->{nodes}}) {
151 push @out, $r->del($parent, $parent->{call}, @_) if $r;
159 for (@{$self->{users}}) {
160 my $ref = Route::User::get($_);
161 $ref->del($self) if $ref;
166 # add a user to this node
174 confess "Trying to add NULL User call to routing tables" unless $ucall;
176 my $uref = Route::User::get($ucall);
179 @out = $uref->addparent($self);
181 $uref = Route::User->new($ucall, $self->{call}, $here, $ip);
184 $self->_adduser($uref);
185 $self->{usercount} = scalar @{$self->{users}};
190 # delete a user from this node
198 @out = $self->_deluser($ref);
201 confess "tried to delete non-existant $ref->{call} from $self->{call}";
203 $self->{usercount} = scalar @{$self->{users}};
207 # is a user on this node
212 return scalar grep {$_ eq $call} @{$self->{users}};
218 if (@_ && @{$self->{users}} == 0) {
219 $self->{usercount} = shift;
221 return $self->{usercount};
227 return @{$self->{users}};
233 return @{$self->{nodes}};
240 foreach my $call (@{$self->{nodes}}) {
241 next if grep $call eq $_, @_;
244 push @out, $r->rnodes($call, @_) if $r;
249 # this takes in a list of node and user calls (not references) from
250 # a config type update for a node and returns
251 # the differences as lists of things that have gone away
252 # and things that have been added.
253 sub calc_config_changes
256 my %nodes = map {$_ => 1} @{$self->{nodes}};
257 my %users = map {$_ => 1} @{$self->{users}};
260 if (isdbg('route')) {
261 dbg("ROUTE: start calc_config_changes");
262 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
263 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
264 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
265 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
267 my (@dnodes, @dusers, @nnodes, @nusers);
268 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
269 push @dnodes, keys %nodes;
270 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
271 push @dusers, keys %users;
272 if (isdbg('route')) {
273 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
274 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
275 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort @nnodes));
276 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
277 dbg("ROUTE: end calc_config_changes");
279 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
288 confess "already have $call in $pkg" if $list{$call};
290 my $self = $pkg->SUPER::new($call);
291 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
292 $self->{version} = shift || 5401;
293 $self->{flags} = shift || Route::here(1);
296 $self->{PC92C_dxchan} = {};
298 $self->{ip} = $ip if defined $ip;
299 $self->reset_obs; # by definition
301 $list{$call} = $self;
309 $call = shift if ref $call;
310 my $ref = $list{uc $call};
311 dbg("ROUTE: Failed to get Node $call" ) if !$ref && isdbg('routerr');
323 return $self->_addlist('parent', @_);
329 return $self->_dellist('parent', @_);
336 return $self->_addlist('nodes', @_);
342 return $self->_dellist('nodes', @_);
349 return $self->_addlist('users', @_);
355 return $self->_dellist('users', @_);
362 return $self->{obscount};
368 $self->{obscount} = $obscount;
375 my $lastid = $parent->{lastid};
377 return ($t < $lastid) ? $t+86400-$lastid : $t - $lastid;
388 if ($call && $hops) {
390 $parent->{PC92C_dxchan}->{$call} = $hops;
393 return (%{$parent->{PC92C_dxchan}});
400 my $call = $self->{call} || "Unknown";
402 dbg("ROUTE: destroying $pkg with $call") if isdbg('routelow');
406 # generic AUTOLOAD for accessors
412 my $name = $AUTOLOAD;
413 return if $name =~ /::DESTROY$/;
416 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
418 # this clever line of code creates a subroutine which takes over from autoload
419 # from OO Perl - Conway
420 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};