2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
18 use vars qw($VERSION $BRANCH);
19 ($VERSION, $BRANCH) = dxver( q$Revision$);
21 use vars qw(%list %valid @ISA $max $filterdef);
25 parent => '0,Parent Calls,parray',
26 nodes => '0,Nodes,parray',
27 users => '0,Users,parray',
28 usercount => '0,User Count',
29 version => '0,Version',
30 handle_xml => '0,Using XML,yesno',
31 lastmsg => '0,Last Route Msg,atime',
32 lastid => '0,Last Route MsgID',
33 do_pc92 => '0,Uses pc92,yesno',
36 $filterdef = $Route::filterdef;
42 my $n = scalar (keys %list);
43 $max = $n if $n > $max;
54 # this routine handles the possible adding of an entry in the routing
55 # table. It will only add an entry if it is new. It may have all sorts of
56 # other side effects which may include fixing up other links.
58 # It will return a node object if (and only if) it is a completely new
59 # object with that callsign. The upper layers are expected to do something
62 # called as $parent->add(call, dxchan, version, flags)
69 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
70 my $self = get($call);
72 $self->_addparent($parent);
73 $parent->_addnode($self);
76 $self = $parent->new($call, @_);
77 $parent->_addnode($self);
82 # this routine is the opposite of 'add' above.
84 # It will return an object if (and only if) this 'del' will remove
85 # this object completely
93 # delete parent from this call's parent list
94 $pref->_delnode($self);
95 $self->_delparent($pref);
97 my $ncall = $self->{call};
99 # is this the last connection, I have no parents anymore?
100 unless (@{$self->{parent}}) {
101 foreach my $rcall (@{$self->{nodes}}) {
102 next if grep $rcall eq $_, @_;
103 my $r = Route::Node::get($rcall);
104 push @nodes, $r->del($self, $ncall, @_) if $r;
107 delete $list{$self->{call}};
117 foreach my $rcall (@{$parent->{nodes}}) {
119 push @out, $r->del($parent, $parent->{call}, @_) if $r;
127 for (@{$self->{users}}) {
128 my $ref = Route::User::get($_);
129 $ref->del($self) if $ref;
134 # add a user to this node
140 confess "Trying to add NULL User call to routing tables" unless $ucall;
142 my $uref = Route::User::get($ucall);
145 @out = $uref->addparent($self);
147 $uref = Route::User->new($ucall, $self->{call}, @_);
150 $self->_adduser($uref);
151 $self->{usercount} = scalar @{$self->{users}};
156 # delete a user from this node
164 @out = $self->_deluser($ref);
167 confess "tried to delete non-existant $ref->{call} from $self->{call}";
169 $self->{usercount} = scalar @{$self->{users}};
176 if (@_ && @{$self->{users}} == 0) {
177 $self->{usercount} = shift;
179 return $self->{usercount};
185 return @{$self->{users}};
191 return @{$self->{nodes}};
197 return @{$self->{parent}};
204 foreach my $call (@{$self->{nodes}}) {
205 next if grep $call eq $_, @_;
208 push @out, $r->rnodes($call, @_) if $r;
213 # this takes in a list of node and user calls (not references) from
214 # a config type update for a node and returns
215 # the differences as lists of things that have gone away
216 # and things that have been added.
217 sub calc_config_changes
220 my %nodes = map {$_ => 1} @{$self->{nodes}};
221 my %users = map {$_ => 1} @{$self->{users}};
224 my (@dnodes, @dusers, @nnodes, @nusers);
225 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
226 push @dnodes, keys %nodes;
227 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
228 push @dusers, keys %users;
229 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
237 confess "already have $call in $pkg" if $list{$call};
239 my $self = $pkg->SUPER::new($call);
240 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
241 $self->{version} = shift || 5000;
242 $self->{flags} = shift || Route::here(1);
245 $self->{lastid} = {};
247 $list{$call} = $self;
255 $call = shift if ref $call;
256 my $ref = $list{uc $call};
257 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
269 return $self->_addlist('parent', @_);
275 return $self->_dellist('parent', @_);
282 return $self->_addlist('nodes', @_);
288 return $self->_dellist('nodes', @_);
295 return $self->_addlist('users', @_);
301 return $self->_dellist('users', @_);
308 my $call = $self->{call} || "Unknown";
310 dbg("destroying $pkg with $call") if isdbg('routelow');
314 # generic AUTOLOAD for accessors
320 my $name = $AUTOLOAD;
321 return if $name =~ /::DESTROY$/;
324 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
326 # this clever line of code creates a subroutine which takes over from autoload
327 # from OO Perl - Conway
328 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};