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