comparison bot.py @ 24:4e1f27ea527b

First hack at locks for thread safety. Some other minor changes. Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Thu, 20 Aug 2009 21:52:52 +0200
parents abdb7a2b6c6d
children a9066c416533
comparison
equal deleted inserted replaced
23:abdb7a2b6c6d 24:4e1f27ea527b
22 version = 0, 1 22 version = 0, 1
23 23
24 24
25 import irclib 25 import irclib
26 import xmppony as xmpp 26 import xmppony as xmpp
27 from threading import Thread 27 import threading
28 from bridge import * 28 from bridge import *
29 from time import sleep 29 from time import sleep
30 import re 30 import re
31 import sys 31 import sys
32 import xml.parsers.expat 32 import xml.parsers.expat
71 71
72 def _xmpp_loop(self): 72 def _xmpp_loop(self):
73 """[Internal] XMPP infinite loop.""" 73 """[Internal] XMPP infinite loop."""
74 while True: 74 while True:
75 try: 75 try:
76 self.xmpp_c.lock.acquire()
76 self.xmpp_c.Process(0.5) 77 self.xmpp_c.Process(0.5)
78 self.xmpp_c.lock.release()
77 try: 79 try:
78 for c in self.xmpp_connections.itervalues(): 80 for c in self.xmpp_connections.itervalues():
79 if hasattr(c, 'Process'): 81 if hasattr(c, 'Process'):
82 c.lock.acquire()
80 c.Process(0.5) 83 c.Process(0.5)
84 c.lock.release()
81 else: 85 else:
82 sleep(0.5) 86 sleep(0.5)
83 except RuntimeError: 87 except RuntimeError:
84 pass 88 pass
85 except xml.parsers.expat.ExpatError: 89 except (xml.parsers.expat.ExpatError, xmpp.protocol.XMLNotWellFormed):
86 self.error('=> Debug: received invalid stanza', debug=True) 90 self.error('=> Debug: received invalid stanza', debug=True)
87 continue 91 continue
88 92
89 93
90 def _xmpp_presence_handler(self, dispatcher, presence): 94 def _xmpp_presence_handler(self, dispatcher, presence):
241 self.error('=> Debug: ignoring '+event.eventtype(), debug=True) 245 self.error('=> Debug: ignoring '+event.eventtype(), debug=True)
242 return 246 return
243 247
244 248
245 nickname = None 249 nickname = None
246 if '!' in event.source(): 250 if event.source() != None:
247 nickname = event.source().split('!')[0] 251 if '!' in event.source():
252 nickname = event.source().split('!')[0]
248 253
249 254
250 # Events that we want to ignore only in some cases 255 # Events that we want to ignore only in some cases
251 if event.eventtype() in ['umode', 'welcome', 'yourhost', 'created', 'myinfo', 'featurelist', 'luserclient', 'luserop', 'luserchannels', 'luserme', 'n_local', 'n_global', 'endofnames', 'luserunknown', 'luserconns']: 256 if event.eventtype() in ['umode', 'welcome', 'yourhost', 'created', 'myinfo', 'featurelist', 'luserclient', 'luserop', 'luserchannels', 'luserme', 'n_local', 'n_global', 'endofnames', 'luserunknown', 'luserconns']:
252 if connection.really_connected == False: 257 if connection.really_connected == False:
413 c.used_by += 1 418 c.used_by += 1
414 self.error('===> Debug: using existing XMPP connection for "'+nickname+'", now used by '+str(c.used_by)+' bridges', debug=True) 419 self.error('===> Debug: using existing XMPP connection for "'+nickname+'", now used by '+str(c.used_by)+' bridges', debug=True)
415 return c 420 return c
416 self.error('===> Debug: opening new XMPP connection for "'+nickname+'"', debug=True) 421 self.error('===> Debug: opening new XMPP connection for "'+nickname+'"', debug=True)
417 c = xmpp.client.Client(self.bare_jid.getDomain(), debug=[]) 422 c = xmpp.client.Client(self.bare_jid.getDomain(), debug=[])
423 c.lock = threading.Lock()
424 c.lock.acquire()
418 self.xmpp_connections[nickname] = c 425 self.xmpp_connections[nickname] = c
419 c.used_by = 1 426 c.used_by = 1
420 c.nickname = nickname 427 c.nickname = nickname
421 c.connect() 428 c.connect()
422 c.auth(self.bare_jid.getNode(), self.password) 429 c.auth(self.bare_jid.getNode(), self.password)
423 c.RegisterHandler('presence', self._xmpp_presence_handler) 430 c.RegisterHandler('presence', self._xmpp_presence_handler)
424 c.RegisterHandler('iq', self._xmpp_iq_handler) 431 c.RegisterHandler('iq', self._xmpp_iq_handler)
425 c.RegisterHandler('message', self._xmpp_message_handler) 432 c.RegisterHandler('message', self._xmpp_message_handler)
426 c.sendInitPresence() 433 c.sendInitPresence()
434 c.lock.release()
427 return c 435 return c
428 436
429 437
430 def close_xmpp_connection(self, nickname): 438 def close_xmpp_connection(self, nickname):
431 self.xmpp_connections[nickname].used_by -= 1 439 self.xmpp_connections[nickname].used_by -= 1