added mkver
[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
19 main::mkver($VERSION = q$Revision$);
20
21 use vars qw(%list %valid @ISA $max $filterdef);
22 @ISA = qw(Route);
23
24 %valid = (
25                   parent => '0,Parent Calls,parray',
26                   nodes => '0,Nodes,parray',
27                   users => '0,Users,parray',
28                   usercount => '0,User Count',
29                   version => '0,Version',
30                   np => '0,Using New Prot,yesno',
31                   lid => '0,Last Msgid',
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     $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 (@{$self->{parent}}) {
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 parents
193 {
194         my $self = shift;
195         return @{$self->{parent}};
196 }
197
198 sub rnodes
199 {
200         my $self = shift;
201         my @out;
202         foreach my $call (@{$self->{nodes}}) {
203                 next if grep $call eq $_, @_;
204                 push @out, $call;
205                 my $r = get($call);
206                 push @out, $r->rnodes($call, @_) if $r;
207         }
208         return @out;
209 }
210
211
212 sub new
213 {
214         my $pkg = shift;
215         my $call = uc shift;
216         
217         confess "already have $call in $pkg" if $list{$call};
218         
219         my $self = $pkg->SUPER::new($call);
220         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
221         $self->{version} = shift;
222         $self->{flags} = shift;
223         $self->{users} = [];
224         $self->{nodes} = [];
225         $self->{lid} = 0;
226         
227         $list{$call} = $self;
228         
229         return $self;
230 }
231
232 sub get
233 {
234         my $call = shift;
235         $call = shift if ref $call;
236         my $ref = $list{uc $call};
237         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
238         return $ref;
239 }
240
241 sub get_all
242 {
243         return values %list;
244 }
245
246 sub newid
247 {
248         my $self = shift;
249         my $id = shift;
250         
251         return 0 if $id == $self->{lid};
252         if ($id > $self->{lid}) {
253                 $self->{lid} = $id;
254                 return 1;
255         } elsif ($self->{lid} - $id > 500) {
256                 $self->{id} = $id;
257                 return 1;
258         }
259         return 0;
260 }
261
262 sub _addparent
263 {
264         my $self = shift;
265     return $self->_addlist('parent', @_);
266 }
267
268 sub _delparent
269 {
270         my $self = shift;
271     return $self->_dellist('parent', @_);
272 }
273
274
275 sub _addnode
276 {
277         my $self = shift;
278     return $self->_addlist('nodes', @_);
279 }
280
281 sub _delnode
282 {
283         my $self = shift;
284     return $self->_dellist('nodes', @_);
285 }
286
287
288 sub _adduser
289 {
290         my $self = shift;
291     return $self->_addlist('users', @_);
292 }
293
294 sub _deluser
295 {
296         my $self = shift;
297     return $self->_dellist('users', @_);
298 }
299
300 sub DESTROY
301 {
302         my $self = shift;
303         my $pkg = ref $self;
304         my $call = $self->{call} || "Unknown";
305         
306         dbg("destroying $pkg with $call") if isdbg('routelow');
307 }
308
309 #
310 # generic AUTOLOAD for accessors
311 #
312
313 sub AUTOLOAD
314 {
315         no strict;
316         my $name = $AUTOLOAD;
317         return if $name =~ /::DESTROY$/;
318         $name =~ s/^.*:://o;
319   
320         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
321
322         # this clever line of code creates a subroutine which takes over from autoload
323         # from OO Perl - Conway
324         *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
325         goto &$AUTOLOAD;
326 }
327
328 1;
329