Exemplo n.º 1
0
    def __init__(self, receiver, server, outputs, freq):
        self.receiver = receiver
        self.server = server
        self.outputs = outputs
        self.freq = freq

        self.aircraft = {}
        self.requested_traffic = set()
        self.df_handlers = {
            -1: self.received_clock_reset_marker,
            0: self.received_df_misc,
            4: self.received_df_misc,
            5: self.received_df_misc,
            16: self.received_df_misc,
            20: self.received_df_misc,
            21: self.received_df_misc,
            11: self.received_df11,
            17: self.received_df17
        }
        self.next_report = None
        self.next_stats = monotonic_time() + self.stats_interval
        self.next_profile = monotonic_time()

        receiver.coordinator = self
        server.coordinator = self
Exemplo n.º 2
0
    def run_until(self, termination_condition):
        try:
            next_heartbeat = monotonic_time() + 0.5
            next_server_send = monotonic_time()
            while not termination_condition():
                # maybe there are no active sockets and
                # we're just waiting on a timeout
                if asyncore.socket_map:
                    asyncore.loop(timeout=0.1, count=5)
                else:
                    time.sleep(0.5)

                now = monotonic_time()
                if now >= next_heartbeat:
                    next_heartbeat = now + 0.5
                    self.heartbeat(now)

                if now >= next_server_send:
                    self.server_send = 1
                    next_server_send = now + 0.25

        finally:
            self.receiver.disconnect('Client shutting down')
            self.server.disconnect('Client shutting down')
            for o in self.outputs:
                o.disconnect('Client shutting down')
Exemplo n.º 3
0
    def __init__(self, receiver, server, outputs, freq):
        self.receiver = receiver
        self.server = server
        self.outputs = outputs
        self.freq = freq

        self.aircraft = {}
        self.requested_traffic = set()
        self.reported = set()
        self.df_handlers = {
            _modes.DF_EVENT_MODE_CHANGE: self.received_mode_change_event,
            _modes.DF_EVENT_EPOCH_ROLLOVER: self.received_epoch_rollover_event,
            _modes.DF_EVENT_TIMESTAMP_JUMP: self.received_timestamp_jump_event,
            0: self.received_df_misc,
            4: self.received_df_misc,
            5: self.received_df_misc,
            16: self.received_df_misc,
            20: self.received_df_misc,
            21: self.received_df_misc,
            11: self.received_df11,
            17: self.received_df17
        }
        self.next_report = None
        self.next_stats = monotonic_time() + self.stats_interval
        self.next_profile = monotonic_time()
        self.next_aircraft_update = self.last_aircraft_update = monotonic_time(
        )
        self.recent_jumps = 0

        receiver.coordinator = self
        server.coordinator = self
Exemplo n.º 4
0
    def __init__(self, receiver, server, outputs, freq):
        self.receiver = receiver
        self.server = server
        self.outputs = outputs
        self.freq = freq

        self.aircraft = {}
        self.requested_traffic = set()
        self.reported = set()
        self.df_handlers = {
            _modes.DF_EVENT_MODE_CHANGE: self.received_mode_change_event,
            _modes.DF_EVENT_EPOCH_ROLLOVER: self.received_epoch_rollover_event,
            _modes.DF_EVENT_TIMESTAMP_JUMP: self.received_timestamp_jump_event,
            0: self.received_df_misc,
            4: self.received_df_misc,
            5: self.received_df_misc,
            16: self.received_df_misc,
            20: self.received_df_misc,
            21: self.received_df_misc,
            11: self.received_df11,
            17: self.received_df17
        }
        self.next_report = None
        self.next_stats = monotonic_time() + self.stats_interval
        self.next_profile = monotonic_time()
        self.next_aircraft_update = self.last_aircraft_update = monotonic_time()
        self.recent_jumps = 0

        receiver.coordinator = self
        server.coordinator = self
Exemplo n.º 5
0
    def periodic_stats(self, now):
        log('Receiver status: {0}', self.receiver.state)
        log('Server status:   {0}', self.server.state)
        global_stats.log_and_reset()

        adsb_req = adsb_total = modes_req = modes_total = 0
        now = monotonic_time()
        for ac in self.aircraft.values():
            if ac.messages < 2:
                continue

            if now - ac.last_position_time < self.position_expiry_age:
                adsb_total += 1
                if ac.requested:
                    adsb_req += 1
            else:
                modes_total += 1
                if ac.requested:
                    modes_req += 1

        log('Aircraft: {modes_req} of {modes_total} Mode S, {adsb_req} of {adsb_total} ADS-B used',
            modes_req=modes_req,
            modes_total=modes_total,
            adsb_req=adsb_req,
            adsb_total=adsb_total)

        if self.recent_jumps > 0:
            log('Out-of-order timestamps: {recent}', recent=self.recent_jumps)
            self.recent_jumps = 0
Exemplo n.º 6
0
    def start_connection(self):
        log('Connected to multilateration server at {0}:{1}, handshaking',
            self.host, self.port)
        self.state = 'handshaking'
        self.last_data_received = monotonic_time()

        compress_methods = ['none']
        if self.offer_zlib:
            compress_methods.append('zlib')
            compress_methods.append('zlib2')

        handshake_msg = {
            'version': 3,
            'client_version': mlat.client.version.CLIENT_VERSION,
            'compress': compress_methods,
            'selective_traffic': True,
            'heartbeat': True,
            'return_results': self.return_results,
            'udp_transport': 2 if self.offer_udp else False,
            'return_result_format': 'ecef'
        }
        handshake_msg.update(self.handshake_data)
        if DEBUG:
            log("Handshake: {0}", handshake_msg)
        self.writebuf += (json.dumps(handshake_msg) + '\n').encode(
            'ascii')  # linebuf not used yet
        self.consume_readbuf = self.consume_readbuf_uncompressed
        self.handle_server_line = self.handle_handshake_response
