コード例 #1
0
    def __init__(self):
        self.cl_connector = None
        self.dl_connector = None

        self.commander = CommanderService()
        self.commander.parent = self

        # Prepare DeviceLink client
        self.dl_client = DeviceLinkClient(
            address=(settings.IL2_CONNECTION['host'],
                     settings.IL2_CONNECTION['dl_port']),
            parser=self.commander.dl_parser,
            timeout_value=settings.COMMANDER_TIMEOUT['device_link'])

        # Prepare for connection with server
        self.client_factory = ConsoleClientFactory(
            parser=self.commander.console_parser,
            timeout_value=settings.COMMANDER_TIMEOUT['console'])

        # Prepare API listener
        self.api_service = APIService()
        self.api_service.parent = self
コード例 #2
0
class RootService(Service):
    """
    Main service which manages connections and all main work.
    """
    dl_client = None

    def __init__(self):
        self.cl_connector = None
        self.dl_connector = None

        self.commander = CommanderService()
        self.commander.parent = self

        # Prepare DeviceLink client
        self.dl_client = DeviceLinkClient(
            address=(settings.IL2_CONNECTION['host'],
                     settings.IL2_CONNECTION['dl_port']),
            parser=self.commander.dl_parser,
            timeout_value=settings.COMMANDER_TIMEOUT['device_link'])

        # Prepare for connection with server
        self.client_factory = ConsoleClientFactory(
            parser=self.commander.console_parser,
            timeout_value=settings.COMMANDER_TIMEOUT['console'])

        # Prepare API listener
        self.api_service = APIService()
        self.api_service.parent = self

    @property
    def cl_client(self):
        """
        Get instance of concole protocol.
        """
        return self.client_factory.client

    def startService(self):
        """
        Start API requests listening port and UPD port for communicating with
        server via DeviceLink interface.

        This method is called by application.
        """
        self.api_service.startService()
        self.dl_client.on_start.addCallback(self.startConsoleConnection)

        from twisted.internet import reactor
        self.dl_connector = reactor.listenUDP(0, self.dl_client)

    def startConsoleConnection(self, unused):
        """
        Start reliable connection to game server's console with reconnectinon
        support.
        """
        self._update_connection_callbacks()

        from twisted.internet import reactor
        self.cl_connector = reactor.connectTCP(
            settings.IL2_CONNECTION['host'],
            settings.IL2_CONNECTION['cl_port'], self.client_factory)

    @defer.inlineCallbacks
    def stopService(self):
        """
        Stop everything. This method is called automatically when the reactor
        is stopped.
        """
        # Stop commander service if running
        if self.commander.running:
            yield self.commander.stopService(clean_stop=True)

        # Stop API listener
        yield self.api_service.stopService()

        # Stop DeviceLink UDP listener
        yield defer.maybeDeferred(self.dl_connector.stopListening)

        # Disconnect from game server's console, if connecting was started
        # or if connection was already established
        if self.cl_connector:
            self.client_factory.stopTrying()
            self.cl_connector.disconnect()

    def stop(self):
        """
        Public method for stopping the whole commander.
        """
        from twisted.internet import reactor
        if reactor.running:
            reactor.stop()

    def _update_connection_callbacks(self):
        """
        Update callbacks which are called after the connection with game
        server's console is established or lost.
        """
        self.client_factory.on_connected.addCallback(self.on_connected)
        self.client_factory.on_disconnected.addErrback(self.on_disconnected)

    def on_connected(self, client):
        """
        This method is called after the connection with server's console is
        established. Main work starts from here.
        """
        self.commander.startService()

    def on_disconnected(self, reason):
        """
        This method is called after the connection with server's console is
        lost. Stop every work and clean up resources.
        """
        self._update_connection_callbacks()
        return self.commander.stopService()