changeset 40:faa468731d8a

Tried to get thread safety and handle disconnections in muc.py Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sun, 23 Aug 2009 22:35:24 +0200
parents 3b06450d60cd
children 4c4f4ee72e17
files bridge.py muc.py
diffstat 2 files changed, 41 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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:
--- 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):