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, dxchan, version, flags)
68 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
69 my $self = get($call);
71 $self->_addparent($parent);
72 $parent->_addnode($self);
75 $self = $parent->new($call, @_);
76 $parent->_addnode($self);
81 # this routine is the opposite of 'add' above.
83 # It will return an object if (and only if) this 'del' will remove
84 # this object completely
92 # delete parent from this call's parent list
93 $pref->_delnode($self);
94 $self->_delparent($pref);
96 my $ncall = $self->{call};
98 # is this the last connection, I have no parents anymore?
99 unless (@{$self->{parent}}) {
100 foreach my $rcall (@{$self->{nodes}}) {
101 next if grep $rcall eq $_, @_;
102 my $r = Route::Node::get($rcall);
103 push @nodes, $r->del($self, $ncall, @_) if $r;
106 delete $list{$self->{call}};
116 foreach my $rcall (@{$parent->{nodes}}) {
118 push @out, $r->del($parent, $parent->{call}, @_) if $r;
126 for (@{$self->{users}}) {
127 my $ref = Route::User::get($_);
128 $ref->del($self) if $ref;
133 # add a user to this node
139 confess "Trying to add NULL User call to routing tables" unless $ucall;
141 my $uref = Route::User::get($ucall);
144 push @out, $uref->addparent($self);
146 $uref = Route::User->new($ucall, $self->{call}, @_);
149 $self->_adduser($uref);
150 $self->{usercount} = scalar @{$self->{users}};
155 # delete a user from this node
163 @out = $self->_deluser($ref);
166 confess "tried to delete non-existant $ref->{call} from $self->{call}";
168 $self->{usercount} = scalar @{$self->{users}};
175 if (@_ && @{$self->{users}} == 0) {
176 $self->{usercount} = shift;
178 return $self->{usercount};
184 return @{$self->{users}};
190 return @{$self->{nodes}};
196 return @{$self->{parent}};
203 foreach my $call (@{$self->{nodes}}) {
204 next if grep $call eq $_, @_;
207 push @out, $r->rnodes($call, @_) if $r;
212 # return the differences in nodes between what we currently have and
213 # the list proffered. Returns two refs one to a list of nodes to remove and
214 # the other a list of nodes to add
216 # input is a list of callsigns (not refs)
220 my $in = ref $_[0] ? shift : \@_;
221 my %del = map {($_, 1)} nodes($self);
222 my %in = map {($_, 1)} @$in;
224 # remove all the calls that are in both lists
226 delete $in{$_} if delete $del{$_};
228 return ([keys %del], [keys %in]);
231 # same as above but for users
235 my $in = ref $_[0] ? shift : \@_;
236 my %del = map {($_, 1)} users($self);
237 my %in = map {($_, 1)} @$in;
239 # remove all the calls that are in both lists
241 delete $in{$_} if delete $del{$_};
243 return ([keys %del], [keys %in]);
251 confess "already have $call in $pkg" if $list{$call};
253 my $self = $pkg->SUPER::new($call);
254 $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
255 $self->{version} = 0 || shift;
256 $self->{flags} = 0 || shift;
261 $list{$call} = $self;
269 $call = shift if ref $call;
270 my $ref = $list{uc $call};
271 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
285 return 0 if $id == $self->{lid};
286 if ($id > $self->{lid}) {
289 } elsif ($self->{lid} - $id > 500) {
299 return $self->_addlist('parent', @_);
305 return $self->_dellist('parent', @_);
312 return $self->_addlist('nodes', @_);
318 return $self->_dellist('nodes', @_);
325 return $self->_addlist('users', @_);
331 return $self->_dellist('users', @_);
338 my $call = $self->{call} || "Unknown";
340 dbg("destroying $pkg with $call") if isdbg('routelow');
344 # generic AUTOLOAD for accessors
350 my $name = $AUTOLOAD;
351 return if $name =~ /::DESTROY$/;
354 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
356 # this clever line of code creates a subroutine which takes over from autoload
357 # from OO Perl - Conway
358 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};