10a45eaef7a8da949af57e550a550392bfc58ab7
[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 use DXUtil;
15
16 use strict;
17
18 use vars qw($VERSION $BRANCH);
19 ($VERSION, $BRANCH) = dxver( q$Revision$);
20
21 use vars qw(%list %valid @ISA $max $filterdef $obscount);
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                   handle_xml => '0,Using XML,yesno',
31                   lastmsg => '0,Last Route Msg,atime',
32                   lastid => '0,Last Route MsgID',
33                   do_pc92 => '0,Uses pc92,yesno',
34                   via_pc92 => '0,Came in via pc92,yesno',
35                   obscount => '0,Obscount',
36 );
37
38 $filterdef = $Route::filterdef;
39 %list = ();
40 $max = 0;
41 $obscount = 3;
42
43 sub count
44 {
45         my $n = scalar (keys %list);
46         $max = $n if $n > $max;
47         return $n;
48 }
49
50 sub max
51 {
52         count();
53         return $max;
54 }
55
56 #
57 # this routine handles the possible adding of an entry in the routing
58 # table. It will only add an entry if it is new. It may have all sorts of
59 # other side effects which may include fixing up other links.
60 #
61 # It will return a node object if (and only if) it is a completely new
62 # object with that callsign. The upper layers are expected to do something
63 # sensible with this!
64 #
65 # called as $parent->add(call, dxchan, version, flags) 
66 #
67
68 sub add
69 {
70         my $parent = shift;
71         my $call = uc shift;
72         confess "Route::add trying to add $call to myself" if $call eq $parent->{call};
73         my $self = get($call);
74         if ($self) {
75                 $self->_addparent($parent);
76                 $parent->_addnode($self);
77                 return undef;
78         }
79         $self = $parent->new($call, @_);
80         $parent->_addnode($self);
81         return $self;
82 }
83
84 #
85 # this routine is the opposite of 'add' above.
86 #
87 # It will return an object if (and only if) this 'del' will remove
88 # this object completely
89 #
90
91 sub del
92 {
93         my $self = shift;
94         my $pref = shift;
95
96         # delete parent from this call's parent list
97         $pref->_delnode($self);
98     $self->_delparent($pref);
99         my @nodes;
100         my $ncall = $self->{call};
101         
102         # is this the last connection, I have no parents anymore?
103         unless (@{$self->{parent}}) {
104                 foreach my $rcall (@{$self->{nodes}}) {
105                         next if grep $rcall eq $_, @_;
106                         my $r = Route::Node::get($rcall);
107                         push @nodes, $r->del($self, $ncall, @_) if $r;
108                 }
109                 $self->_del_users;
110                 delete $list{$self->{call}};
111                 push @nodes, $self;
112         }
113         return @nodes;
114 }
115
116 sub del_nodes
117 {
118         my $parent = shift;
119         my @out;
120         foreach my $rcall (@{$parent->{nodes}}) {
121                 my $r = get($rcall);
122                 push @out, $r->del($parent, $parent->{call}, @_) if $r;
123         }
124         return @out;
125 }
126
127 sub _del_users
128 {
129         my $self = shift;
130         for (@{$self->{users}}) {
131                 my $ref = Route::User::get($_);
132                 $ref->del($self) if $ref;
133         }
134         $self->{users} = [];
135 }
136
137 # add a user to this node
138 sub add_user
139 {
140         my $self = shift;
141         my $ucall = shift;
142
143         confess "Trying to add NULL User call to routing tables" unless $ucall;
144
145         my $uref = Route::User::get($ucall);
146         my @out;
147         if ($uref) {
148                 @out = $uref->addparent($self);
149         } else {
150                 $uref = Route::User->new($ucall, $self->{call}, @_);
151                 @out = $uref;
152         }
153         $self->_adduser($uref);
154         $self->{usercount} = scalar @{$self->{users}};
155
156         return @out;
157 }
158
159 # delete a user from this node
160 sub del_user
161 {
162         my $self = shift;
163         my $ref = shift;
164         my @out;
165         
166         if ($ref) {
167                 @out = $self->_deluser($ref);
168                 $ref->del($self);
169         } else {
170                 confess "tried to delete non-existant $ref->{call} from $self->{call}";
171         }
172         $self->{usercount} = scalar @{$self->{users}};
173         return @out;
174 }
175
176 sub usercount
177 {
178         my $self = shift;
179         if (@_ && @{$self->{users}} == 0) {
180                 $self->{usercount} = shift;
181         }
182         return $self->{usercount};
183 }
184
185 sub users
186 {
187         my $self = shift;
188         return @{$self->{users}};
189 }
190
191 sub nodes
192 {
193         my $self = shift;
194         return @{$self->{nodes}};
195 }
196
197 sub parents
198 {
199         my $self = shift;
200         return @{$self->{parent}};
201 }
202
203 sub rnodes
204 {
205         my $self = shift;
206         my @out;
207         foreach my $call (@{$self->{nodes}}) {
208                 next if grep $call eq $_, @_;
209                 push @out, $call;
210                 my $r = get($call);
211                 push @out, $r->rnodes($call, @_) if $r;
212         }
213         return @out;
214 }
215
216 # this takes in a list of node and user calls (not references) from 
217 # a config type update for a node and returns
218 # the differences as lists of things that have gone away
219 # and things that have been added. 
220 sub calc_config_changes
221 {
222         my $self = shift;
223         my %nodes = map {$_ => 1} @{$self->{nodes}};
224         my %users = map {$_ => 1} @{$self->{users}};
225         my $cnodes = shift;
226         my $cusers = shift;
227         if (isdbg('route')) {
228                 dbg("ROUTE: start calc_config_changes");
229                 dbg("ROUTE: incoming nodes on $self->{call}: " . join(',', sort @$cnodes));
230                 dbg("ROUTE: incoming users on $self->{call}: " . join(',', sort @$cusers));
231                 dbg("ROUTE: existing nodes on $self->{call}: " . join(',', sort keys %nodes));
232                 dbg("ROUTE: existing users on $self->{call}: " . join(',', sort keys %users));
233         }
234         my (@dnodes, @dusers, @nnodes, @nusers);
235         push @nnodes, map {my @r = $nodes{$_} ? () : $_; delete $nodes{$_}; @r} @$cnodes;
236         push @dnodes, keys %nodes;
237         push @nusers, map {my @r = $users{$_} ? () : $_; delete $users{$_}; @r} @$cusers;
238         push @dusers, keys %users;
239         if (isdbg('route')) {
240                 dbg("ROUTE: deleted nodes on $self->{call}: " . join(',', sort @dnodes));
241                 dbg("ROUTE: deleted users on $self->{call}: " . join(',', sort @dusers));
242                 dbg("ROUTE: added nodes on $self->{call}: " . join(',', sort  @nnodes));
243                 dbg("ROUTE: added users on $self->{call}: " . join(',', sort @nusers));
244                 dbg("ROUTE: end calc_config_changes");
245         }
246         return (\@dnodes, \@dusers, \@nnodes, \@nusers);
247 }
248
249 sub new
250 {
251         my $pkg = shift;
252         my $call = uc shift;
253         
254         confess "already have $call in $pkg" if $list{$call};
255         
256         my $self = $pkg->SUPER::new($call);
257         $self->{parent} = ref $pkg ? [ $pkg->{call} ] : [ ];
258         $self->{version} = shift || 5401;
259         $self->{flags} = shift || Route::here(1);
260         $self->{users} = [];
261         $self->{nodes} = [];
262         $self->{lastid} = {};
263         $self->reset_obs;                       # by definition
264         
265         $list{$call} = $self;
266         
267         return $self;
268 }
269
270 sub get
271 {
272         my $call = shift;
273         $call = shift if ref $call;
274         my $ref = $list{uc $call};
275         dbg("Failed to get Node $call" ) if !$ref && isdbg('routerr');
276         return $ref;
277 }
278
279 sub get_all
280 {
281         return values %list;
282 }
283
284 sub _addparent
285 {
286         my $self = shift;
287     return $self->_addlist('parent', @_);
288 }
289
290 sub _delparent
291 {
292         my $self = shift;
293     return $self->_dellist('parent', @_);
294 }
295
296
297 sub _addnode
298 {
299         my $self = shift;
300     return $self->_addlist('nodes', @_);
301 }
302
303 sub _delnode
304 {
305         my $self = shift;
306     return $self->_dellist('nodes', @_);
307 }
308
309
310 sub _adduser
311 {
312         my $self = shift;
313     return $self->_addlist('users', @_);
314 }
315
316 sub _deluser
317 {
318         my $self = shift;
319     return $self->_dellist('users', @_);
320 }
321
322 sub dec_obs
323 {
324         my $self = shift;
325         $self->{obscount}--;
326         return $self->{obscount};
327 }
328
329 sub reset_obs
330 {
331         my $self = shift;
332         $self->{obscount} = $obscount;
333 }
334
335 sub DESTROY
336 {
337         my $self = shift;
338         my $pkg = ref $self;
339         my $call = $self->{call} || "Unknown";
340         
341         dbg("destroying $pkg with $call") if isdbg('routelow');
342 }
343
344 #
345 # generic AUTOLOAD for accessors
346 #
347
348 sub AUTOLOAD
349 {
350         no strict;
351         my $name = $AUTOLOAD;
352         return if $name =~ /::DESTROY$/;
353         $name =~ s/^.*:://o;
354   
355         confess "Non-existant field '$AUTOLOAD'" unless $valid{$name} || $Route::valid{$name};
356
357         # this clever line of code creates a subroutine which takes over from autoload
358         # from OO Perl - Conway
359         *$AUTOLOAD = sub {$_[0]->{$name} = $_[1] if @_ > 1; return $_[0]->{$name}};
360         goto &$AUTOLOAD;
361 }
362
363 1;
364