Fix IPCInterface on Windows

This commit is contained in:
Andrew Resch 2009-03-04 02:08:52 +00:00
parent 80fdc3356c
commit 204f6ee89e
2 changed files with 57 additions and 37 deletions

View File

@ -111,9 +111,6 @@ DEFAULT_PREFS = {
class GtkUI: class GtkUI:
def __init__(self, args): def __init__(self, args):
# Initialize gdk threading
gtk.gdk.threads_init()
gobject.threads_init()
# Initialize gettext # Initialize gettext
try: try:
@ -141,7 +138,6 @@ class GtkUI:
from win32api import SetConsoleCtrlHandler from win32api import SetConsoleCtrlHandler
from win32con import CTRL_CLOSE_EVENT from win32con import CTRL_CLOSE_EVENT
from win32con import CTRL_SHUTDOWN_EVENT from win32con import CTRL_SHUTDOWN_EVENT
result = 0
def win_handler(ctrl_type): def win_handler(ctrl_type):
log.debug("ctrl_type: %s", ctrl_type) log.debug("ctrl_type: %s", ctrl_type)
if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT: 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 # Make sure gtkui.conf has at least the defaults set
self.config = deluge.configmanager.ConfigManager("gtkui.conf", DEFAULT_PREFS) 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 # We need to check on exit if it was started in classic mode to ensure we
# shutdown the daemon. # shutdown the daemon.
self.started_in_classic = self.config["classic_mode"] 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. # already running.
self.queuedtorrents = QueuedTorrents() self.queuedtorrents = QueuedTorrents()
self.ipcinterface = IPCInterface(args) 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 # We make sure that the UI components start once we get a core URI
client.set_disconnect_callback(self.__on_disconnect) client.set_disconnect_callback(self.__on_disconnect)

View File

@ -56,8 +56,42 @@ class IPCInterface(component.Component):
if not os.path.exists(deluge.configmanager.get_config_dir("ipc")): if not os.path.exists(deluge.configmanager.get_config_dir("ipc")):
os.makedirs(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") socket = os.path.join(deluge.configmanager.get_config_dir("ipc"), "deluge-gtk")
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:
try: try:
self.factory = Factory() self.factory = Factory()
self.factory.protocol = IPCProtocolServer self.factory.protocol = IPCProtocolServer
@ -73,6 +107,11 @@ class IPCInterface(component.Component):
else: else:
process_args(args) process_args(args)
def shutdown(self):
if deluge.common.windows_check():
import win32api
win32api.CloseHandle(self.mutex)
def process_args(args): def process_args(args):
"""Process arguments sent to already running Deluge""" """Process arguments sent to already running Deluge"""
# Make sure args is a list # Make sure args is a list