2 # Node routing routines
4 # Copyright (c) 2001 Dirk Koopman G1TLH
17 use vars qw($VERSION $BRANCH);
19 main::mkver($VERSION = 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',
32 np => '0,Using New Prot,yesno',
35 $filterdef = $Route::filterdef;
41 my $n = scalar (keys %list);
42 $max = $n if $n > $max;
53 # this routine handles the possible adding of an entry in the routing
54 # table. It will only add an entry if it is new. It may have all sorts of
55 # other side effects which may include fixing up other links.
57 # It will return a node object if (and only if) it is a completely new
58 # object with that callsign. The upper layers are expected to do something
61 # called as $parent->add(call, version, flags)
68 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);
76 if ($self->{version} != $version || $self->{flags} != $here) {
77 $self->{version} = $version;
78 $self->{flags} = $here;
83 $self = $parent->new($call, $version, $here);
84 $parent->_addnode($self);
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{$self->{call}};
124 foreach my $rcall (@{$parent->{nodes}}) {
126 push @out, $r->del($parent, $parent->{call}, @_) if $r;
134 for (@{$self->{users}}) {
135 my $ref = Route::User::get($_);
136 $ref->del($self) if $ref;
141 # add a user to this node
147 confess "Trying to add NULL User call to routing tables" unless $ucall;
149 my $uref = Route::User::get($ucall);
152 push @out, $uref->addparent($self);
154 $uref = Route::User->new($ucall, $self->{call}, @_);
157 $self->_adduser($uref);
158 $self->{usercount} = scalar @{$self->{users}};
163 # delete a user from this node
171 @out = $self->_deluser($ref);
174 confess "tried to delete non-existant $ref->{call} from $self->{call}";
176 $self->{usercount} = scalar @{$self->{users}};
183 if (@_ && @{$self->{users}} == 0) {
184 $self->{usercount} = shift;
186 return $self->{usercount};
192 return @{$self->{users}};
198 return @{$self->{nodes}};
204 return @{$self->{parent}};
210 return $self->_haslist('users', shift);
216 return $self->_haslist('nodes', shift);
222 return $self->_haslist('parent', shift);
230 foreach my $call (@{$self->{nodes}}) {
231 next if grep $call eq $_, @_;
234 push @out, $r->rnodes($call, @_) if $r;
239 # return the differences in nodes between what we currently have and
240 # the list proffered. Returns two refs one to a list of nodes to remove and
241 # the other a list of nodes to add
243 # input is a list of callsigns (not refs)
247 my $in = ref $_[0] ? shift : \@_;
248 my %del = map {($_, 1)} nodes($self);
249 my %in = map {($_, 1)} @$in;
251 # remove all the calls that are in both lists
253 delete $in{$_} if delete $del{$_};
255 return ([keys %del], [keys %in]);
258 # same as above but for users
262 my $in = ref $_[0] ? shift : \@_;
263 my %del = map {($_, 1)} users($self);
264 my %in = map {($_, 1)} @$in;
266 # remove all the calls that are in both lists
268 delete $in{$_} if delete $del{$_};
270 return ([keys %del], [keys %in]);
278 confess "already have $call in $pkg" if $list{$call};
280 my $self = $pkg->SUPER::new($call);
281 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
282 $self->{version} = 0 || shift;
283 $self->{flags} = 0 || shift;
288 $list{$call} = $self;
296 $call = shift if ref $call;
297 my $ref = $list{uc $call};
298 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
312 return 0 if $id == $self->{lid};
313 if ($id > $self->{lid}) {
316 } elsif ($self->{lid} - $id > 500) {
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', @_);
362 # generic AUTOLOAD for accessors
368 my $name = $AUTOLOAD;
369 return if $name =~ /::DESTROY$/;
372 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
374 # this clever line of code creates a subroutine which takes over from autoload
375 # from OO Perl - Conway
376 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};