From 204f6ee89edb829cb3ac0ec12dd4801fe40a3f2f Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Wed, 4 Mar 2009 02:08:52 +0000 Subject: [PATCH] Fix IPCInterface on Windows --- deluge/ui/gtkui/gtkui.py | 29 +++------------ deluge/ui/gtkui/ipcinterface.py | 65 ++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 37 deletions(-) diff --git a/deluge/ui/gtkui/gtkui.py b/deluge/ui/gtkui/gtkui.py index 87436d966..876e2f48e 100644 --- a/deluge/ui/gtkui/gtkui.py +++ b/deluge/ui/gtkui/gtkui.py @@ -111,9 +111,6 @@ DEFAULT_PREFS = { class GtkUI: def __init__(self, args): - # Initialize gdk threading - gtk.gdk.threads_init() - gobject.threads_init() # Initialize gettext try: @@ -141,7 +138,6 @@ class GtkUI: from win32api import SetConsoleCtrlHandler from win32con import CTRL_CLOSE_EVENT from win32con import CTRL_SHUTDOWN_EVENT - result = 0 def win_handler(ctrl_type): log.debug("ctrl_type: %s", ctrl_type) if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT: @@ -152,34 +148,19 @@ class GtkUI: # Make sure gtkui.conf has at least the defaults set self.config = deluge.configmanager.ConfigManager("gtkui.conf", DEFAULT_PREFS) - # We need to check for the existence of 'deluged' in the system path - # before allowing to continue in classic mode. - if self.config["classic_mode"]: - try: - if deluge.common.windows_check(): - import win32api - win32api.WinExec("deluged --version") - else: - import subprocess - retcode = subprocess.call("deluged" + " --version", shell=True) - log.debug("retcode: %s", retcode) - if retcode == 127: - log.error("Unable to find deluged!") - self.config["classic_mode"] = False - except Exception, e: - log.error("Unable to find deluged: %s", e) - self.config["classic_mode"] = False - # We need to check on exit if it was started in classic mode to ensure we # shutdown the daemon. self.started_in_classic = self.config["classic_mode"] - # Start the Dbus Interface before anything else.. Just in case we are + # Start the IPC Interface before anything else.. Just in case we are # already running. self.queuedtorrents = QueuedTorrents() - self.ipcinterface = IPCInterface(args) + # Initialize gdk threading + gtk.gdk.threads_init() + gobject.threads_init() + # We make sure that the UI components start once we get a core URI client.set_disconnect_callback(self.__on_disconnect) diff --git a/deluge/ui/gtkui/ipcinterface.py b/deluge/ui/gtkui/ipcinterface.py index d0a25fece..1459953cb 100644 --- a/deluge/ui/gtkui/ipcinterface.py +++ b/deluge/ui/gtkui/ipcinterface.py @@ -56,22 +56,61 @@ class IPCInterface(component.Component): if not os.path.exists(deluge.configmanager.get_config_dir("ipc")): os.makedirs(deluge.configmanager.get_config_dir("ipc")) + # Make the args absolute paths + _args = [] + for arg in args: + _args.append(os.path.abspath(arg)) + args = _args + socket = os.path.join(deluge.configmanager.get_config_dir("ipc"), "deluge-gtk") - try: - self.factory = Factory() - self.factory.protocol = IPCProtocolServer - reactor.listenUNIX(socket, self.factory, wantPID=True) - except twisted.internet.error.CannotListenError, e: - log.info("Deluge is already running! Sending arguments to running instance..") - self.factory = ClientFactory() - self.factory.args = args - self.factory.protocol = IPCProtocolClient - reactor.connectUNIX(socket, self.factory, checkPID=True) - reactor.run() - sys.exit(0) + if deluge.common.windows_check(): + # If we're on windows we need to check the global mutex to see if deluge is + # already running. + import win32event + import win32api + import winerror + self.mutex = win32event.CreateMutex(None, False, "deluge") + if win32api.GetLastError() != winerror.ERROR_ALREADY_EXISTS: + # Create listen socket + self.factory = Factory() + self.factory.protocol = IPCProtocolServer + import random + port = random.randrange(20000, 65535) + reactor.listenTCP(port, self.factory) + # Store the port number in the socket file + open(socket, "w").write(str(port)) + # We need to process any args when starting this process + process_args(args) + else: + # Send to existing deluge process + port = int(open(socket, "r").readline()) + self.factory = ClientFactory() + self.factory.args = args + self.factory.protocol = IPCProtocolClient + reactor.connectTCP("127.0.0.1", port, self.factory) + reactor.run() + sys.exit(0) else: - process_args(args) + try: + self.factory = Factory() + self.factory.protocol = IPCProtocolServer + reactor.listenUNIX(socket, self.factory, wantPID=True) + except twisted.internet.error.CannotListenError, e: + log.info("Deluge is already running! Sending arguments to running instance..") + self.factory = ClientFactory() + self.factory.args = args + self.factory.protocol = IPCProtocolClient + reactor.connectUNIX(socket, self.factory, checkPID=True) + reactor.run() + sys.exit(0) + else: + process_args(args) + + def shutdown(self): + if deluge.common.windows_check(): + import win32api + win32api.CloseHandle(self.mutex) def process_args(args): """Process arguments sent to already running Deluge"""