From 943a62f62ce9faadda4b8c889348a899de418294 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Thu, 20 Sep 2007 22:10:03 +0000 Subject: [PATCH] Have all core config use set functions to apply the configuration to the session. --- deluge/common.py | 12 +++ deluge/config.py | 11 ++- deluge/core/core.py | 139 +++++++++++++++++++++++++++++----- deluge/core/torrentmanager.py | 20 ++++- 4 files changed, 160 insertions(+), 22 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index 54ccf5975..7490de859 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -38,6 +38,18 @@ import os import pkg_resources import xdg, xdg.BaseDirectory + +TORRENT_STATE = [ + "Queued", + "Checking", + "Connecting", + "Downloading Metadata", + "Downloading", + "Finished", + "Seeding", + "Allocating" +] + def get_version(): """Returns the program version from the egg metadata""" return pkg_resources.require("Deluge")[0].version diff --git a/deluge/config.py b/deluge/config.py index 6f551c2ea..8db92a86c 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -119,7 +119,7 @@ class Config: self.save() # Run the set_function for this key if any try: - self.set_functions[key](value) + self.set_functions[key](key, value) except KeyError: pass else: @@ -145,9 +145,16 @@ class Config: def register_set_function(self, key, function): """Register a function to be run when a config value changes.""" + log.debug("Registering function for %s key..", key) self.set_functions[key] = function return - + + def apply_all(self): + """Runs all set functions""" + log.debug("Running all set functions..") + for key in self.set_functions.keys(): + self.set_functions[key](key, self.config[key]) + def __getitem__(self, key): return self.config[key] diff --git a/deluge/core/core.py b/deluge/core/core.py index 30e4aeb3b..3f0d9d862 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -98,28 +98,9 @@ class Core(dbus.service.Object): self.settings.user_agent = "Deluge %s" % deluge.common.get_version() self.session.set_settings(self.settings) - # Set the listening ports - if self.config["random_port"]: - import random - listen_ports = [] - randrange = lambda: random.randrange(49152, 65525) - listen_ports.append(randrange()) - listen_ports.append(listen_ports[0]+10) - else: - listen_ports = self.config["listen_ports"] - - log.debug("Listening on ports %i-%i", listen_ports[0], - listen_ports[1]) - self.session.listen_on(listen_ports[0], - listen_ports[1]) - # Load metadata extension self.session.add_extension(lt.create_metadata_plugin) - # Load utorrent peer-exchange - if self.config["utpex"]: - self.session.add_extension(lt.create_ut_pex_plugin) - # Start the TorrentManager self.torrents = TorrentManager(self.session) @@ -129,6 +110,39 @@ class Core(dbus.service.Object): # Start the AlertManager self.alerts = AlertManager(self.session) + # Register set functions in the Config + self.config.register_set_function("listen_ports", + self.on_set_listen_ports) + self.config.register_set_function("random_port", + self.on_set_random_port) + self.config.register_set_function("dht", self.on_set_dht) + self.config.register_set_function("upnp", self.on_set_upnp) + self.config.register_set_function("natpmp", self.on_set_natpmp) + self.config.register_set_function("utpex", self.on_set_utpex) + self.config.register_set_function("enc_in_policy", + self.on_set_encryption) + self.config.register_set_function("enc_out_policy", + self.on_set_encryption) + self.config.register_set_function("enc_level", + self.on_set_encryption) + self.config.register_set_function("enc_prefer_rc4", + self.on_set_encryption) + self.config.register_set_function("max_connections_global", + self.on_set_max_connections_global) + self.config.register_set_function("max_upload_speed", + self.on_set_max_upload_speed) + self.config.register_set_function("max_download_speed", + self.on_set_max_download_speed) + self.config.register_set_function("max_upload_slots_global", + self.on_set_max_upload_slots_global) + self.config.register_set_function("max_connections_per_torrent", + self.on_set_max_connections_per_torrent) + self.config.register_set_function("max_upload_slots_per_torrent", + self.on_set_max_upload_slots_per_torrent) + + # Run all the set functions now to set the config for the session + self.config.apply_all() + log.debug("Starting main loop..") self.loop = gobject.MainLoop() self.loop.run() @@ -301,3 +315,90 @@ class Core(dbus.service.Object): def torrent_resumed(self, torrent_id): """Emitted when a torrent is resumed""" log.debug("torrent_resumed signal emitted") + + # Config set functions + def on_set_listen_ports(self, key, value): + # Only set the listen ports if random_port is not true + if self.config["random_port"] is not True: + log.debug("listen port range set to %s-%s", value[0], value[1]) + self.session.listen_on(value[0], value[1]) + + def on_set_random_port(self, key, value): + log.debug("random port value set to %s", value) + # We need to check if the value has been changed to true and false + # and then handle accordingly. + if value: + import random + listen_ports = [] + randrange = lambda: random.randrange(49152, 65525) + listen_ports.append(randrange()) + listen_ports.append(listen_ports[0]+10) + else: + listen_ports = self.config["listen_ports"] + + # Set the listen ports + log.debug("listen port range set to %s-%s", listen_ports[0], + listen_ports[1]) + self.session.listen_on(listen_ports[0], listen_ports[1]) + + def on_set_dht(self, key, value): + log.debug("dht value set to %s", value) + if value: + self.session.start_dht(None) + else: + self.session.stop_dht() + + def on_set_upnp(self, key, value): + log.debug("upnp value set to %s", value) + if value: + self.session.start_upnp() + else: + self.session.stop_upnp() + + def on_set_natpmp(self, key, value): + log.debug("natpmp value set to %s", value) + if value: + self.session.start_natpmp() + else: + self.session.stop_natpmp() + + def on_set_utpex(self, key, value): + log.debug("utpex value set to %s", value) + if value: + self.session.add_extension(lt.create_ut_pex_plugin) + + def on_set_encryption(self, key, value): + log.debug("encryption value %s set to %s..", key, value) + pe_settings = lt.pe_settings() + pe_settings.out_enc_policy = \ + lt.enc_policy(self.config["enc_out_policy"]) + pe_settings.in_enc_policy = lt.enc_policy(self.config["enc_in_policy"]) + pe_settings.allow_enc_level = lt.enc_level(self.config["enc_level"]) + pe_settings.prefer_rc4 = self.config["enc_prefer_rc4"] + self.session.set_pe_settings(pe_settings) + + def on_set_max_connections_global(self, key, value): + log.debug("max_connections_global set to %s..", value) + self.session.set_max_connections(value) + + def on_set_max_upload_speed(self, key, value): + log.debug("max_upload_speed set to %s..", value) + # We need to convert Kb/s to B/s + self.session.set_upload_rate_limit(int(value * 1024)) + + def on_set_max_download_speed(self, key, value): + log.debug("max_download_speed set to %s..", value) + # We need to convert Kb/s to B/s + self.session.set_download_rate_limit(int(value * 1024)) + + def on_set_max_upload_slots_global(self, key, value): + log.debug("max_upload_slots_global set to %s..", value) + self.session.set_max_uploads(value) + + def on_set_max_connections_per_torrent(self, key, value): + log.debug("max_connections_per_torrent set to %s..", value) + self.torrents.set_max_connections(value) + + def on_set_max_upload_slots_per_torrent(self, key, value): + log.debug("max_upload_slots_per_torrent set to %s..", value) + self.torrents.set_max_uploads(value) diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 464a941be..baf6e7c55 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -63,6 +63,9 @@ class TorrentManager: log.debug("TorrentManager init..") # Set the libtorrent session self.session = session + # Per torrent connection limit and upload slot limit + self.max_connections = -1 + self.max_uploads = -1 # Create the torrents dict { torrent_id: Torrent } self.torrents = {} # Try to load the state from file @@ -126,6 +129,10 @@ class TorrentManager: # The torrent was not added to the session return None + # Set per-torrent limits + handle.set_max_connections(self.max_connections) + handle.set_max_uploads(self.max_uploads) + log.debug("Attemping to save torrent file: %s", filename) # Test if the torrentfiles_location is accessible if os.access(os.path.join(config["torrentfiles_location"]), os.F_OK) \ @@ -224,4 +231,15 @@ class TorrentManager: state_file.close() except IOError: log.warning("Unable to save state file.") - + + def set_max_connections(self, value): + """Sets the per-torrent connection limit""" + self.max_connections = value + for key in self.torrents.keys(): + self.torrents[key].handle.set_max_connections(value) + + def set_max_uploads(self, value): + """Sets the per-torrent upload slot limit""" + self.max_uploads = value + for key in self.torrents.keys(): + self.torrents[key].handle.set_max_uploads(value)