9687b18f6f2741ac15931eae6a0fda7e36b5b91e
[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 sub new 
24 {
25         my $self = DXChannel::alloc(@_);
26
27         # routing, this must go out here to prevent race condx
28         my $pkg = shift;
29         my $call = shift;
30
31         DXProt::_add_thingy($main::routeroot, [$call, 0, 0, 1, undef, undef, $self->hostname], );
32         $self->{d} = {};
33         $self->{spot} = {};
34         $self->{last} = 0;
35         $self->{noraw} = 0;
36         $self->{nospot} = 0;
37         $self->{norbn} = 0;
38         $self->{sort} = 'N';
39         $self->{lasttime} = $main::systime;
40         $self->{minspottime} = 60*60;
41         $self->{showstats} = 0;
42
43         return $self;
44 }
45
46 sub start
47
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};
54                 
55         # log it
56         my $host = $self->{conn}->peerhost;
57         $host ||= "unknown";
58         $self->{hostname} = $host;
59
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;
66                 unless ($h) {
67                         ($h) = $line =~ /host=([\da..fA..F:]+)/;
68                         $line =~ s/\s*host=[\da..fA..F:]+// if $h;
69                 }
70                 $self->{hostname} = $h if $h;
71         }
72         $self->{width} = 80 unless $self->{width} && $self->{width} > 80;
73         $self->{consort} = $line;       # save the connection type
74
75         LogDbg('DXCommand', "$call connected from $self->{hostname}");
76
77         # set some necessary flags on the user if they are connecting
78         $self->{registered} = 1;
79         # sort out privilege reduction
80         $self->{priv} = 0;
81
82         # get the filters
83         my $nossid = $call;
84         $nossid =~ s/-\d+$//;
85         
86         $self->{spotsfilter} = Filter::read_in('spots', $call, 0) 
87                 || Filter::read_in('spots', $nossid, 0)
88                         || Filter::read_in('spots', 'user_default', 0);
89
90         # clean up qra locators
91         my $qra = $user->qra;
92         $qra = undef if ($qra && !DXBearing::is_qra($qra));
93         unless ($qra) {
94                 my $lat = $user->lat;
95                 my $long = $user->long;
96                 $user->qra(DXBearing::lltoqra($lat, $long)) if (defined $lat && defined $long);  
97         }
98 }
99
100 sub normal
101 {
102         my $self = shift;
103         my $line = shift;
104         my @ans;
105         my $d = $self->{d};
106         my $spot = $self->{spot};
107         
108         # save this for them's that need it
109         my $rawline = $line;
110         
111         # remove leading and trailing spaces
112         chomp $line;
113         $line =~ s/^\s*//;
114         $line =~ s/\s*$//;
115
116         # add base RBN
117
118         my $tim = $main::systime;
119
120         # parse line
121         dbg "RBN:RAW,$line" if isdbg('rbnraw');
122
123         my (undef, undef, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
124
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);
129         $u = $qra if $qra;
130         
131 #       no warnings qw(uninitialized);
132         
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/;
134
135 #       use warnings;
136         
137         my $b;
138         
139         if ($t || $tx) {
140
141                 # fix up times for things like 'NXDXF B' etc
142                 if ($tx && is_ztime($t)) {
143                         if (is_ztime($tx)) {
144                                 $b = $t;
145                                 $t = $tx;
146                         } else {
147                                 dbg "RBN:ERR,$line";
148                                 return (0);
149                         }
150                 }
151                 
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 :-)
156
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.
160
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.
165
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. 
173
174                 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
175                 # ignored.
176
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).  
181                 
182                 my $p = "$t|$call";
183                 ++$self->{noraw};
184                 return if $d->{$p};
185
186                 # new RBN input
187                 $d->{$p} = $tim;
188                 ++$self->{norbn};
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);
192                         $ss .= ",$b" if $b;
193                         dbg "RBNRAW:$ss";
194                 }
195
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};
202
203                 if (!$ts || ($self->{minspottime} > 0 && $tim - $ts >= $self->{minspottime})) {
204                         ++$self->{nospot};
205                         my $tag = $ts ? "RESPOT" : "SPOT";
206                         $t .= ",$b" if $b;
207                         $sort ||= '';
208                         dbg "RBN:" . join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
209
210                         send_dx_spot($self, $line, $mode);
211                         
212                         $spot->{$sp} = $tim;
213                 }
214         } else {
215                 dbg "RBN:DATA,$line" if isdbg('rbn');
216         }
217
218         # periodic clearing out of the two caches
219         if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
220                 my $count = 0;
221                 my $removed = 0;
222
223                 while (my ($k,$v) = each %{$d}) {
224                         if ($tim-$v > 60) {
225                                 delete $d->{$k};
226                                 ++$removed
227                         } else {
228                                 ++$count;
229                         }
230                 }
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) {
235                                 delete $spot->{$k};
236                                 ++$removed;
237                         } else {
238                                 ++$count;
239                         }
240                 }
241                 dbg "RBN:ADMIN,spot cache: $removed removed $count remain" if isdbg('rbn');
242
243                 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
244                 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
245
246                 $self->{last} = int($tim / 60) * 60;
247         }
248 }
249
250 # we only send to users and we send the original line (possibly with a
251 # Q:n in it)
252 sub send_dx_spot
253 {
254         my $self = shift;
255         my $line = shift;
256         my $mode = shift;
257         
258         my @dxchan = DXChannel::get_all();
259
260         foreach my $dxchan (@dxchan) {
261                 next unless $dxchan->is_user;
262                 my $user = $dxchan->{user};
263                 next unless $user->wantrbn;
264
265                 my $want = 0;
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/;
272
273                 ++$want unless $want;   # send everything if nothing is selected.
274
275                 $dxchan->send($line) if $want;
276         }
277 }
278
279
280 1;