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)
152 for (my $i = length $pref; $i; $i--) {
154 my $s = substr($pref, 0, $i);
158 if (isdbg('prefix')) {
159 my $percent = sprintf "%.1f", $hits * 100 / $misses;
160 dbg("Partial Prefix Cache Hit: $s Hits: $hits/$misses of $matchtotal = $percent\%");
167 if (isdbg('prefix')) {
168 my $part = $out[0] || "*";
169 $part .= '*' unless $part eq '*' || $part eq $s;
170 dbg("Partial prefix: $pref $s $part" );
172 if (@out && $out[0] eq $s) {
173 $cache{$_} = \@out for @partials;
182 # extract a 'prefix' from a callsign, in other words the largest entity that will
183 # obtain a result from the prefix table.
185 # This is done by repeated probing, callsigns of the type VO1/G1TLH or
186 # G1TLH/VO1 (should) return VO1
191 my $calls = uc shift;
197 # clear out the cache periodically to stop it growing for ever.
198 if ($main::systime - $lasttime >= 20*60) {
199 if (isdbg('prefix')) {
200 my $percent = sprintf "%.1f", $hits * 100 / $misses;
201 dbg("Prefix Cache Cleared, Hits: $hits/$misses of $matchtotal = $percent\%") ;
204 $lasttime = $main::systime;
205 $hits = $matchtotal = 0;
208 LM: foreach $call (split /,/, $calls) {
210 # first check if the whole thing succeeds either because it is cached
211 # or because it simply is a stored prefix as callsign (or even a prefix)
213 my $p = $cache{$call};
217 if (isdbg('prefix')) {
218 my $percent = sprintf "%.1f", $hits * 100 / $misses;
219 dbg("Prefix Cache Hit: $call Hits: $hits/$misses of $matchtotal = $percent\%");
225 if (@nout && $nout[0] eq $call) {
227 $cache{$call} = \@nout;
228 dbg("got exact prefix: $nout[0]") if isdbg('prefix');
234 # now split the call into parts if required
235 @parts = ($call =~ '/') ? split('/', $call) : ($call);
236 dbg("Parts: $call = " . join(' ', @parts)) if isdbg('prefix');
238 # remove any /0-9 /P /A /M /MM /AM suffixes etc
240 @parts = grep { !/^\d+$/ && !/^[PABM]$/ && !/^(?:|AM|MM|BCN|JOTA|SIX|WEB|NET|Q\w+)$/; } @parts;
242 # can we resolve them by direct lookup
243 my $s = join('/', @parts);
245 if (@nout && $nout[0] eq $s) {
246 dbg("got exact multipart prefix: $call $s") if isdbg('prefix');
248 $cache{$call} = \@nout;
253 dbg("Parts now: $call = " . join(' ', @parts)) if isdbg('prefix');
255 # at this point we should have two or three parts
256 # if it is three parts then join the first and last parts together
259 # first deal with prefix/x00xx/single letter things
260 if (@parts == 3 && length $parts[0] <= length $parts[1]) {
261 @nout = matchprefix($parts[0]);
263 my $s = join('/', $nout[0], $parts[2]);
265 if (@try && $try[0] eq $s) {
266 dbg("got 3 part prefix: $call $s") if isdbg('prefix');
268 $cache{$call} = \@try;
273 # if the second part is a callsign and the last part is one letter
274 if (is_callsign($parts[1]) && length $parts[2] == 1) {
280 # if it is a two parter
283 # try it as it is as compound, taking the first part as the prefix
284 @nout = matchprefix($parts[0]);
286 my $s = join('/', $nout[0], $parts[1]);
288 if (@try && $try[0] eq $s) {
289 dbg("got 2 part prefix: $call $s") if isdbg('prefix');
291 $cache{$call} = \@try;
298 # remove the problematic /J suffix
299 pop @parts if @parts > 1 && $parts[$#parts] eq 'J';
303 @nout = matchprefix($parts[0]);
305 dbg("got prefix: $call = $nout[0]") if isdbg('prefix');
307 $cache{$call} = \@nout;
316 L1: for ($n = 0; $n < @parts; $n++) {
319 for ($i = $k = 0; $i < @parts; $i++) {
320 next if $checked[$i];
322 if (!$sp || length $p < length $sp) {
323 dbg("try part: $p") if isdbg('prefix');
329 $sp =~ s/-\d+$//; # remove any SSID
331 # now start to resolve it from the right hand end
332 @nout = matchprefix($sp);
334 # try and search for it in the descriptions as
335 # a whole callsign if it has multiple parts and the output
336 # is more two long, this should catch things like
337 # FR5DX/T without having to explicitly stick it into
342 $parts[$k] = $nout[0];
343 my $try = join('/', @parts);
345 if (isdbg('prefix')) {
346 my $part = $try[0] || "*";
347 $part .= '*' unless $part eq '*' || $part eq $try;
348 dbg("Compound prefix: $try $part" );
350 if (@try && $try eq $try[0]) {
352 $cache{$call} = \@try;
356 $cache{$call} = \@nout;
361 $cache{$call} = \@nout;
369 @nout = matchprefix('Q');
371 $cache{$call} = \@nout;
375 if (isdbg('prefixdata')) {
376 my $dd = new Data::Dumper([ \@out ], [qw(@out)]);
383 lat => '0,Latitude,slat',
384 long => '0,Longitude,slong',
389 utcoff => '0,UTC offset',
390 cont => '0,Continent',
397 my $name = $AUTOLOAD;
399 return if $name =~ /::DESTROY$/;
402 confess "Non-existant field '$AUTOLOAD'" if !$valid{$name};
403 # this clever line of code creates a subroutine which takes over from autoload
404 # from OO Perl - Conway
405 *{$AUTOLOAD} = sub {@_ > 1 ? $_[0]->{$name} = $_[1] : $_[0]->{$name}} ;
407 $self->{$name} = shift;
409 return $self->{$name};
414 # return a prompt for a field
419 my ($self, $ele) = @_;