コード例 #1
0
    def __init__(self):
        MultiService.__init__(self)

        # Init shared storage which is used to share information about server
        # to the ouside world
        self.shared_storage = get_storage()

        # Init pilots service
        from commander.service.pilots import PilotService
        self.pilots = PilotService()
        self.pilots.setServiceParent(self)

        # Init objects service
        from commander.service.objects import ObjectsService
        self.objects = ObjectsService()
        self.objects.setServiceParent(self)

        # Init missions service with log watcher
        from commander.service.missions import MissionService
        log_watcher = LogWatchingService(settings.IL2_EVENTS_LOG_PATH)
        self.missions = MissionService(log_watcher)
        self.log_parser = EventLogParser(
            (self.pilots, self.objects, self.missions, ))
        log_watcher.set_parser(self.log_parser)
        self.missions.setServiceParent(self)

        # Init console and DeviceLink parsers
        self.console_parser = ConsoleParser((self.pilots, self.missions, ))
        self.dl_parser = DeviceLinkParser()
コード例 #2
0
class CommanderService(MultiService, CommanderServiceMixin):
    """
    Service which puts together all working services. Works only when the
    connection with server's console is established.
    """
    pilots = None
    objects = None
    missions = None

    console_parser = None
    dl_parser = None
    log_parser = None

    confs = None

    def __init__(self):
        MultiService.__init__(self)

        # Init shared storage which is used to share information about server
        # to the ouside world
        self.shared_storage = get_storage()

        # Init pilots service
        from commander.service.pilots import PilotService
        self.pilots = PilotService()
        self.pilots.setServiceParent(self)

        # Init objects service
        from commander.service.objects import ObjectsService
        self.objects = ObjectsService()
        self.objects.setServiceParent(self)

        # Init missions service with log watcher
        from commander.service.missions import MissionService
        log_watcher = LogWatchingService(settings.IL2_EVENTS_LOG_PATH)
        self.missions = MissionService(log_watcher)
        self.log_parser = EventLogParser(
            (self.pilots, self.objects, self.missions, ))
        log_watcher.set_parser(self.log_parser)
        self.missions.setServiceParent(self)

        # Init console and DeviceLink parsers
        self.console_parser = ConsoleParser((self.pilots, self.missions, ))
        self.dl_parser = DeviceLinkParser()

    def startService(self):
        def on_everyone_kicked(unused):
            MultiService.startService(self)

        self._load_server_config()
        self._share_data()
        self._greet_n_kick_all().addCallback(on_everyone_kicked)

    def _load_server_config(self):
        config = ConfigParser.ConfigParser()
        config.read(settings.IL2_CONFIG_PATH)

        self.confs = {
            'name'      : config.get(   'NET', 'serverName'),
            'user_port' : config.getint('NET', 'localPort'),
            'channels'  : config.getint('NET', 'serverChannels'),
            'difficulty': config.getint('NET', 'difficulty'),
        }

    def _share_data(self):
        self.shared_storage.set(KEY_SERVER_RUNNING, True)
        self.shared_storage.set(KEY_SERVER_NAME, self.confs['name'])
        self.shared_storage.set(KEY_SERVER_LOCAL_ADDRESS,
                                self.cl_client.transport.getHost().host)
        self.shared_storage.set(KEY_SERVER_USER_PORT, self.confs['user_port'])
        self.shared_storage.set(KEY_SERVER_CHANNELS, self.confs['channels'])
        self.shared_storage.set(KEY_SERVER_DIFFICULTY,
                                self.confs['difficulty'])

    @defer.inlineCallbacks
    def _greet_n_kick_all(self):
        """
        Kick every connected user, so information about them will be obtained
        after they reconnect. If there are some connected users, notify them
        about kick during 5 seconds.
        """
        count = yield self.cl_client.user_count()
        if not count:
            LOG.debug("No users to kick")
            defer.returnValue(None)

        def notify_users(seconds_left):
            msg1 = _("Everyone will be kicked in {sec}...").format(
                     sec=seconds_left) if seconds_left else \
                   _("Everyone will be kicked NOW!")
            msg2 = _("Please, reconnect after kick.")
            self.cl_client.chat_all("{0} {1}".format(unicode(msg1),
                                                     unicode(msg2)))

        self.cl_client.chat_all(
            _("Hello everyone! This server is captured by {commander}.")
            .format(commander=unicode(settings.COMMANDER_NAME)))

        LOG.debug("Greeting users with notification about kick")
        from twisted.internet import reactor
        for i in range(5, -1, -1):
            yield task.deferLater(reactor, 1, notify_users, i)

        LOG.debug("Kicking all users")
        self.cl_client.kick_all(self.confs['channels'])

    @defer.inlineCallbacks
    def stopService(self, clean_stop=False):
        # If commander was stopped manually instead of connection was lost
        if clean_stop:
            count = yield self.cl_client.user_count()
            if count:
                LOG.debug("Notifying users about quitting")
                self.cl_client.chat_all(
                    _("{commander} is quitting. Goodbye everyone!").format(
                      commander=unicode(settings.COMMANDER_NAME)))

        yield MultiService.stopService(self)
        if not self.shared_storage.flushdb():
            LOG.error("Failed to flush commander's shared storage")