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