Fix issue where port was not opened before attempting to add torrents.

All config 'set functions' will apply automatically when they are 
registered unless otherwise specified.
This commit is contained in:
Andrew Resch 2007-09-27 13:29:20 +00:00
parent 4401c8f238
commit 936df09c60
3 changed files with 40 additions and 41 deletions

View File

@ -138,10 +138,13 @@ class Config:
"""Returns the entire configuration as a dictionary.""" """Returns the entire configuration as a dictionary."""
return self.config return self.config
def register_set_function(self, key, function): def register_set_function(self, key, function, apply_now=True):
"""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) log.debug("Registering function for %s key..", key)
self.set_functions[key] = function self.set_functions[key] = function
# Run the function now if apply_now is set
if apply_now:
self.set_functions[key](key, self.config[key])
return return
def apply_all(self): def apply_all(self):

View File

@ -101,21 +101,6 @@ class Core(dbus.service.Object):
# Load metadata extension # Load metadata extension
self.session.add_extension(lt.create_metadata_plugin) self.session.add_extension(lt.create_metadata_plugin)
# Start the TorrentManager
self.torrents = TorrentManager(self.session)
# Load plugins
self.plugins = PluginManager()
# Start the AlertManager
self.alerts = AlertManager(self.session)
# Register alert functions
self.alerts.register_handler("torrent_finished_alert",
self.on_alert_torrent_finished)
self.alerts.register_handler("torrent_paused_alert",
self.on_alert_torrent_paused)
# Register set functions in the Config # Register set functions in the Config
self.config.register_set_function("listen_ports", self.config.register_set_function("listen_ports",
self.on_set_listen_ports) self.on_set_listen_ports)
@ -141,14 +126,22 @@ class Core(dbus.service.Object):
self.on_set_max_download_speed) self.on_set_max_download_speed)
self.config.register_set_function("max_upload_slots_global", self.config.register_set_function("max_upload_slots_global",
self.on_set_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) # Start the TorrentManager
self.config.register_set_function("max_upload_slots_per_torrent", self.torrents = TorrentManager(self.session)
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()
# Load plugins
self.plugins = PluginManager()
# Start the AlertManager
self.alerts = AlertManager(self.session)
# Register alert functions
self.alerts.register_handler("torrent_finished_alert",
self.on_alert_torrent_finished)
self.alerts.register_handler("torrent_paused_alert",
self.on_alert_torrent_paused)
log.debug("Starting main loop..") log.debug("Starting main loop..")
self.loop = gobject.MainLoop() self.loop = gobject.MainLoop()
self.loop.run() self.loop.run()
@ -479,14 +472,6 @@ class Core(dbus.service.Object):
log.debug("max_upload_slots_global set to %s..", value) log.debug("max_upload_slots_global set to %s..", value)
self.session.set_max_uploads(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)
## Alert handlers ## ## Alert handlers ##
def on_alert_torrent_finished(self, alert): def on_alert_torrent_finished(self, alert):
log.debug("on_alert_torrent_finished") log.debug("on_alert_torrent_finished")

View File

@ -73,6 +73,12 @@ class TorrentManager:
# Try to load the state from file # Try to load the state from file
self.load_state() self.load_state()
# Register set functions
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)
def __del__(self): def __del__(self):
log.debug("TorrentManager shutting down..") log.debug("TorrentManager shutting down..")
# Save state on shutdown # Save state on shutdown
@ -107,17 +113,19 @@ class TorrentManager:
# Get the data from the file # Get the data from the file
try: try:
log.debug("Attempting to open %s for add.", filename) log.debug("Attempting to open %s for add.", filename)
filedump = open( _file = open(
os.path.join(self.config["torrentfiles_location"], os.path.join(
filename), "rb").read() self.config["torrentfiles_location"], filename), "rb")
filedump = _file.read()
_file.close()
except IOError: except IOError:
log.warning("Unable to open %s", filename) log.warning("Unable to open %s", filename)
return None return None
# Bdecode the filedata # Bdecode the filedata
torrent_filedump = lt.bdecode(filedump) torrent_filedump = lt.bdecode(filedump)
handle = None handle = None
# Make sure we are adding it with the correct allocation method. # Make sure we are adding it with the correct allocation method.
if compact is None: if compact is None:
compact = self.config["compact_allocation"] compact = self.config["compact_allocation"]
@ -154,7 +162,7 @@ class TorrentManager:
save_file = open(os.path.join(self.config["torrentfiles_location"], save_file = open(os.path.join(self.config["torrentfiles_location"],
filename), filename),
"wb") "wb")
save_file.write(filedump) save_file.write(lt.bencode(torrent_filedump))
save_file.close() save_file.close()
except IOError: except IOError:
log.warning("Unable to save torrent file: %s", filename) log.warning("Unable to save torrent file: %s", filename)
@ -302,15 +310,18 @@ class TorrentManager:
fastresume.close() fastresume.close()
except IOError: except IOError:
log.warning("Error trying to save fastresume file") log.warning("Error trying to save fastresume file")
def set_max_connections(self, value): def on_set_max_connections_per_torrent(self, key, value):
"""Sets the per-torrent connection limit""" """Sets the per-torrent connection limit"""
log.debug("max_connections_per_torrent set to %s..", value)
self.max_connections = value self.max_connections = value
for key in self.torrents.keys(): for key in self.torrents.keys():
self.torrents[key].handle.set_max_connections(value) self.torrents[key].handle.set_max_connections(value)
def set_max_uploads(self, value): def on_set_max_upload_slots_per_torrent(self, key, value):
"""Sets the per-torrent upload slot limit""" """Sets the per-torrent upload slot limit"""
log.debug("max_upload_slots_per_torrent set to %s..", value)
self.max_uploads = value self.max_uploads = value
for key in self.torrents.keys(): for key in self.torrents.keys():
self.torrents[key].handle.set_max_uploads(value) self.torrents[key].handle.set_max_uploads(value)