# HG changeset patch # User Charly COSTE # Date 1251059724 -7200 # Node ID faa468731d8a16c0d1240b50968c497073cfbee5 # Parent 3b06450d60cd6f2e4fa8bb71760eeb248d57cbc6 Tried to get thread safety and handle disconnections in muc.py Signed-off-by: Charly COSTE diff --git a/bridge.py b/bridge.py --- a/bridge.py +++ b/bridge.py @@ -54,7 +54,7 @@ class bridge: raise Exception('[Error] "'+mode+'" is not a correct value for a bridge\'s "mode" attribute') self.mode = mode - self.lock = threading.Lock() + self.lock = threading.RLock() # Join XMPP room try: diff --git a/muc.py b/muc.py --- a/muc.py +++ b/muc.py @@ -43,7 +43,14 @@ class muc: self.xmpp_c = xmpp_c self.callback = callback self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) - self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status, payload=[xmpp.simplexml.Node(tag='x', attrs={'xmlns': 'http://jabber.org/protocol/muc'}, payload=[xmpp.simplexml.Node(tag='history', attrs={'maxchars': '0'})])])) + self.xmpp_c.lock.acquire() + try: + self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status, payload=[xmpp.simplexml.Node(tag='x', attrs={'xmlns': 'http://jabber.org/protocol/muc'}, payload=[xmpp.simplexml.Node(tag='history', attrs={'maxchars': '0'})])])) + except IOError: + print 'IOError, reconnecting ...' + self.xmpp_c.reconnectAndReauth() + self.join(xmpp_c, nickname, status=status, callback=callback) + self.xmpp_c.lock.release() def _xmpp_presence_handler(self, xmpp_c, presence): @@ -90,12 +97,26 @@ class muc: def say(self, message): """Say message in the room""" - self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message)) + self.xmpp_c.lock.acquire() + try: + self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message)) + except IOError: + print 'IOError, reconnecting ...' + self.xmpp_c.reconnectAndReauth() + self.say(message) + self.xmpp_c.lock.release() def sayTo(self, to, message): """Send a private message""" - self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message)) + self.xmpp_c.lock.acquire() + try: + self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message)) + except IOError: + print 'IOError, reconnecting ...' + self.xmpp_c.reconnectAndReauth() + self.sayTo(to, message) + self.xmpp_c.lock.release() def change_nick(self, nickname, status=None, callback=None): @@ -103,13 +124,27 @@ class muc: self.jid = self.room_jid+'/'+nickname self.callback = callback self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) - self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status)) + self.xmpp_c.lock.acquire() + try: + self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status)) + except IOError: + print 'IOError, reconnecting ...' + self.xmpp_c.reconnectAndReauth() + self.change_nick(nickname, status=status, callback=callback) + self.xmpp_c.lock.release() def leave(self, message=''): """Leave the room""" - self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message)) + self.xmpp_c.lock.acquire() + try: + self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message)) + except IOError: + print 'IOError, reconnecting ...' + self.xmpp_c.reconnectAndReauth() + self.leave(message=message) self.connected = False + self.xmpp_c.lock.release() def __del__(self):