ac20b7d8de3266bc5d6ebeefcc0cd652fde05cef
[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 = uc shift;
60         confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
61         my $self = get($call);
62         if ($self) {
63                 $self->_addparent($parent->{call});
64                 $parent->_addnode($call);
65                 return undef;
66         }
67         $parent->_addnode($call);
68         $self = $parent->new($call, @_);
69         return $self;
70 }
71
72 #
73 # this routine is the opposite of 'add' above.
74 #
75 # It will return an object if (and only if) this 'del' will remove
76 # this object completely
77 #
78
79 sub del
80 {
81         my $self = shift;
82         my $pref = shift;
83
84         # delete parent from this call's parent list
85         my $pcall = $pref->{call};
86         my $ref = $self->_delparent($pcall);
87         my @nodes;
88         
89         # is this the last connection, I have no parents anymore?
90         unless (@$ref) {
91                 my $ncall = $self->{call};
92                 foreach my $rcall (@{$self->{nodes}}) {
93                         next if grep $rcall eq $_, @_;
94                         my $r = Route::Node::get($rcall);
95                         push @nodes, $r->del($self, $ncall, @_) if $r;
96                 }
97                 $self->_del_users;
98                 delete $list{$self->{call}};
99                 push @nodes, $self;
100         }
101         return @nodes;
102 }
103
104 sub del_nodes
105 {
106         my $parent = shift;
107         my @out;
108         foreach my $rcall (@{$parent->{nodes}}) {
109                 my $r = get($rcall);
110                 push @out, $r->del($parent, $parent->{call}, @_) if $r;
111         }
112         return @out;
113 }
114
115 sub _del_users
116 {
117         my $self = shift;
118         for (@{$self->{users}}) {
119                 my $ref = Route::User::get($_);
120                 $ref->del($self) if $ref;
121         }
122         $self->{users} = [];
123 }
124
125 # add a user to this node
126 sub add_user
127 {
128         my $self = shift;
129         my $ucall = shift;
130         $self->_adduser($ucall);
131
132         $self->{usercount} = scalar @{$self->{users}};
133         my $uref = Route::User::get($ucall);
134         my @out = (Route::User->new($ucall, $self->{call}, @_)) unless $uref;
135         return @out;
136 }
137
138 # delete a user from this node
139 sub del_user
140 {
141         my $self = shift;
142         my $ucall = shift;
143         my $ref = Route::User::get($ucall);
144         $self->_deluser($ucall);
145         my @out = $ref->del($self) if $ref;
146         return @out;
147 }
148
149 sub usercount
150 {
151         my $self = shift;
152         if (@_ && @{$self->{users}} == 0) {
153                 $self->{usercount} = shift;
154         }
155         return $self->{usercount};
156 }
157
158 sub users
159 {
160         my $self = shift;
161         return @{$self->{users}};
162 }
163
164 sub nodes
165 {
166         my $self = shift;
167         return @{$self->{nodes}};
168 }
169
170 sub rnodes
171 {
172         my $self = shift;
173         my @out;
174         foreach my $call (@{$self->{nodes}}) {
175                 next if grep $call eq $_, @_;
176                 push @out, $call;
177                 my $r = get($call);
178                 push @out, $r->rnodes($call, @_) if $r;
179         }
180         return @out;
181 }
182
183
184 sub new
185 {
186         my $pkg = shift;
187         my $call = uc shift;
188         
189         confess "already have $call in $pkg" if $list{$call};
190         
191         my $self = $pkg->SUPER::new($call);
192         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
193         $self->{version} = shift;
194         $self->{flags} = shift;
195         $self->{users} = [];
196         $self->{nodes} = [];
197         
198         $list{$call} = $self;
199         
200         return $self;
201 }
202
203 sub get
204 {
205         my $call = shift;
206         $call = shift if ref $call;
207         my $ref = $list{uc $call};
208         dbg('routerr', "Failed to get Node $call" ) unless $ref;
209         return $ref;
210 }
211
212 sub get_all
213 {
214         return values %list;
215 }
216
217 sub _addparent
218 {
219         my $self = shift;
220     return $self->_addlist('parent', @_);
221 }
222
223 sub _delparent
224 {
225         my $self = shift;
226     return $self->_dellist('parent', @_);
227 }
228
229
230 sub _addnode
231 {
232         my $self = shift;
233     return $self->_addlist('nodes', @_);
234 }
235
236 sub _delnode
237 {
238         my $self = shift;
239     return $self->_dellist('nodes', @_);
240 }
241
242
243 sub _adduser
244 {
245         my $self = shift;
246     return $self->_addlist('users', @_);
247 }
248
249 sub _deluser
250 {
251         my $self = shift;
252     return $self->_dellist('users', @_);
253 }
254
255 sub DESTROY
256 {
257         my $self = shift;
258         my $pkg = ref $self;
259         my $call = $self->{call} || "Unknown";
260         
261         dbg('route', "destroying $pkg with $call");
262 }
263
264 #
265 # generic AUTOLOAD for accessors
266 #
267
268 sub AUTOLOAD
269 {
270         no strict;
271
272         my $self = shift;
273         $name = $AUTOLOAD;
274         return if $name =~ /::DESTROY$/;
275         $name =~ s/.*:://o;
276   
277         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
278
279         # this clever line of code creates a subroutine which takes over from autoload
280         # from OO Perl - Conway
281 #       print "AUTOLOAD: $AUTOLOAD\n";
282 #       *{$AUTOLOAD} = sub {my $self = shift; @_ ? $self->{$name} = shift : $self->{$name}} ;
283     @_ ? $self->{$name} = shift : $self->{$name} ;
284 }
285
286 1;
287