From 1f8884f903f42320508b111abaadbfa4b178f9ed Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Thu, 25 Oct 2007 07:42:14 +0000 Subject: [PATCH] Enabled 'start local daemon' in ConnectionManager. Change startup scripts: deluged runs daemon and deluge runs ui. Minor fix-ups. --- deluge/core/core.py | 14 +++++-- deluge/core/daemon.py | 4 +- deluge/main.py | 59 +++++++++++++--------------- deluge/ui/client.py | 14 ++++++- deluge/ui/gtkui/connectionmanager.py | 10 ++++- deluge/ui/signalreceiver.py | 1 + setup.py | 2 +- 7 files changed, 62 insertions(+), 42 deletions(-) diff --git a/deluge/core/core.py b/deluge/core/core.py index 5a3c26906..18283052f 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -53,6 +53,7 @@ from deluge.core.signalmanager import SignalManager from deluge.log import LOG as log DEFAULT_PREFS = { + "daemon_port": 58846, "compact_allocation": True, "download_location": deluge.common.get_default_download_dir(), "listen_ports": [6881, 6891], @@ -81,14 +82,21 @@ class Core( threading.Thread, ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer): - def __init__(self): + def __init__(self, port): log.debug("Core init..") threading.Thread.__init__(self) + + # Get config + self.config = ConfigManager("core.conf", DEFAULT_PREFS) + if port == None: + port = self.config["daemon_port"] + # Setup the xmlrpc server try: + log.info("Starting XMLRPC server on port %s", port) SimpleXMLRPCServer.SimpleXMLRPCServer.__init__( - self, ("localhost", 58846), logRequests=False, allow_none=True) + self, ("localhost", port), logRequests=False, allow_none=True) except: log.info("Daemon already running or port not available..") sys.exit(0) @@ -116,8 +124,6 @@ class Core( def run(self): """Starts the core""" - # Get config - self.config = ConfigManager("core.conf", DEFAULT_PREFS) # Create the client fingerprint version = [] diff --git a/deluge/core/daemon.py b/deluge/core/daemon.py index 024cebe54..6e23b6c95 100644 --- a/deluge/core/daemon.py +++ b/deluge/core/daemon.py @@ -35,9 +35,9 @@ from deluge.core.core import Core from deluge.log import LOG as log class Daemon: - def __init__(self): + def __init__(self, port): # Start the core as a thread and join it until it's done - self.core = Core() + self.core = Core(port) self.core.start() self.core.join() diff --git a/deluge/main.py b/deluge/main.py index 6a5ee878b..4a6e4dcc3 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -39,52 +39,47 @@ import os from optparse import OptionParser -from deluge.core.daemon import Daemon -from deluge.ui.ui import UI -from deluge.log import LOG as log import deluge.common -def main(): - """Entry point for Deluge""" +def start_ui(): + """Entry point for ui script""" # Setup the argument parser parser = OptionParser(usage="%prog [options] [actions]", version=deluge.common.get_version()) - parser.add_option("--daemon", dest="daemon", help="Start Deluge daemon", - metavar="DAEMON", action="store_true", default=False) - parser.add_option("--ui", dest="ui", help="Start Deluge UI", - metavar="UI", action="store_true", default=False) # Get the options and args from the OptionParser (options, args) = parser.parse_args() - log.info("Deluge %s", deluge.common.get_version()) - + from deluge.log import LOG as log + + log.info("Deluge ui %s", deluge.common.get_version()) log.debug("options: %s", options) log.debug("args: %s", args) - - pid = None - - # Start the daemon - if options.daemon: - log.info("Starting daemon..") - # We need to fork() the process to run it in the background... - # FIXME: We cannot use fork() on Windows - pid = os.fork() - if not pid: - # Since we are starting daemon this process will not start a UI - options.ui = False - # Create the daemon object - Daemon() - # Start the UI - if options.ui: - log.info("Starting ui..") - UI() - + from deluge.ui.ui import UI + log.info("Starting ui..") + UI() + def start_daemon(): """Entry point for daemon script""" - log.info("Deluge daemon %s", deluge.common.get_version()) + # Setup the argument parser + parser = OptionParser(usage="%prog [options] [actions]", + version=deluge.common.get_version()) + parser.add_option("-p", "--port", dest="port", + help="Port daemon will listen on", action="store", type="int") + + # Get the options and args from the OptionParser + (options, args) = parser.parse_args() + + from deluge.log import LOG as log + + log.info("Deluge daemon %s", deluge.common.get_version()) + log.debug("options: %s", options) + log.debug("args: %s", args) + + from deluge.core.daemon import Daemon + log.info("Starting daemon..") pid = os.fork() if not pid: - Daemon() + Daemon(options.port) diff --git a/deluge/ui/client.py b/deluge/ui/client.py index 07990221b..15f59b553 100644 --- a/deluge/ui/client.py +++ b/deluge/ui/client.py @@ -114,7 +114,19 @@ def set_core_uri(uri): def get_core_uri(): """Get the core URI""" return _core.get_core_uri() - + +def is_localhost(): + """Returns True if core is a localhost""" + # Get the uri + uri = _core.get_core_uri() + if uri != None: + # Get the host + host = uri[7:].split(":")[0] + if host == "localhost" or host == "127.0.0.1": + return True + + return False + def shutdown(): """Shutdown the core daemon""" try: diff --git a/deluge/ui/gtkui/connectionmanager.py b/deluge/ui/gtkui/connectionmanager.py index 71cafdad1..9276226e3 100644 --- a/deluge/ui/gtkui/connectionmanager.py +++ b/deluge/ui/gtkui/connectionmanager.py @@ -35,6 +35,7 @@ import gtk, gtk.glade import pkg_resources import gobject import socket +import os import deluge.ui.component as component import deluge.xmlrpclib as xmlrpclib @@ -266,17 +267,22 @@ class ConnectionManager(component.Component): paths = self.hostlist.get_selection().get_selected_rows()[1] row = self.liststore.get_iter(paths[0]) status = self.liststore.get_value(row, HOSTLIST_COL_STATUS) + uri = self.liststore.get_value(row, HOSTLIST_COL_URI) + port = uri.split(":")[1] if HOSTLIST_STATUS[status] == "Online" or\ HOSTLIST_STATUS[status] == "Connected": # We need to stop this daemon - uri = self.liststore.get_value(row, HOSTLIST_COL_URI) uri = "http://" + uri # Call the shutdown method on the daemon core = xmlrpclib.ServerProxy(uri) core.shutdown() # Update display to show change self.update() - + elif HOSTLIST_STATUS[status] == "Offline": + log.debug("Start localhost daemon..") + # Spawn a local daemon + os.popen("deluged -p %s" % port) + def on_button_close_clicked(self, widget): log.debug("on_button_close_clicked") self.hide() diff --git a/deluge/ui/signalreceiver.py b/deluge/ui/signalreceiver.py index a5435e20c..b3a8a1938 100644 --- a/deluge/ui/signalreceiver.py +++ b/deluge/ui/signalreceiver.py @@ -32,6 +32,7 @@ # statement from all source files in the program, then also delete it here. import sys +import socket import gobject diff --git a/setup.py b/setup.py index 0c04904b7..77a648e53 100644 --- a/setup.py +++ b/setup.py @@ -194,6 +194,6 @@ setup( cmdclass=cmdclass, entry_points = """ [console_scripts] - deluge = deluge.main:main + deluge = deluge.main:start_ui deluged = deluge.main:start_daemon """)