Beispiel #1
0
    def AddToReactor(self, reactor):
        try:
            BotList = []
            ssbot = SubspaceBot(self.host, self.port, self.bname,
                                self.bpassword, self.arena, False, BM_SLAVE,
                                self.MQueue,
                                logging.getLogger("ML." + self.type + ".Core"))
            ssbot.setBotInfo(self.type, self.description, self.owner)
            self.ssbot = ssbot
            ssbot.arena = self.arena  # serexl's bots look at arena in init
            bot = None
            for m in self.modules:
                bot = LoadBot(
                    ssbot, m[0], m[1], self.inifile, self.args,
                    logging.getLogger("ML." + self.type + "." + m[0]))
                if bot:
                    BotList.append(bot)
                bot = None
            ssbot.setBotList(BotList)
            self.listeningport = reactor.listenUDP(0, ssbot)
            ssbot.setReactorData(reactor, self.listeningport)

        except:
            LogException(self.logger)
        finally:
            if ssbot.isConnected():
                ssbot.disconnectFromServer()
            for b in BotList:
                b.Cleanup()
Beispiel #2
0
def MasterMain():
    try:
        #other bots use logging i dont want it to spamm the main logger
        rootlogger = logging.getLogger('')
        rootlogger.addHandler(NullHandler())
        rootlogger.setLevel(logging.DEBUG)
        #logging.basicConfig(level=logging.ERROR,
        #					format='%(asctime)s:%(name)s:%(levelname)s:%(message)s',
        #					datefmt='%m-%d %H:%M')

        logger = logging.getLogger("ML")
        logger.setLevel(logging.DEBUG)

        # set a format
        formatter = logging.Formatter(
            '%(asctime)s:%(name)s:%(levelname)s:%(message)s')

        # define a Handler which writes INFO messages or higher to the sys.stderr
        console = logging.StreamHandler()
        console.setLevel(logging.DEBUG)
        # tell the handler to use this format
        console.setFormatter(formatter)
        # add the handler to the mainloop logger
        logger.addHandler(console)

        filehandler = logging.FileHandler(os.path.join(os.getcwd(),
                                                       "Bots.log"),
                                          mode='a')
        filehandler.setLevel(logging.ERROR)
        filehandler.setFormatter(formatter)
        logger.addHandler(filehandler)

        #command Line Options
        parser = OptionParser()

        parser.add_option("-c",
                          "--ConfigFile",
                          dest="ConfigFile",
                          help="Load Configuration from a non default file",
                          default=os.path.join(os.getcwd(), "Bots.json"))

        parser.add_option(
            "-p",
            "--Password",
            dest="Password",
            help="pass sysop/smod pass by commandline instead of in config",
            default=None)

        (options, args) = parser.parse_args()

        Queue = MasterQue()
        config = GlobalConfiguration(options.ConfigFile, options.Password)
        ssbot = SubspaceBot(config.Host, config.Port, config.MasterName,
                            config.MasterPassword, config.MasterArena, False,
                            BM_MASTER, Queue,
                            logging.getLogger("ML.Master.Core"))
        ssbot.setBotInfo("Master",
                         "MasterBot Manages the starting/stopping of bots",
                         None)
        BotList = []
        #this adds dir's to pythonpath so we can run the dev code out of seperate dirs
        for p in config.paths:
            sys.path.append(p)
        #get the module object for the current file...
        module = sys.modules[globals()['__name__']]
        #loads atleast the masterbot
        md = ModuleData("Master", module, "None", config.ConfigurationFile, "",
                        logging.getLogger("ML.Master"))
        master = Bot(ssbot, md, config, Queue)
        master.setReactor(reactor)

        BotList.append(master)
        #load any bots that are specified in the config
        bot = None
        for m in config.Modules:
            bot = LoadBot(ssbot, m[0], m[1], config.ConfigurationFile, "",
                          logging.getLogger("ML.Master." + m[0]))
            if bot:
                BotList.append(bot)
            bot = None

        ssbot.setBotList(BotList)
        lp = reactor.listenUDP(0, ssbot)
        ssbot.setReactorData(
            reactor, lp
        )  # so clients can disconnect themselves when they get disconnect packet or master kills them
        reactor.run()

    except (KeyboardInterrupt, SystemExit):
        logger.critical("CTRL-c or System.exit() detected")
    except:
        logger.critical("Unhandled Exception")
        LogException(logger)
    finally:
        # 		if 'master' in locals():
        # 			master.StopAllBots()
        if reactor.running:
            reactor.stop()
        for b in BotList:
            b.Cleanup()
        logger.critical("Master Bot behaviors cleansed")
        filehandler.close()
        os._exit(1)