Exemplo n.º 7
0
    def periodic_stats(self, now):
        global_stats.log_and_reset(self)

        adsb_req = adsb_total = modes_req = modes_total = 0
        now = monotonic_time()
        for ac in self.aircraft.values():
            if ac.messages < 2:
                continue

            if ac.adsb_good:
                adsb_total += 1
                if ac.requested:
                    adsb_req += 1
            else:
                modes_total += 1
                if ac.requested:
                    modes_req += 1

        log('Aircraft: {modes_req} of {modes_total} Mode S, {adsb_req} of {adsb_total} ADS-B used',
            modes_req=modes_req,
            modes_total=modes_total,
            adsb_req=adsb_req,
            adsb_total=adsb_total)

        if self.recent_jumps > 0:
            log('Out-of-order timestamps: {recent}', recent=self.recent_jumps)
            self.recent_jumps = 0
Exemplo n.º 8
0
    def handle_read(self):
        try:
            moredata = self.recv(16384)
        except socket.error as e:
            if e.errno == errno.EAGAIN:
                return
            raise

        if not moredata:
            self.close()
            return

        global_stats.receiver_rx_bytes += len(moredata)

        if self.residual:
            moredata = self.residual + moredata

        self.last_data_received = monotonic_time()

        consumed, messages = self.packetize(moredata, self.last_timestamp)
        if consumed < len(moredata):
            self.residual = moredata[consumed:]
            if len(self.residual) > 5120:
                raise RuntimeError('parser broken - buffer not being consumed')
        else:
            self.residual = None

        if messages:
            global_stats.receiver_rx_messages += len(messages)
            self.last_timestamp = messages[-1].timestamp
            self.coordinator.input_received_messages(messages)
Exemplo n.º 9
0
    def __init__(self, addr, connection_factory):
        self.addr = addr
        self.connection_factory = connection_factory

        self.output_channel = None
        self.next_reconnect = monotonic_time()
        self.addrlist = []
Exemplo n.º 10
0
    def start(self, coordinator):
        self.coordinator = coordinator

        self.reader = AdeptReader(self, coordinator)
        self.writer = AdeptWriter(self)

        if self.udp_transport:
            self.udp_transport.start()
            self.send_mlat = self.udp_transport.send_mlat
            self.send_sync = self.udp_transport.send_sync
        else:
            self.send_mlat = self.writer.send_mlat
            self.send_sync = self.writer.send_sync

        self.send_split_sync = None
        self.send_seen = self.writer.send_seen
        self.send_lost = self.writer.send_lost
        self.send_rate_report = self.writer.send_rate_report
        self.send_clock_reset = self.writer.send_clock_reset
        self.send_input_connected = self.writer.send_input_connected
        self.send_input_disconnected = self.writer.send_input_disconnected

        self.state = 'connected'
        self.writer.send_ready()
        self.next_udp_report = util.monotonic_time() + self.UDP_REPORT_INTERVAL
        self.coordinator.server_connected()
Exemplo n.º 11
0
 def server_connected(self):
     self.requested_traffic = set()
     self.newly_seen = set()
     self.aircraft = {}
     self.next_report = monotonic_time() + self.report_interval
     if self.receiver.state != 'ready':
         self.receiver.reconnect()
Exemplo n.º 12
0
    def __init__(self, addr, connection_factory):
        self.addr = addr
        self.connection_factory = connection_factory

        self.output_channel = None
        self.next_reconnect = monotonic_time()
        self.addrlist = []
Exemplo n.º 13
0
    def start(self, coordinator):
        self.coordinator = coordinator

        self.reader = AdeptReader(self, coordinator)
        self.writer = AdeptWriter(self)

        if self.udp_transport:
            self.udp_transport.start()
            self.send_mlat = self.udp_transport.send_mlat
            self.send_sync = self.udp_transport.send_sync
        else:
            self.send_mlat = self.writer.send_mlat
            self.send_sync = self.writer.send_sync

        self.send_split_sync = None
        self.send_seen = self.writer.send_seen
        self.send_lost = self.writer.send_lost
        self.send_rate_report = self.writer.send_rate_report
        self.send_clock_reset = self.writer.send_clock_reset
        self.send_input_connected = self.writer.send_input_connected
        self.send_input_disconnected = self.writer.send_input_disconnected

        self.state = 'connected'
        self.writer.send_ready()
        self.next_udp_report = util.monotonic_time() + self.UDP_REPORT_INTERVAL
        self.coordinator.server_connected()
Exemplo n.º 14
0
    def periodic_stats(self, now):
        log('Receiver status: {0}', self.receiver.state)
        log('Server status:   {0}', self.server.state)
        global_stats.log_and_reset()

        adsb_req = adsb_total = modes_req = modes_total = 0
        now = monotonic_time()
        for ac in self.aircraft.values():
            if ac.messages < 2:
                continue

            if now - ac.last_position_time < self.position_expiry_age:
                adsb_total += 1
                if ac.requested:
                    adsb_req += 1
            else:
                modes_total += 1
                if ac.requested:
                    modes_req += 1

        log('Aircraft: {modes_req} of {modes_total} Mode S, {adsb_req} of {adsb_total} ADS-B used',
            modes_req=modes_req,
            modes_total=modes_total,
            adsb_req=adsb_req,
            adsb_total=adsb_total)

        if self.recent_jumps > 0:
            log('Out-of-order timestamps: {recent}', recent=self.recent_jumps)
            self.recent_jumps = 0
