From 298fd1bc7fb185a7be809e36bf5c18272c1a6d56 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sun, 21 Oct 2007 01:53:40 +0000 Subject: [PATCH] Fixed and enabled the SignalReceiver. --- deluge/core/signalmanager.py | 10 +++++----- deluge/ui/client.py | 13 ++++++++----- deluge/ui/gtkui/gtkui.py | 4 ++-- deluge/ui/gtkui/signals.py | 28 +++++++++++++++++----------- deluge/ui/signalreceiver.py | 34 ++++++++++++++++++++++++++-------- 5 files changed, 58 insertions(+), 31 deletions(-) diff --git a/deluge/core/signalmanager.py b/deluge/core/signalmanager.py index db018330f..270414193 100644 --- a/deluge/core/signalmanager.py +++ b/deluge/core/signalmanager.py @@ -37,20 +37,20 @@ from deluge.log import LOG as log class SignalManager: def __init__(self): - self.clients = [] + self.clients = {} def deregister_client(self, uri): """Deregisters a client""" log.debug("Deregistering %s as a signal reciever..", uri) - self.clients.remove(self.clients.index(uri)) + del self.clients[uri] def register_client(self, uri): """Registers a client to emit signals to.""" log.debug("Registering %s as a signal reciever..", uri) - self.clients.append(xmlrpclib.ServerProxy(uri)) - + self.clients[uri] = xmlrpclib.ServerProxy(uri) + def emit(self, signal, data): - for client in self.clients: + for client in self.clients.values(): try: client.emit_signal(signal, data) except: diff --git a/deluge/ui/client.py b/deluge/ui/client.py index 29f5af92c..fefba48a6 100644 --- a/deluge/ui/client.py +++ b/deluge/ui/client.py @@ -62,14 +62,17 @@ class CoreProxy: def set_core_uri(self, uri): log.info("Setting core uri as %s", uri) - self._uri = uri - if self._uri == None: + + if uri == None: for callback in self._on_no_core_callbacks: callback() + self._uri = None self._core = None - else: - # Get a new core - self.get_core() + return + + self._uri = uri + # Get a new core + self.get_core() def get_core_uri(self): """Returns the URI of the core currently being used.""" diff --git a/deluge/ui/gtkui/gtkui.py b/deluge/ui/gtkui/gtkui.py index 79509b56d..92fcbaf0e 100644 --- a/deluge/ui/gtkui/gtkui.py +++ b/deluge/ui/gtkui/gtkui.py @@ -115,7 +115,7 @@ class GtkUI: self.connectionmanager = ConnectionManager() # Start the signal receiver - #self.signal_receiver = Signals(self) + self.signal_receiver = Signals() # Initalize the plugins self.plugins = PluginManager(self) @@ -141,6 +141,6 @@ class GtkUI: del self.torrentview del self.torrentdetails del self.preferences - # del self.signal_receiver + del self.signal_receiver del self.plugins del deluge.configmanager diff --git a/deluge/ui/gtkui/signals.py b/deluge/ui/gtkui/signals.py index dff1f8533..bbf4d7dca 100644 --- a/deluge/ui/gtkui/signals.py +++ b/deluge/ui/gtkui/signals.py @@ -31,13 +31,16 @@ # this exception statement from your version. If you delete this exception # statement from all source files in the program, then also delete it here. +import deluge.ui.component as component from deluge.ui.signalreceiver import SignalReceiver from deluge.log import LOG as log -class Signals: - def __init__(self, ui): - self.ui = ui - self.receiver = SignalReceiver(6667, "http://localhost:56684") +class Signals(component.Component): + def __init__(self): + component.Component.__init__(self, "Signals") + + def start(self): + self.receiver = SignalReceiver(6667) self.receiver.start() self.receiver.connect_to_signal("torrent_added", self.torrent_added_signal) @@ -50,31 +53,34 @@ class Signals: self.receiver.connect_to_signal("torrent_all_resumed", self.torrent_all_resumed) + def stop(self): + self.receiver.shutdown() + def torrent_added_signal(self, torrent_id): log.debug("torrent_added signal received..") log.debug("torrent id: %s", torrent_id) # Add the torrent to the treeview - self.ui.mainwindow.torrentview.add_row(torrent_id) + component.get("TorrentView").add_row(torrent_id) def torrent_removed_signal(self, torrent_id): log.debug("torrent_remove signal received..") log.debug("torrent id: %s", torrent_id) # Remove the torrent from the treeview - self.ui.mainwindow.torrentview.remove_row(torrent_id) - self.ui.mainwindow.torrentdetails.clear() + component.get("TorrentView").remove_row(torrent_id) + component.get("TorrentDetails").clear() def torrent_paused(self, torrent_id): log.debug("torrent_paused signal received..") - self.ui.mainwindow.torrentview.update() + component.get("TorrentView").update() def torrent_resumed(self, torrent_id): log.debug("torrent_resumed signal received..") - self.ui.mainwindow.torrentview.update() + component.get("TorrentView").update() def torrent_all_paused(self): log.debug("torrent_all_paused signal received..") - self.ui.mainwindow.torrentview.update() + component.get("TorrentView").update() def torrent_all_resumed(self): log.debug("torrent_all_resumed signal received..") - self.ui.mainwindow.torrentview.update() + component.get("TorrentView").update() diff --git a/deluge/ui/signalreceiver.py b/deluge/ui/signalreceiver.py index 0c1f7e08e..1bec7f433 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 deluge.ui.client as client import deluge.SimpleXMLRPCServer as SimpleXMLRPCServer from SocketServer import ThreadingMixIn import deluge.xmlrpclib as xmlrpclib @@ -44,10 +45,12 @@ class SignalReceiver( ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer): - def __init__(self, port, core_uri): + def __init__(self, port): log.debug("SignalReceiver init..") threading.Thread.__init__(self) - + + # Set to true so that the receiver thread will exit + self._shutdown = False self.port = port # Daemonize the thread so it exits when the main program does @@ -68,18 +71,33 @@ class SignalReceiver( # Register the signal receiver with the core # FIXME: send actual URI not localhost - core = xmlrpclib.ServerProxy(core_uri) + core = client.get_core() core.register_client("http://localhost:" + str(port)) - - def __del__(self): - core.deregister_client("http://localhost:" + str(self.port)) + def shutdown(self): + """Shutdowns receiver thread""" + self._shutdown = True + # De-register with the daemon so it doesn't try to send us more signals + client.get_core().deregister_client( + "http://localhost:" + str(self.port)) + + # Hacky.. sends a request to our local receiver to ensure that it + # shutdowns.. This is because handle_request() is a blocking call. + receiver = xmlrpclib.ServerProxy("http://localhost:" + str(self.port), + allow_none=True) + receiver.emit_signal("shutdown", None) def run(self): """This gets called when we start the thread""" - t = threading.Thread(target=self.serve_forever) + t = threading.Thread(target=self.handle_thread) t.start() - + + def handle_thread(self): + while not self._shutdown: + self.handle_request() + self._shutdown = False + self.server_close() + def emit_signal(self, signal, data): """Exported method used by the core to emit a signal to the client""" log.debug("Received signal %s with data %s from core..", signal, data)