Revert "new version of ip address storage"
[spider.git] / perl / Route.pm
1 #
2 #
3 # This module impliments the abstracted routing for all protocols and
4 # is probably what I SHOULD have done the first time.
5 #
6 # Heyho.
7 #
8 # This is just a container class which I expect to subclass
9 #
10 # Copyright (c) 2001 Dirk Koopman G1TLH
11 #
12 #
13 #
14
15 package Route;
16
17 use DXDebug;
18 use DXChannel;
19 use Prefix;
20 use DXUtil;
21
22 use strict;
23
24
25 use vars qw(%list %valid $filterdef $maxlevel);
26
27 %valid = (
28                   call => "0,Callsign",
29                   flags => "0,Flags,phex",
30                   dxcc => '0,Country Code',
31                   itu => '0,ITU Zone',
32                   cq => '0,CQ Zone',
33                   state => '0,State',
34                   city => '0,City',
35                   ip => '0,IP Address',
36                  );
37
38 $filterdef = bless ([
39                           # tag, sort, field, priv, special parser
40                           ['channel', 'c', 0],
41                           ['channel_dxcc', 'nc', 1],
42                           ['channel_itu', 'ni', 2],
43                           ['channel_zone', 'nz', 3],
44                           ['call', 'c', 4],
45                           ['by', 'c', 4],
46                           ['call_dxcc', 'nc', 5],
47                           ['by_dxcc', 'nc', 5],
48                           ['call_itu', 'ni', 6],
49                           ['by_itu', 'ni', 6],
50                           ['call_zone', 'nz', 7],
51                           ['by_zone', 'nz', 7],
52                           ['channel_state', 'ns', 8],
53                           ['call_state', 'ns', 9],
54                           ['by_state', 'ns', 9],
55                          ], 'Filter::Cmd');
56
57 $maxlevel = 25;                 # maximum recursion level in Route::config
58
59 sub new
60 {
61         my ($pkg, $call) = @_;
62         $pkg = ref $pkg if ref $pkg;
63
64         my $self = bless {call => $call}, $pkg;
65         dbg("create $pkg with $call") if isdbg('routelow');
66
67         # add in all the dxcc, itu, zone info
68         ($self->{dxcc}, $self->{itu}, $self->{cq}, $self->{state}, $self->{city}) =
69                 Prefix::cty_data($call);
70
71         $self->{flags} = here(1);
72
73         return $self;
74 }
75
76 #
77 # get a callsign from a passed reference or a string
78 #
79
80 sub _getcall
81 {
82         my $self = shift;
83         my $thingy = shift;
84         $thingy = $self unless $thingy;
85         $thingy = $thingy->call if ref $thingy;
86         $thingy = uc $thingy if $thingy;
87         return $thingy;
88 }
89
90 #
91 # add and delete a callsign to/from a list
92 #
93
94 sub _addlist
95 {
96         my $self = shift;
97         my $field = shift;
98         my @out;
99         foreach my $c (@_) {
100                 confess "Need a ref here" unless ref($c);
101
102                 my $call = $c->{call};
103                 unless (grep $_ eq $call, @{$self->{$field}}) {
104                         push @{$self->{$field}}, $call;
105                         dbg(ref($self) . " adding $call to " . $self->{call} . "->\{$field\}") if isdbg('routelow');
106                         push @out, $c;
107                 }
108         }
109         return @out;
110 }
111
112 sub _dellist
113 {
114         my $self = shift;
115         my $field = shift;
116         my @out;
117         foreach my $c (@_) {
118                 confess "Need a ref here" unless ref($c);
119                 my $call = $c->{call};
120                 if (grep $_ eq $call, @{$self->{$field}}) {
121                         $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
122                         dbg(ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}") if isdbg('routelow');
123                         push @out, $c;
124                 }
125         }
126         return @out;
127 }
128
129 sub is_empty
130 {
131         my $self = shift;
132         return @{$self->{$_[0]}} == 0;
133 }
134
135 #
136 # flag field constructors/enquirers
137 #
138 # These can be called in various ways:-
139 #
140 # Route::here or $ref->here returns 1 or 0 depending on value of the here flag
141 # Route::here(1) returns 2 (the bit value of the here flag)
142 # $ref->here(1) or $ref->here(0) sets the here flag
143 #
144
145 sub here
146 {
147         my $self = shift;
148         my $r = shift;
149         return $self ? 2 : 0 unless ref $self;
150         return ($self->{flags} & 2) ? 1 : 0 unless defined $r;
151         $self->{flags} = (($self->{flags} & ~2) | ($r ? 2 : 0));
152         return $r ? 1 : 0;
153 }
154
155 sub conf
156 {
157         my $self = shift;
158         my $r = shift;
159         return $self ? 1 : 0 unless ref $self;
160         return ($self->{flags} & 1) ? 1 : 0 unless defined $r;
161         $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
162         return $r ? 1 : 0;
163 }
164
165 sub parents
166 {
167         my $self = shift;
168         return @{$self->{parent}};
169 }
170
171 #
172 # display routines
173 #
174
175 sub user_call
176 {
177         my $self = shift;
178         my $call = sprintf "%s", $self->{call};
179         return $self->here ? "$call" : "($call)";
180 }
181
182 sub config
183 {
184         my $self = shift;
185         my $nodes_only = shift || 0;
186         my $width = shift || 79;
187         my $level = shift;
188         my $seen = shift;
189         my @out;
190         my $line;
191         my $call = $self->{call};
192         my $printit = 1;
193
194         dbg("config: $call nodes: $nodes_only level: $level calls: " . join(',', @_)) if isdbg('routec');
195
196         # allow ranges
197         if (@_) {
198                 $printit = grep $call =~ m|$_|, @_;
199         }
200
201         if ($printit) {
202                 my $pcall = $self->user_call;
203                 $pcall .= ":" . $self->obscount if isdbg('obscount');
204
205
206                 $line = ' ' x ($level*2) . $pcall;
207                 $pcall = ' ' x length $pcall;
208
209                 # recursion detector
210                 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
211                         $line .= ' ...';
212                         push @out, $line;
213                         return @out;
214                 }
215                 $seen->{$call}++;
216
217                 # print users
218                 unless ($nodes_only) {
219                         if (@{$self->{users}}) {
220                                 $line .= '->';
221                                 foreach my $ucall (sort @{$self->{users}}) {
222                                         my $uref = Route::User::get($ucall);
223                                         my $c;
224                                         if ($uref) {
225                                                 $c = $uref->user_call;
226                                         } else {
227                                                 $c = "$ucall?";
228                                         }
229                                         if ((length $line) + (length $c) + 1 < $width) {
230                                                 $line .= $c . ' ';
231                                         } else {
232                                                 $line =~ s/\s+$//;
233                                                 push @out, $line;
234                                                 $line = ' ' x ($level*2) . "$pcall->$c ";
235                                         }
236                                 }
237                         }
238                 }
239                 $line =~ s/->$//g;
240                 $line =~ s/\s+$//;
241                 push @out, $line if length $line;
242         } else {
243                 # recursion detector
244                 if ((DXChannel::get($call) && $level > 1) || $seen->{$call} || $level > $maxlevel) {
245                         return @out;
246                 }
247                 $seen->{$call}++;
248         }
249
250         # deal with more nodes
251         foreach my $ncall (sort @{$self->{nodes}}) {
252                 my $nref = Route::Node::get($ncall);
253
254                 if ($nref) {
255                         my $c = $nref->user_call;
256                         dbg("recursing from $call -> $c") if isdbg('routec');
257                         my @rout = $nref->config($nodes_only, $width, $level+1, $seen, @_);
258                         if (@rout && @_) {
259                                 push @out, ' ' x ($level*2) . $self->user_call unless grep /^\s+$call/, @out;
260                         }
261                         push @out, @rout;
262                 } else {
263                         push @out, ' ' x (($level+1)*2)  . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_);
264                 }
265         }
266
267         return @out;
268 }
269
270 sub cluster
271 {
272         my $nodes = Route::Node::count();
273         my $tot = Route::User::count();
274         my ($users, $maxlocalusers) = DXCommandmode::user_count();
275         my $maxusers = Route::User::max();
276         my $uptime = main::uptime();
277         my $localnodes = $DXChannel::count - $users;
278         
279         return ($nodes, $tot, $users, $maxlocalusers, $maxusers, $uptime, $localnodes);
280         
281
282 }
283
284 #
285 # routing things
286 #
287
288 sub get
289 {
290         my $call = shift;
291         return Route::Node::get($call) || Route::User::get($call);
292 }
293
294 sub findroutes
295 {
296         my $call = shift;
297         my %cand;
298         my @out;
299
300         dbg("ROUTE: findroutes $call") if isdbg('findroutes');
301
302         my $nref = Route::get($call);
303         return () unless $nref;
304
305         # we are directly connected, force "best possible" priority, but
306         # carry on in case user is connected on other nodes.
307         my $dxchan = DXChannel::get($call);
308         if ($dxchan) {
309                 dbg("ROUTE: findroutes $call -> directly connected") if isdbg('findroutes');
310                 $cand{$call} = 99;
311         }
312
313         # obtain the dxchannels that have seen this thingy
314         my @parent = $nref->isa('Route::User') ? @{$nref->{parent}} : $call;
315         foreach my $p (@parent) {
316                 next if $p eq $main::mycall; # this is dealt with above
317
318                 # deal with directly connected nodes, again "best priority"
319                 $dxchan = DXChannel::get($p);
320                 if ($dxchan) {
321                         dbg("ROUTE: findroutes $call -> connected direct via parent $p") if isdbg('findroutes');
322                         $cand{$p} = 99;
323                         next;
324                 }
325
326                 my $r = Route::Node::get($p);
327                 if ($r) {
328                         my %r = $r->PC92C_dxchan;
329                         while (my ($k, $v) = each %r) {
330                                 $cand{$k} = $v if $v > ($cand{$k} || 0);
331                         }
332                 }
333         }
334
335         # remove any dxchannels that have gone away
336         while (my ($k, $v) = each %cand) {
337                 if (my $dxc = DXChannel::get($k)) {
338                         push @out, [$v, $dxc];
339                 }
340         }
341
342         # get a sorted list of dxchannels with the highest hop count first
343         my @nout = sort {$b->[0] <=> $a->[0]} @out;
344         if (isdbg('findroutes')) {
345                 if (@nout) {
346                         for (@nout) {
347                                 dbg("ROUTE: findroutes $call -> $_->[0] " . $_->[1]->call);
348                         }
349                 }
350         }
351
352         return @nout;
353 }
354
355 # find all the possible dxchannels which this object might be on
356 sub alldxchan
357 {
358         my $self = shift;
359         my @dxchan = findroutes($self->{call});
360         return map {$_->[1]} @dxchan;
361 }
362
363 sub dxchan
364 {
365         my $self = shift;
366
367         # ALWAYS return the locally connected channel if present;
368         my $dxchan = DXChannel::get($self->call);
369         return $dxchan if $dxchan;
370
371         my @dxchan = $self->alldxchan;
372         return undef unless @dxchan;
373
374         # dxchannels are now returned in order of "closeness"
375         return $dxchan[0];
376 }
377
378 sub delete_interface
379 {
380
381 }
382
383 #
384 # track destruction
385 #
386
387 sub DESTROY
388 {
389         my $self = shift;
390         my $pkg = ref $self;
391
392         dbg("$pkg $self->{call} destroyed") if isdbg('routelow');
393 }
394
395 no strict;
396 #
397 # return a list of valid elements
398 #
399
400 sub fields
401 {
402         my $pkg = shift;
403         $pkg = ref $pkg if ref $pkg;
404     my $val = "${pkg}::valid";
405         my @out = keys %$val;
406         push @out, keys %valid;
407         return @out;
408 }
409
410 #
411 # return a prompt for a field
412 #
413
414 sub field_prompt
415 {
416         my ($self, $ele) = @_;
417         my $pkg = ref $self;
418     my $val = "${pkg}::valid";
419         return $val->{$ele} || $valid{$ele};
420 }
421
422 #
423 # generic AUTOLOAD for accessors
424 #
425 sub AUTOLOAD
426 {
427         no strict;
428         my $name = $AUTOLOAD;
429         return if $name =~ /::DESTROY$/;
430         $name =~ s/^.*:://o;
431
432         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
433
434         # this clever line of code creates a subroutine which takes over from autoload
435         # from OO Perl - Conway
436         *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}};
437        goto &$AUTOLOAD;
438
439 }
440
441 1;