예제 #1
0
	def __init__(self):
	
		self.runCondition = True
		self.restarting   = False
	
		logging.basicConfig(filename='RoRservices.log',level=logging.DEBUG,format="%(asctime)s|%(name)-12s|%(levelname)-8s| %(message)s",filemode="w")
		self.logger = logging.getLogger('main')
		self.logger.info('LOG STARTED')
		
		self.queue_to_main = Queue.Queue()
		self.RoRclients = {}
		self.RoRqueue = {}

		self.settings = Config("configuration.xml")
		self.main_queue = Queue.Queue()
		self.queue_IRC_in = Queue.Queue()

		if use_irc is True:
			# start IRC bot
			# IRC_bot = IRC_client.IRC_client(channel, nickname, server, port, realname)
			self.IRC_bot = IRC_client.IRC_client(self)
			self.IRC_bot.setName('IRC_thread')
			self.IRC_bot.start()
		
			# We wait until the IRC has successfully connected
			try:
				response = self.queue_to_main.get(True, 30)
				if response[1] == "connect_success":
					self.logger.info("Successfully connected to IRC. Starting RoR clients.")
				elif response[1] == "connect_failure":
					self.logger.critical("Couldn't connect to the IRC server.")
					print "Couldn't connect to the IRC server. Exiting.."
					self.__shutDown()
					sys.exit(1)
				else:
					self.logger.critical("Received an unhandled response from the IRC client, while connecting.")
					print "Received an unhandled response from the IRC client, while connecting. Exiting.."
					self.__shutDown()
					sys.exit(1)
			except Queue.Empty:
				self.logger.critical("Couldn't connect to the IRC server.")
				print "Couldn't connect to the IRC server. Exiting.."
				self.__shutDown()
				sys.exit(1)
			
		time.sleep(2)
		
		self.logger.debug("IRC_bot started, will now start RoR_client(s)")
		RoRclients_tmp = self.settings.getSetting('RoRclients')
		for ID in RoRclients_tmp.keys():
			self.logger.debug("in iteration, ID=%s", ID)
			self.queue_IRC_in.put(("join", self.settings.getSetting('RoRclients', ID, "ircchannel")))
			self.RoRqueue[ID] = Queue.Queue()
			self.RoRclients[ID] = RoR_client.Client(ID, self)
			self.RoRclients[ID].setName('RoR_thread_'+ID)
			self.RoRclients[ID].start()
				
		self.stayAlive()
예제 #2
0
 def startRoRclientOnDemand(self, channel):
     RoRclients_tmp = self.settings.getSetting('RoRclients')
     for ID in list(RoRclients_tmp.keys()):
         if self.settings.getSetting(
                 'RoRclients', ID, "discordchannel") == str(
                     channel) and not self.RoRclients[ID].is_alive():
             self.logger.debug("in iteration, ID=%s", ID)
             self.RoRqueue[ID] = queue.Queue()
             self.RoRclients[ID] = RoR_client.Client(ID, self)
             self.RoRclients[ID].setName('RoR_thread_' + ID)
             self.RoRclients[ID].start()
예제 #3
0
    async def on_ready(self):
        print("Connected to Discord")

        if not self.initialised:
            RoRclients_tmp = self.settings.getSetting('RoRclients')
            for ID in list(RoRclients_tmp.keys()):
                self.logger.debug("in iteration, ID=%s", ID)
                self.RoRqueue[ID] = queue.Queue()
                self.RoRclients[ID] = RoR_client.Client(ID, self)
                self.RoRclients[ID].setName('RoR_thread_' + ID)
                self.RoRclients[ID].start()
            self.initialised = True
예제 #4
0
    def stayAlive(self):
        try:
            while self.runCondition:
                #time.sleep( 1 )
                # do some timer stuff here

                # if not self.IRC_bot.is_alive():
                # print "IRC bot gave up on us, exiting..."
                # for ID in self.RoRclients:
                # if self.RoRclients[ID].is_alive():
                # print "terminating RoRclient %s" % ID
                # self.messageRoRclient(ID, ("disconnect", False))
                # time.sleep(5)
                # sys.exit(0)
                # else:
                # try:
                # self.queue_IRC_in.put_nowait( ("privmsg", "#RigsOfRods", "this is a testmessage") )
                # except Queue.Full:
                # self.logger.warning("queue is full")
                # continue

                try:
                    response = self.queue_to_main.get()
                except Queue.Empty:
                    self.logger.error("Main queue timed out.")
                else:
                    if response[0] == "IRC":
                        if response[1] == "connect_success":
                            pass
                        elif response[1] == "connect_failure":
                            self.logger.critical(
                                "Couldn't connect to the IRC server.")
                            break
                        elif response[1] == "shut_down":
                            break
                        elif response[1] == "connect":
                            for ID in self.settings.getSetting(
                                    'RoRclients').keys():
                                if self.settings.getSetting(
                                        'RoRclients', ID, "ircchannel"
                                ) == response[2] and not self.RoRclients[
                                        ID].is_alive():
                                    self.logger.debug("Starting RoR_client " +
                                                      ID)
                                    self.queue_IRC_in.put(
                                        ("join",
                                         self.settings.getSetting(
                                             'RoRclients', ID, "ircchannel")))
                                    self.RoRqueue[ID] = Queue.Queue()
                                    self.RoRclients[ID] = RoR_client.Client(
                                        ID, self)
                                    self.RoRclients[ID].setName('RoR_thread_' +
                                                                ID)
                                    self.RoRclients[ID].start()

                        elif response[1] == "serverlist":
                            for ID in self.settings.getSetting(
                                    'RoRclients').keys():
                                connected = "Not connected"
                                if self.RoRclients[ID].is_alive():
                                    connected = "connected"
                                self.messageIRCclient(
                                    ("privmsg", response[2],
                                     "%-12s | %-21s | %s" %
                                     (ID, "%s:%d" %
                                      (self.settings.getSetting(
                                          'RoRclients', ID, 'host'),
                                       self.settings.getSetting(
                                           'RoRclients', ID, 'port')),
                                      connected), "syst"))
                        else:
                            self.logger.error(
                                "Received an unhandled message from the IRC client."
                            )

                    else:
                        pass

        except (KeyboardInterrupt, SystemExit):
            print "Terminating on demand..."
        self.__shutDown()
        sys.exit(0)