From e9f8c7b0e83c3f8d016f0ea29350ce0819e20815 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Wed, 28 Jan 2009 17:59:27 +0000 Subject: [PATCH] Get Classic mode working somewhat. There are still some issues to resolve with it. --- deluge/component.py | 9 ++++----- deluge/core/daemon.py | 27 +++++++++++++++++---------- deluge/core/rpcserver.py | 21 ++++++++++++--------- deluge/ui/client.py | 26 +++++++++++++++++++++----- deluge/ui/common.py | 2 +- deluge/ui/gtkui/connectionmanager.py | 4 ---- deluge/ui/gtkui/gtkui.py | 9 +++++++-- deluge/ui/gtkui/preferences.py | 4 +--- 8 files changed, 63 insertions(+), 39 deletions(-) diff --git a/deluge/component.py b/deluge/component.py index bc6850a6f..a2bbba9ae 100644 --- a/deluge/component.py +++ b/deluge/component.py @@ -22,8 +22,7 @@ # Boston, MA 02110-1301, USA. # - -import gobject +from twisted.internet.task import LoopingCall from deluge.log import LOG as log COMPONENT_STATE = [ @@ -49,7 +48,7 @@ class Component(object): def _start(self): self._state = COMPONENT_STATE.index("Started") if self._update(): - self._timer = gobject.timeout_add(self._interval, self._update) + self._timer = LoopingCall(self._interval, self._update) def stop(self): pass @@ -57,14 +56,14 @@ class Component(object): def _stop(self): self._state = COMPONENT_STATE.index("Stopped") try: - gobject.source_remove(self._timer) + self._timer.stop() except: pass def _pause(self): self._state = COMPONENT_STATE.index("Paused") try: - gobject.source_remove(self._timer) + self._timer.stop() except: pass diff --git a/deluge/core/daemon.py b/deluge/core/daemon.py index 691c1e5e7..fec03c232 100644 --- a/deluge/core/daemon.py +++ b/deluge/core/daemon.py @@ -36,7 +36,7 @@ from deluge.core.rpcserver import RPCServer, export from deluge.log import LOG as log class Daemon(object): - def __init__(self, options, args): + def __init__(self, options=None, args=None, classic=False): # Initialize gettext try: locale.setlocale(locale.LC_ALL, '') @@ -74,29 +74,36 @@ class Daemon(object): log.debug("options: %s", options) log.debug("args: %s", args) # Set the config directory - deluge.configmanager.set_config_dir(options.config) + if options: + deluge.configmanager.set_config_dir(options.config) from deluge.core.core import Core # Start the core as a thread and join it until it's done self.core = Core() + port = self.core.config["daemon_port"] + if options and options.port: + port = options.port self.rpcserver = RPCServer( - port=options.port if options.port else self.core.config["daemon_port"], - allow_remote=self.core.config["allow_remote"] + port=port, + allow_remote=self.core.config["allow_remote"], + listen=not classic ) + + # Register the daemon and the core RPCs self.rpcserver.register_object(self.core) self.rpcserver.register_object(self) # Make sure we start the PreferencesManager first component.start("PreferencesManager") - component.start() -# reactor.run() - try: - reactor.run() - except KeyboardInterrupt: - self.shutdown() + if not classic: + component.start() + try: + reactor.run() + except KeyboardInterrupt: + self.shutdown() @export() def shutdown(self, *args, **kwargs): diff --git a/deluge/core/rpcserver.py b/deluge/core/rpcserver.py index 815e59b8d..34f2a515b 100644 --- a/deluge/core/rpcserver.py +++ b/deluge/core/rpcserver.py @@ -236,11 +236,22 @@ class RPCServer(component.Component): :param port: int, the port the RPCServer will listen on :param interface: str, the interface to listen on, this may override the `:param:allow_remote` setting :param allow_remote: bool, set True if the server should allow remote connections + :param listen: bool, if False, will not start listening.. This is only useful in Classic Mode """ - def __init__(self, port=58846, interface="", allow_remote=False): + def __init__(self, port=58846, interface="", allow_remote=False, listen=True): component.Component.__init__(self, "RPCServer") + self.factory = Factory() + self.factory.protocol = DelugeRPCProtocol + # Holds the registered methods + self.factory.methods = {} + # Holds the session_ids and auth levels + self.factory.authorized_sessions = {} + + if not listen: + return + if allow_remote: hostname = "" else: @@ -251,13 +262,6 @@ class RPCServer(component.Component): log.info("Starting DelugeRPC server %s:%s", hostname, port) - self.factory = Factory() - self.factory.protocol = DelugeRPCProtocol - # Holds the registered methods - self.factory.methods = {} - # Holds the session_ids and auth levels - self.factory.authorized_sessions = {} - # Check for SSL cert/key and create them if necessary ssl_dir = deluge.configmanager.get_config_dir("ssl") if not os.path.exists(ssl_dir): @@ -297,7 +301,6 @@ class RPCServer(component.Component): self.factory.methods[name + "." + d] = getattr(obj, d) def get_object_method(self, name): - log.debug(self.factory.methods) return self.factory.methods[name] def __generate_ssl_keys(self): diff --git a/deluge/ui/client.py b/deluge/ui/client.py index c9462289e..5f297d223 100644 --- a/deluge/ui/client.py +++ b/deluge/ui/client.py @@ -391,21 +391,36 @@ class DaemonSSLProxy(DaemonProxy): class DaemonClassicProxy(DaemonProxy): def __init__(self): - import daemon - self.__daemon = daemon.Daemon() + import deluge.core.daemon + self.__daemon = deluge.core.daemon.Daemon(classic=True) + log.debug("daemon created!") self.connected = True + self.host = "localhost" + self.port = 58846 + self.user = "localclient" + + + + def disconnect(self): + self.__daemon = None def call(self, method, *args, **kwargs): log.debug("call: %s %s %s", method, args, kwargs) - m = self.__daemon.rpcserver.get_object_method(method) d = defer.Deferred() + try: + m = self.__daemon.rpcserver.get_object_method(method) + except Exception, e: + log.exception(e) + d.errback(e) + return d + try: result = m(*args, **kwargs) except Exception, e: d.errback(e) else: - d.callbacks(result) + d.callback(result) return d @@ -502,7 +517,8 @@ class Client(object): :returns: bool, True if connected to a localhost """ - if self._daemon_proxy and self._daemon_proxy.host in ("127.0.0.1", "localhost"): + if self._daemon_proxy and self._daemon_proxy.host in ("127.0.0.1", "localhost") or\ + isinstance(self._daemon_proxy, DaemonClassicProxy): return True return False diff --git a/deluge/ui/common.py b/deluge/ui/common.py index 1fc37bf63..d88eb007a 100644 --- a/deluge/ui/common.py +++ b/deluge/ui/common.py @@ -152,4 +152,4 @@ def get_localhost_auth(): if username == "localclient": return (username, password) - return None + return ("", "") diff --git a/deluge/ui/gtkui/connectionmanager.py b/deluge/ui/gtkui/connectionmanager.py index d4d0c0830..a365ddfa7 100644 --- a/deluge/ui/gtkui/connectionmanager.py +++ b/deluge/ui/gtkui/connectionmanager.py @@ -85,10 +85,6 @@ class ConnectionManager(component.Component): component.Component.__init__(self, "ConnectionManager") self.gtkui_config = ConfigManager("gtkui.conf") - if self.gtkui_config["classic_mode"]: - client.start_classic_mode() - return - self.config = ConfigManager("hostlist.conf.1.2", DEFAULT_CONFIG) # Component overrides diff --git a/deluge/ui/gtkui/gtkui.py b/deluge/ui/gtkui/gtkui.py index d8d3cd73b..e34628a64 100644 --- a/deluge/ui/gtkui/gtkui.py +++ b/deluge/ui/gtkui/gtkui.py @@ -243,11 +243,16 @@ class GtkUI: def _on_reactor_start(self): log.debug("_on_reactor_start") - # XXX: We need to call a simulate() here, but this could be a bug in twisted - reactor.simulate() if self.config["show_connection_manager_on_start"] and not self.config["classic_mode"]: + # XXX: We need to call a simulate() here, but this could be a bug in twisted + reactor.simulate() self.connectionmanager.show() + if self.config["classic_mode"]: + client.start_classic_mode() + self._on_new_core() + return + def _on_new_core(self): component.start() diff --git a/deluge/ui/gtkui/preferences.py b/deluge/ui/gtkui/preferences.py index 95a356eda..d5ff19fcc 100644 --- a/deluge/ui/gtkui/preferences.py +++ b/deluge/ui/gtkui/preferences.py @@ -174,15 +174,13 @@ class Preferences(component.Component): # Update the preferences dialog to reflect current config settings self.core_config = {} - try: + if client.connected(): client.core.get_config().addCallback(self._on_get_config) client.core.get_available_plugins().addCallback(self._on_get_available_plugins) client.core.get_enabled_plugins().addCallback(self._on_get_enabled_plugins) client.core.get_listen_port().addCallback(self._on_get_listen_port) # Force these calls and block until we've done them all client.force_call() - except deluge.error.NoCoreError: - log.debug("Not connected to a daemon..") if self.core_config != {} and self.core_config != None: core_widgets = {