send hash in session-init when the file is less than 10 mb

This commit is contained in:
Jefry Lagrange 2012-05-28 19:54:59 -04:00
parent ae4596bd2e
commit e195cfb188
4 changed files with 39 additions and 21 deletions

View File

@ -1980,6 +1980,10 @@ class FileRequestReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
if val is None:
continue
self.file_props[name] = val
# Delete this, it shouldn't be necesary after file_props gets
# refactored.
if name == 'hash':
self.file_props['algo'] = child.getAttr('algo')
file_desc_tag = file_tag.getTag('desc')
if file_desc_tag is not None:
self.file_props['desc'] = file_desc_tag.getData()

View File

@ -162,7 +162,7 @@ class JingleContent(object):
self.sent = True
content.addChild(node=self.transport.make_transport())
def _fill_content(self, content, action):
def _fill_content(self, content):
description_node = xmpp.simplexml.Node(
tag=xmpp.NS_JINGLE_FILE_TRANSFER + ' description')
@ -183,6 +183,11 @@ class JingleContent(object):
if 'hash' in self.file_props:
# TODO: use xep-300 for this bit
pass
# if the file is less than 10 mb, then it is small
# lets calculate it right away
if int(self.file_props['size']) < 10000000:
h = self._calcHash()
file_tag.addChild(node=h)
desc = file_tag.setTag('desc')
if 'desc' in self.file_props:
desc.setData(self.file_props['desc'])

View File

@ -117,12 +117,27 @@ class JingleFileTransfer(JingleContent):
gajim.nec.push_incoming_event(FileRequestReceivedEvent(None,
conn=self.session.connection, stanza=stanza, jingle_content=content,
FT_content=self))
# 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
self.hashThread = threading.Thread(target=self.__calcHash)
self.hashThread.start()
# if we haven't sent the hash already.
if 'hash' not in self.file_props:
self.hashThread = threading.Thread(target=self.__send_hash)
self.hashThread.start()
def __calcHash(self):
def __send_hash(self):
# Send hash in a session info
checksum = xmpp.Node(tag='checksum',
payload=[xmpp.Node(tag='file',
payload=[self._calcHash()])])
checksum.setNamespace(xmpp.NS_JINGLE_FILE_TRANSFER)
self.session.__session_info(checksum )
def _calcHash(self):
# Caculates the hash and returns a xep-300 hash stanza
if self.session.hash_algo == None:
return
try:
@ -139,13 +154,8 @@ class JingleFileTransfer(JingleContent):
return
self.file_props['hash'] = hash_
h.addHash(hash_, self.session.hash_algo)
checksum = xmpp.Node(tag='checksum',
payload=[xmpp.Node(tag='file', payload=[h])])
checksum.setNamespace(xmpp.NS_JINGLE_FILE_TRANSFER)
# Send hash in a session info
self.session.__session_info(checksum )
return h
def __on_session_accept(self, stanza, content, error, action):
log.info("__on_session_accept")
con = self.session.connection

View File

@ -433,17 +433,16 @@ class JingleSession(object):
payload = jingle.getPayload()
for p in payload:
if p.getName() == 'checksum':
hashes = p.getTag('file').getTag(name='hashes',
hash_ = p.getTag('file').getTag(name='hash',
namespace=xmpp.NS_HASHES)
for hash in hashes.getChildren():
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
raise xmpp.NodeProcessed
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
raise xmpp.NodeProcessed
self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info', type_='modify')
raise xmpp.NodeProcessed