change build number calculation to be more accurate
[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         return $max;
48 }
49
50 #
51 # this routine handles the possible adding of an entry in the routing
52 # table. It will only add an entry if it is new. It may have all sorts of
53 # other side effects which may include fixing up other links.
54 #
55 # It will return a node object if (and only if) it is a completely new
56 # object with that callsign. The upper layers are expected to do something
57 # sensible with this!
58 #
59 # called as $parent->add(call, dxchan, version, flags) 
60 #
61
62 sub add
63 {
64         my $parent = shift;
65         my $call = uc shift;
66         confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
67         my $self = get($call);
68         if ($self) {
69                 $self->_addparent($parent->{call});
70                 $parent->_addnode($call);
71                 return undef;
72         }
73         $parent->_addnode($call);
74         $self = $parent->new($call, @_);
75         return $self;
76 }
77
78 #
79 # this routine is the opposite of 'add' above.
80 #
81 # It will return an object if (and only if) this 'del' will remove
82 # this object completely
83 #
84
85 sub del
86 {
87         my $self = shift;
88         my $pref = shift;
89
90         # delete parent from this call's parent list
91         my $pcall = $pref->{call};
92         my $ncall = $self->{call};
93         $pref->_delnode($ncall);;
94         my $ref = $self->_delparent($pcall);
95         my @nodes;
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         $self->_adduser($ucall);
141
142         $self->{usercount} = scalar @{$self->{users}};
143         my $uref = Route::User::get($ucall);
144         my @out;
145         if ($uref) {
146                 $uref->addparent($self->{call});
147         } else {
148                 @out = Route::User->new($ucall, $self->{call}, @_);
149         }
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($call, @_) 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         my $ref = $list{uc $call};
223         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
224         return $ref;
225 }
226
227 sub get_all
228 {
229         return values %list;
230 }
231
232 sub _addparent
233 {
234         my $self = shift;
235     return $self->_addlist('parent', @_);
236 }
237
238 sub _delparent
239 {
240         my $self = shift;
241     return $self->_dellist('parent', @_);
242 }
243
244
245 sub _addnode
246 {
247         my $self = shift;
248     return $self->_addlist('nodes', @_);
249 }
250
251 sub _delnode
252 {
253         my $self = shift;
254     return $self->_dellist('nodes', @_);
255 }
256
257
258 sub _adduser
259 {
260         my $self = shift;
261     return $self->_addlist('users', @_);
262 }
263
264 sub _deluser
265 {
266         my $self = shift;
267     return $self->_dellist('users', @_);
268 }
269
270 sub DESTROY
271 {
272         my $self = shift;
273         my $pkg = ref $self;
274         my $call = $self->{call} || "Unknown";
275         
276         dbg("destroying $pkg with $call") if isdbg('routelow');
277 }
278
279 #
280 # generic AUTOLOAD for accessors
281 #
282
283 sub AUTOLOAD
284 {
285         no strict;
286
287         my $self = shift;
288         $name = $AUTOLOAD;
289         return if $name =~ /::DESTROY$/;
290         $name =~ s/.*:://o;
291   
292         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
293
294         # this clever line of code creates a subroutine which takes over from autoload
295         # from OO Perl - Conway
296 #       print "AUTOLOAD: $AUTOLOAD\n";
297 #       *{$AUTOLOAD} = sub {my $self = shift; @_ ? $self->{$name} = shift : $self->{$name}} ;
298     @_ ? $self->{$name} = shift : $self->{$name} ;
299 }
300
301 1;
302