Added input route filtering
[spider.git] / perl / Route / Node.pm
1 #
2 # Node routing routines
3 #
4 # Copyright (c) 2001 Dirk Koopman G1TLH
5 #
6 # $Id$
7
8
9 package Route::Node;
10
11 use DXDebug;
12 use Route;
13 use Route::User;
14
15 use strict;
16
17 use vars qw(%list %valid @ISA $max $filterdef);
18 @ISA = qw(Route);
19
20 %valid = (
21                   parent => '0,Parent Calls,parray',
22                   nodes => '0,Nodes,parray',
23                   users => '0,Users,parray',
24                   usercount => '0,User Count',
25                   version => '0,Version',
26 );
27
28 $filterdef = $Route::filterdef;
29 %list = ();
30 $max = 0;
31
32 sub count
33 {
34         my $n = scalar (keys %list);
35         $max = $n if $n > $max;
36         return $n;
37 }
38
39 sub max
40 {
41         return $max;
42 }
43
44 #
45 # this routine handles the possible adding of an entry in the routing
46 # table. It will only add an entry if it is new. It may have all sorts of
47 # other side effects which may include fixing up other links.
48 #
49 # It will return a node object if (and only if) it is a completely new
50 # object with that callsign. The upper layers are expected to do something
51 # sensible with this!
52 #
53 # called as $parent->add(call, dxchan, version, flags) 
54 #
55
56 sub add
57 {
58         my $parent = shift;
59         my $call = shift;
60         my $self;
61         
62         if (ref $call) {
63                 $self = $call;
64                 $call = $self->{call};
65         } else {
66                 $self = get($call);
67         }
68
69         confess "Trying to add NULL Node call to routing tables" unless $call;
70         
71         if ($self) {
72                 $self->_addparent($parent->{call});
73                 $parent->_addnode($call);
74                 return undef;
75         }
76         confess "Route::Node::add trying to add $call to myself" if $call eq $parent->{call};   
77         $parent->_addnode($call);
78         $self = $parent->new($call, @_);
79         return $self;
80 }
81
82 #
83 # this routine is the opposite of 'add' above.
84 #
85 # It will return an object if (and only if) this 'del' will remove
86 # this object completely
87 #
88
89 sub del
90 {
91         my $self = shift;
92         my $pref = shift;
93
94         # delete parent from this call's parent list
95         my $pcall = $pref->{call};
96         my $ncall = $self->{call};
97         $pref->_delnode($ncall);;
98         my $ref = $self->_delparent($pcall);
99         my @nodes;
100         
101         # is this the last connection, I have no parents anymore?
102         unless (@$ref) {
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;
107                 }
108                 $self->_del_users;
109                 delete $list{$self->{call}};
110                 push @nodes, $self;
111         }
112         return @nodes;
113 }
114
115 sub del_nodes
116 {
117         my $parent = shift;
118         my @out;
119         foreach my $rcall (@{$parent->{nodes}}) {
120                 my $r = get($rcall);
121                 push @out, $r->del($parent, $parent->{call}, @_) if $r;
122         }
123         return @out;
124 }
125
126 sub _del_users
127 {
128         my $self = shift;
129         for (@{$self->{users}}) {
130                 my $ref = Route::User::get($_);
131                 $ref->del($self) if $ref;
132         }
133         $self->{users} = [];
134 }
135
136 # add a user to this node
137 sub add_user
138 {
139         my $self = shift;
140         my $ucall = shift;
141
142         confess "Trying to add NULL User call to routing tables" unless $ucall;
143
144         $self->_adduser($ucall);
145
146         $self->{usercount} = scalar @{$self->{users}};
147         my $uref = Route::User::get($ucall);
148         my @out = (Route::User->new($ucall, $self->{call}, @_)) unless $uref;
149         return @out;
150 }
151
152 # delete a user from this node
153 sub del_user
154 {
155         my $self = shift;
156         my $ucall = shift;
157         my $ref = Route::User::get($ucall);
158         $self->_deluser($ucall);
159         my @out = $ref->del($self) if $ref;
160         return @out;
161 }
162
163 sub usercount
164 {
165         my $self = shift;
166         if (@_ && @{$self->{users}} == 0) {
167                 $self->{usercount} = shift;
168         }
169         return $self->{usercount};
170 }
171
172 sub users
173 {
174         my $self = shift;
175         return @{$self->{users}};
176 }
177
178 sub nodes
179 {
180         my $self = shift;
181         return @{$self->{nodes}};
182 }
183
184 sub rnodes
185 {
186         my $self = shift;
187         my @out;
188         foreach my $call (@{$self->{nodes}}) {
189                 next if grep $call eq $_, @_;
190                 push @out, $call;
191                 my $r = get($call);
192                 push @out, $r->rnodes($call, @_) if $r;
193         }
194         return @out;
195 }
196
197
198 sub new
199 {
200         my $pkg = shift;
201         my $call = uc shift;
202         
203         confess "already have $call in $pkg" if $list{$call};
204         
205         my $self = $pkg->SUPER::new($call);
206         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
207         $self->{version} = shift;
208         $self->{flags} = shift;
209         $self->{users} = [];
210         $self->{nodes} = [];
211         
212         $list{$call} = $self;
213         
214         return $self;
215 }
216
217 sub get
218 {
219         my $call = shift;
220         $call = shift if ref $call;
221         my $ref = $list{uc $call};
222         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
223         return $ref;
224 }
225
226 sub get_all
227 {
228         return values %list;
229 }
230
231 sub _addparent
232 {
233         my $self = shift;
234     return $self->_addlist('parent', @_);
235 }
236
237 sub _delparent
238 {
239         my $self = shift;
240     return $self->_dellist('parent', @_);
241 }
242
243
244 sub _addnode
245 {
246         my $self = shift;
247     return $self->_addlist('nodes', @_);
248 }
249
250 sub _delnode
251 {
252         my $self = shift;
253     return $self->_dellist('nodes', @_);
254 }
255
256
257 sub _adduser
258 {
259         my $self = shift;
260     return $self->_addlist('users', @_);
261 }
262
263 sub _deluser
264 {
265         my $self = shift;
266     return $self->_dellist('users', @_);
267 }
268
269 sub DESTROY
270 {
271         my $self = shift;
272         my $pkg = ref $self;
273         my $call = $self->{call} || "Unknown";
274         
275         dbg("destroying $pkg with $call") if isdbg('routelow');
276 }
277
278 #
279 # generic AUTOLOAD for accessors
280 #
281
282 sub AUTOLOAD
283 {
284         no strict;
285
286         my $self = shift;
287         $name = $AUTOLOAD;
288         return if $name =~ /::DESTROY$/;
289         $name =~ s/.*:://o;
290   
291         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
292
293         # this clever line of code creates a subroutine which takes over from autoload
294         # from OO Perl - Conway
295 #       print "AUTOLOAD: $AUTOLOAD\n";
296 #       *{$AUTOLOAD} = sub {my $self = shift; @_ ? $self->{$name} = shift : $self->{$name}} ;
297     @_ ? $self->{$name} = shift : $self->{$name} ;
298 }
299
300 1;
301