81d161b2b003417b8dd1f1ab741fe1ff1652bd4f
[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
21 our @ISA = qw(DXChannel);
22
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. 
26
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.
31
32 our %hfitu = (
33                           1 => [1, 2,],
34                           2 => [1, 2, 3,],
35                           3 => [2,3, 4,],
36                           4 => [3,4, 9,],
37 #                         5 => [0],
38                           6 => [7],
39                           7 => [7, 6, 8, 10],
40                           8 => [7, 8, 9],
41                           9 => [8, 9],
42                           10 => [10],
43                           11 => [11],
44                           12 => [12, 13],
45                           13 => [12, 13],
46                           14 => [14, 15],
47                           15 => [15, 14],
48                           16 => [16],
49                           17 => [17],
50                          );
51
52 sub new 
53 {
54         my $self = DXChannel::alloc(@_);
55
56         # routing, this must go out here to prevent race condx
57         my $pkg = shift;
58         my $call = shift;
59
60         DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
61         $self->{d} = {};
62         $self->{spot} = {};
63         $self->{last} = 0;
64         $self->{noraw} = 0;
65         $self->{nospot} = 0;
66         $self->{norbn} = 0;
67         $self->{sort} = 'N';
68         $self->{lasttime} = $main::systime;
69         $self->{minspottime} = $minspottime;
70         $self->{showstats} = 0;
71
72         return $self;
73 }
74
75 sub start
76
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};
83                 
84         # log it
85         my $host = $self->{conn}->peerhost;
86         $host ||= "unknown";
87         $self->{hostname} = $host;
88
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;
95                 unless ($h) {
96                         ($h) = $line =~ /host=([\da..fA..F:]+)/;
97                         $line =~ s/\s*host=[\da..fA..F:]+// if $h;
98                 }
99                 $self->{hostname} = $h if $h;
100         }
101         $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
102         $self->{consort} = $line;       # save the connection type
103
104         LogDbg('DXCommand', "$call connected from $self->{hostname}");
105
106         # set some necessary flags on the user if they are connecting
107         $self->{registered} = 1;
108         # sort out privilege reduction
109         $self->{priv} = 0;
110
111         # get the filters
112         my $nossid = $call;
113         $nossid =~ s/-\d+$//;
114         
115         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) 
116                 || Filter::read_in('spots', $nossid, 0)
117                         || Filter::read_in('spots', 'user_default', 0);
118
119         # clean up qra locators
120         my $qra = $user->qra;
121         $qra = undef if ($qra && !DXBearing::is_qra($qra));
122         unless ($qra) {
123                 my $lat = $user->lat;
124                 my $long = $user->long;
125                 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);  
126         }
127
128         # start inrush timer
129         $self->{inrushpreventor} = $main::systime + $startup_delay;
130 }
131
132 sub normal
133 {
134         my $self = shift;
135         my $line = shift;
136         my @ans;
137         my $d = $self->{d};
138         my $spot = $self->{spot};
139         
140         # save this for them's that need it
141         my $rawline = $line;
142         
143         # remove leading and trailing spaces
144         chomp $line;
145         $line =~ s/^\s*//;
146         $line =~ s/\s*$//;
147
148         # add base RBN
149
150         my $tim = $main::systime;
151
152         # parse line
153         dbg "RBN:RAW,$line" if isdbg('rbnraw');
154
155         my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
156
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);
161         $u = $qra if $qra;
162         
163 #       no warnings qw(uninitialized);
164         
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/;
166
167 #       use warnings;
168         
169         my $b;
170         
171         if ($t || $tx) {
172
173                 # fix up times for things like 'NXDXF B' etc
174                 if ($tx && is_ztime($t)) {
175                         if (is_ztime($tx)) {
176                                 $b = $t;
177                                 $t = $tx;
178                         } else {
179                                 dbg "RBN:ERR,$line";
180                                 return (0);
181                         }
182                 }
183                 
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 :-)
188
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.
192
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.
197
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. 
205
206                 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
207                 # ignored.
208
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).  
213                 
214                 my $p = "$t|$call";
215                 ++$self->{noraw};
216                 return if $d->{$p};
217
218                 # new RBN input
219                 $d->{$p} = $tim;
220                 ++$self->{norbn};
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);
224                         $ss .= ",$b" if $b;
225                         dbg "RBNRAW:$ss";
226                 }
227
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};
234
235                 if (!$ts || ($self->{minspottime} > 0 && $tim - $ts >= $self->{minspottime})) {
236                         ++$self->{nospot};
237                         my $tag = $ts ? "RESPOT" : "SPOT";
238                         $t .= ",$b" if $b;
239                         $sort ||= '';
240                         $origin =~ s/-?\d+?-?\#?\s*$//;
241                         
242                         dbg "RBN:" . join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t) if dbg('rbn');
243
244                         my @s = Spot::prepare($qrg, $call, $t, "$mode $s $m", $origin);
245                         
246                         send_dx_spot($self, $line, $mode, \@s) unless $self->{inrushpreventor} > $main::systime;
247
248                         $spot->{$sp} = $tim;
249                 }
250         } else {
251                 dbg "RBN:DATA,$line" if isdbg('rbn');
252         }
253
254         # periodic clearing out of the two caches
255         if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
256                 my $count = 0;
257                 my $removed = 0;
258
259                 while (my ($k,$v) = each %{$d}) {
260                         if ($tim-$v > 60) {
261                                 delete $d->{$k};
262                                 ++$removed
263                         } else {
264                                 ++$count;
265                         }
266                 }
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) {
271                                 delete $spot->{$k};
272                                 ++$removed;
273                         } else {
274                                 ++$count;
275                         }
276                 }
277                 dbg "RBN:ADMIN,spot cache: $removed removed $count remain" if isdbg('rbn');
278
279                 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
280                 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
281
282                 $self->{last} = int($tim / 60) * 60;
283         }
284 }
285
286 # we only send to users and we send the original line (possibly with a
287 # Q:n in it)
288 sub send_dx_spot
289 {
290         my $self = shift;
291         my $line = shift;
292         my $mode = shift;
293         my $sref = shift;
294         
295         my @dxchan = DXChannel::get_all();
296
297         foreach my $dxchan (@dxchan) {
298                 next unless $dxchan->is_user;
299                 my $user = $dxchan->{user};
300                 next unless $user->wantrbn;
301
302                 my $want = 0;
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/;
309
310                 ++$want unless $want;   # send everything if nothing is selected.
311
312                 
313                 $self->dx_spot($dxchan, $sref) if $want;
314                 dbg("RBN: $line") if isdbg('progress');
315         }
316 }
317
318 sub dx_spot
319 {
320         my $self = shift;
321         my $dxchan = shift;
322         my $sref = shift;
323         
324 #       return unless $dxchan->{rbn};
325
326         my ($filter, $hops);
327
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;
334         }
335
336         dbg('RBN::dx_spot spot: "' . join('","', @$sref) . '"') if isdbg('rbn');
337
338         my $buf;
339         if ($self->{ve7cc}) {
340                 $buf = VE7CC::dx_spot($dxchan, @$sref);
341         } else {
342                 $buf = $self->format_dx_spot(@$sref);
343                 $buf =~ s/\%5E/^/g;
344         }
345
346         $dxchan->local_send('N', $buf);
347 }
348
349 sub format_dx_spot
350 {
351         my $self = shift;
352
353         my $t = ztime($_[2]);
354         my $loc = '';
355         my $clth = $self->{consort} eq 'local' ? 29 : 30;
356         my $comment = $_[3] || '';
357         my $ref = DXUser::get_current($_[1]);
358         if ($ref) {
359                 $loc = $ref->qra;
360                 $loc = ' ' . substr($loc, 0, 4) if $loc;
361         }
362         $comment .= ' ' x ($clth - (length($comment)+length($loc)));
363         $comment .= $loc if $loc;
364         $loc = '';
365         $ref = DXUser::get_current($_[4]);
366         if ($ref) {
367                 $loc = $ref->qra;
368                 $loc = ' ' . substr($loc, 0, 4) if $loc;
369                 $loc ||= '';
370         }
371         return sprintf "RB de %7.7s:%11.1f  %-12.12s %-s $t$loc", $_[4], $_[0], $_[1], $comment;
372 }
373 1;