Exemplo n.º 15
0
    def send_position(self, timestamp, addr, lat, lon, alt, callsign, squawk, error_est, nstations):
        now = time.time()

        line = self.template.format(addr=addr,
                                    rcv_date=format_date(timestamp),
                                    rcv_time=format_time(timestamp),
                                    now_date=format_date(now),
                                    now_time=format_time(now),
                                    callsign=csv_quote(callsign) if callsign else '',
                                    altitude=int(alt),
                                    speed='',
                                    heading='',
                                    lat=round(lat, 4),
                                    lon=round(lon, 4),
                                    vrate='',
                                    squawk=csv_quote(squawk) if squawk else '',
                                    fs='',
                                    emerg='',
                                    ident='',
                                    aog='',
                                    error_est=error_est,
                                    nstations=nstations)

        try:
            self.send((line + '\n').encode('ascii'))
        except socket.error:
            self.handle_error()

        self.next_heartbeat = monotonic_time() + self.heartbeat_interval
Exemplo n.º 16
0
 def server_connected(self):
     self.requested_traffic = set()
     self.newly_seen = set()
     self.aircraft = {}
     self.reported = set()
     self.next_report = monotonic_time() + self.report_interval
     if self.receiver.state != 'ready':
         self.receiver.reconnect()
Exemplo n.º 17
0
    def start_connection(self):
        log('Input connected to {0}:{1}', self.host, self.port)
        self.last_data_received = monotonic_time()
        self.state = 'connected'
        self.coordinator.input_connected()

        # synthesize a mode change immediately if we are not autodetecting
        if self.reader.mode is not None:
            self.coordinator.input_received_messages((mode_change_event(self.reader),))
Exemplo n.º 18
0
 def reset(self, now=None):
     if now is None:
         now = monotonic_time()
     self.start = now
     self.server_tx_bytes = 0
     self.server_rx_bytes = 0
     self.server_udp_bytes = 0
     self.receiver_rx_bytes = 0
     self.receiver_rx_messages = 0
     self.mlat_positions = 0
Exemplo n.º 19
0
    def handle_read(self):
        try:
            moredata = self.recv(16384)
        except socket.error as e:
            if e.errno == errno.EAGAIN:
                return
            raise

        if not moredata:
            self.close()
            return

        global_stats.receiver_rx_bytes += len(moredata)

        self.coordinator.copy_received_messages(moredata)

        if self.residual:
            moredata = self.residual + moredata

        self.last_data_received = monotonic_time()

        try:
            consumed, messages, pending_error = self.feed(moredata)
        except ValueError as e:
            log("Parsing receiver data failed: {e}", e=str(e))
            self.reconnect_interval = 5.0
            self.close()
            return

        if consumed < len(moredata):
            self.residual = moredata[consumed:]
            if len(self.residual) > 5120:
                raise RuntimeError('parser broken - buffer not being consumed')
        else:
            self.residual = None

        global_stats.receiver_rx_messages += self.reader.received_messages
        global_stats.receiver_rx_filtered += self.reader.suppressed_messages
        self.reader.received_messages = self.reader.suppressed_messages = 0

        if messages:
            self.coordinator.input_received_messages(messages)

        if pending_error:
            # call it again to get the exception
            # now that we've handled all the messages
            try:
                if self.residual is None:
                    self.feed(b'')
                else:
                    self.feed(self.residual)
            except ValueError as e:
                log("Parsing receiver data failed: {e}", e=str(e))
                self.close()
                return
Exemplo n.º 20
0
    def start_connection(self):
        log('Input connected to {0}:{1}', self.host, self.port)
        self.last_data_received = monotonic_time()
        self.state = 'connected'
        self.coordinator.input_connected()

        # synthesize a mode change immediately if we are not autodetecting
        if self.reader.mode is not None:
            self.coordinator.input_received_messages((mode_change_event(self.reader),))

        self.send_settings_message()
Exemplo n.º 21
0
 def reset(self, now=None):
     if now is None:
         now = monotonic_time()
     self.start = now
     self.server_tx_bytes = 0
     self.server_rx_bytes = 0
     self.server_udp_bytes = 0
     self.receiver_rx_bytes = 0
     self.receiver_rx_messages = 0
     self.receiver_rx_filtered = 0
     self.mlat_positions = 0
Exemplo n.º 22
0
    def update_receiver_filter(self):
        now = monotonic_time()

        mlat = set()
        for icao in self.requested_traffic:
            ac = self.aircraft.get(icao)
            if not ac or (now - ac.last_position_time > self.position_expiry_age):
                # requested, and we have not seen a recent ADS-B message from it
                mlat.add(icao)

        self.receiver.update_filter(mlat)
Exemplo n.º 23
0
    def __init__(self, receiver, server, outputs, freq, allow_anon,
                 allow_modeac):
        self.receiver = receiver
        self.server = server
        self.outputs = outputs
        self.freq = freq
        self.allow_anon = allow_anon
        self.allow_modeac = allow_modeac

        self.aircraft = {}
        self.requested_traffic = set()
        self.requested_modeac = set()
        self.reported = set()
        self.df_handlers = {
            _modes.DF_EVENT_MODE_CHANGE: self.received_mode_change_event,
            _modes.DF_EVENT_EPOCH_ROLLOVER: self.received_epoch_rollover_event,
            _modes.DF_EVENT_TIMESTAMP_JUMP: self.received_timestamp_jump_event,
            _modes.DF_EVENT_RADARCAPE_POSITION:
            self.received_radarcape_position_event,
            0: self.received_df_misc,
            4: self.received_df_misc,
            5: self.received_df_misc,
            16: self.received_df_misc,
            20: self.received_df_misc,
            21: self.received_df_misc,
            11: self.received_df11,
            17: self.received_df17,
            _modes.DF_MODEAC: self.received_modeac
        }
        self.next_report = None
        self.next_stats = monotonic_time() + self.stats_interval
        self.next_profile = monotonic_time()
        self.next_aircraft_update = self.last_aircraft_update = monotonic_time(
        )
        self.recent_jumps = 0
        self.last_jump_message = 0

        self.server_send = 1

        receiver.coordinator = self
        server.coordinator = self
