This commit is contained in:
Stephen Dranger 2011-02-19 20:38:06 -06:00
parent 3cbfc193b4
commit 90e3a4155b
5 changed files with 65 additions and 23 deletions

22
irc.py
View file

@ -19,28 +19,37 @@ class PesterIRC(QtCore.QThread):
def IRCConnect(self): def IRCConnect(self):
server = self.config.server() server = self.config.server()
port = self.config.port() port = self.config.port()
self.cli = IRCClient(PesterHandler, host=server, port=int(port), nick=self.mainwindow.profile().handle, real_name='pcc30', blocking=True) self.cli = IRCClient(PesterHandler, host=server, port=int(port), nick=self.mainwindow.profile().handle, real_name='pcc30', blocking=True, timeout=5)
self.cli.command_handler.parent = self self.cli.command_handler.parent = self
self.cli.command_handler.mainwindow = self.mainwindow self.cli.command_handler.mainwindow = self.mainwindow
self.conn = self.cli.connect() self.conn = self.cli.connect()
self.brokenConnection = False self.stopIRC = None
self.registeredIRC = False self.registeredIRC = False
def run(self): def run(self):
self.IRCConnect() self.IRCConnect()
while 1: while 1:
try: try:
self.updateIRC() res = self.updateIRC()
except socket.timeout, se:
print "timeout in thread %s" % (self)
self.cli.close()
self.stopIRC = se
return
except socket.error, se: except socket.error, se:
if self.registeredIRC: if self.registeredIRC:
self.stopIRC = None self.stopIRC = None
else: else:
self.stopIRC = se self.stopIRC = se
return return
else:
if not res:
return
def setConnected(self): def setConnected(self):
self.registeredIRC = True self.registeredIRC = True
self.connected.emit() self.connected.emit()
def setConnectionBroken(self): def setConnectionBroken(self):
print "setconnection broken"
self.reconnectIRC() self.reconnectIRC()
#self.brokenConnection = True #self.brokenConnection = True
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
@ -49,15 +58,18 @@ class PesterIRC(QtCore.QThread):
res = self.conn.next() res = self.conn.next()
except socket.timeout, se: except socket.timeout, se:
if self.registeredIRC: if self.registeredIRC:
return return True
else: else:
raise se raise se
except socket.error, se: except socket.error, se:
raise se raise se
except StopIteration: except StopIteration:
pass return True
else:
return res
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def reconnectIRC(self): def reconnectIRC(self):
print "reconnectIRC() from thread %s" % (self)
self.cli.close() self.cli.close()
@QtCore.pyqtSlot(PesterProfile) @QtCore.pyqtSlot(PesterProfile)

BIN
irc.pyc

Binary file not shown.

View file

@ -82,7 +82,6 @@ class IRCClient:
... ...
""" """
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "initalizing socket %d" % (self.socket.fileno())
self.nick = None self.nick = None
self.real_name = None self.real_name = None
self.host = None self.host = None
@ -155,9 +154,8 @@ class IRCClient:
self.socket.connect(("%s" % self.host, self.port)) self.socket.connect(("%s" % self.host, self.port))
if not self.blocking: if not self.blocking:
self.socket.setblocking(0) self.socket.setblocking(0)
#if self.timeout: if self.timeout:
# self.socket.settimeout(self.timeout) self.socket.settimeout(self.timeout)
self.socket.settimeout(10)
helpers.nick(self, self.nick) helpers.nick(self, self.nick)
helpers.user(self, self.nick, self.real_name) helpers.user(self, self.nick, self.real_name)
@ -169,8 +167,14 @@ class IRCClient:
try: try:
buffer += self.socket.recv(1024) buffer += self.socket.recv(1024)
except socket.timeout, e: except socket.timeout, e:
if self._end:
break
print "timeout in client.py"
raise e raise e
except socket.error, e: except socket.error, e:
if self._end:
break
print "error %s" % e
try: # a little dance of compatibility to get the errno try: # a little dance of compatibility to get the errno
errno = e.errno errno = e.errno
except AttributeError: except AttributeError:
@ -180,6 +184,8 @@ class IRCClient:
else: else:
raise e raise e
else: else:
if self._end:
break
if len(buffer) == 0 and self.blocking: if len(buffer) == 0 and self.blocking:
raise socket.error("Connection closed") raise socket.error("Connection closed")
@ -198,18 +204,21 @@ class IRCClient:
except socket.timeout, se: except socket.timeout, se:
raise se raise se
except socket.error, se: except socket.error, se:
print "problem: %s" % (se)
if self.socket: if self.socket:
logging.info('closing socket') logging.info('error: closing socket')
self.socket.close() self.socket.close()
raise se raise se
else: else:
if self.socket: if self.socket:
logging.info('closing socket') logging.info('finished: closing socket')
self.socket.close() self.socket.close()
yield False
def close(self): def close(self):
# with extreme prejudice # with extreme prejudice
if self.socket: if self.socket:
logging.info('shutdown socket') logging.info('shutdown socket')
self._end = True
self.socket.shutdown(socket.SHUT_RDWR) self.socket.shutdown(socket.SHUT_RDWR)
class IRCApp: class IRCApp:

