2 # The RBN connection system
4 # Copyright (c) 2020 Dirk Koopman G1TLH
19 use Math::Round qw(nearest);
21 our @ISA = qw(DXChannel);
25 my $self = DXChannel::alloc(@_);
27 # routing, this must go out here to prevent race condx
31 DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
39 $self->{lasttime} = $main::systime;
40 $self->{minspottime} = 60*60;
41 $self->{showstats} = 0;
48 my ($self, $line, $sort) = @_;
49 my $user = $self->{user};
50 my $call = $self->{call};
51 my $name = $user->{name};
52 my $dref = $self->{d};
53 my $spotref = $self->{spot};
56 my $host = $self->{conn}->peerhost;
58 $self->{hostname} = $host;
60 $self->{name} = $name ? $name : $call;
61 $self->state('prompt'); # a bit of room for further expansion, passwords etc
62 $self->{lang} = $user->lang || $main::lang || 'en';
63 if ($line =~ /host=/) {
64 my ($h) = $line =~ /host=(\d+\.\d+\.\d+\.\d+)/;
65 $line =~ s/\s*host=\d+\.\d+\.\d+\.\d+// if $h;
67 ($h) = $line =~ /host=([\da..fA..F:]+)/;
68 $line =~ s/\s*host=[\da..fA..F:]+// if $h;
70 $self->{hostname} = $h if $h;
72 $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
73 $self->{consort} = $line; # save the connection type
75 LogDbg('DXCommand', "$call connected from $self->{hostname}");
77 # set some necessary flags on the user if they are connecting
78 $self->{registered} = 1;
79 # sort out privilege reduction
86 $self->{spotsfilter} = Filter::read_in('spots', $call, 0)
87 || Filter::read_in('spots', $nossid, 0)
88 || Filter::read_in('spots', 'user_default', 0);
90 # clean up qra locators
92 $qra = undef if ($qra && !DXBearing::is_qra($qra));
95 my $long = $user->long;
96 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);
106 my $spot = $self->{spot};
108 # save this for them's that need it
111 # remove leading and trailing spaces
118 my $tim = $main::systime;
121 dbg "RBN:RAW,$line" if isdbg('rbnraw');
123 my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
125 # fix up FT8 spots from 7001
126 $t = $u, $u = '' if !$t && is_ztime($u);
127 $t = $sort, $sort = '' if !$t && is_ztime($sort);
128 my $qra = $spd, $spd = '' if is_qra($spd);
131 # no warnings qw(uninitialized);
133 # 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/;
141 # fix up times for things like 'NXDXF B' etc
142 if ($tx && is_ztime($t)) {
152 # We have an RBN data line, dedupe it very simply on time, ignore QRG completely.
153 # This works because the skimmers are NTP controlled (or should be) and will receive
154 # the spot at the same time (velocity factor of the atmosphere and network delays
155 # carefully (not) taken into account :-)
157 # Note, there is no intelligence here, but there are clearly basic heuristics that could
158 # be applied at this point that reject (more likely rewrite) the call of a busted spot that would
159 # useful for a zonal hotspot requirement from the cluster node.
161 # In reality, this mechanism would be incorporated within the cluster code, utilising the dxqsl database,
162 # and other resources in DXSpider, thus creating a zone map for an emitted spot. This is then passed through the
163 # normal "to-user" spot system (where normal spots are sent to be displayed per user) and then be
164 # processed through the normal, per user, spot filtering system - like a regular spot.
166 # The key to this is deducing the true callsign by "majority voting" (the greater the number of spotters
167 # the more effective this is) together with some lexical analsys probably in conjuction with DXSpider
168 # 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)
169 # and some heuristical "Kwalitee" rating given distance from the zone centres of spotter, recipient user
170 # and spotted. A map can be generated once per user and spotter as they are essentially mostly static.
171 # The spotted will only get a coarse position unless other info is available. Programs that parse
172 # DX bulletins and the online data online databases could be be used and then cached.
174 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
177 # Clearly this will only work in the 'mojo' branch of DXSpider where it is possible to pass off external
178 # data requests to ephemeral or semi resident forked processes that do any grunt work and the main
179 # process to just the standard "message passing" which has been shown to be able to sustain over 5000
180 # per second (limited by the test program's output and network speed, rather than DXSpider's handling).
189 $qrg = sprintf('%.1f', nearest(.1, $qrg)); # to nearest 100Hz (to catch the odd multiple decpl QRG [eg '7002.07']).
190 if (isdbg('rbnraw')) {
191 my $ss = join(',', "RBN", $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
196 # Determine whether to "SPOT" it based on whether we have not seen it before (near this QRG) or,
197 # if we have, has it been a "while" since the last time we spotted it? If it has been spotted
198 # before then "RESPOT" it.
199 my $nqrg = nearest(1, $qrg); # normalised to nearest Khz
200 my $sp = "$call|$nqrg"; # hopefully the skimmers will be calibrated at least this well!
201 my $ts = $spot->{$sp};
203 if (!$ts || ($self->{minspottime} > 0 && $tim - $ts >= $self->{minspottime})) {
205 my $tag = $ts ? "RESPOT" : "SPOT";
208 dbg "RBN:" . join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
210 send_dx_spot($self, $line, $mode);
215 dbg "RBN:DATA,$line" if isdbg('rbn');
218 # periodic clearing out of the two caches
219 if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
223 while (my ($k,$v) = each %{$d}) {
231 dbg "RBN:ADMIN,rbn cache: $removed removed $count remain" if isdbg('rbn');
232 $count = $removed = 0;
233 while (my ($k,$v) = each %{$spot}) {
234 if ($tim-$v > $self->{minspottime}*2) {
241 dbg "RBN:ADMIN,spot cache: $removed removed $count remain" if isdbg('rbn');
243 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
244 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
246 $self->{last} = int($tim / 60) * 60;
250 # we only send to users and we send the original line (possibly with a
258 my @dxchan = DXChannel::get_all();
260 foreach my $dxchan (@dxchan) {
261 next unless $dxchan->is_user;
262 my $user = $dxchan->{user};
263 next unless $user->wantrbn;
266 ++$want if $user->wantbeacon && $mode =~ /^BEA|NCD/;
267 ++$want if $user->wantcw && $mode =~ /^CW/;
268 ++$want if $user->wantrtty && $mode =~ /^RTTY/;
269 ++$want if $user->wantpsk && $mode =~ /^PSK/;
270 ++$want if $user->wantcw && $mode =~ /^CW/;
271 ++$want if $user->wantft && $mode =~ /^FT/;
273 ++$want unless $want; # send everything if nothing is selected.
275 $dxchan->send($line) if $want;