2 # The RBN connection system
4 # Copyright (c) 2020 Dirk Koopman G1TLH
19 use Math::Round qw(nearest);
22 our @ISA = qw(DXChannel);
24 our $startup_delay =5*60; # don't send anything out until this timer has expired
25 # this is to allow the feed to "warm up" with duplicates
26 # so that the "big rush" doesn't happen.
28 our $minspottime = 60*60; # the time between respots of a callsign - if a call is
29 # still being spotted (on the same freq) and it has been
30 # spotted before, it's spotted again after this time
31 # until the next minspottime has passed.
55 my $self = DXChannel::alloc(@_);
57 # routing, this must go out here to prevent race condx
61 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
69 $self->{lasttime} = $main::systime;
70 $self->{minspottime} = $minspottime;
71 $self->{showstats} = 0;
78 my ($self, $line, $sort) = @_;
79 my $user = $self->{user};
80 my $call = $self->{call};
81 my $name = $user->{name};
82 my $dref = $self->{d};
83 my $spotref = $self->{spot};
86 my $host = $self->{conn}->peerhost;
88 $self->{hostname} = $host;
90 $self->{name} = $name ? $name : $call;
91 $self->state('prompt'); # a bit of room for further expansion, passwords etc
92 $self->{lang} = $user->lang || $main::lang || 'en';
93 if ($line =~ /host=/) {
94 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
95 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
97 ($h) = $line =~ /host=([\da..fA..F:]+)/;
98 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
100 $self->{hostname} = $h if $h;
102 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
103 $self->{consort} = $line; # save the connection type
105 LogDbg('DXCommand', "$call connected from $self->{hostname}");
107 # set some necessary flags on the user if they are connecting
108 $self->{registered} = 1;
109 # sort out privilege reduction
114 $nossid =~ s/-\d+$//;
116 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
117 || Filter::read_in('spots', $nossid, 0)
118 || Filter::read_in('spots', 'user_default', 0);
120 # clean up qra locators
121 my $qra = $user->qra;
122 $qra = undef if ($qra && !DXBearing::is_qra($qra));
124 my $lat = $user->lat;
125 my $long = $user->long;
126 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
130 $self->{inrushpreventor} = $main::systime + $startup_delay;
139 my $spot = $self->{spot};
141 # save this for them's that need it
144 # remove leading and trailing spaces
151 my $tim = $main::systime;
154 dbg "RBN:RAW,$line" if isdbg('rbnraw');
155 return unless $line=~/^DX\s+de/;
157 my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
159 # fix up FT8 spots from 7001
160 $t = $u, $u = '' if !$t && is_ztime($u);
161 $t = $sort, $sort = '' if !$t && is_ztime($sort);
162 my $qra = $spd, $spd = '' if is_qra($spd);
165 $origin =~ s/\-(?:\d{1,2}\-)?\#$//; # get rid of all the crap we aren't interested in
171 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');
178 # fix up times for things like 'NXDXF B' etc
179 if ($tx && is_ztime($t)) {
188 if ($sort && $sort eq 'NCDXF') {
192 if ($sort && $sort eq 'BEACON') {
196 # We have an RBN data line, dedupe it very simply on time, ignore QRG completely.
197 # This works because the skimmers are NTP controlled (or should be) and will receive
198 # the spot at the same time (velocity factor of the atmosphere and network delays
199 # carefully (not) taken into account :-)
201 # Note, there is no intelligence here, but there are clearly basic heuristics that could
202 # be applied at this point that reject (more likely rewrite) the call of a busted spot that would
203 # useful for a zonal hotspot requirement from the cluster node.
205 # In reality, this mechanism would be incorporated within the cluster code, utilising the dxqsl database,
206 # and other resources in DXSpider, thus creating a zone map for an emitted spot. This is then passed through the
207 # normal "to-user" spot system (where normal spots are sent to be displayed per user) and then be
208 # processed through the normal, per user, spot filtering system - like a regular spot.
210 # The key to this is deducing the true callsign by "majority voting" (the greater the number of spotters
211 # the more effective this is) together with some lexical analsys probably in conjuction with DXSpider
212 # 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)
213 # and some heuristical "Kwalitee" rating given distance from the zone centres of spotter, recipient user
214 # and spotted. A map can be generated once per user and spotter as they are essentially mostly static.
215 # The spotted will only get a coarse position unless other info is available. Programs that parse
216 # DX bulletins and the online data online databases could be be used and then cached.
218 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
221 # Clearly this will only work in the 'mojo' branch of DXSpider where it is possible to pass off external
222 # data requests to ephemeral or semi resident forked processes that do any grunt work and the main
223 # process to just the standard "message passing" which has been shown to be able to sustain over 5000
224 # per second (limited by the test program's output and network speed, rather than DXSpider's handling).
233 $qrg = sprintf('%.1f', nearest(.1, $qrg)); # to nearest 100Hz (to catch the odd multiple decpl QRG [eg '7002.07']).
234 if (isdbg('rbnraw')) {
235 my $ss = join(',', "RBN", $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
240 # Determine whether to "SPOT" it based on whether we have not seen it before (near this QRG) or,
241 # if we have, has it been a "while" since the last time we spotted it? If it has been spotted
242 # before then "RESPOT" it.
243 my $nqrg = nearest(1, $qrg); # normalised to nearest Khz
244 my $sp = "$call|$nqrg"; # hopefully the skimmers will be calibrated at least this well!
245 my $ts = $spot->{$sp};
247 if (!$ts || ($self->{minspottime} > 0 && $tim - $ts >= $self->{minspottime})) {
249 my $tag = $ts ? "RESPOT" : "SPOT";
252 my ($hh,$mm) = $t =~ /(\d\d)(\d\d)Z$/;
253 my $utz = str2time(sprintf('%02d:%02dZ', $hh, $mm));
254 dbg "RBN:" . join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t) if dbg('rbn');
257 my @s = Spot::prepare($qrg, $call, $utz, sprintf("%-5s%3d $m", $mode, $s), $origin);
259 if (isdbg('progress')) {
260 my $d = ztime($s[2]);
261 my $s = "RBN: $s[1] on $s[0] \@ $d by $s[4]";
262 $s .= $s[3] ? " '$s[3]'" : q{ ''};
263 $s .= " route: $self->{call}";
267 send_dx_spot($self, $line, $mode, \@s) unless $self->{inrushpreventor} > $main::systime;
272 dbg "RBN:DATA,$line" if isdbg('rbn');
275 # periodic clearing out of the two caches
276 if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
280 while (my ($k,$v) = each %{$d}) {
288 dbg "RBN:ADMIN,rbn cache: $removed removed $count remain" if isdbg('rbn');
289 $count = $removed = 0;
290 while (my ($k,$v) = each %{$spot}) {
291 if ($tim-$v > $self->{minspottime}*2) {
298 dbg "RBN:ADMIN,spot cache: $removed removed $count remain" if isdbg('rbn');
300 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
301 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
303 $self->{last} = int($tim / 60) * 60;
307 # we only send to users and we send the original line (possibly with a
316 my @dxchan = DXChannel::get_all();
318 foreach my $dxchan (@dxchan) {
319 next unless $dxchan->is_user;
320 my $user = $dxchan->{user};
321 next unless $user && $user->wantrbn;
324 ++$want if $user->wantbeacon && $mode =~ /^BEA|NCD/;
325 ++$want if $user->wantcw && $mode =~ /^CW/;
326 ++$want if $user->wantrtty && $mode =~ /^RTTY/;
327 ++$want if $user->wantpsk && $mode =~ /^PSK/;
328 ++$want if $user->wantcw && $mode =~ /^CW/;
329 ++$want if $user->wantft && $mode =~ /^FT/;
331 ++$want unless $want; # send everything if nothing is selected.
334 $self->dx_spot($dxchan, $sref) if $want;
344 # return unless $dxchan->{rbn};
348 if ($dxchan->{rbnfilter}) {
349 ($filter, $hops) = $dxchan->{rbnfilter}->it($sref);
350 return unless $filter;
351 } elsif ($self->{rbnfilter}) {
352 ($filter, $hops) = $self->{rbnfilter}->it($sref);
353 return unless $filter;
356 # dbg('RBN::dx_spot spot: "' . join('","', @$sref) . '"') if isdbg('rbn');
359 if ($self->{ve7cc}) {
360 $buf = VE7CC::dx_spot($dxchan, @$sref);
362 $buf = $self->format_dx_spot(@$sref);
365 $dxchan->local_send('N', $buf);
372 my $t = ztime($_[2]);
373 my $clth = $self->{consort} eq 'local' ? 29 : 30;
374 my $comment = $_[3] || '';
376 my $ref = DXUser::get_current($_[1]);
377 if ($ref && $ref->qra) {
378 $loc = ' ' . substr($ref->qra, 0, 4);
380 $comment .= ' ' x ($clth - (length($comment)+length($loc)+1));
383 my $ref = DXUser::get_current($_[4]);
384 if ($ref && $ref->qra) {
385 $loc = ' ' . substr($ref->qra, 0, 4);
387 return sprintf "RB de %-7.7s%11.1f %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;