comparison irclib.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 ebf516b2e5c9
comparison
equal deleted inserted replaced
23:abdb7a2b6c6d 24:4e1f27ea527b
67 import socket 67 import socket
68 import string 68 import string
69 import sys 69 import sys
70 import time 70 import time
71 import types 71 import types
72 import threading
72 73
73 VERSION = 0, 4, 8 74 VERSION = 0, 4, 8
74 DEBUG = 0 75 DEBUG = 0
75 76
76 # TODO 77 # TODO
187 See documentation for IRC.__init__. 188 See documentation for IRC.__init__.
188 """ 189 """
189 for s in sockets: 190 for s in sockets:
190 for c in self.connections: 191 for c in self.connections:
191 if s == c._get_socket(): 192 if s == c._get_socket():
193 c.lock.acquire()
192 c.process_data() 194 c.process_data()
195 c.lock.release()
193 196
194 def process_timeout(self): 197 def process_timeout(self):
195 """Called when a timeout notification is due. 198 """Called when a timeout notification is due.
196 199
197 See documentation for IRC.__init__. 200 See documentation for IRC.__init__.
334 if handler[1](connection, event) == "NO MORE": 337 if handler[1](connection, event) == "NO MORE":
335 return 338 return
336 339
337 def _remove_connection(self, connection): 340 def _remove_connection(self, connection):
338 """[Internal]""" 341 """[Internal]"""
339 self.connections.remove(connection) 342 if connection in self.connections:
343 self.connections.remove(connection)
340 if self.fn_to_remove_socket: 344 if self.fn_to_remove_socket:
341 self.fn_to_remove_socket(connection._get_socket()) 345 self.fn_to_remove_socket(connection._get_socket())
342 346
343 _rfc_1459_command_regexp = re.compile("^(:(?P<prefix>[^ ]+) +)?(?P<command>[^ ]+)( *(?P<argument> .+))?") 347 _rfc_1459_command_regexp = re.compile("^(:(?P<prefix>[^ ]+) +)?(?P<command>[^ ]+)( *(?P<argument> .+))?")
344 348
419 423
420 This function can be called to reconnect a closed connection. 424 This function can be called to reconnect a closed connection.
421 425
422 Returns the ServerConnection object. 426 Returns the ServerConnection object.
423 """ 427 """
428
429
424 if self.connected == True: 430 if self.connected == True:
431 self.lock.acquire()
425 self.used_by += 1 432 self.used_by += 1
426 self.irclibobj.bot.error('===> Debug: using existing IRC connection for '+str(self)+', this connection is now used by '+str(self.used_by)+' bridges', debug=True) 433 self.irclibobj.bot.error('===> Debug: using existing IRC connection for '+str(self)+', this connection is now used by '+str(self.used_by)+' bridges', debug=True)
427 self.nick(self.real_nickname, callback=nick_callback) 434 self.nick(self.real_nickname, callback=nick_callback)
428 return 435 self.lock.release()
429 436 return self
430 437
438
439 self.lock = threading.Lock()
440 self.lock.acquire()
431 self.nick_callbacks = [] 441 self.nick_callbacks = []
432 self.previous_buffer = "" 442 self.previous_buffer = ""
433 self.handlers = {} 443 self.handlers = {}
434 self.real_server_name = "" 444 self.real_server_name = ""
435 self.real_nickname = self.nickname 445 self.real_nickname = self.nickname
462 # Log on... 472 # Log on...
463 if self.password: 473 if self.password:
464 self.pass_(self.password) 474 self.pass_(self.password)
465 self.nick(self.nickname, callback=nick_callback) 475 self.nick(self.nickname, callback=nick_callback)
466 self.user(self.username, self.ircname) 476 self.user(self.username, self.ircname)
477 self.lock.release()
467 return self 478 return self
468 479
469 480
470 def _call_nick_callbacks(self, error, arguments=[]): 481 def _call_nick_callbacks(self, error, arguments=[]):
471 i = 0 482 i = 0