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