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