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