Have all core config use set functions to apply the configuration to the

session.
This commit is contained in:
Andrew Resch 2007-09-20 22:10:03 +00:00
parent d3c5064353
commit 943a62f62c
4 changed files with 160 additions and 22 deletions

View File

@ -38,6 +38,18 @@ import os
import pkg_resources import pkg_resources
import xdg, xdg.BaseDirectory import xdg, xdg.BaseDirectory
TORRENT_STATE = [
"Queued",
"Checking",
"Connecting",
"Downloading Metadata",
"Downloading",
"Finished",
"Seeding",
"Allocating"
]
def get_version(): def get_version():
"""Returns the program version from the egg metadata""" """Returns the program version from the egg metadata"""
return pkg_resources.require("Deluge")[0].version return pkg_resources.require("Deluge")[0].version

View File

@ -119,7 +119,7 @@ class Config:
self.save() self.save()
# Run the set_function for this key if any # Run the set_function for this key if any
try: try:
self.set_functions[key](value) self.set_functions[key](key, value)
except KeyError: except KeyError:
pass pass
else: else:
@ -145,9 +145,16 @@ class Config:
def register_set_function(self, key, function): def register_set_function(self, key, function):
"""Register a function to be run when a config value changes.""" """Register a function to be run when a config value changes."""
log.debug("Registering function for %s key..", key)
self.set_functions[key] = function self.set_functions[key] = function
return 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): def __getitem__(self, key):
return self.config[key] return self.config[key]

View File

@ -98,28 +98,9 @@ class Core(dbus.service.Object):
self.settings.user_agent = "Deluge %s" % deluge.common.get_version() self.settings.user_agent = "Deluge %s" % deluge.common.get_version()
self.session.set_settings(self.settings) 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 # Load metadata extension
self.session.add_extension(lt.create_metadata_plugin) 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 # Start the TorrentManager
self.torrents = TorrentManager(self.session) self.torrents = TorrentManager(self.session)
@ -129,6 +110,39 @@ class Core(dbus.service.Object):
# Start the AlertManager # Start the AlertManager
self.alerts = AlertManager(self.session) 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..") log.debug("Starting main loop..")
self.loop = gobject.MainLoop() self.loop = gobject.MainLoop()
self.loop.run() self.loop.run()
@ -301,3 +315,90 @@ class Core(dbus.service.Object):
def torrent_resumed(self, torrent_id): def torrent_resumed(self, torrent_id):
"""Emitted when a torrent is resumed""" """Emitted when a torrent is resumed"""
log.debug("torrent_resumed signal emitted") 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)

View File

@ -63,6 +63,9 @@ class TorrentManager:
log.debug("TorrentManager init..") log.debug("TorrentManager init..")
# Set the libtorrent session # Set the libtorrent session
self.session = 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 } # Create the torrents dict { torrent_id: Torrent }
self.torrents = {} self.torrents = {}
# Try to load the state from file # Try to load the state from file
@ -126,6 +129,10 @@ class TorrentManager:
# The torrent was not added to the session # The torrent was not added to the session
return None 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) log.debug("Attemping to save torrent file: %s", filename)
# Test if the torrentfiles_location is accessible # Test if the torrentfiles_location is accessible
if os.access(os.path.join(config["torrentfiles_location"]), os.F_OK) \ if os.access(os.path.join(config["torrentfiles_location"]), os.F_OK) \
@ -225,3 +232,14 @@ class TorrentManager:
except IOError: except IOError:
log.warning("Unable to save state file.") 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)