codging standards

This commit is contained in:
Yann Leboulanger 2011-08-21 19:32:52 +02:00
parent 3eb47cded2
commit b5b5e4be27

View file

@ -150,8 +150,8 @@ class P2PClient(IdleObject):
else: else:
self.sock_type = TYPE_CLIENT self.sock_type = TYPE_CLIENT
self.fd = -1 self.fd = -1
conn = P2PConnection('', _sock, host, port, self._caller, self.on_connect, conn = P2PConnection('', _sock, host, port, self._caller,
self) self.on_connect, self)
if not self.conn_holder: if not self.conn_holder:
# An error occured, disconnect() has been called # An error occured, disconnect() has been called
if on_not_ok: if on_not_ok:
@ -166,15 +166,16 @@ class P2PClient(IdleObject):
if is_message: if is_message:
if self.fd == -1: if self.fd == -1:
if on_not_ok: if on_not_ok:
on_not_ok('Connection to host could not be established.') on_not_ok(
'Connection to host could not be established.')
return return
thread_id = stanza.getThread() thread_id = stanza.getThread()
id_ = stanza.getID() id_ = stanza.getID()
if not id_: if not id_:
id_ = self.Dispatcher.getAnID() id_ = self.Dispatcher.getAnID()
if self.conn_holder.ids_of_awaiting_messages.has_key(self.fd): if self.conn_holder.ids_of_awaiting_messages.has_key(self.fd):
self.conn_holder.ids_of_awaiting_messages[self.fd].append((id_, self.conn_holder.ids_of_awaiting_messages[self.fd].append((
thread_id)) id_, thread_id))
else: else:
self.conn_holder.ids_of_awaiting_messages[self.fd] = [(id_, self.conn_holder.ids_of_awaiting_messages[self.fd] = [(id_,
thread_id)] thread_id)]
@ -235,7 +236,8 @@ class P2PClient(IdleObject):
self.Dispatcher._metastream.setNamespace(self.Namespace) self.Dispatcher._metastream.setNamespace(self.Namespace)
self.Dispatcher._metastream.setAttr('version', '1.0') self.Dispatcher._metastream.setAttr('version', '1.0')
self.Dispatcher._metastream.setAttr('xmlns:stream', NS_STREAMS) self.Dispatcher._metastream.setAttr('xmlns:stream', NS_STREAMS)
self.Dispatcher._metastream.setAttr('from', self.conn_holder.zeroconf.name) self.Dispatcher._metastream.setAttr('from',
self.conn_holder.zeroconf.name)
if self.to: if self.to:
self.Dispatcher._metastream.setAttr('to', self.to) self.Dispatcher._metastream.setAttr('to', self.to)
self.Dispatcher.send("<?xml version='1.0'?>%s>" % str( self.Dispatcher.send("<?xml version='1.0'?>%s>" % str(
@ -243,10 +245,12 @@ class P2PClient(IdleObject):
def _check_stream_start(self, ns, tag, attrs): def _check_stream_start(self, ns, tag, attrs):
if ns != NS_STREAMS or tag != 'stream': if ns != NS_STREAMS or tag != 'stream':
log.error('Incorrect stream start: (%s,%s).Terminating!' % (tag, ns), 'error') log.error('Incorrect stream start: (%s,%s).Terminating!' % (tag,
ns), 'error')
self.Connection.disconnect() self.Connection.disconnect()
if self.on_not_ok: if self.on_not_ok:
self.on_not_ok('Connection to host could not be established: Incorrect answer from server.') self.on_not_ok('Connection to host could not be established: '
'Incorrect answer from server.')
return return
if self.sock_type == TYPE_SERVER: if self.sock_type == TYPE_SERVER:
if attrs.has_key('from'): if attrs.has_key('from'):
@ -303,8 +307,8 @@ class P2PClient(IdleObject):
def _register_handlers(self): def _register_handlers(self):
self._caller.peerhost = self.Connection._sock.getsockname() self._caller.peerhost = self.Connection._sock.getsockname()
self.RegisterHandler('message', lambda conn, data:self._caller._messageCB( self.RegisterHandler('message', lambda conn,
self.Server, conn, data)) data:self._caller._messageCB(self.Server, conn, data))
self.RegisterHandler('iq', self._caller._siSetCB, 'set', self.RegisterHandler('iq', self._caller._siSetCB, 'set',
common.xmpp.NS_SI) common.xmpp.NS_SI)
self.RegisterHandler('iq', self._caller._siErrorCB, 'error', self.RegisterHandler('iq', self._caller._siErrorCB, 'error',
@ -321,7 +325,8 @@ class P2PClient(IdleObject):
common.xmpp.NS_DISCO_ITEMS) common.xmpp.NS_DISCO_ITEMS)
self.RegisterHandler('iq', self._caller._JingleCB, 'result') self.RegisterHandler('iq', self._caller._JingleCB, 'result')
self.RegisterHandler('iq', self._caller._JingleCB, 'error') self.RegisterHandler('iq', self._caller._JingleCB, 'error')
self.RegisterHandler('iq', self._caller._JingleCB, 'set', common.xmpp.NS_JINGLE) self.RegisterHandler('iq', self._caller._JingleCB, 'set',
common.xmpp.NS_JINGLE)
class P2PConnection(IdleObject, PlugIn): class P2PConnection(IdleObject, PlugIn):
def __init__(self, sock_hash, _sock, host=None, port=None, caller=None, def __init__(self, sock_hash, _sock, host=None, port=None, caller=None,
@ -353,8 +358,8 @@ class P2PConnection(IdleObject, PlugIn):
self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC, self.ais = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
socket.SOCK_STREAM) socket.SOCK_STREAM)
except socket.gaierror, e: except socket.gaierror, e:
log.info('Lookup failure for %s: %s[%s]', host, e[1], repr(e[0]), log.info('Lookup failure for %s: %s[%s]', host, e[1],
exc_info=True) repr(e[0]), exc_info=True)
else: else:
self.connect_to_next_ip() self.connect_to_next_ip()
@ -392,8 +397,8 @@ class P2PConnection(IdleObject, PlugIn):
def plugout(self): def plugout(self):
""" """
Disconnect from the remote server and unregister self.disconnected method Disconnect from the remote server and unregister self.disconnected
from the owner's dispatcher method from the owner's dispatcher
""" """
self.disconnect() self.disconnect()
self._owner = None self._owner = None
@ -439,8 +444,8 @@ class P2PConnection(IdleObject, PlugIn):
if self.fd in ids and len(ids[self.fd]) > 0: if self.fd in ids and len(ids[self.fd]) > 0:
for (id_, thread_id) in ids[self.fd]: for (id_, thread_id) in ids[self.fd]:
if hasattr(self._owner, 'Dispatcher'): if hasattr(self._owner, 'Dispatcher'):
self._owner.Dispatcher.Event('', DATA_ERROR, (self.client.to, self._owner.Dispatcher.Event('', DATA_ERROR, (
thread_id)) self.client.to, thread_id))
else: else:
self._owner.on_not_ok('conenction timeout') self._owner.on_not_ok('conenction timeout')
ids[self.fd] = [] ids[self.fd] = []
@ -782,10 +787,11 @@ class ClientZeroconf:
""" """
self.disconnect_handlers.remove(handler) self.disconnect_handlers.remove(handler)
def SendAndWaitForResponse(self, stanza, timeout=None, func=None, args=None): def SendAndWaitForResponse(self, stanza, timeout=None, func=None,
args=None):
""" """
Send stanza and wait for recipient's response to it. Will call transports Send stanza and wait for recipient's response to it. Will call
on_timeout callback if response is not retrieved in time transports on_timeout callback if response is not retrieved in time
Be aware: Only timeout of latest call of SendAndWait is active. Be aware: Only timeout of latest call of SendAndWait is active.
""" """