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}};
211 foreach my $call (@{$self->{nodes}}) {
212 next if grep $call eq $_, @_;
215 push @out, $r->rnodes($call, @_) if $r;
220 # return the differences in nodes between what we currently have and
221 # the list proffered. Returns two refs one to a list of nodes to remove and
222 # the other a list of nodes to add
224 # input is a list of callsigns (not refs)
228 my $in = ref $_[0] ? shift : \@_;
229 my %del = map {($_, 1)} nodes($self);
230 my %in = map {($_, 1)} @$in;
232 # remove all the calls that are in both lists
234 delete $in{$_} if delete $del{$_};
236 return ([keys %del], [keys %in]);
239 # same as above but for users
243 my $in = ref $_[0] ? shift : \@_;
244 my %del = map {($_, 1)} users($self);
245 my %in = map {($_, 1)} @$in;
247 # remove all the calls that are in both lists
249 delete $in{$_} if delete $del{$_};
251 return ([keys %del], [keys %in]);
259 confess "already have $call in $pkg" if $list{$call};
261 my $self = $pkg->SUPER::new($call);
262 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
263 $self->{version} = 0 || shift;
264 $self->{flags} = 0 || shift;
269 $list{$call} = $self;
277 $call = shift if ref $call;
278 my $ref = $list{uc $call};
279 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
293 return 0 if $id == $self->{lid};
294 if ($id > $self->{lid}) {
297 } elsif ($self->{lid} - $id > 500) {
307 return $self->_addlist('parent', @_);
313 return $self->_dellist('parent', @_);
320 return $self->_addlist('nodes', @_);
326 return $self->_dellist('nodes', @_);
333 return $self->_addlist('users', @_);
339 return $self->_dellist('users', @_);
343 # generic AUTOLOAD for accessors
349 my $name = $AUTOLOAD;
350 return if $name =~ /::DESTROY$/;
353 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
355 # this clever line of code creates a subroutine which takes over from autoload
356 # from OO Perl - Conway
357 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};