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