Exemplo n.º 24
0
    def run_until(self, termination_condition):
        try:
            next_heartbeat = monotonic_time() + 0.5
            while not termination_condition():
                # maybe there are no active sockets and
                # we're just waiting on a timeout
                if asyncore.socket_map:
                    asyncore.loop(timeout=0.1, count=5)
                else:
                    time.sleep(0.5)

                now = monotonic_time()
                if now >= next_heartbeat:
                    next_heartbeat = now + 0.5
                    self.heartbeat(now)

        finally:
            self.receiver.disconnect('Client shutting down')
            self.server.disconnect('Client shutting down')
            for o in self.outputs:
                o.disconnect('Client shutting down')
Exemplo n.º 25
0
    def update_receiver_filter(self):
        now = monotonic_time()

        mlat = set()
        for icao in self.requested_traffic:
            ac = self.aircraft.get(icao)
            if not ac or not ac.adsb_good:
                # requested, and we have not seen a recent ADS-B message from it
                mlat.add(icao)

        self.receiver.update_filter(mlat)
        self.receiver.update_modeac_filter(self.requested_modeac)
Exemplo n.º 26
0
    def update_receiver_filter(self):
        now = monotonic_time()

        mlat = set()
        for icao in self.requested_traffic:
            ac = self.aircraft.get(icao)
            if not ac or (now - ac.last_position_time >
                          self.position_expiry_age):
                # requested, and we have not seen a recent ADS-B message from it
                mlat.add(icao)

        self.receiver.update_filter(mlat)
Exemplo n.º 27
0
    def handle_read(self):
        try:
            moredata = self.recv(16384)
        except socket.error as e:
            if e.errno == errno.EAGAIN:
                return
            raise

        if not moredata:
            self.close()
            return

        global_stats.receiver_rx_bytes += len(moredata)

        if self.residual:
            moredata = self.residual + moredata

        self.last_data_received = monotonic_time()

        try:
            consumed, messages, pending_error = self.feed(moredata)
        except ValueError as e:
            log("Parsing receiver data failed: {e}", e=str(e))
            self.close()
            return

        if consumed < len(moredata):
            self.residual = moredata[consumed:]
            if len(self.residual) > 5120:
                raise RuntimeError('parser broken - buffer not being consumed')
        else:
            self.residual = None

        global_stats.receiver_rx_messages += self.reader.received_messages
        global_stats.receiver_rx_filtered += self.reader.suppressed_messages
        self.reader.received_messages = self.reader.suppressed_messages = 0

        if messages:
            self.coordinator.input_received_messages(messages)

        if pending_error:
            # call it again to get the exception
            # now that we've handled all the messages
            try:
                if self.residual is None:
                    self.feed(b'')
                else:
                    self.feed(self.residual)
            except ValueError as e:
                log("Parsing receiver data failed: {e}", e=str(e))
                self.close()
                return
Exemplo n.º 28
0
 def log_and_reset(self):
     now = monotonic_time()
     elapsed = now - self.start
     log('Receiver: {0:6.1f} msg/s received     {1:4.1f}kB/s from receiver',
         self.receiver_rx_messages / elapsed,
         self.receiver_rx_bytes / elapsed / 1000.0)
     log('Server:   {0:6.1f} kB/s from server   {1:4.1f}kB/s TCP to server  {2:4.1f}kB/s UDP to server',
         self.server_rx_bytes / elapsed / 1000.0,
         self.server_tx_bytes / elapsed / 1000.0,
         self.server_udp_bytes / elapsed / 1000.0)
     if self.mlat_positions:
         log('Results:  {0:3.1f} positions/minute',
             self.mlat_positions / elapsed * 60.0)
     self.reset(now)
Exemplo n.º 29
0
    def send_position(self, timestamp, addr, lat, lon, alt, nsvel, ewvel,
                      vrate, callsign, squawk, error_est, nstations, anon,
                      modeac):
        if not self.connected:
            return

        now = time.time()
        if timestamp is None:
            timestamp = now

        if nsvel is not None and ewvel is not None:
            speed = math.sqrt(nsvel**2 + ewvel**2)
            heading = math.degrees(math.atan2(ewvel, nsvel))
            if heading < 0:
                heading += 360
        else:
            speed = None
            heading = None

        if modeac:
            addrtype = '@'
        elif anon:
            addrtype = '~'
        else:
            addrtype = ''

        line = self.template.format(
            addr=addr,
            addrtype=addrtype,
            rcv_date=format_date(timestamp),
            rcv_time=format_time(timestamp),
            now_date=format_date(now),
            now_time=format_time(now),
            callsign=csv_quote(callsign) if callsign else '',
            altitude=int(alt),
            speed=int(speed) if (speed is not None) else '',
            heading=int(heading) if (heading is not None) else '',
            lat=round(lat, 4),
            lon=round(lon, 4),
            vrate=int(vrate) if (vrate is not None) else '',
            squawk=csv_quote(squawk) if (squawk is not None) else '',
            fs='',
            emerg='',
            ident='',
            aog='',
            error_est=round(error_est, 0) if (error_est is not None) else '',
            nstations=nstations if (nstations is not None) else '')

        self.send((line + '\n').encode('ascii'))
        self.next_heartbeat = monotonic_time() + self.heartbeat_interval
Exemplo n.º 30
0
 def read_data(self):
     while True:
         try:
             data = self.port.readline()
             line = data.decode()
             if (line.startswith("$GPGGA")):
                 rmc = pynmea2.parse(line, check=False)
                 lon = rmc.longitude
                 lat = rmc.latitude
                 alt = rmc.altitude
                 if (lon == 0 or lat == 0):
                     continue
                 if (self.coordinator):
                     now = monotonic_time()
                     if now > self.next_update_time:
                         self.coordinator.gps_position_update_event(
                             lat, lon, alt)
                         self.next_update_time = monotonic_time(
                         ) + self.report_interval
         except Exception as e:
             print(
                 "Error:Read GPS Serial Port Data Failed! Stop Read!!! ==>",
                 e)
             break
