Skip to content
Snippets Groups Projects
Commit 88f3104c authored by Yann Leboulanger's avatar Yann Leboulanger
Browse files

refactor normal and zeroconf Connection objects with a CommonConnection class

parent 960e402c
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
......@@ -92,6 +92,7 @@ def get_proxy_data_from_dict(proxy):
DATA_RECEIVED = 'DATA RECEIVED'
DATA_SENT = 'DATA SENT'
DATA_ERROR = 'DATA ERROR'
DISCONNECTED = 'DISCONNECTED'
DISCONNECTING = 'DISCONNECTING'
......
......@@ -23,7 +23,7 @@
from common.xmpp import dispatcher_nb, simplexml
from common.xmpp.plugin import *
from common.xmpp.simplexml import ustr
from common.xmpp.transports_nb import DATA_RECEIVED, DATA_SENT
from common.xmpp.transports_nb import DATA_RECEIVED, DATA_SENT, DATA_ERROR
from common.zeroconf import zeroconf
from common.xmpp.protocol import *
......@@ -395,6 +395,7 @@ def send(self, packet, is_message=False, now=False):
False, else send it instantly.
If supplied data is unicode string, encode it to utf-8.
'''
print 'ici'
if self.state <= 0:
return
......@@ -416,8 +417,11 @@ def read_timeout(self):
ids = self.client.conn_holder.ids_of_awaiting_messages
if self.fd in ids and len(ids[self.fd]) > 0:
for (id_, thread_id) in ids[self.fd]:
self._owner.Dispatcher.Event('', DATA_ERROR, (self.client.to,
thread_id))
if hasattr(self._owner, 'Dispatcher'):
self._owner.Dispatcher.Event('', DATA_ERROR, (self.client.to,
thread_id))
else:
self._owner.on_not_ok('conenction timeout')
ids[self.fd] = []
self.pollend()
......@@ -578,6 +582,8 @@ def __init__(self, caller):
self.hash_to_port = {}
self.listener = None
self.ids_of_awaiting_messages = {}
self.disconnect_handlers = []
self.disconnecting = False
def connect(self, show, msg):
self.port = self.start_listener(self.caller.port)
......@@ -632,6 +638,9 @@ def zeroconf_init(self, show, msg):
self.last_msg = msg
def disconnect(self):
# to avoid recursive calls
if self.disconnecting:
return
if self.listener:
self.listener.disconnect()
self.listener = None
......@@ -642,6 +651,14 @@ def disconnect(self):
self.roster.zeroconf = None
self.roster._data = None
self.roster = None
self.disconnecting = True
for i in reversed(self.disconnect_handlers):
log.debug('Calling disconnect handler %s' % i)
i()
self.disconnecting = False
def start_disconnect(self):
self.disconnect()
def kill_all_connections(self):
for connection in self.connections.values():
......@@ -720,6 +737,14 @@ def send(self, stanza, is_message=False, now=False, on_ok=None,
P2PClient(None, item['address'], item['port'], self,
[(stanza, is_message)], to, on_ok=on_ok, on_not_ok=on_not_ok)
def RegisterDisconnectHandler(self, handler):
''' Register handler that will be called on disconnect.'''
self.disconnect_handlers.append(handler)
def UnregisterDisconnectHandler(self, handler):
''' Unregister handler that is called on disconnect.'''
self.disconnect_handlers.remove(handler)
def SendAndWaitForResponse(self, stanza, timeout=None, func=None, args=None):
'''
Send stanza and wait for recipient's response to it. Will call transports
......
......@@ -57,10 +57,10 @@
class ConnectionVcard(connection_handlers.ConnectionVcard):
def add_sha(self, p, send_caps = True):
pass
return p
def add_caps(self, p):
pass
return p
def request_vcard(self, jid = None, is_fake_jid = False):
pass
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment