diff --git a/deluge/core/alertmanager.py b/deluge/core/alertmanager.py index 54a86e9c0..4b06798b7 100644 --- a/deluge/core/alertmanager.py +++ b/deluge/core/alertmanager.py @@ -38,14 +38,18 @@ class AlertManager(component.Component): self.alert_queue_size = 10000 self.set_alert_queue_size(self.alert_queue_size) - self.session.set_alert_mask( - lt.alert.category_t.error_notification | - lt.alert.category_t.port_mapping_notification | - lt.alert.category_t.storage_notification | - lt.alert.category_t.tracker_notification | - lt.alert.category_t.status_notification | - lt.alert.category_t.ip_block_notification | - lt.alert.category_t.performance_warning) + alert_mask = (lt.alert.category_t.error_notification | + lt.alert.category_t.port_mapping_notification | + lt.alert.category_t.storage_notification | + lt.alert.category_t.tracker_notification | + lt.alert.category_t.status_notification | + lt.alert.category_t.ip_block_notification | + lt.alert.category_t.performance_warning) + + try: + self.session.apply_settings("alert_mask", alert_mask) + except AttributeError: + self.session.set_alert_mask(alert_mask) # handlers is a dictionary of lists {"alert_type": [handler1,h2,..]} self.handlers = {} @@ -120,6 +124,4 @@ class AlertManager(component.Component): """Sets the maximum size of the libtorrent alert queue""" log.info("Alert Queue Size set to %s", queue_size) self.alert_queue_size = queue_size - settings = self.session.get_settings() - settings["alert_queue_size"] = self.alert_queue_size - self.session.set_settings(settings) + component.get("Core").apply_session_setting("alert_queue_size", self.alert_queue_size) diff --git a/deluge/core/core.py b/deluge/core/core.py index ecd6976e3..604c93b91 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -65,17 +65,18 @@ class Core(component.Component): # Load the session state if available self.__load_session_state() - # --- Set session settings --- - settings = self.session.get_settings() - settings["user_agent"] = "Deluge/%(deluge_version)s libtorrent/%(lt_version)s" % { - 'deluge_version': deluge.common.get_version(), - 'lt_version': self.get_libtorrent_version().rpartition(".")[0] - } - # No SSL torrent support in code so disable the listen port. - settings["ssl_listen"] = 0 - self.session.set_settings(settings) + # Apply session settings + self.apply_session_setting( + "user_agent", + "Deluge/%(deluge_version)s libtorrent/%(lt_version)s" % { + 'deluge_version': deluge.common.get_version(), + 'lt_version': self.get_libtorrent_version().rpartition(".")[0]} + ) - # --- libtorrent plugins --- + # No SSL torrent support in code so disable the listen port. + self.apply_session_setting("ssl_listen", 0) + + # Enable libtorrent extensions # Allows peers to download the metadata from the swarm directly self.session.add_extension("ut_metadata") # Ban peers that sends bad data @@ -137,6 +138,22 @@ class Core(component.Component): def shutdown(self): pass + def apply_session_setting(self, key, value): + self.apply_session_settings({key: value}) + + def apply_session_settings(self, settings): + """Apply libtorrent session settings. + + Args: + settings (dict): A dict of lt session settings to apply. + + """ + + try: + self.session.apply_settings(settings) + except AttributeError: + self.session.set_settings(settings) + def __save_session_state(self): """Saves the libtorrent session state""" filename = "session.state" @@ -389,6 +406,7 @@ class Core(component.Component): """ status = {} + # TODO: libtorrent DEPRECATED for session_stats http://libtorrent.org/manual-ref.html#session-statistics session_status = self.session.status() for key in keys: status[key] = getattr(session_status, key) @@ -404,7 +422,7 @@ class Core(component.Component): :rtype: dict """ - + # TODO: libtorrent DEPRECATED for session_stats: disk.num_blocks_cache_hits etc... status = self.session.get_cache_status() cache = {} for attr in dir(status): @@ -559,7 +577,7 @@ class Core(component.Component): @export def get_i2p_proxy(self): """Returns the active listen port""" - i2p_settings = self.session.i2p_proxy() + i2p_settings = self.session.i2p_proxy() # Deprecated, moved to proxy types i2p_dict = {"hostname": i2p_settings.hostname, "port": i2p_settings.port} return i2p_dict diff --git a/deluge/core/preferencesmanager.py b/deluge/core/preferencesmanager.py index 35dfec734..ef928ce8f 100644 --- a/deluge/core/preferencesmanager.py +++ b/deluge/core/preferencesmanager.py @@ -158,9 +158,19 @@ class PreferencesManager(component.Component): on_set_func(key, value) def session_set_setting(self, key, value): - settings = self.session.get_settings() - settings[key] = value - self.session.set_settings(settings) + try: + self.session.apply_settings({key: value}) + except AttributeError: + # Deprecated in libtorrent 1.1 + if key in ("enable_lsd", "enable_upnp", "enable_natpmp", "enable_dht"): + start_stop = key.replace("enable", "start") if value else key.replace("enable", "stop") + getattr(self.session, start_stop)() + elif key == "dht_bootstrap_nodes": + self.session.add_dht_router("router.bittorrent.com", 6881) + self.session.add_dht_router("router.utorrent.com", 6881) + self.session.add_dht_router("router.bitcomet.com", 6881) + else: + self.session.set_settings({key: value}) def _on_config_value_change(self, key, value): if self.get_state() == "Started": @@ -193,22 +203,28 @@ class PreferencesManager(component.Component): self.config["listen_random_port"] = None listen_ports = self.config["listen_ports"] - # If a single port range then always enable re-use port flag. - reuse_port = True if listen_ports[0] == listen_ports[1] else self.config["listen_reuse_port"] - flags = ((lt.listen_on_flags_t.listen_no_system_port - if not self.config["listen_use_sys_port"] else 0) | - (lt.listen_on_flags_t.listen_reuse_address - if reuse_port else 0)) interface = str(self.config["listen_interface"].strip()) - log.debug("Listen Interface: %s, Ports: %s with reuse_port: %s, use_sys_port: %s", - interface, listen_ports, reuse_port, self.config["listen_use_sys_port"]) + log.debug("Listen Interface: %s, Ports: %s with use_sys_port: %s", + interface, listen_ports, self.config["listen_use_sys_port"]) try: - self.session.listen_on(listen_ports[0], listen_ports[1], interface, flags) - except RuntimeError as ex: - if ex.message == "Invalid Argument": - log.error("Error setting listen interface (must be IP Address): %s %s-%s", - interface, listen_ports[0], listen_ports[1]) + interfaces = ["%s:%s" % (interface, port) for port in range(listen_ports[0], listen_ports[1]+1)] + self.session.apply_setting({"listen_system_port_fallback", self.config["listen_use_sys_port"]}) + self.session.apply_setting({"listen_interfaces", interfaces}) + except AttributeError: + # Deprecated in libtorrent 1.1 + # If a single port range then always enable re-use port flag. + reuse_port = True if listen_ports[0] == listen_ports[1] else self.config["listen_reuse_port"] + flags = ((lt.listen_on_flags_t.listen_no_system_port + if not self.config["listen_use_sys_port"] else 0) | + (lt.listen_on_flags_t.listen_reuse_address + if reuse_port else 0)) + try: + self.session.listen_on(listen_ports[0], listen_ports[1], interface, flags) + except RuntimeError as ex: + if ex.message == "Invalid Argument": + log.error("Error setting listen interface (must be IP Address): %s %s-%s", + interface, listen_ports[0], listen_ports[1]) def _on_set_outgoing_ports(self, key, value): self.__set_outgoing_ports() @@ -231,34 +247,21 @@ class PreferencesManager(component.Component): def _on_set_dht(self, key, value): log.debug("dht value set to %s", value) - if value: - self.session.start_dht() - self.session.add_dht_router("router.bittorrent.com", 6881) - self.session.add_dht_router("router.utorrent.com", 6881) - self.session.add_dht_router("router.bitcomet.com", 6881) - else: - self.session.stop_dht() + dht_bootstraps = "router.bittorrent.com:6881,router.utorrent.com:6881,router.bitcomet.com:6881" + self.session_set_setting("dht_bootstrap_nodes", dht_bootstraps) + self.session_set_setting("enable_dht", value) 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() + self.session_set_setting("enable_upnp", value) 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() + self.session_set_setting("enable_natpmp", value) def _on_set_lsd(self, key, value): log.debug("lsd value set to %s", value) - if value: - self.session.start_lsd() - else: - self.session.stop_lsd() + self.session_set_setting("enable_lsd", value) def _on_set_utpex(self, key, value): log.debug("utpex value set to %s", value) @@ -276,22 +279,28 @@ class PreferencesManager(component.Component): def _on_set_encryption(self, key, value): log.debug("encryption value %s set to %s..", key, value) + # Convert Deluge enc_level values to libtorrent enc_level values. pe_enc_level = {0: lt.enc_level.plaintext, 1: lt.enc_level.rc4, 2: lt.enc_level.both} - - 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.allowed_enc_level = lt.enc_level(pe_enc_level[self.config["enc_level"]]) - pe_settings.prefer_rc4 = True - self.session.set_pe_settings(pe_settings) - pe_sess_settings = self.session.get_pe_settings() - log.debug("encryption settings:\n\t\t\tout_policy: %s\n\t\t\ - in_policy: %s\n\t\t\tlevel: %s\n\t\t\tprefer_rc4: %s", - pe_sess_settings.out_enc_policy, - pe_sess_settings.in_enc_policy, - pe_sess_settings.allowed_enc_level, - pe_sess_settings.prefer_rc4) + try: + self.session.apply_setting("out_enc_policy", lt.enc_policy(self.config["enc_out_policy"])) + self.session.apply_setting("in_enc_policy", lt.enc_policy(self.config["enc_in_policy"])) + self.session.apply_setting("allowed_enc_level", lt.enc_level(pe_enc_level[self.config["enc_level"]])) + self.session.apply_setting("prefer_rc4", True) + except AttributeError: + # Deprecated in libtorrent 1.1 + 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.allowed_enc_level = lt.enc_level(pe_enc_level[self.config["enc_level"]]) + pe_settings.prefer_rc4 = True + self.session.set_pe_settings(pe_settings) + pe_sess_settings = self.session.get_pe_settings() + log.debug("encryption settings:\n\t\t\tout_policy: %s\n\t\t\ + in_policy: %s\n\t\t\tlevel: %s\n\t\t\tprefer_rc4: %s", + pe_sess_settings.out_enc_policy, + pe_sess_settings.in_enc_policy, + pe_sess_settings.allowed_enc_level, + pe_sess_settings.prefer_rc4) def _on_set_max_connections_global(self, key, value): log.debug("max_connections_global set to %s..", value) @@ -300,20 +309,14 @@ class PreferencesManager(component.Component): 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 - if value < 0: - _value = -1 - else: - _value = int(value * 1024) - self.session_set_setting("upload_rate_limit", _value) + value = -1 if value < 0 else int(value * 1024) + self.session_set_setting("upload_rate_limit", value) 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 - if value < 0: - _value = -1 - else: - _value = int(value * 1024) - self.session_set_setting("download_rate_limit", _value) + value = -1 if value < 0 else int(value * 1024) + self.session_set_setting("download_rate_limit", value) def _on_set_max_upload_slots_global(self, key, value): log.debug("max_upload_slots_global set to %s..", value) @@ -407,25 +410,42 @@ class PreferencesManager(component.Component): def _on_set_proxy(self, key, value): log.debug("Setting proxy to: %s", value) - proxy_settings = lt.proxy_settings() - proxy_settings.type = lt.proxy_type(value["type"]) - proxy_settings.username = value["username"] - proxy_settings.password = value["password"] - proxy_settings.hostname = value["hostname"] - proxy_settings.port = value["port"] - proxy_settings.proxy_hostnames = value["proxy_hostnames"] - proxy_settings.proxy_peer_connections = value["proxy_peer_connections"] - self.session.set_proxy(proxy_settings) + try: + if key == "i2p_proxy": + self.session.apply_settings("proxy_type", lt.proxy_type("i2p_proxy")) + self.session.apply_settings("i2p_hostname", value["hostname"]) + self.session.apply_settings("i2p_port", value["port"]) + else: + self.session.apply_settings("proxy_type", lt.proxy_type(value["type"])) + self.session.apply_settings("proxy_hostname", value["hostname"]) + self.session.apply_settings("proxy_port", value["port"]) + self.session.apply_settings("proxy_username", value["username"]) + self.session.apply_settings("proxy_password", value["password"]) + self.session.apply_settings("proxy_hostnames", value["proxy_hostnames"]) + self.session.apply_settings("proxy_peer_connections", value["proxy_peer_connections"]) + self.session.apply_settings("proxy_tracker_connections", value["proxy_tracker_connections"]) + except AttributeError: + proxy_settings = lt.proxy_settings() + proxy_settings.hostname = value["hostname"] + proxy_settings.port = value["port"] + if key == "i2p_proxy": + try: + self.session.set_i2p_proxy(proxy_settings) + except RuntimeError as ex: + log.error("Unable to set I2P Proxy: %s", ex) + else: + proxy_settings.type = lt.proxy_type(value["type"]) + proxy_settings.username = value["username"] + proxy_settings.password = value["password"] + proxy_settings.hostname = value["hostname"] + proxy_settings.port = value["port"] + proxy_settings.proxy_hostnames = value["proxy_hostnames"] + proxy_settings.proxy_peer_connections = value["proxy_peer_connections"] + self.session.set_proxy(proxy_settings) def _on_set_i2p_proxy(self, key, value): log.debug("Setting I2P proxy to: %s", value) - proxy_settings = lt.proxy_settings() - proxy_settings.hostname = value["hostname"] - proxy_settings.port = value["port"] - try: - self.session.set_i2p_proxy(proxy_settings) - except RuntimeError as ex: - log.error("Unable to set I2P Proxy: %s", ex) + self._on_set_proxy(key, value) def _on_set_rate_limit_ip_overhead(self, key, value): log.debug("%s: %s", key, value) diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 218f5b015..434d51275 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -1292,8 +1292,7 @@ class TorrentManager(component.Component): if send_buffer_watermark < max_send_buffer_watermark: value = send_buffer_watermark + (500 * 1024) log.info("Increasing send_buffer_watermark from %s to %s Bytes", send_buffer_watermark, value) - settings["send_buffer_watermark"] = value - self.session.set_settings(settings) + component.get("Core").apply_session_setting("send_buffer_watermark", value) else: log.warning("send_buffer_watermark reached maximum value: %s Bytes", max_send_buffer_watermark) diff --git a/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py b/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py index ac4e64184..d3b3a2777 100644 --- a/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py +++ b/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py @@ -120,14 +120,14 @@ class Core(CorePluginBase): self.__apply_set_functions() elif state == "Yellow": # This is Yellow (Slow), so use the settings provided from the user - session = component.get("Core").session - session.set_download_rate_limit(int(self.config["low_down"] * 1024)) - session.set_upload_rate_limit(int(self.config["low_up"] * 1024)) - settings = session.get_settings() - settings["active_limit"] = self.config["low_active"] - settings["active_downloads"] = self.config["low_active_down"] - settings["active_seeds"] = self.config["low_active_up"] - session.set_settings(settings) + settings = { + "active_limit": self.config["low_active"], + "active_downloads": self.config["low_active_down"], + "active_seeds": self.config["low_active_up"], + "download_rate_limit": int(self.config["low_down"] * 1024), + "upload_rate_limit": int(self.config["low_up"] * 1024) + } + component.get("Core").apply_session_settings(settings) # Resume the session if necessary component.get("Core").resume_session() elif state == "Red":