Exemplo n.º 31
0
    def start_connection(self):
        log('Input connected to {0}:{1}', self.host, self.port)
        self.last_data_received = monotonic_time()
        self.state = 'connected'
        self.coordinator.input_connected()

        # synthesize a mode change immediately if we are not autodetecting
        if self.reader.mode is not None:
            self.coordinator.input_received_messages((mode_change_event(self.reader),))

        # if we are connected to something that is Beast-like (or autodetecting), send a beast settings message
        if self.reader.mode in (None, _modes.BEAST, _modes.RADARCAPE, _modes.RADARCAPE_EMULATED):
            # Binary format, no filters, CRC checks enabled, mode A/C disabled
            settings_message = b'\x1a1C\x1a1d\x1a1f\x1a1j'
            self.send(settings_message)
Exemplo n.º 32
0
    def send_position(self, timestamp, addr, lat, lon, alt, nsvel, ewvel, vrate,
                      callsign, squawk, error_est, nstations, anon, modeac):
        if not self.connected:
            return

        now = time.time()
        if timestamp is None:
            timestamp = now

        if nsvel is not None and ewvel is not None:
            speed = math.sqrt(nsvel ** 2 + ewvel ** 2)
            heading = math.degrees(math.atan2(ewvel, nsvel))
            if heading < 0:
                heading += 360
        else:
            speed = None
            heading = None

        if modeac:
            addrtype = '@'
        elif anon:
            addrtype = '~'
        else:
            addrtype = ''

        line = self.template.format(addr=addr,
                                    addrtype=addrtype,
                                    rcv_date=format_date(timestamp),
                                    rcv_time=format_time(timestamp),
                                    now_date=format_date(now),
                                    now_time=format_time(now),
                                    callsign=csv_quote(callsign) if callsign else '',
                                    altitude=int(alt),
                                    speed=int(speed) if (speed is not None) else '',
                                    heading=int(heading) if (heading is not None) else '',
                                    lat=round(lat, 4),
                                    lon=round(lon, 4),
                                    vrate=int(vrate) if (vrate is not None) else '',
                                    squawk=csv_quote(squawk) if (squawk is not None) else '',
                                    fs='',
                                    emerg='',
                                    ident='',
                                    aog='',
                                    error_est=round(error_est, 0) if (error_est is not None) else '',
                                    nstations=nstations if (nstations is not None) else '')

        self.send((line + '\n').encode('ascii'))
        self.next_heartbeat = monotonic_time() + self.heartbeat_interval
Exemplo n.º 33
0
    def schedule_reconnect(self):
        if self.reconnect_at is None:
            if len(self.addrlist) > 0:
                # we still have more addresses to try
                # nb: asyncore breaks in odd ways if you try
                # to reconnect immediately at this point
                # (pending events for the old socket go to
                # the new socket) so do it in 0.5s time
                # so the caller can clean up the old
                # socket and discard the events.
                interval = 0.5
            else:
                interval = self.reconnect_interval

            log('Reconnecting in {0} seconds', interval)
            self.reconnect_at = monotonic_time() + interval
Exemplo n.º 34
0
    def schedule_reconnect(self):
        if self.reconnect_at is None:
            if len(self.addrlist) > 0:
                # we still have more addresses to try
                # nb: asyncore breaks in odd ways if you try
                # to reconnect immediately at this point
                # (pending events for the old socket go to
                # the new socket) so do it in 0.5s time
                # so the caller can clean up the old
                # socket and discard the events.
                interval = 0.5
            else:
                interval = self.reconnect_interval

            log('Reconnecting in {0} seconds', interval)
            self.reconnect_at = monotonic_time() + interval
Exemplo n.º 35
0
    def received_df_misc(self, message, now):
        ac = self.aircraft.get(message.address)
        if not ac:
            return False  # not a known ICAO

        ac.messages += 1
        ac.last_message_time = monotonic_time()

        if ac.messages < 10:
            return   # wait for more messages
        if not ac.requested:
            return

        # Candidate for MLAT
        if now - ac.last_position_time < self.position_expiry_age:
            return   # reported position recently, no need for mlat
        self.server.send_mlat(message)
Exemplo n.º 36
0
    def handle_read(self):
        try:
            moredata = self.recv(16384)
        except socket.error as e:
            if e.errno == errno.EAGAIN:
                return
            raise

        if not moredata:
            self.close()
            self.schedule_reconnect()
            return

        self.last_data_received = monotonic_time()
        self.readbuf += moredata
        global_stats.server_rx_bytes += len(moredata)
        self.consume_readbuf()
Exemplo n.º 37
0
    def log_and_reset(self):
        now = monotonic_time()
        elapsed = now - self.start

        processed = self.receiver_rx_messages - self.receiver_rx_filtered
        log('Receiver: {0:6.1f} msg/s received     {1:6.1f} msg/s processed ({2:.0f}%)',
            self.receiver_rx_messages / elapsed,
            processed / elapsed,
            0 if self.receiver_rx_messages == 0 else 100.0 * processed / self.receiver_rx_messages)
        log('Server:   {0:6.1f} kB/s from server   {1:4.1f}kB/s TCP to server  {2:6.1f}kB/s UDP to server',
            self.server_rx_bytes / elapsed / 1000.0,
            self.server_tx_bytes / elapsed / 1000.0,
            self.server_udp_bytes / elapsed / 1000.0)
        if self.mlat_positions:
            log('Results:  {0:3.1f} positions/minute',
                self.mlat_positions / elapsed * 60.0)
        self.reset(now)
