little changes

This commit is contained in:
Luke Rogers 2013-10-03 16:40:32 +13:00
parent b08c0a46a9
commit d0bf5ecbc8
3 changed files with 33 additions and 24 deletions

View file

@ -19,7 +19,6 @@ signal.signal(signal.SIGINT, exit_gracefully)
# create new bot object
cloudbot = bot.Bot()
cloudbot.logger.debug("Bot initalized, starting main loop.")
while cloudbot.running:
cloudbot.loop()
# start the main loop
cloudbot.run()

View file

@ -61,19 +61,23 @@ class Bot(object):
self.loader = loader.PluginLoader(self)
def loop(self):
def run(self):
"""recieves input from the IRC engine and processes it"""
self.logger.info("Starting main thread.")
while True:
for conn in self.connections.itervalues():
try:
incoming = conn.parsed_queue.get_nowait()
if incoming == StopIteration:
# IRC engine has signalled timeout, so reconnect (ugly)
conn.reconnect()
main.main(self, conn, incoming)
except Queue.Empty:
pass
for conn in self.connections.itervalues():
try:
incoming = conn.parsed_queue.get_nowait()
main.main(self, conn, incoming)
except Queue.Empty:
pass
# if no messages are in the incoming queue, sleep
while all(connection.parsed_queue.empty() for connection in self.connections.itervalues()):
time.sleep(.1)
# if no messages are in the incoming queue, sleep
while all(connection.parsed_queue.empty() for connection in self.connections.itervalues()):
time.sleep(.1)
def setup(self):

View file

@ -125,9 +125,11 @@ class ParseThread(threading.Thread):
# get a message from the input queue
msg = self.input_queue.get()
#if msg == StopIteration:
# self.irc.connect()
# continue
if msg == StopIteration:
# got a StopIteration from the recieve thread, pass it on
# so the main thread can restart the connection
self.parsed_queue.put(StopIteration)
continue
# parse the message
if msg.startswith(":"): # has a prefix
@ -151,7 +153,7 @@ class ParseThread(threading.Thread):
self.output_queue.put(str)
class Connection(object):
class IRCConnection(object):
"""handles an IRC connection"""
def __init__(self, name, host, port, input_queue, output_queue):
self.output_queue = output_queue # lines to be sent out
@ -160,7 +162,7 @@ class Connection(object):
self.conn_name = name
self.host = host
self.port = port
self.timeout = 300
self.timeout = 1
def create_socket(self):
return socket.socket(socket.AF_INET, socket.TCP_NODELAY)
@ -181,16 +183,20 @@ class Connection(object):
time.sleep(.1)
self.socket.close()
def reconnect(self):
self.stop()
self.connect()
class SSLConnection(Connection):
class SSLIRCConnection(IRCConnection):
"""handles a SSL IRC connection"""
def __init__(self, name, host, port, input_queue, output_queue, ignore_cert_errors):
self.ignore_cert_errors = ignore_cert_errors
Connection.__init__(self, name, host, port, input_queue, output_queue)
IRCConnection.__init__(self, name, host, port, input_queue, output_queue)
def create_socket(self):
return wrap_socket(Connection.create_socket(self), server_side=False,
return wrap_socket(IRCConnection.create_socket(self), server_side=False,
cert_reqs=CERT_NONE if self.ignore_cert_errors else
CERT_REQUIRED)
@ -231,7 +237,7 @@ class IRC(object):
self.parse_thread.start()
def create_connection(self):
return Connection(self.name, self.server, self.port,
return IRCConnection(self.name, self.server, self.port,
self.input_queue, self.output_queue)
def stop(self):
@ -284,5 +290,5 @@ class SSLIRC(IRC):
IRC.__init__(self, name, server, nick, port, channels, conf)
def create_connection(self):
return SSLConnection(self.name, self.server, self.port, self.input_queue,
return SSLIRCConnection(self.name, self.server, self.port, self.input_queue,
self.output_queue, self.ignore_cert_errors)