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