Exemplo n.º 38
0
    def handle_read(self):
        try:
            moredata = self.recv(16384)
        except socket.error as e:
            if e.errno == errno.EAGAIN:
                return
            raise

        if not moredata:
            self.close()
            self.schedule_reconnect()
            return

        self.last_data_received = monotonic_time()
        self.readbuf += moredata
        global_stats.server_rx_bytes += len(moredata)
        self.consume_readbuf()
Exemplo n.º 39
0
    def send_frame(self, frame):
        """Send a 14-byte message in the Beast binary format, using the magic mlat timestamp"""

        # format:
        #  1A '3'       long frame follows
        #  FF 00 'MLAT' 6-byte timestamp, this is the magic MLAT timestamp
        #  00           signal level
        #  ...          14 bytes of frame data, with 1A bytes doubled

        self.writebuf.extend(b'\x1A3\xFF\x00MLAT\x00')
        if b'\x1a' not in frame:
            self.writebuf.extend(frame)
        else:
            for b in frame:
                if b == 0x1A:
                    self.writebuf.append(b)
                self.writebuf.append(b)

        self.last_write = monotonic_time()
Exemplo n.º 40
0
    def send_frame(self, frame):
        """Send a 14-byte message in the Beast binary format, using the magic mlat timestamp"""

        # format:
        #  1A '3'       long frame follows
        #  FF 00 'MLAT' 6-byte timestamp, this is the magic MLAT timestamp
        #  00           signal level
        #  ...          14 bytes of frame data, with 1A bytes doubled

        self.writebuf.extend(b'\x1A3\xFF\x00MLAT\x00')
        if b'\x1a' not in frame:
            self.writebuf.extend(frame)
        else:
            for b in frame:
                if b == 0x1A:
                    self.writebuf.append(b)
                self.writebuf.append(b)

        self.last_write = monotonic_time()
Exemplo n.º 41
0
    def log_and_reset(self):
        now = monotonic_time()
        elapsed = now - self.start

        processed = self.receiver_rx_messages - self.receiver_rx_filtered
        log(
            'Receiver: {0:6.1f} msg/s received     {1:6.1f} msg/s processed ({2:.0f}%)',
            self.receiver_rx_messages / elapsed, processed / elapsed,
            0 if self.receiver_rx_messages == 0 else 100.0 * processed /
            self.receiver_rx_messages)
        log(
            'Server:   {0:6.1f} kB/s from server   {1:4.1f}kB/s TCP to server  {2:6.1f}kB/s UDP to server',
            self.server_rx_bytes / elapsed / 1000.0,
            self.server_tx_bytes / elapsed / 1000.0,
            self.server_udp_bytes / elapsed / 1000.0)
        if self.mlat_positions:
            log('Results:  {0:3.1f} positions/minute',
                self.mlat_positions / elapsed * 60.0)
        self.reset(now)
Exemplo n.º 42
0
    def reconnect(self):
        if len(self.addrlist) == 0:
            try:
                self.addrlist = socket.getaddrinfo(host=self.addr[0],
                                                   port=self.addr[1],
                                                   family=socket.AF_UNSPEC,
                                                   type=socket.SOCK_STREAM,
                                                   proto=0,
                                                   flags=0)
            except socket.error as e:
                self.log('{ex!s}', ex=e)
                self.next_reconnect = monotonic_time() + self.reconnect_interval
                return

        # try the next available address
        a_family, a_type, a_proto, a_canonname, a_sockaddr = self.addrlist[0]
        del self.addrlist[0]

        self.output_channel = self.connection_factory(self, None, a_family, a_type, a_sockaddr)
        self.output_channel.connect_now()
Exemplo n.º 43
0
    def reconnect(self):
        if len(self.addrlist) == 0:
            try:
                self.addrlist = socket.getaddrinfo(host=self.addr[0],
                                                   port=self.addr[1],
                                                   family=socket.AF_UNSPEC,
                                                   type=socket.SOCK_STREAM,
                                                   proto=0,
                                                   flags=0)
            except socket.error as e:
                self.log('{ex!s}', ex=e)
                self.next_reconnect = monotonic_time() + self.reconnect_interval
                return

        # try the next available address
        a_family, a_type, a_proto, a_canonname, a_sockaddr = self.addrlist[0]
        del self.addrlist[0]

        self.output_channel = self.connection_factory(self, None, a_family, a_type, a_sockaddr)
        self.output_channel.connect_now()
Exemplo n.º 44
0
    def start_connection(self):
        log('Connected to multilateration server at {0}:{1}, handshaking',
            self.host, self.port)
        self.state = 'handshaking'
        self.last_data_received = monotonic_time()

        compress_methods = ['none']
        if self.offer_zlib:
            compress_methods.append('zlib')
            compress_methods.append('zlib2')

        uuid = None
        for path in self.uuid_path:
            try:
                with open(path) as file:
                    uuid = file.readline().rstrip('\n')
                break
            except Exception:
                pass

        handshake_msg = {
            'version': 3,
            'client_version': mlat.client.version.CLIENT_VERSION,
            'compress': compress_methods,
            'selective_traffic': True,
            'heartbeat': True,
            'return_results': self.return_results,
            'udp_transport': 2 if self.offer_udp else False,
            'return_result_format': 'ecef',
            'uuid': uuid
        }
        handshake_msg.update(self.handshake_data)
        if DEBUG:
            log("Handshake: {0}", handshake_msg)
        self.writebuf += (json.dumps(handshake_msg, sort_keys=True) +
                          16 * '        ' + '\n').encode(
                              'ascii')  # linebuf not used yet
        self.consume_readbuf = self.consume_readbuf_uncompressed
        self.handle_server_line = self.handle_handshake_response
