always set flags field to here on new
[spider.git] / perl / Route.pm
1 #!/usr/bin/perl
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 # $Id$
13
14
15 package Route;
16
17 use DXDebug;
18 use DXChannel;
19 use Prefix;
20
21 use strict;
22
23 use vars qw(%list %valid $filterdef);
24
25 %valid = (
26                   call => "0,Callsign",
27                   flags => "0,Flags,phex",
28                   dxcc => '0,Country Code',
29                   itu => '0,ITU Zone',
30                   cq => '0,CQ Zone',
31                  );
32
33 $filterdef = bless ([
34                           # tag, sort, field, priv, special parser 
35                           ['channel', 'c', 0],
36                           ['channel_dxcc', 'n', 1],
37                           ['channel_itu', 'n', 2],
38                           ['channel_zone', 'n', 3],
39                           ['call', 'c', 4],
40                           ['call_dxcc', 'n', 5],
41                           ['call_itu', 'n', 6],
42                           ['call_zone', 'n', 7],
43                          ], 'Filter::Cmd');
44
45
46 sub new
47 {
48         my ($pkg, $call) = @_;
49         $pkg = ref $pkg if ref $pkg;
50
51         my $self = bless {call => $call}, $pkg;
52         dbg('routelow', "create $pkg with $call");
53
54         # add in all the dxcc, itu, zone info
55         my @dxcc = Prefix::extract($call);
56         if (@dxcc > 0) {
57                 $self->{dxcc} = $dxcc[1]->dxcc;
58                 $self->{itu} = $dxcc[1]->itu;
59                 $self->{cq} = $dxcc[1]->cq;                                             
60         }
61         $self->{flags} = here(1);
62         
63         return $self; 
64 }
65
66 #
67 # get a callsign from a passed reference or a string
68 #
69
70 sub _getcall
71 {
72         my $self = shift;
73         my $thingy = shift;
74         $thingy = $self unless $thingy;
75         $thingy = $thingy->call if ref $thingy;
76         $thingy = uc $thingy if $thingy;
77         return $thingy;
78 }
79
80
81 # add and delete a callsign to/from a list
82 #
83
84 sub _addlist
85 {
86         my $self = shift;
87         my $field = shift;
88         foreach my $c (@_) {
89                 my $call = _getcall($c);
90                 unless (grep {$_ eq $call} @{$self->{$field}}) {
91                         push @{$self->{$field}}, $call;
92                         dbg('routelow', ref($self) . " adding $call to " . $self->{call} . "->\{$field\}");
93                 }
94         }
95         return $self->{$field};
96 }
97
98 sub _dellist
99 {
100         my $self = shift;
101         my $field = shift;
102         foreach my $c (@_) {
103                 my $call = _getcall($c);
104                 if (grep {$_ eq $call} @{$self->{$field}}) {
105                         $self->{$field} = [ grep {$_ ne $call} @{$self->{$field}} ];
106                         dbg('routelow', ref($self) . " deleting $call from " . $self->{call} . "->\{$field\}");
107                 }
108         }
109         return $self->{$field};
110 }
111
112 #
113 # flag field constructors/enquirers
114 #
115
116 sub here
117 {
118         my $self = shift;
119         my $r = shift;
120         return $self ? 2 : 0 unless ref $self;
121         return ($self->{flags} & 2) ? 1 : 0 unless $r;
122         $self->{flags} = (($self->{flags} & ~2) | ($r ? 1 : 0));
123         return $r ? 1 : 0;
124 }
125
126 sub conf
127 {
128         my $self = shift;
129         my $r = shift;
130         return $self ? 1 : 0 unless ref $self;
131         return ($self->{flags} & 1) ? 1 : 0 unless $r;
132         $self->{flags} = (($self->{flags} & ~1) | ($r ? 1 : 0));
133         return $r ? 1 : 0;
134 }
135
136 sub parents
137 {
138         my $self = shift;
139         return @{$self->{parent}};
140 }
141
142
143 # display routines
144 #
145
146 sub user_call
147 {
148         my $self = shift;
149         my $call = sprintf "%s", $self->{call};
150         return $self->here ? "$call" : "($call)";
151 }
152
153 sub config
154 {
155         my $self = shift;
156         my $nodes_only = shift;
157         my $level = shift;
158         my @out;
159         my $line;
160         my $call = $self->user_call;
161         my $printit = 1;
162
163         # allow ranges
164         if (@_) {
165                 $printit = grep $call =~ m|$_|, @_;
166         }
167
168         if ($printit) {
169                 $line = ' ' x ($level*2) . "$call";
170                 $call = ' ' x length $call; 
171                 unless ($nodes_only) {
172                         if (@{$self->{users}}) {
173                                 $line .= '->';
174                                 foreach my $ucall (sort @{$self->{users}}) {
175                                         my $uref = Route::User::get($ucall);
176                                         my $c;
177                                         if ($uref) {
178                                                 $c = $uref->user_call;
179                                         } else {
180                                                 $c = "$ucall?";
181                                         }
182                                         if ((length $line) + (length $c) + 1 < 79) {
183                                                 $line .= $c . ' ';
184                                         } else {
185                                                 $line =~ s/\s+$//;
186                                                 push @out, $line;
187                                                 $line = ' ' x ($level*2) . "$call->$c ";
188                                         }
189                                 }
190                         }
191                 }
192                 $line =~ s/->$//g;
193                 $line =~ s/\s+$//;
194                 push @out, $line if length $line;
195         }
196         
197         foreach my $ncall (sort @{$self->{nodes}}) {
198                 my $nref = Route::Node::get($ncall);
199
200                 if ($nref) {
201                         my $c = $nref->user_call;
202                         push @out, $nref->config($nodes_only, $level+1, @_);
203                 } else {
204                         push @out, ' ' x (($level+1)*2)  . "$ncall?" if @_ == 0 || (@_ && grep $ncall =~ m|$_|, @_); 
205                 }
206         }
207
208         return @out;
209 }
210
211 sub cluster
212 {
213         my $nodes = Route::Node::count();
214         my $tot = Route::User::count();
215         my $users = scalar DXCommandmode::get_all();
216         my $maxusers = Route::User::max();
217         my $uptime = main::uptime();
218         
219         return " $nodes nodes, $users local / $tot total users  Max users $maxusers  Uptime $uptime";
220 }
221
222 #
223 # routing things
224 #
225
226 sub get
227 {
228         my $call = shift;
229         return Route::Node::get($call) || Route::User::get($call);
230 }
231
232 # find all the possible dxchannels which this object might be on
233 sub alldxchan
234 {
235         my $self = shift;
236         my @dxchan;
237         my $dxchan = DXChannel->get($self->{call});
238         push @dxchan, $dxchan if $dxchan;
239         
240         # it isn't, build up a list of dxchannels and possible ping times 
241         # for all the candidates.
242         foreach my $p (@{$self->{parent}}) {
243                 my $dxchan = DXChannel->get($p);
244                 if ($dxchan) {
245                         push @dxchan, $dxchan unless grep $dxchan == $_, @dxchan;
246                 } else {
247                         next if $p eq $main::mycall; # the root
248                         my $ref = $self->get($p);
249                         push @dxchan, $ref->alldxchan if $ref;
250                 }
251         }
252         return @dxchan;
253 }
254
255 sub dxchan
256 {
257         my $self = shift;
258         my $dxchan = DXChannel->get($self->{call});
259         return $dxchan if $dxchan;
260         
261         my @dxchan = $self->alldxchan;
262         return undef unless @dxchan;
263         
264         # determine the minimum ping channel
265         my $minping = 99999999;
266         foreach my $dxc (@dxchan) {
267                 my $p = $dxc->pingave;
268                 if (defined $p  && $p < $minping) {
269                         $minping = $p;
270                         $dxchan = $dxc;
271                 }
272         }
273         $dxchan = shift @dxchan unless $dxchan;
274         return $dxchan;
275 }
276
277 #
278 # track destruction
279 #
280
281 sub DESTROY
282 {
283         my $self = shift;
284         my $pkg = ref $self;
285         
286         dbg('routelow', "$pkg $self->{call} destroyed");
287 }
288
289 no strict;
290 #
291 # return a list of valid elements 
292
293
294 sub fields
295 {
296         my $pkg = shift;
297         $pkg = ref $pkg if ref $pkg;
298     my $val = "${pkg}::valid";
299         my @out = keys %$val;
300         push @out, keys %valid;
301         return @out;
302 }
303
304 #
305 # return a prompt for a field
306 #
307
308 sub field_prompt
309
310         my ($self, $ele) = @_;
311         my $pkg = ref $self;
312     my $val = "${pkg}::valid";
313         return $val->{$ele} || $valid{$ele};
314 }
315
316 #
317 # generic AUTOLOAD for accessors
318 #
319 sub AUTOLOAD
320 {
321         my $self = shift;
322         my $name = $AUTOLOAD;
323         return if $name =~ /::DESTROY$/;
324         $name =~ s/.*:://o;
325   
326         confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
327
328         # this clever line of code creates a subroutine which takes over from autoload
329         # from OO Perl - Conway
330 #       *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
331     @_ ? $self->{$name} = shift : $self->{$name} ;
332 }
333
334 1;