2 # The RBN connection system
4 # Copyright (c) 2020 Dirk Koopman G1TLH
19 use Math::Round qw(nearest);
21 use Time::HiRes qw(clock_gettime CLOCK_REALTIME);
23 our @ISA = qw(DXChannel);
25 our $startup_delay = 5*60; # don't send anything out until this timer has expired
26 # this is to allow the feed to "warm up" with duplicates
27 # so that the "big rush" doesn't happen.
29 our $minspottime = 60*60; # the time between respots of a callsign - if a call is
30 # still being spotted (on the same freq) and it has been
31 # spotted before, it's spotted again after this time
32 # until the next minspottime has passed.
34 our $beacontime = 5*60; # same as minspottime, but for beacons (and shorter)
36 our $dwelltime = 6; # the amount of time to wait for duplicates before issuing
37 # a spot to the user (no doubt waiting with bated breath).
42 my $self = DXChannel::alloc(@_);
44 # routing, this must go out here to prevent race condx
48 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
56 $self->{lasttime} = $main::systime;
57 $self->{minspottime} = $minspottime;
58 $self->{beacontime} = $beacontime;
59 $self->{showstats} = 0;
66 my ($self, $line, $sort) = @_;
67 my $user = $self->{user};
68 my $call = $self->{call};
69 my $name = $user->{name};
70 my $dref = $self->{d};
71 my $spotref = $self->{spot};
74 my $host = $self->{conn}->peerhost;
76 $self->{hostname} = $host;
78 $self->{name} = $name ? $name : $call;
79 $self->state('prompt'); # a bit of room for further expansion, passwords etc
80 $self->{lang} = $user->lang || $main::lang || 'en';
81 if ($line =~ /host=/) {
82 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
83 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
85 ($h) = $line =~ /host=([\da..fA..F:]+)/;
86 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
88 $self->{hostname} = $h if $h;
90 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
91 $self->{consort} = $line; # save the connection type
93 LogDbg('DXCommand', "$call connected from $self->{hostname}");
95 # set some necessary flags on the user if they are connecting
96 $self->{registered} = 1;
97 # sort out privilege reduction
102 $nossid =~ s/-\d+$//;
104 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
105 || Filter::read_in('spots', $nossid, 0)
106 || Filter::read_in('spots', 'user_default', 0);
108 # clean up qra locators
109 my $qra = $user->qra;
110 $qra = undef if ($qra && !DXBearing::is_qra($qra));
112 my $lat = $user->lat;
113 my $long = $user->long;
114 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
118 $self->{inrushpreventor} = $main::systime + $startup_delay;
121 my @queue; # the queue of spots ready to send
128 my $spots = $self->{spot};
130 # save this for them's that need it
133 # remove leading and trailing spaces
140 my $tim = $main::systime;
143 dbg "RBN:RAW,$line" if isdbg('rbnraw');
144 return unless $line=~/^DX\s+de/;
146 my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
148 # fix up FT8 spots from 7001
149 $t = $u, $u = '' if !$t && is_ztime($u);
150 $t = $sort, $sort = '' if !$t && is_ztime($sort);
151 my $qra = $spd, $spd = '' if is_qra($spd);
154 $origin =~ s/\-(?:\d{1,2}\-)?\#$//; # get rid of all the crap we aren't interested in
160 dbg qq{or:$origin qr:$qrg ca:$call mo:$mode s:$s m:$m sp:$spd u:$u sort:$sort t:$t tx:$tx qra:$qra} if isdbg('rbn');
167 # fix up times for things like 'NXDXF B' etc
168 if ($tx && is_ztime($t)) {
177 if ($sort && $sort eq 'NCDXF') {
181 if ($sort && $sort eq 'BEACON') {
184 if ($mode =~ /^PSK/) {
187 if ($mode eq 'RTTY') {
191 # The main de-duping key is [call, $frequency], but we probe a bit around that frequency to find a
192 # range of concurrent frequencies that might be in play.
194 # The key to this is deducing the true callsign by "majority voting" (the greater the number of spotters
195 # the more effective this is) together with some lexical analsys probably in conjuction with DXSpider
196 # data sources (for singleton spots) to then generate a "centre" from and to zone (whatever that will mean if it isn't the usual one)
197 # and some heuristical "Kwalitee" rating given distance from the zone centres of spotter, recipient user
198 # and spotted. A map can be generated once per user and spotter as they are essentially mostly static.
199 # The spotted will only get a coarse position unless other info is available. Programs that parse
200 # DX bulletins and the online data online databases could be be used and then cached.
202 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
205 # Clearly this will only work in the 'mojo' branch of DXSpider where it is possible to pass off external
206 # data requests to ephemeral or semi resident forked processes that do any grunt work and the main
207 # process to just the standard "message passing" which has been shown to be able to sustain over 5000
208 # per second (limited by the test program's output and network speed, rather than DXSpider's handling).
210 my $nqrg = nearest(1, $qrg); # normalised to nearest Khz
211 my $sp = "$call|$nqrg"; # hopefully the skimmers will be calibrated at least this well!
212 my $spp = sprintf("$call|%d", $nqrg+1); # but, clearly, my hopes are rudely dashed
213 my $spm = sprintf("$call|%d", $nqrg-1); # in BOTH directions!
216 my $spot = $spots->{$sp};
217 $spot = $spots->{$spp}, $sp = $spp, dbg('RBN: SPP using $spp for $sp') if !$spot && exists $spots->{$spp};
218 $spot = $spots->{$spm}, $sp = $spm, dbg('RBN: SPM using $spm for $sp') if !$spot && exists $spots->{$spm};
221 # if we have one and there is only one slot and that slot's time isn't expired for respot then return
223 if ($spot && ref $spot) {
225 unless ($self->{minspottime} > 0 && $tim - $spot->[0] >= $self->{minspottime}) {
226 dbg("RBN: key: '$sp' call: $call qrg: $qrg DUPE \@ ". atime(int $spot->[0])) if isdbg('rbn');
230 dbg("RBN: key: '$sp' RESPOTTING call: $call qrg: $qrg last seen \@ ". atime(int $spot->[0])) if isdbg('rbn');
234 dbg("RBN: key '$sp' = '$spot' not ref");
238 # here we either have an existing spot record buildup on the go, or we need to create the first one
240 $spot = [clock_gettime(CLOCK_REALTIME)];
241 $spots->{$sp} = $spot;
242 dbg("RBN: key: '$sp' call: $call qrg: $qrg NEW") if isdbg('rbn');
245 # add me to the display queue unless we are waiting for initial in rush to finish
246 return unless $self->{inrushpreventor} < $main::systime;
247 push @{$self->{queue}}, $sp if @$spot == 1; # queue the KEY (not the record)
249 # build up a new record and store it in the buildup
250 # deal with the unix time
251 my ($hh,$mm) = $t =~ /(\d\d)(\d\d)Z$/;
252 my $utz = $hh*3600 + $mm*60 + $main::systime_daystart; # possible issue with late spot from previous day
253 $utz -= 86400 if $utz > $tim+3600; # too far ahead, drag it back one day
255 # create record and add into the buildup
256 my $r = [$origin, nearest(.1, $qrg), $call, $mode, $s, $t, $utz, $respot, $u];
257 dbg("RBN: key: '$sp' ADD RECORD call: $call qrg: $qrg origin: $origin") if isdbg('rbn');
261 # At this point we run the queue to see if anything can be sent onwards to the punter
262 my $now = clock_gettime(CLOCK_REALTIME);
264 # now run the waiting queue which just contains KEYS ($call|$qrg)
265 foreach $sp (@{$self->{queue}}) {
266 my $cand = $spots->{$sp};
267 unless ($cand && $cand->[0]) {
268 dbg "RBN Cand " . ($cand ? 'def' : 'undef') . " [0] " . ($cand->[0] ? 'def' : 'undef') . " dwell $dwelltime";
271 if ($now >= $cand->[0] + $dwelltime ) {
272 # we have a candidate, create qualitee value(s);
273 unless (@$cand > 1) {
274 dbg "RBN: QUEUE key '$sp' MISSING RECORDS " . dd($cand) if isdbg 'rbn';
275 shift @{$self->{queue}};
278 my $savedtime = shift @$cand; # save the start time
280 my $quality = @$cand;
281 $quality = 9 if $quality > 9;
282 $quality = "Q:$quality";
283 if (isdbg('progress')) {
284 my $s = "RBN: SPOT key: '$sp' = $r->[2] on $r->[1] \@ $r->[5] $quality";
285 $s .= " route: $self->{call}";
289 send_dx_spot($self, $quality, $cand);
291 # clear out the data and make this now just "spotted", but no further action required until respot time
292 dbg "RBN: QUEUE key '$sp' cleared" if isdbg 'rbn';
294 $spots->{$sp} = [$savedtime];
295 shift @{$self->{queue}};
297 dbg sprintf("RBN: QUEUE key: '$sp' SEND time not yet reached %.1f secs left", $spot->[0] + $dwelltime - $now) if isdbg 'rbnqueue';
303 dbg "RBN:DATA,$line" if isdbg('rbn');
306 # # periodic clearing out of the two caches
307 if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
310 while (my ($k,$v) = each %{$spots}) {
311 if ($tim - $v->[0] > $self->{minspottime}*2) {
319 dbg "RBN:ADMIN,spot cache: $removed removed $count remain"; # if isdbg('rbn');
320 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
321 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
322 $self->{last} = int($tim / 60) * 60;
331 # we should get the spot record minus the time, so just an array of record (arrays)
338 # $r = [$origin, $qrg, $call, $mode, $s, $utz, $respot];
340 my $mode = $spot->[0]->[3]; # as all the modes will be the same;
342 my @dxchan = DXChannel::get_all();
344 foreach my $dxchan (@dxchan) {
345 next unless $dxchan->is_user;
346 my $user = $dxchan->{user};
347 next unless $user && $user->wantrbn;
349 # does this user want this sort of spot at all?
351 ++$want if $user->wantbeacon && $mode =~ /^BCN|DXF/;
352 ++$want if $user->wantcw && $mode =~ /^CW/;
353 ++$want if $user->wantrtty && $mode =~ /^RTT/;
354 ++$want if $user->wantpsk && $mode =~ /^PSK/;
355 ++$want if $user->wantcw && $mode =~ /^CW/;
356 ++$want if $user->wantft && $mode =~ /^FT/;
357 ++$want unless $want; # send everything if nothing is selected.
361 # send one spot to one user out of the ones that we have
362 $self->dx_spot($dxchan, $quality, $spot) if $want;
373 my $strength = 100; # because it could if we talk about FTx
381 foreach my $r (@$spot) {
382 # $r = [$origin, $qrg, $call, $mode, $s, $t, $utz, $respot, $qra];
383 # Spot::prepare($qrg, $call, $utz, $comment, $origin);
385 my $comment = sprintf "%-3s %2ddB $quality", $r->[3], $r->[4];
386 $respot = 1 if $r->[7];
387 $qra = $r->[8] if !$qra && $r->[8] && is_qra($r->[8]);
389 my @s = Spot::prepare($r->[1], $r->[2], $r->[6], $comment, $r->[0]);
391 ++$zone{$s[11]}; # save the spotter's zone
392 ++$qrg{$s[0]}; # and the qrg
394 # save the highest strength one
395 if ($r->[4] < $strength) {
398 dbg("RBN: STRENGTH call: $s[1] qrg: $s[0] origin: $s[4] dB: $r->[4]") if isdbg 'rbn';
403 if ($dxchan->{rbnfilter}) {
404 ($filter, undef) = $dxchan->{rbnfilter}->it(\@s);
407 dbg("RBN: FILTERED call: $s[1] qrg: $s[0] origin: $s[4] dB: $r->[4]") if isdbg 'rbn';
415 # create a zone list of spotters
416 delete $zone{$saver->[11]}; # remove this spotter's zone (leaving all the other zones)
417 my $z = join ',', sort {$a <=> $b} keys %zone;
419 # determine the most likely qrg and then set it
423 while (my ($k, $v) = each %qrg) {
424 $fk = $k, $mv = $v if $v > $mv;
428 $saver->[3] .= '*' if $c > 1;
429 $saver->[3] .= '+' if $respot;
430 $saver->[3] .= " Z:$z" if $z;
432 dbg("RBN: SENDING call: $saver->[1] qrg: $saver->[0] origin: $saver->[4] $saver->[3]") if isdbg 'rbn';
433 if ($dxchan->{ve7cc}) {
434 my $call = $saver->[1];
436 $buf = VE7CC::dx_spot($dxchan, @$saver);
439 $buf = $dxchan->format_dx_spot(@$saver);
442 $dxchan->local_send('N', $buf);
445 my $user = DXUser::get_current($saver->[1]) || DXUser->new($saver->[1]);
446 unless ($user->qra && is_qra($user->qra)) {
448 dbg("RBN: update qra on $saver->[1] to $qra");