def trafficRequestReceived(self, host, port, key, payload): if len(payload) != 8: return data = struct.unpack('!II', payload) pilot = User.by_tracking_key(key) if pilot is None: log.err("No such pilot: %d" % key) return flags = data[0] or_filters = [] if flags & TRAFFIC_FLAG_FOLLOWEES: subq = DBSession.query(Follower.destination_id) \ .filter(Follower.source_id == pilot.id) \ .subquery() or_filters.append(TrackingFix.pilot_id.in_(subq)) if flags & TRAFFIC_FLAG_CLUB: subq = DBSession.query(User.id) \ .filter(User.club_id == pilot.club_id) \ .subquery() or_filters.append(TrackingFix.pilot_id.in_(subq)) if len(or_filters) == 0: return query = DBSession.query(TrackingFix) \ .distinct(TrackingFix.pilot_id) \ .filter(and_(TrackingFix.time >= datetime.datetime.utcnow() - datetime.timedelta(hours=2), TrackingFix.pilot_id != pilot.id, TrackingFix.location_wkt != None, TrackingFix.altitude != None, or_(*or_filters))) \ .order_by(TrackingFix.pilot_id, desc(TrackingFix.time)) \ .limit(32) response = '' count = 0 for fix in query: location = fix.location if location is None: continue t = fix.time t = t.hour * 3600000 + t.minute * 60000 + t.second * 1000 + t.microsecond / 1000 response += struct.pack('!IIiihHI', fix.pilot_id, t, int(location.latitude * 1000000), int(location.longitude * 1000000), int(fix.altitude), 0, 0) count += 1 response = struct.pack('!HBBI', 0, 0, count, 0) + response response = struct.pack('!IHHQ', MAGIC, 0, TYPE_TRAFFIC_RESPONSE, 0) + response response = set_crc(response) self.transport.write(response, (host, port))
def pingReceived(self, host, port, key, payload): if len(payload) != 8: return id, reserved, reserved2 = struct.unpack('!HHI', payload) flags = 0 pilot = User.by_tracking_key(key) if not pilot: flags |= FLAG_ACK_BAD_KEY data = struct.pack('!IHHQHHI', MAGIC, 0, TYPE_ACK, 0, id, 0, flags) data = set_crc(data) self.transport.write(data, (host, port))