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