2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
17 use vars qw($VERSION $BRANCH);
18 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
19 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
20 $main::build += $VERSION;
21 $main::branch += $BRANCH;
23 use vars qw(%list %valid @ISA $max $filterdef);
27 parent => '0,Parent Calls,parray',
28 nodes => '0,Nodes,parray',
29 users => '0,Users,parray',
30 usercount => '0,User Count',
31 version => '0,Version',
32 handle_xml => '0,Using XML,yesno',
33 lastmsg => '0,Last Route Msg,atime',
34 lastid => '0,Last Route MsgID',
35 do_pc92 => '0,Uses pc92,yesno',
38 $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);
84 # this routine is the opposite of 'add' above.
86 # It will return an object if (and only if) this 'del' will remove
87 # this object completely
95 # delete parent from this call's parent list
96 $pref->_delnode($self);
97 $self->_delparent($pref);
99 my $ncall = $self->{call};
101 # is this the last connection, I have no parents anymore?
102 unless (@{$self->{parent}}) {
103 foreach my $rcall (@{$self->{nodes}}) {
104 next if grep $rcall eq $_, @_;
105 my $r = Route::Node::get($rcall);
106 push @nodes, $r->del($self, $ncall, @_) if $r;
109 delete $list{$self->{call}};
119 foreach my $rcall (@{$parent->{nodes}}) {
121 push @out, $r->del($parent, $parent->{call}, @_) if $r;
129 for (@{$self->{users}}) {
130 my $ref = Route::User::get($_);
131 $ref->del($self) if $ref;
136 # add a user to this node
142 confess "Trying to add NULL User call to routing tables" unless $ucall;
144 my $uref = Route::User::get($ucall);
147 @out = $uref->addparent($self);
149 $uref = Route::User->new($ucall, $self->{call}, @_);
152 $self->_adduser($uref);
153 $self->{usercount} = scalar @{$self->{users}};
158 # delete a user from this node
166 @out = $self->_deluser($ref);
169 confess "tried to delete non-existant $ref->{call} from $self->{call}";
171 $self->{usercount} = scalar @{$self->{users}};
178 if (@_ && @{$self->{users}} == 0) {
179 $self->{usercount} = shift;
181 return $self->{usercount};
187 return @{$self->{users}};
193 return @{$self->{nodes}};
199 return @{$self->{parent}};
206 foreach my $call (@{$self->{nodes}}) {
207 next if grep $call eq $_, @_;
210 push @out, $r->rnodes($call, @_) if $r;
215 # this takes in a list of node and user calls (not references) from
216 # a config type update for a node and returns
217 # the differences as lists of things that have gone away
218 # and things that have been added.
219 sub calc_config_changes
222 my %nodes = map {$_ => 1} @{$self->{nodes}};
223 my %users = map {$_ => 1} @{$self->{users}};
226 my (@dnodes, @dusers, @nnodes, @nusers);
227 push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
228 push @dnodes, keys %nodes;
229 push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
230 push @dusers, keys %users;
231 return (\@dnodes, \@dusers, \@nnodes, \@nusers);
239 confess "already have $call in $pkg" if $list{$call};
241 my $self = $pkg->SUPER::new($call);
242 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
243 $self->{version} = shift || 5000;
244 $self->{flags} = shift || Route::here(1);
247 $self->{lastid} = {};
249 $list{$call} = $self;
257 $call = shift if ref $call;
258 my $ref = $list{uc $call};
259 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
271 return $self->_addlist('parent', @_);
277 return $self->_dellist('parent', @_);
284 return $self->_addlist('nodes', @_);
290 return $self->_dellist('nodes', @_);
297 return $self->_addlist('users', @_);
303 return $self->_dellist('users', @_);
310 my $call = $self->{call} || "Unknown";
312 dbg("destroying $pkg with $call") if isdbg('routelow');
316 # generic AUTOLOAD for accessors
322 my $name = $AUTOLOAD;
323 return if $name =~ /::DESTROY$/;
326 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
328 # this clever line of code creates a subroutine which takes over from autoload
329 # from OO Perl - Conway
330 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};