comparison muc.py @ 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 32a35f7eff70
children 22cfc31f9c90
comparison
equal deleted inserted replaced
39:3b06450d60cd 40:faa468731d8a
41 self.jid = self.room_jid+'/'+nickname 41 self.jid = self.room_jid+'/'+nickname
42 self.nickname = nickname 42 self.nickname = nickname
43 self.xmpp_c = xmpp_c 43 self.xmpp_c = xmpp_c
44 self.callback = callback 44 self.callback = callback
45 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) 45 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler)
46 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'})])])) 46 self.xmpp_c.lock.acquire()
47 try:
48 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'})])]))
49 except IOError:
50 print 'IOError, reconnecting ...'
51 self.xmpp_c.reconnectAndReauth()
52 self.join(xmpp_c, nickname, status=status, callback=callback)
53 self.xmpp_c.lock.release()
47 54
48 55
49 def _xmpp_presence_handler(self, xmpp_c, presence): 56 def _xmpp_presence_handler(self, xmpp_c, presence):
50 if presence.getFrom() == self.jid: 57 if presence.getFrom() == self.jid:
51 errors = [] 58 errors = []
88 self.callback(errors) 95 self.callback(errors)
89 96
90 97
91 def say(self, message): 98 def say(self, message):
92 """Say message in the room""" 99 """Say message in the room"""
93 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message)) 100 self.xmpp_c.lock.acquire()
101 try:
102 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid, typ='groupchat', body=message))
103 except IOError:
104 print 'IOError, reconnecting ...'
105 self.xmpp_c.reconnectAndReauth()
106 self.say(message)
107 self.xmpp_c.lock.release()
94 108
95 109
96 def sayTo(self, to, message): 110 def sayTo(self, to, message):
97 """Send a private message""" 111 """Send a private message"""
98 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message)) 112 self.xmpp_c.lock.acquire()
113 try:
114 self.xmpp_c.send(xmpp.protocol.Message(to=self.room_jid+'/'+to, typ='chat', body=message))
115 except IOError:
116 print 'IOError, reconnecting ...'
117 self.xmpp_c.reconnectAndReauth()
118 self.sayTo(to, message)
119 self.xmpp_c.lock.release()
99 120
100 121
101 def change_nick(self, nickname, status=None, callback=None): 122 def change_nick(self, nickname, status=None, callback=None):
102 """Change nickname""" 123 """Change nickname"""
103 self.jid = self.room_jid+'/'+nickname 124 self.jid = self.room_jid+'/'+nickname
104 self.callback = callback 125 self.callback = callback
105 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler) 126 self.xmpp_c.RegisterHandler('presence', self._xmpp_presence_handler)
106 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status)) 127 self.xmpp_c.lock.acquire()
128 try:
129 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, status=status))
130 except IOError:
131 print 'IOError, reconnecting ...'
132 self.xmpp_c.reconnectAndReauth()
133 self.change_nick(nickname, status=status, callback=callback)
134 self.xmpp_c.lock.release()
107 135
108 136
109 def leave(self, message=''): 137 def leave(self, message=''):
110 """Leave the room""" 138 """Leave the room"""
111 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message)) 139 self.xmpp_c.lock.acquire()
140 try:
141 self.xmpp_c.send(xmpp.protocol.Presence(to=self.jid, typ='unavailable', status=message))
142 except IOError:
143 print 'IOError, reconnecting ...'
144 self.xmpp_c.reconnectAndReauth()
145 self.leave(message=message)
112 self.connected = False 146 self.connected = False
147 self.xmpp_c.lock.release()
113 148
114 149
115 def __del__(self): 150 def __del__(self):
116 if self.connected: 151 if self.connected:
117 self.leave() 152 self.leave()