latest version of RBN. Add qra to prefixes.
[spider.git] / perl / RBN.pm
1 #
2 # The RBN connection system
3 #
4 # Copyright (c) 2020 Dirk Koopman G1TLH
5 #
6
7 use warnings;
8 use strict;
9
10 package RBN;
11
12 use 5.10.1;
13
14 use DXUtil;
15 use DXDebug;
16 use DXLog;
17 use DXUser;
18 use DXChannel;
19 use Math::Round qw(nearest);
20 use Date::Parse;
21
22 our @ISA = qw(DXChannel);
23
24 our $startup_delay = 0;# 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. 
27
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.
32
33 our %hfitu = (
34                           1 => [1, 2,],
35                           2 => [1, 2, 3,],
36                           3 => [2,3, 4,],
37                           4 => [3,4, 9,],
38 #                         5 => [0],
39                           6 => [7],
40                           7 => [7, 6, 8, 10],
41                           8 => [7, 8, 9],
42                           9 => [8, 9],
43                           10 => [10],
44                           11 => [11],
45                           12 => [12, 13],
46                           13 => [12, 13],
47                           14 => [14, 15],
48                           15 => [15, 14],
49                           16 => [16],
50                           17 => [17],
51                          );
52
53 sub new 
54 {
55         my $self = DXChannel::alloc(@_);
56
57         # routing, this must go out here to prevent race condx
58         my $pkg = shift;
59         my $call = shift;
60
61         DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
62         $self->{d} = {};
63         $self->{spot} = {};
64         $self->{last} = 0;
65         $self->{noraw} = 0;
66         $self->{nospot} = 0;
67         $self->{norbn} = 0;
68         $self->{sort} = 'N';
69         $self->{lasttime} = $main::systime;
70         $self->{minspottime} = $minspottime;
71         $self->{showstats} = 0;
72
73         return $self;
74 }
75
76 sub start
77
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};
84                 
85         # log it
86         my $host = $self->{conn}->peerhost;
87         $host ||= "unknown";
88         $self->{hostname} = $host;
89
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;
96                 unless ($h) {
97                         ($h) = $line =~ /host=([\da..fA..F:]+)/;
98                         $line =~ s/\s*host=[\da..fA..F:]+// if $h;
99                 }
100                 $self->{hostname} = $h if $h;
101         }
102         $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
103         $self->{consort} = $line;       # save the connection type
104
105         LogDbg('DXCommand', "$call connected from $self->{hostname}");
106
107         # set some necessary flags on the user if they are connecting
108         $self->{registered} = 1;
109         # sort out privilege reduction
110         $self->{priv} = 0;
111
112         # get the filters
113         my $nossid = $call;
114         $nossid =~ s/-\d+$//;
115         
116         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) 
117                 || Filter::read_in('spots', $nossid, 0)
118                         || Filter::read_in('spots', 'user_default', 0);
119
120         # clean up qra locators
121         my $qra = $user->qra;
122         $qra = undef if ($qra && !DXBearing::is_qra($qra));
123         unless ($qra) {
124                 my $lat = $user->lat;
125                 my $long = $user->long;
126                 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);  
127         }
128
129         # start inrush timer
130         $self->{inrushpreventor} = $main::systime + $startup_delay;
131 }
132
133 sub normal
134 {
135         my $self = shift;
136         my $line = shift;
137         my @ans;
138         my $d = $self->{d};
139         my $spot = $self->{spot};
140         
141         # save this for them's that need it
142         my $rawline = $line;
143         
144         # remove leading and trailing spaces
145         chomp $line;
146         $line =~ s/^\s*//;
147         $line =~ s/\s*$//;
148
149         # add base RBN
150
151         my $tim = $main::systime;
152
153         # parse line
154         dbg "RBN:RAW,$line" if isdbg('rbnraw');
155         return unless $line=~/^DX\s+de/;
156
157         my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
158
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);
163         $u = $qra if $qra;
164
165         $origin =~ s/\-(?:\d{1,2}\-)?\#$//; # get rid of all the crap we aren't interested in
166
167
168         $sort ||= '';
169         $tx ||= '';
170         $qra ||= '';
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');
172
173         
174         my $b;
175         
176         if ($t || $tx) {
177
178                 # fix up times for things like 'NXDXF B' etc
179                 if ($tx && is_ztime($t)) {
180                         if (is_ztime($tx)) {
181                                 $b = $t;
182                                 $t = $tx;
183                         } else {
184                                 dbg "RBN:ERR,$line";
185                                 return (0);
186                         }
187                 }
188                 if ($sort && $sort eq 'NCDXF') {
189                         $mode = $sort;
190                         $t = $tx;
191                 }
192                 
193                 # We have an RBN data line, dedupe it very simply on time, ignore QRG completely.
194                 # This works because the skimmers are NTP controlled (or should be) and will receive
195                 # the spot at the same time (velocity factor of the atmosphere and network delays
196                 # carefully (not) taken into account :-)
197
198                 # Note, there is no intelligence here, but there are clearly basic heuristics that could
199                 # be applied at this point that reject (more likely rewrite) the call of a busted spot that would
200                 # useful for a zonal hotspot requirement from the cluster node.
201
202                 # In reality, this mechanism would be incorporated within the cluster code, utilising the dxqsl database,
203                 # and other resources in DXSpider, thus creating a zone map for an emitted spot. This is then passed through the
204                 # normal "to-user" spot system (where normal spots are sent to be displayed per user) and then be
205                 # processed through the normal, per user, spot filtering system - like a regular spot.
206
207                 # The key to this is deducing the true callsign by "majority voting" (the greater the number of spotters
208         # the more effective this is) together with some lexical analsys probably in conjuction with DXSpider
209                 # 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)
210                 # and some heuristical "Kwalitee" rating given distance from the zone centres of spotter, recipient user
211         # and spotted. A map can be generated once per user and spotter as they are essentially mostly static. 
212                 # The spotted will only get a coarse position unless other info is available. Programs that parse 
213                 # DX bulletins and the online data online databases could be be used and then cached. 
214
215                 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
216                 # ignored.
217
218                 # Clearly this will only work in the 'mojo' branch of DXSpider where it is possible to pass off external
219                 # data requests to ephemeral or semi resident forked processes that do any grunt work and the main
220                 # process to just the standard "message passing" which has been shown to be able to sustain over 5000 
221                 # per second (limited by the test program's output and network speed, rather than DXSpider's handling).  
222                 
223                 my $p = "$t|$call";
224                 ++$self->{noraw};
225                 return if $d->{$p};
226
227                 # new RBN input
228                 $d->{$p} = $tim;
229                 ++$self->{norbn};
230                 $qrg = sprintf('%.1f', nearest(.1, $qrg));     # to nearest 100Hz (to catch the odd multiple decpl QRG [eg '7002.07']).
231                 if (isdbg('rbnraw')) {
232                         my $ss = join(',', "RBN", $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
233                         $ss .= ",$b" if $b;
234                         dbg "RBNRAW:$ss";
235                 }
236
237                 # Determine whether to "SPOT" it based on whether we have not seen it before (near this QRG) or,
238                 # if we have, has it been a "while" since the last time we spotted it? If it has been spotted
239                 # before then "RESPOT" it.
240                 my $nqrg = nearest(1, $qrg);  # normalised to nearest Khz
241                 my $sp = "$call|$nqrg";           # hopefully the skimmers will be calibrated at least this well! 
242                 my $ts = $spot->{$sp};
243
244                 if (!$ts || ($self->{minspottime} > 0 && $tim - $ts >= $self->{minspottime})) {
245                         ++$self->{nospot};
246                         my $tag = $ts ? "RESPOT" : "SPOT";
247                         $t .= ",$b" if $b;
248
249                         my ($hh,$mm) = $t =~ /(\d\d)(\d\d)Z$/;
250                         my $utz = str2time(sprintf('%02d:%02dZ', $hh, $mm));
251                         dbg "RBN:" . join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t) if dbg('rbn');
252
253
254                         my @s = Spot::prepare($qrg, $call, $utz, sprintf("%-5s%3d $m", $mode, $s), $origin);
255
256                         if (isdbg('progress')) {
257                                 my $d = ztime($s[2]);
258                                 my $s = "RBN: $s[1] on $s[0] \@ $d by $s[4]";
259                                 $s .= $s[3] ? " '$s[3]'" : q{ ''};
260                                 $s .=  " route: $self->{call}";
261                                 dbg($s);
262                         }
263                         
264                         send_dx_spot($self, $line, $mode, \@s) unless $self->{inrushpreventor} > $main::systime;
265
266                         $spot->{$sp} = $tim;
267                 }
268         } else {
269                 dbg "RBN:DATA,$line" if isdbg('rbn');
270         }
271
272         # periodic clearing out of the two caches
273         if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
274                 my $count = 0;
275                 my $removed = 0;
276
277                 while (my ($k,$v) = each %{$d}) {
278                         if ($tim-$v > 60) {
279                                 delete $d->{$k};
280                                 ++$removed
281                         } else {
282                                 ++$count;
283                         }
284                 }
285                 dbg "RBN:ADMIN,rbn cache: $removed removed $count remain" if isdbg('rbn');
286                 $count = $removed = 0;
287                 while (my ($k,$v) = each %{$spot}) {
288                         if ($tim-$v > $self->{minspottime}*2) {
289                                 delete $spot->{$k};
290                                 ++$removed;
291                         } else {
292                                 ++$count;
293                         }
294                 }
295                 dbg "RBN:ADMIN,spot cache: $removed removed $count remain" if isdbg('rbn');
296
297                 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
298                 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
299
300                 $self->{last} = int($tim / 60) * 60;
301         }
302 }
303
304 # we only send to users and we send the original line (possibly with a
305 # Q:n in it)
306 sub send_dx_spot
307 {
308         my $self = shift;
309         my $line = shift;
310         my $mode = shift;
311         my $sref = shift;
312         
313         my @dxchan = DXChannel::get_all();
314
315         foreach my $dxchan (@dxchan) {
316                 next unless $dxchan->is_user;
317                 my $user = $dxchan->{user};
318                 next unless $user &&  $user->wantrbn;
319
320                 my $want = 0;
321                 ++$want if $user->wantbeacon && $mode =~ /^BEA|NCD/;
322                 ++$want if $user->wantcw && $mode =~ /^CW/;
323                 ++$want if $user->wantrtty && $mode =~ /^RTTY/;
324                 ++$want if $user->wantpsk && $mode =~ /^PSK/;
325                 ++$want if $user->wantcw && $mode =~ /^CW/;
326                 ++$want if $user->wantft && $mode =~ /^FT/;
327
328                 ++$want unless $want;   # send everything if nothing is selected.
329
330                 
331                 $self->dx_spot($dxchan, $sref) if $want;
332         }
333 }
334
335 sub dx_spot
336 {
337         my $self = shift;
338         my $dxchan = shift;
339         my $sref = shift;
340         
341 #       return unless $dxchan->{rbn};
342
343         my ($filter, $hops);
344
345         if ($dxchan->{rbnfilter}) {
346                 ($filter, $hops) = $dxchan->{rbnfilter}->it($sref);
347                 return unless $filter;
348         } elsif ($self->{rbnfilter}) {
349                 ($filter, $hops) = $self->{rbnfilter}->it($sref);
350                 return unless $filter;
351         }
352
353 #       dbg('RBN::dx_spot spot: "' . join('","', @$sref) . '"') if isdbg('rbn');
354
355         my $buf;
356         if ($self->{ve7cc}) {
357                 $buf = VE7CC::dx_spot($dxchan, @$sref);
358         } else {
359                 $buf = $self->format_dx_spot(@$sref);
360                 $buf =~ s/\%5E/^/g;
361         }
362         $dxchan->local_send('N', $buf);
363 }
364
365 sub format_dx_spot
366 {
367         my $self = shift;
368
369         my $t = ztime($_[2]);
370         my $clth = $self->{consort} eq 'local' ? 29 : 30;
371         my $comment = $_[3] || '';
372         my $loc = '';
373         my $ref = DXUser::get_current($_[1]);
374         if ($ref && $ref->qra) {
375                 $loc = ' ' . substr($ref->qra, 0, 4);
376         }
377         $comment .= ' ' x ($clth - (length($comment)+length($loc)+1));
378         $comment .= $loc;
379         $loc = '';
380         my $ref = DXUser::get_current($_[4]);
381         if ($ref && $ref->qra) {
382                 $loc = ' ' . substr($ref->qra, 0, 4);
383         }
384         return sprintf "RB de %-7.7s%11.1f  %-12.12s %-s $t$loc", "$_[4]:", $_[0], $_[1], $comment;
385 }
386 1;