Ejemplo n.º 1
0
 def ping_db(self):
     try:
         connection.connection.ping()
     except (OperationalError, AttributeError):
         from django import db
         db.close_old_connections()
     call_later(settings.SERVER_DB_KEEPALIVE, self.ping_db)
Ejemplo n.º 2
0
    def startService(self):
        logger.info('[%s] starting on port: %s' %
                    (self.name, settings.SOCKET_SERVER_PORT))

        self.udp_service = reactor.listenUDP(settings.SOCKET_SERVER_PORT,
                                             self.game_protocol)

        call_later(0, self.ping_db)
Ejemplo n.º 3
0
    def __init__(self, game_service):
        self.game_service = game_service
        self.actions = {"auth": self.auth, "ping": self.ping}
        DatagramProtocol.__init__(self)
        self.reset_db()

        call_later(0, self.consume_broadcast_messages)
        call_later(0, self.check_disconnect)
Ejemplo n.º 4
0
    def consume_broadcast_messages(self):
        for address, conn_data in list(self.connections.items()):
            if conn_data.get('player'):
                messages = yield defer_to_thread(
                    get_message_queued_for_client,
                    conn_data['player'].state.address)
                for action, data in messages:
                    self.send(address, action=action, data=data)

        call_later(0, self.consume_broadcast_messages)
Ejemplo n.º 5
0
    def update_remote_addresses_in_current_game(self):
        if self.disconnected:
            return

        if self.state.game:
            addresses_in_current_game = yield defer_to_thread(
                get_clients_from_group, self.state.game.key)
            addresses_in_current_game.remove(self.state.address)
            self.remote_addresses_in_current_game = addresses_in_current_game

        call_later(1, self.update_remote_addresses_in_current_game)
Ejemplo n.º 6
0
    def __init__(self, protocol, state):
        self.disconnected = False
        self.protocol = protocol
        self.state = state

        self.remote_addresses_in_current_game = []
        call_later(1, self.update_remote_addresses_in_current_game)

        self.actions = {
            "move": self.move,
        }
Ejemplo n.º 7
0
    def delayed_listen_loop(self):
        if tasks.Task.has_delayed_task_available():
            try:
                tasks.Task.queue_next_delayed_task(prioritize=True)
            except IndexError:
                log.err('\x1b[0;37;41m' + "Failed delaying next task."
                        " Was presumably already picked "
                        "up" + '\x1b[0m')

            return call_later(0, self.delayed_listen_loop)

        return call_later(self.LISTEN_LOOP_IDLE_INTERVAL,
                          self.delayed_listen_loop)
Ejemplo n.º 8
0
    def check_players(self):
        addresses = yield defer_to_thread(get_clients_from_group,
                                          self.state.key)
        addresses = set(addresses)

        new_players = addresses - self.addresses
        gone_players = self.addresses - addresses

        for address in new_players:
            yield self.add_address(address)
        for address in gone_players:
            yield self.remove_address(address)

        if not self.addresses:
            self.parent.removeService(self)
            return

        call_later(1, self.check_players)
Ejemplo n.º 9
0
    def startService(self):
        log.msg("Setting up tasker engine...")

        call_later(0, self.heartbeat)

        self.delayed_listen_loop()
        self.launch_workers()

        log.msg(
            splash.format(
                **{
                    'idx': self.idx,
                    'handler_count': len(tasks.Task.handlers),
                    'message': 'tasker ready for action',
                    'redis_host': settings.REDIS_HOST,
                    'redis_port': settings.REDIS_PORT,
                    'redis_tasker_db': settings.REDIS_TASKER_DB
                }))
Ejemplo n.º 10
0
    def worker_loop(self, n):
        self.check_for_idle()

        task_data = tasks.Task.fetch_task()

        if not task_data:
            call_later(1, self.worker_loop, n)
            return

        self._last_action_time = time.time()

        try:
            self.process_task(task_data)
        except Exception as e:
            print('\x1b[0;37;41m' + "Task failed.. Continuing loop" +
                  '\x1b[0m')

        call_later(0, self.worker_loop, n)
Ejemplo n.º 11
0
 def startService(self):
     super().startService()
     call_later(0, self.check_players)
Ejemplo n.º 12
0
    def heartbeat(self):
        self.update_mothership()
        self.check_for_idle()

        call_later(self.HEARTBEAT_INTERVAL, self.heartbeat)
Ejemplo n.º 13
0
    def check_disconnect(self):
        for address, conn_data in list(self.connections.items()):
            if time.time() - conn_data['t'] > 10:
                yield self.disconnnect(address)

        call_later(1, self.check_disconnect)