4 # Copyright (c) - Dirk Koopman G1TLH
21 use vars qw($VERSION $BRANCH);
22 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
23 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
24 $main::build += $VERSION;
25 $main::branch += $BRANCH;
27 use vars qw($db %prefix_loc %pre %cache $misses $hits $matchtotal $lasttime);
29 $db = undef; # the DB_File handle
30 %prefix_loc = (); # the meat of the info
31 %pre = (); # the prefix list
32 %cache = (); # a runtime cache of matched prefixes
33 $lasttime = 0; # last time this cache was cleared
34 $hits = $misses = $matchtotal = 1; # cache stats
36 #my $cachefn = "$main::data/prefix_cache";
51 # tie the main prefix database
52 $db = tie(%pre, "DB_File", undef, O_RDWR|O_CREAT, 0664, $DB_BTREE) or confess "can't tie \%pre ($!)";
54 do "$main::data/prefix_data.pl" if !$out;
57 # tie the prefix cache
58 # tie (%cache, "DB_File", $cachefn, O_RDWR|O_CREAT, 0664, $DB_HASH) or confess "can't tie prefix cache to $cachefn $!";
65 my $fh = new IO::File;
66 my $fn = "$main::data/prefix_data.pl";
68 confess "Prefix system not started" if !$db;
71 rename "$fn.oooo", "$fn.ooooo" if -e "$fn.oooo";
72 rename "$fn.ooo", "$fn.oooo" if -e "$fn.ooo";
73 rename "$fn.oo", "$fn.ooo" if -e "$fn.oo";
74 rename "$fn.o", "$fn.oo" if -e "$fn.o";
75 rename "$fn", "$fn.o" if -e "$fn";
77 $fh->open(">$fn") or die "Can't open $fn ($!)";
79 # prefix location data
80 $fh->print("%prefix_loc = (\n");
81 foreach $l (sort {$a <=> $b} keys %prefix_loc) {
82 my $r = $prefix_loc{$l};
83 $fh->printf(" $l => bless( { name => '%s', dxcc => %d, itu => %d, utcoff => %d, lat => %f, long => %f }, 'Prefix'),\n",
84 $r->{name}, $r->{dxcc}, $r->{itu}, $r->{cq}, $r->{utcoff}, $r->{lat}, $r->{long});
89 $fh->print("%pre = (\n");
90 foreach $k (sort keys %pre) {
91 $fh->print(" '$k' => [");
92 my @list = @{$pre{$k}};
99 $fh->print("$str ],\n");
106 # what you get is a list that looks like:-
108 # prefix => @list of blessed references to prefix_locs
110 # This routine will only do what you ask for, if you wish to be intelligent
111 # then that is YOUR problem!
119 return () if $db->seq($gotkey, $ref, R_CURSOR);
120 return () if $key ne substr $gotkey, 0, length $key;
122 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
126 # get the next key that matches, this assumes that you have done a 'get' first
135 return () if $db->seq($gotkey, $ref, R_NEXT);
136 return () if $key ne substr $gotkey, 0, length $key;
138 return ($gotkey, map { $prefix_loc{$_} } split ',', $ref);
142 # search for the nearest match of a prefix string (starting
143 # from the RH end of the string passed)
151 for (my $i = length $pref; $i; $i--) {
153 my $s = substr($pref, 0, $i);
157 if (isdbg('prefix')) {
158 my $percent = sprintf "%.1f", $hits * 100 / $misses;
159 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
166 if (isdbg('prefix')) {
167 my $part = $out[0] || "*";
168 $part .= '*' unless $part eq '*' || $part eq $s;
169 dbg("Partial prefix: $pref $s $part" );
171 if (@out && $out[0] eq $s) {
172 $cache{$_} = [ @out ] for @partials;
181 # extract a 'prefix' from a callsign, in other words the largest entity that will
182 # obtain a result from the prefix table.
184 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
185 # G1TLH/VO1 (should) return VO1
190 my $calls = uc shift;
196 # clear out the cache periodically to stop it growing for ever.
197 if ($main::systime - $lasttime >= 20*60) {
198 if (isdbg('prefix')) {
199 my $percent = sprintf "%.1f", $hits * 100 / $misses;
200 dbg("Prefix Cache Cleared, Hits: $hits/$misses of $matchtotal = $percent\%") ;
203 $lasttime = $main::systime;
204 $hits = $matchtotal = 0;
207 LM: foreach $call (split /,/, $calls) {
209 # first check if the whole thing succeeds either because it is cached
210 # or because it simply is a stored prefix as callsign (or even a prefix)
212 my $p = $cache{$call};
216 if (isdbg('prefix')) {
217 my $percent = sprintf "%.1f", $hits * 100 / $misses;
218 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
224 if (@nout && $nout[0] eq $call) {
226 $cache{$call} = \@nout;
227 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
233 # now split the call into parts if required
234 @parts = ($call =~ '/') ? split('/', $call) : ($call);
235 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
237 # remove any /0-9 /P /A /M /MM /AM suffixes etc
239 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
241 # can we resolve them by direct lookup
242 my $s = join('/', @parts);
244 if (@nout && $nout[0] eq $s) {
245 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
247 $cache{$call} = \@nout;
252 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
254 # at this point we should have two or three parts
255 # if it is three parts then join the first and last parts together
258 # first deal with prefix/x00xx/single letter things
259 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
260 @nout = matchprefix($parts[0]);
262 my $s = join('/', $nout[0], $parts[2]);
264 if (@try && $try[0] eq $s) {
265 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
267 $cache{$call} = \@try;
272 # if the second part is a callsign and the last part is one letter
273 if (is_callsign($parts[1]) && length $parts[2] == 1) {
279 # if it is a two parter
282 # try it as it is as compound, taking the first part as the prefix
283 @nout = matchprefix($parts[0]);
285 my $s = join('/', $nout[0], $parts[1]);
287 if (@try && $try[0] eq $s) {
288 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
290 $cache{$call} = \@try;
297 # remove the problematic /J suffix
298 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
302 @nout = matchprefix($parts[0]);
304 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
306 $cache{$call} = \@nout;
315 L1: for ($n = 0; $n < @parts; $n++) {
318 for ($i = $k = 0; $i < @parts; $i++) {
319 next if $checked[$i];
321 if (!$sp || length $p < length $sp) {
322 dbg("try part: $p") if isdbg('prefix');
328 $sp =~ s/-\d+$//; # remove any SSID
330 # now start to resolve it from the right hand end
331 @nout = matchprefix($sp);
333 # try and search for it in the descriptions as
334 # a whole callsign if it has multiple parts and the output
335 # is more two long, this should catch things like
336 # FR5DX/T without having to explicitly stick it into
341 $parts[$k] = $nout[0];
342 my $try = join('/', @parts);
344 if (isdbg('prefix')) {
345 my $part = $try[0] || "*";
346 $part .= '*' unless $part eq '*' || $part eq $try;
347 dbg("Compound prefix: $try $part" );
349 if (@try && $try eq $try[0]) {
351 $cache{$call} = \@try;
355 $cache{$call} = \@nout;
360 $cache{$call} = \@nout;
368 @nout = matchprefix('Q');
370 $cache{$call} = \@nout;
374 if (isdbg('prefixdata')) {
375 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
382 lat => '0,Latitude,slat',
383 long => '0,Longitude,slong',
388 utcoff => '0,UTC offset',
389 cont => '0,Continent',
396 my $name = $AUTOLOAD;
398 return if $name =~ /::DESTROY$/;
401 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
402 # this clever line of code creates a subroutine which takes over from autoload
403 # from OO Perl - Conway
404 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
406 $self->{$name} = shift;
408 return $self->{$name};
413 # return a prompt for a field
418 my ($self, $ele) = @_;