Skip to content

Commit

Permalink
Refactor to use deferred
Browse files Browse the repository at this point in the history
  • Loading branch information
ardhipoetra committed Jul 4, 2016
1 parent a30122d commit 5f5db94
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
31 changes: 13 additions & 18 deletions Tribler/Policies/BoostingSource.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ def showtorrent(torrent):
"""
assembly torrent data, call the callback
"""

infohash = torrent.infohash
if torrent.get_files() and infohash in self.unavail_torrent:
if len(self.torrents) >= self.max_torrents:
Expand Down Expand Up @@ -255,7 +254,7 @@ def showtorrent(torrent):
if len(self.unavail_torrent) and self.enabled:
self._logger.debug("Unavailable #torrents : %d from %s", len(self.unavail_torrent), hexlify(self.source))
for torrent in self.unavail_torrent.values():
self._load_torrent(torrent[2], showtorrent)
self._load_torrent(torrent[2]).addCallback(showtorrent)

def _update(self):
if len(self.torrents) < self.max_torrents and self.database_updated:
Expand Down Expand Up @@ -284,7 +283,7 @@ def _on_database_updated(self, dummy_subject, dummy_change_type, dummy_infohash)
def get_source_text(self):
return str(self.channel_dict[2]) if self.channel_dict else None

def _load_torrent(self, infohash, callback=None):
def _load_torrent(self, infohash):
"""
function to load infohash and put in into database.
"""
Expand All @@ -293,30 +292,26 @@ def _load_torrent(self, infohash, callback=None):
if self.session is None or self.session.lm.torrent_store is None or self.session.get_torrent_store() is None:
return

def add_to_loaded(infohash):
def add_to_loaded(infohash_str):
"""
function to add loaded infohash to memory
"""
tdef = TorrentDef.load_from_memory(self.session.get_collected_torrent(unhexlify(infohash)))
self.loaded_torrent[infohash] = tdef

if callback is not None:
callback(tdef)
self.loaded_torrent[unhexlify(infohash_str)].callback(
TorrentDef.load_from_memory(self.session.get_collected_torrent(unhexlify(infohash_str))))
self.loaded_torrent[unhexlify(infohash_str)] = None

if infohash not in self.loaded_torrent:
if self.session.has_collected_torrent(infohash):
meta_dict = self.session.get_collected_torrent(infohash)
tdef = TorrentDef.load_from_memory(meta_dict)
else:
self.loaded_torrent[infohash] = defer.Deferred()

if not self.session.has_collected_torrent(infohash):
if self.session.has_download(infohash):
return
tdef = None
self.session.download_torrentfile(infohash, add_to_loaded, 0)
else:
tdef = self.loaded_torrent[infohash]

if not (tdef is None or callback is None):
callback(tdef)
deferred_load = self.loaded_torrent[infohash]

return deferred_load


class RSSFeedSource(BoostingSource):
"""
Expand Down
25 changes: 13 additions & 12 deletions Tribler/Test/Core/CreditMining/test_creditmining_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from Tribler.Core.TorrentDef import TorrentDef
from Tribler.Core.Utilities.twisted_thread import deferred, reactor
from Tribler.Core.simpledefs import NTFY_TORRENTS, NTFY_UPDATE, NTFY_CHANNELCAST
from Tribler.Main.Utility.GuiDBTuples import CollectedTorrent
from Tribler.Policies.BoostingManager import BoostingManager, BoostingSettings
from Tribler.Test.Core.CreditMining.mock_creditmining import MockLtTorrent, ResourceFailClass
from Tribler.Test.test_as_server import TestAsServer, TESTS_DATA_DIR
Expand Down Expand Up @@ -357,9 +356,10 @@ def test_chn_lookup(self):
self.boosting_manager.add_source(dispersy_cid)
chn_obj = self.boosting_manager.get_source_object(dispersy_cid)

def _load(_, callback=None):
if callback is not None:
callback(self.tdef)
def _load(_):
defer_ret = defer.Deferred()
defer_ret.callback(self.tdef)
return defer_ret

def check_torrents_channel(src, defer_param=None, target=1):
"""
Expand Down Expand Up @@ -433,9 +433,10 @@ def _set_id_channel(channel_id):
self.boosting_manager.add_source(dispersy_cid)
chn_obj = self.boosting_manager.get_source_object(dispersy_cid)

def _load(_, callback=None):
if callback is not None:
callback(self.tdef)
def _load(_):
defer_ret = defer.Deferred()
defer_ret.callback(self.tdef)
return defer_ret

chn_obj._load_torrent = _load

Expand All @@ -448,7 +449,6 @@ def clean_community(_):

chn_obj.kill_tasks()


d = self.check_source(dispersy_cid)
d.addCallback(clean_community)
return d
Expand Down Expand Up @@ -478,11 +478,12 @@ def test_chn_max_torrents(self):
self.boosting_manager.add_source(dispersy_cid)
chn_obj = self.boosting_manager.get_source_object(dispersy_cid)
chn_obj.max_torrents = 2
chn_obj._load_torrent = lambda dummy_1, dummy_2: None
chn_obj._load_torrent = lambda _: defer.Deferred()

def _load(infohash, callback=None):
if callback is not None:
callback(self.tdef if binascii.hexlify(infohash).startswith("fc") else pioneer_tdef)
def _load(infohash):
defer_ret = defer.Deferred()
defer_ret.callback(self.tdef if binascii.hexlify(infohash).startswith("fc") else pioneer_tdef)
return defer_ret

def activate_mgr():
"""
Expand Down

0 comments on commit 5f5db94

Please sign in to comment.