+ # now run the waiting queue which just contains KEYS ($call|$qrg)
+ foreach my $sp (keys %{$dxchan->{queue}}) {
+ my $cand = $spots->{$sp};
+ ++$items;
+
+ unless ($cand && $cand->[CTime]) {
+ dbg "RBN Cand $sp " . ($cand ? 'def' : 'undef') . " [CTime] " . ($cand->[CTime] ? 'def' : 'undef') . " dwell $dwelltime";
+ delete $spots->{$sp};
+ delete $dxchan->{queue}->{$sp}; # remove
+ next;
+ }
+
+ my $ctime = $cand->[CTime];
+ my $quality = @$cand - CData;
+ my $dwellsecs = $now - $ctime;
+ if ($quality >= $maxqual || $dwellsecs >= $dwelltime || $dwellsecs >= $limbotime) {
+ # we have a candidate, create qualitee value(s);
+ unless (@$cand > CData) {
+ dbg "RBN: QUEUE key '$sp' MISSING RECORDS, IGNORED" . dd($cand) if isdbg 'rbnqueue';
+ delete $spots->{$sp}; # don't remember it either - this means that a spot HAS to come in with sufficient spotters to be processed.
+ delete $dxchan->{queue}->{$sp};
+ next;
+ }
+ dbg "RBN: QUEUE PROCESSING key: '$sp' $now >= $cand->[CTime]" if isdbg 'rbnqueue';
+ my $spotters = $quality;
+
+ # dump it and remove it from the queue if it is of unadequate quality, but only if it is no longer in Limbo and can be reasonably passed on to its demise
+ my $r = $cand->[CData];
+ if ($dwellsecs > $limbotime && $quality < $minqual) {
+ if ( $rbnskim && isdbg('rbnskim')) {
+ $r = $cand->[CData];
+ if ($r) {
+ my $lastin = difft($ctime, $now, 2);
+ my $s = "RBN:SKIM time in Limbo exceeded DUMPED (lastin: $lastin Q:$quality < Q:$minqual) key: '$sp' = $r->[RCall] on $r->[RQrg] by $r->[ROrigin] \@ $r->[RTime] route: $dxchan->{call}";
+ dbg($s);
+ }
+ }
+ delete $spots->{$sp}; # don't remember it either - this means that a spot HAS to come in with sufficient spotters to be processed.
+ delete $dxchan->{queue}->{$sp};
+ next;
+ }
+
+ # we have a possible removal from Limbo, check for more than one skimmer and reset the quality if required
+ # DOES THIS TEST CAUSE RACES?
+ if (!$r->[Respot] && $quality >= $minqual && $dwellsecs > $dwelltime+1) {
+
+ # because we don't need to check for repeats by the same skimmer in the normal case, we do here
+ my %seen;
+ my @origin;
+ foreach my $wr (@$cand) {
+ next unless ref $wr;
+ push @origin, $wr->[ROrigin];
+ if (exists $seen{$wr->[ROrigin]}) {
+ next;
+ }
+ $seen{$wr->[ROrigin]} = $wr;
+ }
+ # reset the quality to ignore dupes
+ my $oq = $quality;
+ $quality = keys %seen;
+ if ($quality >= $minqual) {
+ if ( $rbnskim && isdbg('rbnskim')) {
+ my $lastin = difft($ctime, $now, 2);
+ my $sk = join ' ', keys %seen;
+ my $or = join ' ', @origin;
+ my $s = "RBN:SKIM promoted from Limbo - key: '$sp' (lastin: $lastin Q now: $quality was $oq skimmers now: $sk";
+ $s .= " was $or" if $or ne $sk;
+ $s .= ')';
+ dbg($s);
+ }
+ } elsif ($oq != $quality) {
+ if ( $rbnskim && isdbg('rbnskim')) {
+ my $lastin = difft($ctime, $now, 2);
+ my $sk = join ' ', keys %seen;
+ my $or = join ' ', @origin;
+ my $s = "RBN:SKIM quality reset key: '$sp' (lastin: $lastin Q now: $quality was $oq skimmers now: $sk was: $or)";
+ dbg($s);
+ }
+ # remove the excess
+ my @ncand = (@$cand[CTime, CQual], values %seen);
+ $spots->{$sp} = \@ncand;
+ }
+ }
+
+ # we now kick this spot into Limbo
+ if ($quality < $minqual) {
+ next;
+ }
+
+ $quality = 9 if $quality > 9;
+ $cand->[CQual] = $quality if $quality > $cand->[CQual];
+
+ # this scores each candidate according to its skimmer's QRG score (i.e. how often it agrees with its peers)
+ # what happens is hash of all QRGs in candidates are incremented by that skimmer's reputation for "accuracy"
+ # or, more exactly, past agreement with the consensus. This score can be from -5 -> +5.
+ my %qrg = ();
+ my $skimmer;
+ my $sk;
+ my $band;
+ my %seen = ();
+ foreach $r (@$cand) {
+ next unless ref $r;
+ if (exists $seen{$r->[ROrigin]}) {
+ $r = 0;
+ next;
+ }
+ $seen{$r->[ROrigin]} = 1;
+ $band ||= int $r->[RQrg] / 1000;
+ $sk = "SKIM|$r->[ROrigin]|$band"; # thus only once per set of candidates
+ $skimmer = $spots->{$sk};
+ unless ($skimmer) {
+ $skimmer = $spots->{$sk} = [1, 0, 0, $now, []]; # this first time, this new skimmer gets the benefit of the doubt on frequency.
+ dbg("RBN:SKIM new slot $sk " . $json->encode($skimmer)) if $rbnskim && isdbg('rbnskim');
+ }
+ $qrg{$r->[RQrg]} += ($skimmer->[DScore] || 1);
+ }
+
+ # determine the most likely qrg and then set it - NOTE (-)ve votes, generated by the skimmer scoring system above, are ignored
+ my @deviant;
+ my $c = 0;
+ my $mv = 0;
+ my $qrg = 0;
+ while (my ($k, $votes) = each %qrg) {
+ if ($votes >= $mv) {
+ $qrg = $k;
+ $mv = $votes;
+ }
+ ++$c;
+ }
+
+ # Ignore possible spots with 0 QRG score - as determined by the skimmer scoring system above - as they are likely to be wrong
+ unless ($qrg > 0) {
+ if ( $rbnskim && isdbg('rbnskim')) {
+ my $keys;
+ while (my ($k, $v) = (each %qrg)) {
+ $keys .= "$k=>$v, ";
+ }
+ $keys =~ /,\s*$/;
+ my $i = 0;
+ foreach $r (@$cand) {
+ next unless $r && ref $r;
+ dbg "RBN:SKIM cand $i QRG likely wrong from '$sp' = $r->[RCall] on $r->[RQrg] by $r->[ROrigin] \@ $r->[RTime] (qrgs: $keys c: $c) route: $dxchan->{call}, ignored";
+ ++$i;
+ }
+ }
+ delete $spots->{$sp}; # get rid
+ delete $dxchan->{queue}->{$sp};
+ next;
+ }
+
+ # detemine and spit out the deviants. Then adjust the scores according to whether it is a deviant or good
+ # NOTE: deviant nodes can become good (or less bad), and good nodes bad (or less good) on each spot that
+ # they generate. This is based solely on each skimmer's agreement (or not) with the "consensus" score generated
+ # above ($qrg). The resultant score + good + bad is stored per band and will be used the next time a spot
+ # appears on this band from each skimmer.
+ foreach $r (@$cand) {
+ next unless $r && ref $r;
+ my $diff = $c > 1 ? nearest(.1, $r->[RQrg] - $qrg) : 0;
+ $sk = "SKIM|$r->[ROrigin]|$band";
+ $skimmer = $spots->{$sk};
+ if ($diff) {
+ ++$skimmer->[DBad] if $skimmer->[DBad] < $maxdeviants;
+ --$skimmer->[DGood] if $skimmer->[DGood] > 0;
+ push @deviant, sprintf("$r->[ROrigin]:%+.1f", $diff);
+ push @{$skimmer->[DEviants]}, $diff;
+ shift @{$skimmer->[DEviants]} while @{$skimmer->[DEviants]} > $maxdeviants;
+ } else {
+ ++$skimmer->[DGood] if $skimmer->[DGood] < $maxdeviants;
+ --$skimmer->[DBad] if $skimmer->[DBad] > 0;
+ shift @{$skimmer->[DEviants]};
+ }
+ $skimmer->[DScore] = $skimmer->[DGood] - $skimmer->[DBad];
+ if ($rbnskim && isdbg('rbnskim')) {
+ my $lastin = difft($skimmer->[DLastin], $now, 2);
+ my $difflist = join(', ', @{$skimmer->[DEviants]});
+ $difflist = " band qrg diffs: $difflist" if $difflist;
+ dbg("RBN:SKIM key $sp slot $sk $r->[RQrg] - $qrg = $diff Skimmer score: $skimmer->[DGood] - $skimmer->[DBad] = $skimmer->[DScore] lastseen:$lastin ago$difflist");
+ }
+ $skimmer->[DLastin] = $now;
+ $r->[RSpotData]->[SQrg] = $qrg if $qrg && $c > 1; # set all the QRGs to the agreed value
+ }
+
+ $qrg = (sprintf "%.1f", $qrg)+0;
+ $r = $cand->[CData];
+ $r->[RQrg] = $qrg;
+ my $squality = "Q:$cand->[CQual]";
+ $squality .= '*' if $c > 1;
+ $squality .= '+' if $r->[Respot];
+
+ if (isdbg('progress')) {
+ my $rt = difft($ctime, $now, 2);
+ my $s = "RBN: SPOT key: '$sp' = $r->[RCall] on $r->[RQrg] by $r->[ROrigin] \@ $r->[RTime] $squality route: $dxchan->{call} dwell:$rt";
+ my $td = @deviant;
+ $s .= " QRGScore: $mv Deviants: $td/$spotters";
+ $s .= ' (' . join(', ', sort @deviant) . ')' if $td;
+ dbg($s);
+ }
+
+ # finally send it out to any waiting public
+ send_dx_spot($dxchan, $squality, $cand);
+
+ # clear out the data and make this now just "spotted", but no further action required until respot time
+ dbg "RBN: QUEUE key '$sp' cleared" if isdbg 'rbn';
+
+ delete $dxchan->{queue}->{$sp};
+
+ # calculate new sp (which will be 70% likely the same as the old one)
+ # we do this to cope with the fact that the first spotter may well be "wrongly calibrated" giving a qrg that disagrees with the majority.
+ # and we want to store the key that corresponds to majority opinion.
+ my $nqrg = nearest(1, $qrg * 10); # normalised to nearest Khz
+ my $nsp = "$r->[RCall]|$nqrg";
+ if ($sp ne $nsp) {
+ dbg("RBN:SKIM CHANGE KEY sp '$sp' -> '$nsp' for storage") if $rbnskim && isdbg('rbnskim');
+ delete $spots->{$sp};
+ $spots->{$nsp} = [$now, $cand->[CQual]];
+ } else {
+ $spots->{$sp} = [$now, $cand->[CQual]];
+ }
+ }
+ else {
+ dbg sprintf("RBN: QUEUE key: '$sp' SEND time not yet reached %.1f secs left", $cand->[CTime] + $dwelltime - $now) if isdbg 'rbnqueue';
+ }
+ }
+ if (isdbg('rbntimer')) {
+ my $diff = _diffus($ta);
+ dbg "RBN: TIMER process queue for call: $dxchan->{call} $items spots $diff uS";