2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
16 use Time::HiRes qw(gettimeofday);
20 use vars qw(%list %valid @ISA $max $filterdef $obscount);
24 nodes => '0,Nodes,parray',
25 users => '0,Users,parray',
26 usercount => '0,User Count',
27 version => '0,Version',
29 handle_xml => '0,Using XML,yesno',
30 lastmsg => '0,Last Route Msg,atime',
31 lastid => '0,Last Route MsgID',
32 do_pc9x => '0,Uses pc9x,yesno',
33 via_pc92 => '0,In via pc92?,yesno',
34 obscount => '0,Obscount',
35 last_PC92C => '9,Last PC92C',
36 PC92C_dxchan => '9,PC92C hops,phash',
39 $filterdef = $Route::filterdef;
43 our $cachefn = localdata('route_node_cache');
47 my $n = scalar (keys %list);
48 $max = $n if $n > $max;
59 # this routine handles the possible adding of an entry in the routing
60 # table. It will only add an entry if it is new. It may have all sorts of
61 # other side effects which may include fixing up other links.
63 # It will return a node object if (and only if) it is a completely new
64 # object with that callsign. The upper layers are expected to do something
67 # called as $parent->add(call, dxchan, version, flags)
74 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
75 my $self = get($call);
77 $self->_addparent($parent);
78 $parent->_addnode($self);
81 $self = $parent->new($call, @_);
82 $parent->_addnode($self);
83 dbg("CLUSTER: node $call added") if isdbg('cluster');
88 # this routine is the opposite of 'add' above.
90 # It will return an object if (and only if) this 'del' will remove
91 # this object completely
99 # delete parent from this call's parent list
100 $pref->_delnode($self);
101 $self->_delparent($pref);
103 my $ncall = $self->{call};
105 # is this the last connection, I have no parents anymore?
106 unless (@{$self->{parent}}) {
107 foreach my $rcall (@{$self->{nodes}}) {
108 next if grep $rcall eq $_, @_;
109 my $r = Route::Node::get($rcall);
110 push @nodes, $r->del($self, $ncall, @_) if $r;
113 delete $list{$ncall};
115 dbg("CLUSTER: node $ncall deleted") if isdbg('cluster');
120 # this deletes this node completely by grabbing the parents
121 # and deleting me from them, then deleting me from all the
127 my $ncall = $self->{call};
129 # get rid of users and parents
131 if (@{$self->{parent}}) {
132 foreach my $call (@{$self->{parent}}) {
133 my $parent = Route::Node::get($call);
134 push @out, $parent->del($self) if $parent;
137 # get rid of my nodes
138 push @out, $self->del_nodes;
139 # this only happens if we a orphan with no parents
142 delete $list{$ncall};
151 foreach my $rcall (@{$parent->{nodes}}) {
153 push @out, $r->del($parent, $parent->{call}, @_) if $r;
161 for (@{$self->{users}}) {
162 my $ref = Route::User::get($_);
163 $ref->del($self) if $ref;
168 # add a user to this node
176 confess "Trying to add NULL User call to routing tables" unless $ucall;
178 my $uref = Route::User::get($ucall);
181 @out = $uref->addparent($self);
183 $uref = Route::User->new($ucall, $self->{call}, $here, $ip);
186 $self->_adduser($uref);
187 $self->{usercount} = scalar @{$self->{users}};
192 # delete a user from this node
200 @out = $self->_deluser($ref);
203 confess "tried to delete non-existant $ref->{call} from $self->{call}";
205 $self->{usercount} = scalar @{$self->{users}};
209 # is a user on this node
214 return scalar grep {$_ eq $call} @{$self->{users}};
220 if (@_ && @{$self->{users}} == 0) {
221 $self->{usercount} = shift;
223 return $self->{usercount};
229 return @{$self->{users}};
235 return @{$self->{nodes}};
242 foreach my $call (@{$self->{nodes}}) {
243 next if grep $call eq $_, @_;
246 push @out, $r->rnodes($call, @_) if $r;
251 # this takes in a list of node and user calls (not references) from
252 # a config type update for a node and returns
253 # the differences as lists of things that have gone away
254 # and things that have been added.
255 sub calc_config_changes
258 my %nodes = map {$_ => 1} @{$self->{nodes}};
259 my %users = map {$_ => 1} @{$self->{users}};
262 if (isdbg('route')) {
263 dbg("ROUTE: start calc_config_changes");
264 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
265 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
266 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
267 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
269 my (@dnodes, @dusers, @nnodes, @nusers);
270 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
271 push @dnodes, keys %nodes;
272 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
273 push @dusers, keys %users;
274 if (isdbg('route')) {
275 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
276 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
277 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort @nnodes));
278 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
279 dbg("ROUTE: end calc_config_changes");
281 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
290 confess "already have $call in $pkg" if $list{$call};
292 my $self = $pkg->SUPER::new($call);
293 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
294 $self->{version} = shift || 5401;
295 $self->{flags} = shift || Route::here(1);
298 $self->{PC92C_dxchan} = {};
300 $self->{ip} = $ip if defined $ip;
301 $self->reset_obs; # by definition
303 $list{$call} = $self;
311 $call = shift if ref $call;
312 my $ref = $list{uc $call};
313 dbg("ROUTE: Failed to get Node $call" ) if !$ref && isdbg('routerr');
325 return $self->_addlist('parent', @_);
331 return $self->_dellist('parent', @_);
338 return $self->_addlist('nodes', @_);
344 return $self->_dellist('nodes', @_);
351 return $self->_addlist('users', @_);
357 return $self->_dellist('users', @_);
364 return $self->{obscount};
370 $self->{obscount} = $obscount;
377 my $lastid = $parent->{lastid};
379 return ($t < $lastid) ? $t+86400-$lastid : $t - $lastid;
390 if ($call && $hops) {
392 $parent->{PC92C_dxchan}->{$call} = $hops;
395 return (%{$parent->{PC92C_dxchan}});
398 sub TO_JSON { return { %{ shift() } }; }
402 my $json = DXJSON->new;
403 $json->canonical(isdbg('routecache'));
405 my $ta = [ gettimeofday ];
408 while (my ($k, $v) = each %list) {
409 push @s, "$k:" . $json->encode($v) . "\n";
413 my $fh = IO::File->new(">$cachefn") or confess("writing $cachefn $!");
414 if (isdbg("routecache")) {
422 dbg("Route::Node:Write_cache error '$@'");
425 $json->indent(0)->canonical(0);
426 my $diff = _diffms($ta);
427 dbg("Route::Node:WRITE_CACHE time to write: $diff mS");
435 my $call = $self->{call} || "Unknown";
437 dbg("ROUTE: destroying $pkg with $call") if isdbg('routelow');
441 # generic AUTOLOAD for accessors
447 my $name = $AUTOLOAD;
448 return if $name =~ /::DESTROY$/;
451 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
453 # this clever line of code creates a subroutine which takes over from autoload
454 # from OO Perl - Conway
455 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};