From 2cd936318ddf7a0b3e096f7070ec83a16ddb9e89 Mon Sep 17 00:00:00 2001 From: Sandro Munda Date: Tue, 5 Jun 2012 08:33:47 +0200 Subject: Improved the close of the proxy thread (and the socket) in the xep_0065 plugin. --- sleekxmpp/plugins/xep_0065/proxy.py | 63 +++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'sleekxmpp/plugins/xep_0065/proxy.py') diff --git a/sleekxmpp/plugins/xep_0065/proxy.py b/sleekxmpp/plugins/xep_0065/proxy.py index 865c2f90..86598df3 100644 --- a/sleekxmpp/plugins/xep_0065/proxy.py +++ b/sleekxmpp/plugins/xep_0065/proxy.py @@ -184,33 +184,39 @@ class xep_0065(base_plugin): # Send the IQ. act_iq.send() - def send(self, sid, msg): - """ Sends the msg to the socket. + def deactivate(self, sid): + """ Closes the Proxy thread associated to this SID. + """ + + proxy = self.proxy_threads.get(sid) + if proxy: + proxy.s.close() + del self.proxy_threads[sid] + + def close(self): + """ Closes all Proxy threads. + """ + + for sid, proxy in self.proxy_threads.items(): + proxy.s.close() + del self.proxy_threads[sid] - sid : The SID to retrieve the good proxy stored in the - proxy_threads dict - msg : The message data. + def send(self, sid, data): + """ Sends the data over the Proxy socket associated to the + SID. """ proxy = self.proxy_threads.get(sid) if proxy: - proxy.send(msg) - else: - # TODO: raise an exception. - pass + proxy.s.sendall(data) def on_recv(self, sid, data): - """ Called when receiving data + """ Calls when data is recv from the Proxy socket associated + to the SID. """ - if not data: - try: - del self.proxy_threads[sid] - except KeyError: - # TODO: internal error, raise an exception ? - pass - else: - log.debug('Received data: %s' % data) + proxy = self.proxy_threads.get(sid) + if proxy: self.xmpp.event('socks_recv', data) @@ -275,10 +281,10 @@ class Proxy(Thread): log.info('Socket connected.') self.connected.set() - # Listen for data on the socket + # Blocks until the socket need to be closed. self.listen() - # Listen returns when the socket must be closed. + # Closes the socket. self.s.close() log.info('Socket closed.') @@ -289,7 +295,16 @@ class Proxy(Thread): socket_open = True while socket_open: - ins, out, err = select([self.s, ], [], []) + ins = [] + try: + # Wait any read available data on socket. Timeout + # after 5 secs. + ins, out, err = select([self.s, ], [], [], 5) + except Exception, e: + # There's an error with the socket (maybe the socket + # has been closed and the file descriptor is bad). + log.debug('Socket error: %s' % e) + break for s in ins: data = self.recv_size(self.s) @@ -324,9 +339,3 @@ class Proxy(Thread): total_data.append(sock_data) total_len = sum([len(i) for i in total_data]) return ''.join(total_data) - - def send(self, msg): - """ Sends the data over the socket. - """ - - self.s.sendall(msg) -- cgit v1.2.3