Exemplo n.º 45
0
    def start_connection(self):
        log('Connected to multilateration server at {0}:{1}, handshaking', self.host, self.port)
        self.state = 'handshaking'
        self.last_data_received = monotonic_time()

        compress_methods = ['none']
        if self.offer_zlib:
            compress_methods.append('zlib')
            compress_methods.append('zlib2')

        handshake_msg = {'version': 3,
                         'client_version': mlat.client.version.CLIENT_VERSION,
                         'compress': compress_methods,
                         'selective_traffic': True,
                         'heartbeat': True,
                         'return_results': self.return_results,
                         'udp_transport': 2 if self.offer_udp else False,
                         'return_result_format': 'ecef'}
        handshake_msg.update(self.handshake_data)
        self.writebuf += (json.dumps(handshake_msg) + '\n').encode('ascii')   # linebuf not used yet
        self.consume_readbuf = self.consume_readbuf_uncompressed
        self.handle_server_line = self.handle_handshake_response
Exemplo n.º 46
0
    def handle_read(self):
        try:
            moredata = self.recv(16384)
        except socket.error as e:
            if e.errno == errno.EAGAIN:
                return
            raise

        if not moredata:
            self.close()
            return

        global_stats.receiver_rx_bytes += len(moredata)

        if self.residual:
            moredata = self.residual + moredata

        self.last_data_received = monotonic_time()

        try:
            consumed, messages = self.packetize(moredata, self.last_timestamp)
        except _modes.ClockResetError as e:
            log("Problem reading receiver messages: " + str(e))
            log("Ensure that only one receiver is feeding data to this client.")
            log("A single multilateration client cannot handle data from multiple receivers.")
            self.close()
            return

        if consumed < len(moredata):
            self.residual = moredata[consumed:]
            if len(self.residual) > 5120:
                raise RuntimeError('parser broken - buffer not being consumed')
        else:
            self.residual = None

        if messages:
            global_stats.receiver_rx_messages += len(messages)
            self.last_timestamp = messages[-1].timestamp
            self.coordinator.input_received_messages(messages)
Exemplo n.º 47
0
    def log_and_reset(self):
        now = monotonic_time()
        elapsed = now - self.start

        processed = self.receiver_rx_messages - self.receiver_rx_filtered
        log(
            'Receiver: {0:6.1f} msg/s received     {1:6.1f} msg/s processed ({2:.0f}%)',
            self.receiver_rx_messages / elapsed, processed / elapsed,
            0 if self.receiver_rx_messages == 0 else 100.0 * processed /
            self.receiver_rx_messages)
        if self.receiver_rx_mlat:
            log(
                'WARNING: Ignored {0:5d} messages with MLAT magic timestamp (do you have --forward-mlat on?)',
                self.receiver_rx_mlat)
        log(
            'Server:   {0:6.1f} kB/s from server   {1:4.1f}kB/s TCP to server  {2:6.1f}kB/s UDP to server',
            self.server_rx_bytes / elapsed / 1000.0,
            self.server_tx_bytes / elapsed / 1000.0,
            self.server_udp_bytes / elapsed / 1000.0)
        if self.mlat_positions:
            log('Results:  {0:3.1f} positions/minute',
                self.mlat_positions / elapsed * 60.0)
        self.reset(now)
Exemplo n.º 48
0
    def log_and_reset(self, coordinator):
        now = monotonic_time()
        elapsed = now - self.start

        #log('Receiver status: {0}', coordinator.receiver.state)
        #log('Server status:   {0}', coordinator.server.state)

        processed = self.receiver_rx_messages - self.receiver_rx_filtered
        log(
            'Receiver: {3:10s} {0:6.1f} msg/s received     {1:6.1f} msg/s processed ({2:.0f}%)',
            self.receiver_rx_messages / elapsed, processed / elapsed,
            0 if self.receiver_rx_messages == 0 else 100.0 * processed /
            self.receiver_rx_messages, coordinator.receiver.state)
        if self.receiver_rx_mlat:
            log(
                'WARNING: Ignored {0:5d} messages with MLAT magic timestamp (do you have --forward-mlat on?)',
                self.receiver_rx_mlat)
        log(
            'Server:   {0:10s} {1:6.1f} kB/s from server   {2:6.1f} kB/s to server',
            coordinator.server.state, self.server_rx_bytes / elapsed / 1000.0,
            (self.server_tx_bytes + self.server_udp_bytes) / elapsed / 1000.0)
        log('Results:  {0:3.1f} positions/minute',
            self.mlat_positions / elapsed * 60.0)
        self.reset(now)
Exemplo n.º 49
0
 def __init__(self, listener, socket, addr):
     asyncore.dispatcher_with_send.__init__(self, sock=socket)
     self.listener = listener
     self.addr = addr
     self.next_heartbeat = monotonic_time() + self.heartbeat_interval
Exemplo n.º 50
0
 def start_connection(self):
     log('Input connected to {0}:{1}', self.host, self.port)
     self.last_data_received = monotonic_time()
     self.state = 'ready'
     self.coordinator.input_connected()
Exemplo n.º 51
0
 def __init__(self, listener, socket, addr):
     super().__init__(listener, socket, addr)
     self.next_heartbeat = monotonic_time() + self.heartbeat_interval
Exemplo n.º 52
0
 def __init__(self, listener, socket, addr):
     super().__init__(listener, socket, addr)
     self.writebuf = bytearray()
     self.last_write = monotonic_time()
Exemplo n.º 53
0
 def __init__(self, listener, socket, s_family, s_type, addr):
     super().__init__(listener, socket, s_family, s_type, addr)
     self.writebuf = bytearray()
     self.last_write = monotonic_time()
