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 users => '0,Users,parray',
28 usercount => '0,User Count',
29 version => '0,Version',
30 np => '0,Using New Prot,yesno',
31 lid => '0,Last Msgid',
34 $filterdef = $Route::filterdef;
40 my $n = scalar (keys %list);
41 $max = $n if $n > $max;
52 # this routine handles the possible adding of an entry in the routing
53 # table. It will only add an entry if it is new. It may have all sorts of
54 # other side effects which may include fixing up other links.
56 # It will return a node object if (and only if) it is a completely new
57 # object with that callsign. The upper layers are expected to do something
60 # called as $parent->add(call, dxchan, version, flags)
67 confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
68 my $self = get($call);
70 $self->_addlink($parent);
71 $parent->_addlink($self);
74 $self = $parent->new($call, @_);
75 $parent->_addlink($self);
80 # this routine is the opposite of 'add' above.
82 # It will return an object if (and only if) this 'del' will remove
83 # this object completely
91 # delete parent from this call's parent list
92 $pref->_dellink($self);
93 $self->_dellink($pref);
95 my $ncall = $self->{call};
97 # is this the last connection, I have no parents anymore?
98 unless (@{$self->{links}}) {
99 foreach my $rcall (@{$self->{links}}) {
100 next if grep $rcall eq $_, @_;
101 my $r = Route::Node::get($rcall);
102 push @nodes, $r->del($self, $ncall, @_) if $r;
104 if ($ncall ne $main::mycall) {
106 delete $list{$self->{call}};
109 croak "trying to delete route node";
119 foreach my $rcall (@{$parent->{links}}) {
120 next if $rcall eq $parent->{call};
121 next if DXChannel->get($rcall);
123 push @out, $r->del($parent, $parent->{call}, @_) if $r;
131 for (@{$self->{users}}) {
132 my $ref = Route::User::get($_);
133 $ref->del($self) if $ref;
138 # add a user to this node
144 confess "Trying to add NULL User call to routing tables" unless $ucall;
146 my $uref = Route::User::get($ucall);
149 @out = $uref->addparent($self);
151 $uref = Route::User->new($ucall, $self->{call}, @_);
154 $self->_adduser($uref);
155 $self->{usercount} = scalar @{$self->{users}};
160 # delete a user from this node
168 @out = $self->_deluser($ref);
171 confess "tried to delete non-existant $ref->{call} from $self->{call}";
173 $self->{usercount} = scalar @{$self->{users}};
180 if (@_ && @{$self->{users}} == 0) {
181 $self->{usercount} = shift;
183 return $self->{usercount};
189 return @{$self->{users}};
195 return @{$self->{links}};
203 confess "already have $call in $pkg" if $list{$call};
205 my $self = $pkg->SUPER::new($call);
206 $self->{links} = ref $pkg ? [ $pkg->{call} ] : [ ];
207 $self->{version} = shift;
208 $self->{flags} = shift;
212 $list{$call} = $self;
220 $call = shift if ref $call;
221 my $ref = $list{uc $call};
222 dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
236 return 0 if $id == $self->{lid};
237 if ($id > $self->{lid}) {
240 } elsif ($self->{lid} - $id > 500) {
251 return $self->_addlist('users', @_);
257 return $self->_dellist('users', @_);
264 my $call = $self->{call} || "Unknown";
266 dbg("destroying $pkg with $call") if isdbg('routelow');
270 # generic AUTOLOAD for accessors
276 my $name = $AUTOLOAD;
277 return if $name =~ /::DESTROY$/;
280 confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
282 # this clever line of code creates a subroutine which takes over from autoload
283 # from OO Perl - Conway
284 *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};