2 # The RBN connection system
4 # Copyright (c) 2020 Dirk Koopman G1TLH
19 use Math::Round qw(nearest);
21 our @ISA = qw(DXChannel);
23 our $startup_delay = 0;# 2*60; # don't send anything out until this timer has expired
24 # this is to allow the feed to "warm up" with duplicates
25 # so that the "big rush" doesn't happen.
27 our $minspottime = 60*60; # the time between respots of a callsign - if a call is
28 # still being spotted (on the same freq) and it has been
29 # spotted before, it's spotted again after this time
30 # until the next minspottime has passed.
54 my $self = DXChannel::alloc(@_);
56 # routing, this must go out here to prevent race condx
60 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
68 $self->{lasttime} = $main::systime;
69 $self->{minspottime} = $minspottime;
70 $self->{showstats} = 0;
77 my ($self, $line, $sort) = @_;
78 my $user = $self->{user};
79 my $call = $self->{call};
80 my $name = $user->{name};
81 my $dref = $self->{d};
82 my $spotref = $self->{spot};
85 my $host = $self->{conn}->peerhost;
87 $self->{hostname} = $host;
89 $self->{name} = $name ? $name : $call;
90 $self->state('prompt'); # a bit of room for further expansion, passwords etc
91 $self->{lang} = $user->lang || $main::lang || 'en';
92 if ($line =~ /host=/) {
93 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
94 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
96 ($h) = $line =~ /host=([\da..fA..F:]+)/;
97 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
99 $self->{hostname} = $h if $h;
101 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
102 $self->{consort} = $line; # save the connection type
104 LogDbg('DXCommand', "$call connected from $self->{hostname}");
106 # set some necessary flags on the user if they are connecting
107 $self->{registered} = 1;
108 # sort out privilege reduction
113 $nossid =~ s/-\d+$//;
115 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
116 || Filter::read_in('spots', $nossid, 0)
117 || Filter::read_in('spots', 'user_default', 0);
119 # clean up qra locators
120 my $qra = $user->qra;
121 $qra = undef if ($qra && !DXBearing::is_qra($qra));
123 my $lat = $user->lat;
124 my $long = $user->long;
125 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
129 $self->{inrushpreventor} = $main::systime + $startup_delay;
138 my $spot = $self->{spot};
140 # save this for them's that need it
143 # remove leading and trailing spaces
150 my $tim = $main::systime;
153 dbg "RBN:RAW,$line" if isdbg('rbnraw');
155 my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
157 # fix up FT8 spots from 7001
158 $t = $u, $u = '' if !$t && is_ztime($u);
159 $t = $sort, $sort = '' if !$t && is_ztime($sort);
160 my $qra = $spd, $spd = '' if is_qra($spd);
163 # no warnings qw(uninitialized);
165 # 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 $line =~ /DX/;
173 # fix up times for things like 'NXDXF B' etc
174 if ($tx && is_ztime($t)) {
184 # We have an RBN data line, dedupe it very simply on time, ignore QRG completely.
185 # This works because the skimmers are NTP controlled (or should be) and will receive
186 # the spot at the same time (velocity factor of the atmosphere and network delays
187 # carefully (not) taken into account :-)
189 # Note, there is no intelligence here, but there are clearly basic heuristics that could
190 # be applied at this point that reject (more likely rewrite) the call of a busted spot that would
191 # useful for a zonal hotspot requirement from the cluster node.
193 # In reality, this mechanism would be incorporated within the cluster code, utilising the dxqsl database,
194 # and other resources in DXSpider, thus creating a zone map for an emitted spot. This is then passed through the
195 # normal "to-user" spot system (where normal spots are sent to be displayed per user) and then be
196 # processed through the normal, per user, spot filtering system - like a regular spot.
198 # The key to this is deducing the true callsign by "majority voting" (the greater the number of spotters
199 # the more effective this is) together with some lexical analsys probably in conjuction with DXSpider
200 # 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)
201 # and some heuristical "Kwalitee" rating given distance from the zone centres of spotter, recipient user
202 # and spotted. A map can be generated once per user and spotter as they are essentially mostly static.
203 # The spotted will only get a coarse position unless other info is available. Programs that parse
204 # DX bulletins and the online data online databases could be be used and then cached.
206 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
209 # Clearly this will only work in the 'mojo' branch of DXSpider where it is possible to pass off external
210 # data requests to ephemeral or semi resident forked processes that do any grunt work and the main
211 # process to just the standard "message passing" which has been shown to be able to sustain over 5000
212 # per second (limited by the test program's output and network speed, rather than DXSpider's handling).
221 $qrg = sprintf('%.1f', nearest(.1, $qrg)); # to nearest 100Hz (to catch the odd multiple decpl QRG [eg '7002.07']).
222 if (isdbg('rbnraw')) {
223 my $ss = join(',', "RBN", $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
228 # Determine whether to "SPOT" it based on whether we have not seen it before (near this QRG) or,
229 # if we have, has it been a "while" since the last time we spotted it? If it has been spotted
230 # before then "RESPOT" it.
231 my $nqrg = nearest(1, $qrg); # normalised to nearest Khz
232 my $sp = "$call|$nqrg"; # hopefully the skimmers will be calibrated at least this well!
233 my $ts = $spot->{$sp};
235 if (!$ts || ($self->{minspottime} > 0 && $tim - $ts >= $self->{minspottime})) {
237 my $tag = $ts ? "RESPOT" : "SPOT";
240 $origin =~ s/-?\d+?-?\#?\s*$//;
242 dbg "RBN:" . join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t) if dbg('rbn');
244 my @s = Spot::prepare($qrg, $call, $t, "$mode $s $m", $origin);
246 send_dx_spot($self, $line, $mode, \@s) unless $self->{inrushpreventor} > $main::systime;
251 dbg "RBN:DATA,$line" if isdbg('rbn');
254 # periodic clearing out of the two caches
255 if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
259 while (my ($k,$v) = each %{$d}) {
267 dbg "RBN:ADMIN,rbn cache: $removed removed $count remain" if isdbg('rbn');
268 $count = $removed = 0;
269 while (my ($k,$v) = each %{$spot}) {
270 if ($tim-$v > $self->{minspottime}*2) {
277 dbg "RBN:ADMIN,spot cache: $removed removed $count remain" if isdbg('rbn');
279 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
280 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
282 $self->{last} = int($tim / 60) * 60;
286 # we only send to users and we send the original line (possibly with a
295 my @dxchan = DXChannel::get_all();
297 foreach my $dxchan (@dxchan) {
298 next unless $dxchan->is_user;
299 my $user = $dxchan->{user};
300 next unless $user->wantrbn;
303 ++$want if $user->wantbeacon && $mode =~ /^BEA|NCD/;
304 ++$want if $user->wantcw && $mode =~ /^CW/;
305 ++$want if $user->wantrtty && $mode =~ /^RTTY/;
306 ++$want if $user->wantpsk && $mode =~ /^PSK/;
307 ++$want if $user->wantcw && $mode =~ /^CW/;
308 ++$want if $user->wantft && $mode =~ /^FT/;
310 ++$want unless $want; # send everything if nothing is selected.
313 $self->dx_spot($dxchan, $sref) if $want;
314 dbg("RBN: $line") if isdbg('progress');
324 # return unless $dxchan->{rbn};
328 if ($dxchan->{rbnfilter}) {
329 ($filter, $hops) = $dxchan->{rbnfilter}->it($sref);
330 return unless $filter;
331 } elsif ($self->{rbnfilter}) {
332 ($filter, $hops) = $self->{rbnfilter}->it($sref);
333 return unless $filter;
336 dbg('RBN::dx_spot spot: "' . join('","', @$sref) . '"') if isdbg('rbn');
339 if ($self->{ve7cc}) {
340 $buf = VE7CC::dx_spot($dxchan, @$sref);
342 $buf = $self->format_dx_spot(@$sref);
346 $dxchan->local_send('N', $buf);
353 my $t = ztime($_[2]);
355 my $clth = $self->{consort} eq 'local' ? 29 : 30;
356 my $comment = $_[3] || '';
357 my $ref = DXUser::get_current($_[1]);
360 $loc = ' ' . substr($loc, 0, 4) if $loc;
362 $comment .= ' ' x ($clth - (length($comment)+length($loc)));
363 $comment .= $loc if $loc;
365 $ref = DXUser::get_current($_[4]);
368 $loc = ' ' . substr($loc, 0, 4) if $loc;
371 return sprintf "RB de %7.7s:%11.1f %-12.12s %-s $t$loc", $_[4], $_[0], $_[1], $comment;