Exemplo n.º 54
0
    def handle_handshake_response(self, response):
        if 'reconnect_in' in response:
            self.reconnect_interval = response['reconnect_in']

        if 'deny' in response:
            log('Server explicitly rejected our connection, saying:')
            for reason in response['deny']:
                log('  {0}', reason)
            raise IOError('Server rejected our connection attempt')

        if 'motd' in response:
            log('Server says: {0}', response['motd'])

        compress = response.get('compress', 'none')
        if response['compress'] == 'none':
            self.fill_writebuf = self.fill_uncompressed
            self.consume_readbuf = self.consume_readbuf_uncompressed
        elif response['compress'] == 'zlib' and self.offer_zlib:
            self.compressor = zlib.compressobj(1)
            self.fill_writebuf = self.fill_zlib
            self.consume_readbuf = self.consume_readbuf_uncompressed
        elif response['compress'] == 'zlib2' and self.offer_zlib:
            self.compressor = zlib.compressobj(1)
            self.decompressor = zlib.decompressobj()
            self.fill_writebuf = self.fill_zlib
            self.consume_readbuf = self.consume_readbuf_zlib
        else:
            raise IOError(
                'Server response asked for a compression method {0}, which we do not support'
                .format(response['compress']))

        self.server_heartbeat_at = monotonic_time() + self.heartbeat_interval

        if 'udp_transport' in response:
            host, port, key = response['udp_transport']
            if not host:
                host = self.host

            self.udp_transport = UdpServerConnection(host, port, key)

            self.send_mlat = self.udp_transport.send_mlat
            self.send_sync = self.udp_transport.send_sync
            self.send_split_sync = self.udp_transport.send_split_sync
        else:
            self.udp_transport = None
            self.send_mlat = self.send_tcp_mlat
            self.send_sync = self.send_tcp_sync
            self.send_split_sync = self.send_tcp_split_sync

        # turn off the sync method we don't want
        if response.get('split_sync', False):
            self.send_sync = None
        else:
            self.send_split_sync = None

        log(
            'Handshake complete: Compression {0}, UDP transport {1}, Split sync {2}',
            compress, self.udp_transport and str(self.udp_transport)
            or 'disabled', self.send_split_sync and 'enabled' or 'disabled')

        self.state = 'ready'
        self.handle_server_line = self.handle_connected_request
        self.coordinator.server_connected()

        # dummy rate report to indicate we'll be sending them
        self.send_rate_report({})
Exemplo n.º 55
0
    def handle_handshake_response(self, response):
        if 'reconnect_in' in response:
            self.reconnect_interval = response['reconnect_in']

        if 'deny' in response:
            log('Server explicitly rejected our connection, saying:')
            for reason in response['deny']:
                log('  {0}', reason)
            raise IOError('Server rejected our connection attempt')

        if 'motd' in response:
            log('Server says: {0}', response['motd'])

        compress = response.get('compress', 'none')
        if response['compress'] == 'none':
            self.fill_writebuf = self.fill_uncompressed
            self.consume_readbuf = self.consume_readbuf_uncompressed
        elif response['compress'] == 'zlib' and self.offer_zlib:
            self.compressor = zlib.compressobj(1)
            self.fill_writebuf = self.fill_zlib
            self.consume_readbuf = self.consume_readbuf_uncompressed
        elif response['compress'] == 'zlib2' and self.offer_zlib:
            self.compressor = zlib.compressobj(1)
            self.decompressor = zlib.decompressobj()
            self.fill_writebuf = self.fill_zlib
            self.consume_readbuf = self.consume_readbuf_zlib
        else:
            raise IOError('Server response asked for a compression method {0}, which we do not support'.format(
                response['compress']))

        self.server_heartbeat_at = monotonic_time() + self.heartbeat_interval

        if 'udp_transport' in response:
            host, port, key = response['udp_transport']
            if not host:
                host = self.host

            self.udp_transport = UdpServerConnection(host, port, key)

            self.send_mlat = self.udp_transport.send_mlat
            self.send_sync = self.udp_transport.send_sync
            self.send_split_sync = self.udp_transport.send_split_sync
        else:
            self.udp_transport = None
            self.send_mlat = self.send_tcp_mlat
            self.send_sync = self.send_tcp_sync
            self.send_split_sync = self.send_tcp_split_sync

        # turn off the sync method we don't want
        if response.get('split_sync', False):
            self.send_sync = None
        else:
            self.send_split_sync = None

        log('Handshake complete.')
        log('  Compression:       {0}', compress)
        log('  UDP transport:     {0}', self.udp_transport and str(self.udp_transport) or 'disabled')
        log('  Split sync:        {0}', self.send_split_sync and 'enabled' or 'disabled')

        self.state = 'ready'
        self.handle_server_line = self.handle_connected_request
        self.coordinator.server_connected()

        # dummy rate report to indicate we'll be sending them
        self.send_rate_report({})
Exemplo n.º 56
0
 def connection_lost(self, child):
     if self.output_channel is child:
         self.output_channel = None
         self.next_reconnect = monotonic_time() + self.reconnect_interval
Exemplo n.º 57
0
 def input_received_messages(self, messages):
     now = monotonic_time()
     for message in messages:
         handler = self.df_handlers.get(message.df)
         if handler:
             handler(message, now)
Exemplo n.º 58
0
 def schedule_reconnect(self):
     if self.reconnect_at is None:
         log("Reconnecting in {0} seconds", self.reconnect_interval)
         self.reconnect_at = monotonic_time() + self.reconnect_interval
Exemplo n.º 59
0
 def __init__(self, port, buand, coor):
     self.port = serial.Serial(port, baudrate=buand)
     self.coordinator = coor
     self.next_update_time = monotonic_time() + self.report_interval
     if not self.port.isOpen():
         self.port.open()
Exemplo n.º 60
0
 def __init__(self, listener, socket, s_family, s_type, addr):
     super().__init__(listener, socket, s_family, s_type, addr)
     self.next_heartbeat = monotonic_time() + self.heartbeat_interval