From 3ac9af0486b860b3913adea4e0322b984e99cd4d Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sun, 13 Apr 2008 06:07:19 +0000 Subject: [PATCH] State fixes. --- deluge/common.py | 11 +++++++- deluge/core/torrent.py | 35 ++++++++++++++++--------- deluge/core/torrentmanager.py | 49 ++++++++++++++++------------------- 3 files changed, 56 insertions(+), 39 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index 00d54c25c..1dc143288 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -47,7 +47,16 @@ LT_TORRENT_STATE = { "Finished": 5, "Seeding": 6, "Allocating": 7, - "Paused": 8 + "Paused": 8, + 0: "Queued", + 1: "Checking", + 2: "Connecting", + 3: "Downloading Metadata", + 4: "Downloading", + 5: "Finished", + 6: "Seeding", + 7: "Allocating", + 8: "Paused" } TORRENT_STATE = [ diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index d8ed93828..f26f255ea 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -72,17 +72,13 @@ class Torrent: # Status message holds error info about the torrent self.statusmsg = "OK" + # The torrents state + self.state = "" + # Holds status info so that we don't need to keep getting it from lt self.status = self.handle.status() self.torrent_info = self.handle.get_torrent_info() - - # Set the initial state - self.state = "Checking" - if self.handle.is_seed(): - self.state = "Seeding" - else: - self.state = "Downloading" - + # Various torrent options self.max_connections = -1 self.max_upload_slots = -1 @@ -160,6 +156,21 @@ class Torrent: self.file_priorities = file_priorities self.handle.prioritize_files(file_priorities) + def set_state_based_on_ltstate(self): + """Updates the state based on what libtorrent's state for the torrent is""" + # Set the initial state based on the lt state + LTSTATE = deluge.common.LT_TORRENT_STATE + ltstate = self.handle.status().state + if ltstate == LTSTATE["Queued"] or ltstate == LTSTATE["Checking"]: + self.set_state("Checking") + elif ltstate == LTSTATE["Connecting"] or ltstate == LTSTATE["Downloading"] or\ + ltstate == LTSTATE["Downloading Metadata"]: + self.set_state("Downloading") + elif ltstate == LTSTATE["Finished"] or ltstate == LTSTATE["Seeding"]: + self.set_state("Seeding") + elif ltstate == LTSTATE["Allocating"]: + self.set_state("Allocating") + def set_state(self, state): """Accepts state strings, ie, "Paused", "Seeding", etc.""" @@ -388,7 +399,7 @@ class Torrent: # Reset the status message just in case of resuming an Error'd torrent self.set_status_message("OK") - if self.handle.is_seed(): + if self.handle.is_finished(): # If the torrent has already reached it's 'stop_seed_ratio' then do not do anything if self.config["stop_seed_at_ratio"]: if self.get_ratio() >= self.config["stop_seed_ratio"]: @@ -416,17 +427,17 @@ class Torrent: except: pass - if self.handle.is_seed(): + if self.handle.is_finished(): self.state = "Seeding" + else: # Only delete the .fastresume file if we're still downloading stuff self.delete_fastresume() - else: self.state = "Downloading" return True elif self.state == "Queued": - if self.handle.is_seed(): + if self.handle.is_finished(): if self.torrentqueue.get_num_seeding() < self.config["max_active_seeding"] or\ self.config["max_active_seeding"] == -1: self.handle.resume() diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 9a66f115f..cdcb38b00 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -157,7 +157,7 @@ class TorrentManager(component.Component): self.save_state() for key in self.torrents.keys(): if not self.torrents[key].handle.is_paused() and \ - not self.torrents[key].handle.is_seed(): + not self.torrents[key].handle.is_finished(): if self.torrents[key].compact: try: self.torrents[key].pause() @@ -301,15 +301,19 @@ class TorrentManager(component.Component): log.debug("set file priorities: %s", options["file_priorities"]) torrent.set_file_priorities(options["file_priorities"]) + log.debug("state: %s", state) + # Resume the torrent if needed if state == "Queued": - torrent.state = "Queued" + torrent.set_state("Queued") elif state == "Paused" or state == "Error": - torrent.state = "Paused" + torrent.set_state("Paused") if state == None and not options["add_paused"]: torrent.handle.resume() + # We set the state based on libtorrent's state + torrent.set_state_based_on_ltstate() if state == None and options["add_paused"]: - torrent.state = "Paused" + torrent.set_state("Paused") # Emit the torrent_added signal self.signals.emit("torrent_added", torrent.torrent_id) @@ -472,7 +476,7 @@ class TorrentManager(component.Component): log.warning("Unable to load state file.") # Try to add the torrents in the state to the session - add_paused = {} + resume_torrents = [] # First lets clear the queue and make it the correct length.. This will # help with inserting values at the right position. self.queue.set_size(len(state.torrents)) @@ -503,11 +507,8 @@ class TorrentManager(component.Component): "file_priorities": torrent_state.file_priorities } # We need to resume all non-add_paused torrents after plugin hook - if torrent_state.state == "Paused" or torrent_state.state == "Queued" or torrent_state.state == "Error": - log.debug("torrent state: %s", torrent_state.state) - add_paused[torrent_state.torrent_id] = True - else: - add_paused[torrent_state.torrent_id] = False + if torrent_state.state not in ["Paused", "Queued", "Error"]: + resume_torrents.append(torrent_state.torrent_id) self.add( torrent_state.filename, @@ -527,15 +528,10 @@ class TorrentManager(component.Component): self.plugins.run_post_session_load() # Resume any torrents that need to be resumed - log.debug("add_paused: %s", add_paused) - for key in add_paused.keys(): - if add_paused[key] == False: - self.torrents[key].handle.resume() - if self.torrents[key].get_status(["is_seed"])["is_seed"]: - self.torrents[key].state = "Seeding" - else: - self.torrents[key].state = "Downloading" - + for torrent_id in resume_torrents: + self.torrents[torrent_id].handle.resume() + self.torrents[torrent_id].set_state_based_on_ltstate() + def save_state(self): """Save the state of the TorrentManager to the torrents.state file""" state = TorrentManagerState() @@ -608,12 +604,13 @@ class TorrentManager(component.Component): if self.config["queue_finished_to_bottom"]: self.queue.bottom(torrent_id) - # Set the torrent state - if self.queue.get_num_seeding() < self.config["max_active_seeding"] or\ - self.config["max_active_seeding"] == -1: - self.torrents[torrent_id].set_state("Seeding") - else: - self.torrents[torrent_id].set_state("Queued") + # Set the torrent state if not paused + if not self.torrents[torrent_id].handle.is_paused(): + if self.queue.get_num_seeding() < self.config["max_active_seeding"] or\ + self.config["max_active_seeding"] == -1: + self.torrents[torrent_id].set_state("Seeding") + else: + self.torrents[torrent_id].set_state("Queued") # Write the fastresume file self.torrents[torrent_id].write_fastresume() @@ -640,7 +637,7 @@ class TorrentManager(component.Component): torrent_id = str(alert.handle.info_hash()) # Set the torrent state if not self.torrents[torrent_id].handle.is_paused(): - if self.torrents[torrent_id].handle.is_seed(): + if self.torrents[torrent_id].handle.is_finished(): self.torrents[torrent_id].set_state("Seeding") else: self.torrents[torrent_id].set_state("Downloading")