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