From b1d206d2ec52521317b7e3023fdf0b477cd41ee4 Mon Sep 17 00:00:00 2001 From: Jefry Lagrange Date: Fri, 24 Aug 2012 20:13:25 -0400 Subject: [PATCH] refactoring --- src/common/jingle.py | 2 +- src/common/jingle_ft.py | 11 +++----- src/common/jingle_session.py | 49 +++++++++++++++++++----------------- src/gui_interface.py | 6 ++--- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/common/jingle.py b/src/common/jingle.py index 770ff9087..57ca2ba06 100644 --- a/src/common/jingle.py +++ b/src/common/jingle.py @@ -163,7 +163,7 @@ class ConnectionJingle(object): transport = JingleTransportIBB() c = JingleFileTransfer(jingle, transport=transport, file_props=file_props, use_security=use_security) - jingle.hash_algo = self.__hash_support(contact) + file_props.algo = self.__hash_support(contact) jingle.add_content('file' + helpers.get_random_string_16(), c) jingle.start_session() return c.transport.sid diff --git a/src/common/jingle_ft.py b/src/common/jingle_ft.py index 641a33bdd..49d875f18 100644 --- a/src/common/jingle_ft.py +++ b/src/common/jingle_ft.py @@ -117,12 +117,9 @@ class JingleFileTransfer(JingleContent): conn=self.session.connection, stanza=stanza, jingle_content=content, FT_content=self)) self._listen_host() - # Delete this after file_props refactoring this shouldn't be necesary - self.session.file_hash = self.file_props.hash_ - self.session.hash_algo = self.file_props.algo def __on_session_initiate_sent(self, stanza, content, error, action): - # Calculate file_hash in a new thread + # Calculate file hash in a new thread # if we haven't sent the hash already. if self.file_props.hash_ is None: self.hashThread = threading.Thread(target=self.__send_hash) @@ -137,7 +134,7 @@ class JingleFileTransfer(JingleContent): def _calcHash(self): # Caculates the hash and returns a xep-300 hash stanza - if self.session.hash_algo == None: + if self.file_props.algo == None: return try: file_ = open(self.file_props.file_name, 'r') @@ -145,14 +142,14 @@ class JingleFileTransfer(JingleContent): # can't open file return h = xmpp.Hashes() - hash_ = h.calculateHash(self.session.hash_algo, file_) + hash_ = h.calculateHash(self.file_props.algo, file_) # DEBUG #hash_ = '1294809248109223' if not hash_: # Hash alogrithm not supported return self.file_props.hash_ = hash_ - h.addHash(hash_, self.session.hash_algo) + h.addHash(hash_, self.file_props.algo) return h def __on_session_accept(self, stanza, content, error, action): diff --git a/src/common/jingle_session.py b/src/common/jingle_session.py index 24aa55b21..41d0d56cd 100644 --- a/src/common/jingle_session.py +++ b/src/common/jingle_session.py @@ -98,34 +98,33 @@ class JingleSession(object): else: self.iq_ids = [] self.accepted = True # is this session accepted by user - # Hash algorithm that we are using to calculate the integrity of the - # file. Could be 'md5', 'sha-1', etc... - self.hash_algo = None - self.file_hash = None # Tells whether this session is a file transfer or not self.session_type_FT = False # callbacks to call on proper contents # use .prepend() to add new callbacks, especially when you're going # to send error instead of ack self.callbacks = { - 'content-accept': [self.__on_content_accept, self.__broadcast, - self.__ack], + 'content-accept': [self.__on_content_accept, + self.__broadcast, self.__ack], 'content-add': [self.__on_content_add, self.__broadcast, - self.__ack], #TODO + self.__ack], #TODO 'content-modify': [self.__ack], #TODO - 'content-reject': [self.__ack, self.__on_content_remove], #TODO + 'content-reject': [self.__ack, self.__on_content_remove], 'content-remove': [self.__ack, self.__on_content_remove], 'description-info': [self.__broadcast, self.__ack], #TODO 'security-info': [self.__ack], #TODO - 'session-accept': [self.__on_session_accept, self.__on_content_accept, - self.__broadcast], - 'session-info': [self.__broadcast, self.__on_session_info, self.__ack], - 'session-initiate': [self.__on_session_initiate, self.__broadcast, - self.__ack], - 'session-terminate': [self.__on_session_terminate, self.__broadcast_all, - self.__ack], + 'session-accept': [self.__on_session_accept, + self.__on_content_accept, + self.__broadcast], + 'session-info': [self.__broadcast, + self.__on_session_info, self.__ack], + 'session-initiate': [self.__on_session_initiate, + self.__broadcast, self.__ack], + 'session-terminate': [self.__on_session_terminate, + self.__broadcast_all, self.__ack], 'transport-info': [self.__broadcast, self.__ack], - 'transport-replace': [self.__broadcast, self.__on_transport_replace], #TODO + 'transport-replace': [self.__broadcast, + self.__on_transport_replace], #TODO 'transport-accept': [self.__ack], #TODO 'transport-reject': [self.__ack], #TODO 'iq-result': [self.__broadcast], @@ -327,7 +326,8 @@ class JingleSession(object): self.__send_error(stanza, 'bad-request') return # FIXME: If we aren't initiated and it's not a session-initiate... - if action not in ['session-initiate','session-terminate'] and self.state == JingleStates.ended: + if action not in ['session-initiate','session-terminate'] \ + and self.state == JingleStates.ended: self.__send_error(stanza, 'item-not-found', 'unknown-session') return else: @@ -364,6 +364,7 @@ class JingleSession(object): elif child.getNamespace() == xmpp.NS_STANZAS: error_name = child.getName() self.__dispatch_error(error_name, text, error.getAttr('type')) + # FIXME: Not sure when we would want to do that... def transport_replace(self): transport = JingleTransportIBB() @@ -416,6 +417,8 @@ class JingleSession(object): def __on_session_info(self, stanza, jingle, error, action): # TODO: ringing, active, (un)hold, (un)mute + if self.state != JingleStates.active: + raise OutOfOrder payload = jingle.getPayload() for p in payload: if p.getName() == 'checksum': @@ -423,13 +426,13 @@ class JingleSession(object): namespace=xmpp.NS_HASHES) algo = hash_.getAttr('algo') if algo in xmpp.Hashes.supported: - self.hash_algo = algo - data = hash_.getData() - # This only works because there is only one session - # per file in jingleFT - self.file_hash = data + file_props = FilesProp.getFileProp(self.connection.name, + self.sid) + file_props.algo = algo + file_props.hash_ = hash_.getData() raise xmpp.NodeProcessed - self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info', type_='modify') + self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info', + type_='modify') raise xmpp.NodeProcessed def __on_content_remove(self, stanza, jingle, error, action): diff --git a/src/gui_interface.py b/src/gui_interface.py index d9ba07c71..abfbca9fc 100644 --- a/src/gui_interface.py +++ b/src/gui_interface.py @@ -938,7 +938,7 @@ class Interface: session = gajim.connections[account].get_jingle_session(jid=None, sid=file_props.session_sid) ft_win = self.instances['file_transfers'] - if not session.file_hash: + if not file_props.hash_: # We disn't get the hash, sender probably don't support that jid = unicode(file_props.sender) self.popup_ft_result(account, jid, file_props) @@ -948,12 +948,12 @@ class Interface: file_ = open(file_props.file_name, 'r') except: return - hash_ = h.calculateHash(session.hash_algo, file_) + hash_ = h.calculateHash(file_props.algo, file_) file_.close() # If the hash we received and the hash of the file are the same, # then the file is not corrupt jid = unicode(file_props.sender) - if session.file_hash == hash_: + if file_props.hash_ == hash_: gobject.idle_add(self.popup_ft_result, account, jid, file_props) gobject.idle_add(ft_win.set_status, file_props, 'ok') else: