WIP RBN
[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 ($origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t, $tx) = split /[:\s]+/, $line;
124         $tx ||= '';
125         dbg qq{0:$origin 1:$qrg 2:$call 3:$mode 4:$s 5:m 6:$spd 7:$u 8:$sort 9:$t 10:$tx} if $line =~ /DX/;
126
127         my $b;
128         
129         if ($t || $tx) {
130
131                 # fix up times for things like 'NXDXF B' etc
132                 if ($tx && $t !~ /^\d{4}Z$/) {
133                         if ($tx =~ /^\d{4}Z$/) {
134                                 $b = $t;
135                                 $t = $tx;
136                         } else {
137                                 dbg "RBN:ERR,$line";
138                                 return (0);
139                         }
140                 }
141
142                 # We have an RBN data line, dedupe it very simply on time, ignore QRG completely.
143                 # This works because the skimmers are NTP controlled (or should be) and will receive
144                 # the spot at the same time (velocity factor of the atmosphere and network delays
145                 # carefully (not) taken into account :-)
146
147                 # Note, there is no intelligence here, but there are clearly basic heuristics that could
148                 # be applied at this point that reject (more likely rewrite) the call of a busted spot that would
149                 # useful for a zonal hotspot requirement from the cluster node.
150
151                 # In reality, this mechanism would be incorporated within the cluster code, utilising the dxqsl database,
152                 # and other resources in DXSpider, thus creating a zone map for an emitted spot. This is then passed through the
153                 # normal "to-user" spot system (where normal spots are sent to be displayed per user) and then be
154                 # processed through the normal, per user, spot filtering system - like a regular spot.
155
156                 # The key to this is deducing the true callsign by "majority voting" (the greater the number of spotters
157         # the more effective this is) together with some lexical analsys probably in conjuction with DXSpider
158                 # 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)
159                 # and some heuristical "Kwalitee" rating given distance from the zone centres of spotter, recipient user
160         # and spotted. A map can be generated once per user and spotter as they are essentially mostly static. 
161                 # The spotted will only get a coarse position unless other info is available. Programs that parse 
162                 # DX bulletins and the online data online databases could be be used and then cached. 
163
164                 # Obviously users have to opt in to receiving RBN spots and other users will simply be passed over and
165                 # ignored.
166
167                 # Clearly this will only work in the 'mojo' branch of DXSpider where it is possible to pass off external
168                 # data requests to ephemeral or semi resident forked processes that do any grunt work and the main
169                 # process to just the standard "message passing" which has been shown to be able to sustain over 5000 
170                 # per second (limited by the test program's output and network speed, rather than DXSpider's handling).  
171                 
172                 my $p = "$t|$call";
173                 ++$self->{noraw};
174                 return if $d->{$p};
175
176                 # new RBN input
177                 $d->{$p} = $tim;
178                 ++$self->{norbn};
179                 $qrg = sprintf('%.1f', nearest(.1, $qrg));     # to nearest 100Hz (to catch the odd multiple decpl QRG [eg '7002.07']).
180                 if (isdbg('rbnraw')) {
181                         my $ss = join(',', "RBN", $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
182                         $ss .= ",$b" if $b;
183                         dbg "RBNRAW:$ss";
184                 }
185
186                 # Determine whether to "SPOT" it based on whether we have not seen it before (near this QRG) or,
187                 # if we have, has it been a "while" since the last time we spotted it? If it has been spotted
188                 # before then "RESPOT" it.
189                 my $nqrg = nearest(1, $qrg);  # normalised to nearest Khz
190                 my $sp = "$call|$nqrg";           # hopefully the skimmers will be calibrated at least this well! 
191                 my $ts = $spot->{$sp};
192
193                 if (!$ts || ($self->{minspottime} > 0 && $tim - $ts >= $self->{minspottime})) {
194                         ++$self->{nospot};
195                         my $tag = $ts ? "RESPOT" : "SPOT";
196                         $t .= ",$b" if $b;
197                         dbg "RBN:" . join(',', $tag, $origin, $qrg, $call, $mode, $s, $m, $spd, $u, $sort, $t);
198                         $spot->{$sp} = $tim;
199                 }
200         } else {
201                 dbg "RBN:DATA,$line" if isdbg('rbn');
202         }
203
204         # periodic clearing out of the two caches
205         if (($tim % 60 == 0 && $tim > $self->{last}) || ($self->{last} && $tim >= $self->{last} + 60)) {
206                 my $count = 0;
207                 my $removed = 0;
208
209                 while (my ($k,$v) = each %{$d}) {
210                         if ($tim-$v > 60) {
211                                 delete $d->{$k};
212                                 ++$removed
213                         } else {
214                                 ++$count;
215                         }
216                 }
217                 dbg "RBN:ADMIN,rbn cache: $removed removed $count remain" if isdbg('rbn');
218                 $count = $removed = 0;
219                 while (my ($k,$v) = each %{$spot}) {
220                         if ($tim-$v > $self->{minspottime}*2) {
221                                 delete $spot->{$k};
222                                 ++$removed;
223                         } else {
224                                 ++$count;
225                         }
226                 }
227                 dbg "RBN:ADMIN,spot cache: $removed removed $count remain" if isdbg('rbn');
228
229                 dbg "RBN:" . join(',', "STAT", $self->{noraw}, $self->{norbn}, $self->{nospot}) if $self->{showstats};
230                 $self->{noraw} = $self->{norbn} = $self->{nospot} = 0;
231
232                 $self->{last} = int($tim / 60) * 60;
233         }
234 }
235
236
237
238
239 1;