Binary file not shown.

View file

@ -1725,9 +1725,9 @@ class MainProgram(QtCore.QObject):
self.trayicon, self.trayicon,
QtCore.SLOT('mainWindowClosed()')) QtCore.SLOT('mainWindowClosed()'))
self.attempts = 0
self.irc = PesterIRC(self.widget.config, self.widget) self.irc = PesterIRC(self.widget.config, self.widget)
self.connect(self.irc, QtCore.SIGNAL('finished()'),
self, QtCore.SLOT('restartIRC()'))
self.connectWidgets(self.irc, self.widget) self.connectWidgets(self.irc, self.widget)
widget2irc = [('sendMessage(QString, QString)', widget2irc = [('sendMessage(QString, QString)',
@ -1778,6 +1778,10 @@ class MainProgram(QtCore.QObject):
'timeCommand(QString, QString, QString)') 'timeCommand(QString, QString, QString)')
] ]
def connectWidgets(self, irc, widget): def connectWidgets(self, irc, widget):
self.connect(irc, QtCore.SIGNAL('finished()'),
self, QtCore.SLOT('restartIRC()'))
self.connect(irc, QtCore.SIGNAL('connected()'),
self, QtCore.SLOT('connected()'))
for c in self.widget2irc: for c in self.widget2irc:
self.connect(widget, QtCore.SIGNAL(c[0]), self.connect(widget, QtCore.SIGNAL(c[0]),
irc, QtCore.SLOT(c[1])) irc, QtCore.SLOT(c[1]))
@ -1791,6 +1795,10 @@ class MainProgram(QtCore.QObject):
for c in self.irc2widget: for c in self.irc2widget:
self.disconnect(irc, QtCore.SIGNAL(c[0]), self.disconnect(irc, QtCore.SIGNAL(c[0]),
widget, QtCore.SLOT(c[1])) widget, QtCore.SLOT(c[1]))
self.disconnect(irc, QtCore.SIGNAL('connected()'),
self, QtCore.SLOT('connected()'))
self.disconnect(self.irc, QtCore.SIGNAL('finished()'),
self, QtCore.SLOT('restartIRC()'))
def showLoading(self, widget, msg="CONN3CT1NG"): def showLoading(self, widget, msg="CONN3CT1NG"):
self.widget.show() self.widget.show()
@ -1806,27 +1814,40 @@ class MainProgram(QtCore.QObject):
sys.exit(0) sys.exit(0)
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def connected(self):
self.attempts = 0
@QtCore.pyqtSlot()
def tryAgain(self): def tryAgain(self):
if self.loadingscreen: if self.widget.loadingscreen:
self.loadingscreen.accept() self.widget.loadingscreen.accept()
self.attempts += 1
if hasattr(self, 'irc') and self.irc:
print "tryagain: reconnectIRC()"
self.irc.reconnectIRC()
print "finishing"
self.irc.quit()
else:
print "tryagain: restartIRC"
self.restartIRC() self.restartIRC()
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def restartIRC(self): def restartIRC(self):
if self.irc: if hasattr(self, 'irc') and self.irc:
self.disconnectWidgets(self.irc, self.widget) self.disconnectWidgets(self.irc, self.widget)
self.disconnect(self.irc, QtCore.SIGNAL('finished()'),
self, QtCore.SLOT('restartIRC()'))
stop = self.irc.stopIRC stop = self.irc.stopIRC
del self.irc del self.irc
else: else:
stop = None stop = None
if not stop: if not stop:
self.irc = PesterIRC(self.widget.config, self.widget) self.irc = PesterIRC(self.widget.config, self.widget)
self.connect(self.irc, QtCore.SIGNAL('finished()'),
self, QtCore.SLOT('restartIRC()'))
self.connectWidgets(self.irc, self.widget) self.connectWidgets(self.irc, self.widget)
self.irc.start() self.irc.start()
self.showLoading(self.widget) if self.attempts == 1:
msg = "R3CONN3CT1NG"
elif self.attempts > 1:
msg = "R3CONN3CT1NG %d" % (self.attempts)
else:
msg = "CONN3CT1NG"
self.showLoading(self.widget, msg)
else: else:
self.showLoading(self.widget, "F41L3D: %s" % stop) self.showLoading(self.widget, "F41L3D: %s" % stop)