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 K => '9,Seen on PC92K,yesno',
25 PC92C_dxchan => '9,PC92C hops,phash',
27 do_pc9x => '0,Uses pc9x,yesno',
28 handle_xml => '0,Using XML,yesno',
29 last_PC92C => '9,Last PC92C',
30 lastid => '0,Last Route MsgID',
31 lastmsg => '0,Last Route Msg,atime',
32 nodes => '0,Nodes,parray',
33 obscount => '0,Obscount',
34 usercount => '0,User Count',
35 users => '0,Users,parray',
36 version => '0,Version',
37 via_pc92 => '0,In via pc92?,yesno',
40 $filterdef = $Route::filterdef;
44 our $cachefn = localdata('route_node_cache');
48 my $n = scalar (keys %list);
49 $max = $n if $n > $max;
60 # this routine handles the possible adding of an entry in the routing
61 # table. It will only add an entry if it is new. It may have all sorts of
62 # other side effects which may include fixing up other links.
64 # It will return a node object if (and only if) it is a completely new
65 # object with that callsign. The upper layers are expected to do something
68 # called as $parent->add(call, dxchan, version, flags)
75 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
76 my $self = get($call);
78 $self->_addparent($parent);
79 $parent->_addnode($self);
82 $self = $parent->new($call, @_);
83 $parent->_addnode($self);
84 dbg("CLUSTER: node $call added") if isdbg('cluster');
89 # this routine is the opposite of 'add' above.
91 # It will return an object if (and only if) this 'del' will remove
92 # this object completely
100 # delete parent from this call's parent list
101 $pref->_delnode($self);
102 $self->_delparent($pref);
104 my $ncall = $self->{call};
106 # is this the last connection, I have no parents anymore?
107 unless (@{$self->{parent}}) {
108 foreach my $rcall (@{$self->{nodes}}) {
109 next if grep $rcall eq $_, @_;
110 my $r = Route::Node::get($rcall);
111 push @nodes, $r->del($self, $ncall, @_) if $r;
114 delete $list{$ncall};
116 dbg("CLUSTER: node $ncall deleted") if isdbg('cluster');
121 # this deletes this node completely by grabbing the parents
122 # and deleting me from them, then deleting me from all the
128 my $ncall = $self->{call};
130 # get rid of users and parents
132 if (@{$self->{parent}}) {
133 foreach my $call (@{$self->{parent}}) {
134 my $parent = Route::Node::get($call);
135 push @out, $parent->del($self) if $parent;
138 # get rid of my nodes
139 push @out, $self->del_nodes;
140 # this only happens if we a orphan with no parents
143 delete $list{$ncall};
152 foreach my $rcall (@{$parent->{nodes}}) {
154 push @out, $r->del($parent, $parent->{call}, @_) if $r;
162 for (@{$self->{users}}) {
163 my $ref = Route::User::get($_);
164 $ref->del($self) if $ref;
169 # add a user to this node
177 confess "Trying to add NULL User call to routing tables" unless $ucall;
179 my $uref = Route::User::get($ucall);
182 @out = $uref->addparent($self);
184 $uref = Route::User->new($ucall, $self->{call}, $here, $ip);
187 $self->_adduser($uref);
188 $self->{usercount} = scalar @{$self->{users}};
193 # delete a user from this node
201 @out = $self->_deluser($ref);
204 confess "tried to delete non-existant $ref->{call} from $self->{call}";
206 $self->{usercount} = scalar @{$self->{users}};
210 # is a user on this node
215 return scalar grep {$_ eq $call} @{$self->{users}};
221 if (@_ && @{$self->{users}} == 0) {
222 $self->{usercount} = shift;
224 return $self->{usercount};
230 return @{$self->{users}};
236 return @{$self->{nodes}};
243 foreach my $call (@{$self->{nodes}}) {
244 next if grep $call eq $_, @_;
247 push @out, $r->rnodes($call, @_) if $r;
252 # this takes in a list of node and user calls (not references) from
253 # a config type update for a node and returns
254 # the differences as lists of things that have gone away
255 # and things that have been added.
256 sub calc_config_changes
259 my %nodes = map {$_ => 1} @{$self->{nodes}};
260 my %users = map {$_ => 1} @{$self->{users}};
263 if (isdbg('route')) {
264 dbg("ROUTE: start calc_config_changes");
265 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
266 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
267 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
268 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
270 my (@dnodes, @dusers, @nnodes, @nusers);
271 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
272 push @dnodes, keys %nodes;
273 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
274 push @dusers, keys %users;
275 if (isdbg('route')) {
276 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
277 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
278 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort @nnodes));
279 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
280 dbg("ROUTE: end calc_config_changes");
282 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
291 confess "already have $call in $pkg" if $list{$call};
293 my $self = $pkg->SUPER::new($call);
294 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
295 $self->{version} = shift || 5401;
296 $self->{flags} = shift || Route::here(1);
299 $self->{PC92C_dxchan} = {};
301 $self->{ip} = $ip if defined $ip;
302 $self->reset_obs; # by definition
304 $list{$call} = $self;
312 $call = shift if ref $call;
313 my $ref = $list{uc $call};
314 dbg("ROUTE: Failed to get Node $call" ) if !$ref && isdbg('routerr');
326 return $self->_addlist('parent', @_);
332 return $self->_dellist('parent', @_);
339 return $self->_addlist('nodes', @_);
345 return $self->_dellist('nodes', @_);
352 return $self->_addlist('users', @_);
358 return $self->_dellist('users', @_);
365 return $self->{obscount};
371 $self->{obscount} = $obscount;
378 my $lastid = $parent->{lastid};
380 return ($t < $lastid) ? $t+86400-$lastid : $t - $lastid;
391 if ($call && $hops) {
393 $parent->{PC92C_dxchan}->{$call} = $hops;
396 return (%{$parent->{PC92C_dxchan}});
399 sub TO_JSON { return { %{ shift() } }; }
403 my $json = DXJSON->new;
404 $json->canonical(isdbg('routecache')||0);
406 my $ta = [ gettimeofday ];
409 while (my ($k, $v) = each %list) {
410 push @s, "$k:" . $json->encode($v) . "\n";
414 my $fh = IO::File->new(">$cachefn") or dbg("Route::Node: Error writing $cachefn $!"), return;
415 print $fh "$_" for (sort @s);
418 dbg("Route::Node::write_cache error '$@'");
421 $json->indent(0)->canonical(0);
422 my $diff = _diffms($ta);
423 dbg("Route::Node::write_cache time to write: $diff mS");
428 my $json = DXJSON->new;
429 $json->canonical(isdbg('routecache'));
431 my $ta = [ gettimeofday ];
434 my $fh = IO::File->new("$cachefn") or dbg("Route::Node ERROR reading $cachefn $!"), return;
435 while (my $l = <$fh>) {
437 my ($k, $v) = split /:/, $l, 2;
438 $list{$k} = bless $json->decode($v) or carp("Route::Node json error $! decoding '$v'"), next;
443 my $diff = _diffms($ta);
444 dbg("Route::Node::read_cache time to read $count records from $cachefn : $diff mS");
451 my $call = $self->{call} || "Unknown";
453 dbg("ROUTE: destroying $pkg with $call") if isdbg('routelow');
457 # generic AUTOLOAD for accessors
463 my $name = $AUTOLOAD;
464 return if $name =~ /::DESTROY$/;
467 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
469 # this clever line of code creates a subroutine which takes over from autoload
470 # from OO Perl - Conway
471 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};